Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Fill in the Python code to play Tic Tac Toe. I won\'t award points unless it run

ID: 3557429 • Letter: F

Question

Fill in the Python code to play Tic Tac Toe. I won't award points unless it runs succesfully. # Tic-Tac-Toe Game def drawBoard(board): # Draws the board using the list of numbers print(" ") print(" ",board[0]," | ",board[1]," | ", board[2]) print("----------------") print(" ",board[3]," | ",board[4]," | ", board[5]) print("----------------") print(" ", board[6]," | ",board[7]," | ", board[8]) print(" ") return def nextMove(turn,bd): # Asks the player whose turn it is to choose a spot on the board # The turn parameter represents the player ("O" or "X") # Replace the spot on the board with the right symbol # Return the board return bd def isWinner(bd): # Determines if there is a winner # If X is the winner, return "X" # If O is the winner, return "O" # If there is no winner, return "none" return "X" def moreMoves(bd): # Determines if there are any moves left # if any spaces are left to play # return 1 if there are any moves left # return 0 if there are no moves left return 0 def nextTurn(turn): # Determines whose turn it is based on whose turn # just finished return turn # Main code # # Initialize the board with position numbers # Start out with X going first # Set the winner to 'none' to start # As long as there is no winner and there are still moves to make, loop through the game # Each time through the loop # Draw the board # Make the next move # Determine if there is a winner # Set the turn to the next player # # Once the loop is complete, display the board one more time and print the winner def main(): bd = [0,1,2,3,4,5,6,7,8] turn = 'X' winner = 'none' while winner == 'none' and moreMoves(bd)==1: drawBoard(bd) bd = nextMove(turn,bd) winner = isWinner(bd) turn = nextTurn(turn) drawBoard(bd) print("Player ",winner," is the winner")

Explanation / Answer

def print_board(board): print "The board look like this: " for i in range(3): print " ", for j in range(3): if board[i*3+j] == 1: print 'X', elif board[i*3+j] == 0: print 'O', elif board[i*3+j] != -1: print board[i*3+j]-1, else: print ' ', if j != 2: print " | ", print if i != 2: print "-----------------" else: print def print_instruction(): print "Please use the following cell numbers to make your move" print_board([2,3,4,5,6,7,8,9,10]) def get_input(turn): valid = False while not valid: try: user = raw_input("Where would you like to place " + turn + " (1-9)? ") user = int(user) if user >= 1 and user 4: winner = check_win(board) if winner != -1: out = "The winner is " out += "X" if winner == 1 else "O" out += " :)" quit_game(board,out) elif move == 9: quit_game(board,"No winner :(") if __name__ == "__main__": main()
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote