I'm trying to create a simple tic-tac-toe game for a class project, but I'm not exactly sure how to go about finishing the rest of the code. I'd really appreciate any insight or input as to how I can finish the rest of this off. The code will technically work at the moment, but all it does is display a board filled with none
and "The game was a tie.".
PLAYERS = ["X", "O"]
def display_board(board):
print board[0][0:3]
print board[1][0:3]
print board[2][0:3]
def create_empty_board():
return [[None, None, None], [None, None, None], [None, None, None]]
def board_is_full(board):
for row in board:
if None not in board:
return True
def winner(board):
if board[0][0] == board[0][1] == board[0][2] != None:
return board[0][0]
elif board[1][0] == board[1][1] == board[1][2] != None:
return board[1][0]
elif board[2][0] == board[2][1] == board[2][2] != None:
return board[2][0]
elif board[0][0] == board[1][0] == board[2][0] != None:
return board[0][0]
elif board[0][1] == board[1][1] == board[2][1] != None:
return board[0][1]
elif board[0][2] == board[1][2] == board[2][2] != None:
return board[0][2]
elif board[0][0] == board[1][1] == board[2][2] != None:
return board[0][0]
else:
return None
def game_over(board):
if board_is_full(board) == True:
return True
def player_turn(board, playerid):
""" Ask the player to select a coordinates for their next move. The player needs to select a row and a column. If the coordinates the player selects are outside of the board, or are already occupied, they need to be prompted to select coordinates again, until their input is valid."""
return 1, 1 # by default, return row 1, column 1 as the player's desired location on the board; you need to implement this
def play():
""" This is the main function that implements a hot seat version of Tic Tac Toe."""
# the code below is just an example of how you could structure your play() function
# if you implement all the functions above correctly, this function will work
# however, feel free to change it if you want to organize your code differently
board = create_empty_board()
display_board(board)
current_player = 0
while not game_over(board):
board = player_turn(board, PLAYERS[current_player])
current_player = (current_player + 1) % len(PLAYERS)
display_board(board)
who_won = winner(board)
if who_won is None:
print "The game was a tie."
else:
print "The winning player is", who_won
if __name__ == "__main__":
play()
Your first problem is that your board_is_full
is wrong. For each row, instead of checking you're checking None not in board
. That's always true; board
is a list of three lists, so None
will never be in it. You want to check each row, not the whole board. But just if None not in row
isn't right either—that will be true as soon as any row is full, not all rows. So you want to return False
when you find a non-full row, and True
if you get to the end without finding any. You should be able to do that yourself.
Your next problem is that you haven't implemented player_turn
yet. For a really hacky implementation, you can just use return input('row, col: ')
. Unless you can understand why that works, you shouldn't turn that in as your homework (and if you do understand why it works, you wouldn't want to use it). Also, it's not very user-friendly—it doesn't tell you whose turn it is, or show you the board, or anything. You get those things as parameters, so you can use them. Anyway, the one-liner is good enough to get you to the next problem for now, so you can come back to it later.
Your next problem is that player_turn
returns a pair of numbers, and you're just assigning that pair of numbers to board
. You can't do that. What you want is something like this:
row, col = player_turn(board, PLAYERS[current_player])
board[row][col] = PLAYERS[current_player]
The game also makes you keep playing until the board is full, even if someone has already won. But really, you want to finish if the board is full, or there's a winner. (In fact, ideally, you want to finish as soon as a win is impossible, but that's a bit more complicated. You can do the simple thing just by translating that previous sentence into a replacement for your while
statement.)
And that should get you to the point where you can find any other minor bugs, write a proper player_turn
function, look for ways to simplify the existing code, etc.