Tic Tac Toe Python workshop

From Knowledge Kitchen
Jump to navigation Jump to search


Create a game of Tic Tac Toe that two players can play together.

Pseudo-code outline

#Output a board that looks something like this:
# X | O | X
# ---------
# O | X | O
# ---------
# X | O | X

#I suggest you set up three lists for each row of the board:
row1 = ['', '', '']
row2 = ['', '', '']
row3 = ['', '', '']

#loop until the game is over
## in each cycle of the loop, you must do the following:
## - output the board
## - ask the user where they want to go
## - validate their response
## - if valid, put their character into the proper position in a list
## - if invalid, ask them to enter their response again until valid
## - alternate players, so next iteration it's the other player's turn
## - check whether the game is over... quit the loop when it is

#output the winner (either player X or player O)

Suggestions

  • store each row of the board in a list of 3 elements
  • make a global variable that holds which user's turn it is ('X', or 'O'). and flip this variable's value at the end of every iteration of the loop.
  • make discrete functions for each task (outputting the board, asking the user for their move, validating their response, etc
  • ask the user to enter their move as coordinates, like '1,3' for the first row, third column, and use the string's split() function to chop up this response into a list