"""A simple game of tic tac toe. It requires that Zelle's graphics.py is in the same directory as this file. Author: YOUR NAMES HERE, and Titus Klinge In this game of tic tac toe, the game board is represented by a 3x3 list of strings. For example, the state of the board might be represented by the following variable: board = [["X", "O", ""], ["O", "X", ""], ["", "X", "O"]] This list of list of strings represents the following board: +---+---+---+ | X | O | | +---+---+---+ | O | X | | +---+---+---+ | | X | O | +---+---+---+ Therefore empty cells of the board are represented with empty strings. The game is split up into the following functions: drawBoard(win, board) update(board, point, player) validMove(board, point, player) checkWinner(board) drawEndGameMessage(win, winner) """ from graphics import * def drawBoard(win, board): """Draws the board to the GraphWin object given. Parameters: win: a GraphWin object board: a 3x3 list of "", "X", and "O" characters Returns: Nothing; called for the side effect Postconditions: - Four lines are drawn on the GraphWin object that separates it into a 3x3 grid - Each character from the board parameter is printed centered in its appropriate square on the grid """ pass # IMPLEMENT ME! def updateBoard(board, point, player): """Updates the board with the current player's mouse click location. Parameters: board: a 3x3 list of "", "X", and "O" characters point: a Point object of where the player clicked player: either "X" or "O" depending on the current player Returns: Nothing; called for the side effect Preconditions: - point is a valid location in a cell that has not been previously clicked by either player Postconditions: - The 3x3 board is updated so that the appropriate cell is replaced by the player's character """ pass # IMPLEMENT ME! def validMove(board, point): """Checks if the player's selection has been previously picked earlier in the game. Parameters board: a 3x3 list of "", "X", and "O" characters point: a Point object of where the player clicked Returns: True if the play has not been previously picked and False otherwise """ return True # IMPLEMENT ME def checkWinner(board): """Checks if a player has won the game. Parameters: board: a 3x3 list of "", "X", and "O" characters Returns: "" if neither player has three in a row "X" if player X wins "O" if player O wins """ return "" # IMPLEMENT ME def drawEndGameMessage(win, winner): """Shows a message to the user what the outcome of the game was. Parameters: win: a GraphWin object winner: "X", "O", or "" (i.e. the winner of the game) Returns: Nothing; called for the side effect Postconditions: - If winner = "X", a message is displayed that X wins - If winner = "O", a message is displayed that O wins - If winner = "", a message is displayed that the game was a draw """ pass # IMPLEMENT ME def main(): """The main function that is called when the program is run.""" # Initializes the window and changes the coordinate system # so that (0,0) is the upper left corner and (3,3) is the # lower right corner win = GraphWin("Tic-Tac-Toe", 240, 240) win.setCoords(0.0, 3.0, 3.0, 0.0) # Initializes the board to all empty strings which means # that no player has picked any cell yet. When a spot in # the board is selected by a player, it will be updated # to either an "X" or an "O" board = [["", "", ""], ["", "", ""], ["", "", ""]] drawBoard(win, board) # The player characters. # - Player 0 is "X" # - Player 1 is "O" players = ["X", "O"] curPlayer = 0 # To keep track of who won the game # - winner = "" if no one has won yet # - winner = "X" if Player 0 won # - winner = "O" if Player 1 won winner = "" # Keeps track of the number of moves so far movesSoFar = 0 # The main game loop. Will continue looping until the game is over while movesSoFar < 9 and winner == "" and win.isOpen(): # Waits until the current player clicks somewhere on the window point = win.getMouse() # If the selection is a valid move... if validMove(board, point): # Update the board, check for the winner, switch players, # and draw the updated board updateBoard(board, point, players[curPlayer]) winner = checkWinner(board) curPlayer = 1 - curPlayer movesSoFar = movesSoFar + 1 drawBoard(win, board) # If the window is still open when the game is finished, show a message if win.isOpen(): drawEndGameMessage(win, winner) # Wait until the user clicks before ending the program win.getMouse() if __name__ == "__main__": main()