# tictactoe.py # YOUR NAMES HERE, and Titus Klinge # # A simple game of tic tac toe. It requires that Zelle's # graphics.py is in the same directory as this file. from graphics import * # Draws the board to the GraphWin object given. # # PARAMETERS: # win, a GraphWin object # board, a 3x3 list of "", "X", and "O" characters # # RETURNS: # nothing, it only draws to the win object # # 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 def drawBoard(win, board): return # IMPLEMENT ME! # Updates the board with the current player's mouse click # location. # # PARAMETERS: # board, a 3x3 list of "", "X", and "O" characters # point, a graphics.py Point object of where the player clicked # player, either "X" or "O" depending on the current player # # RETURNS: # nothing, it only changes the board object # # PRECONDITONS: # - 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 def updateBoard(board, point, player): return # IMPLEMENT ME! # 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 graphics.py Point object of where the player clicked # # RETURNS: # True if the play has not been previously picked and # False otherwise def validMove(board, point): return True # IMPLEMENT ME # 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 def checkWinner(board): return "" # IMPLEMENT ME # 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, it just draws to the window # # 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 def drawEndGameMessage(win, winner): return # IMPLEMENT ME # The main function that is called when the program is run. def main(): # Initializes the window win = GraphWin("Tic-Tac-Toe", 240, 240) win.setBackground("white") win.setCoords(0.0, 0.0, 3.0, 3.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 = [["", "", ""], ["", "", ""], ["", "", ""]] # 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()