in python, 7. Assume board represents a tic-tac-toe board (3 x 3). For example i
ID: 3920332 • Letter: I
Question
in python,
7. Assume board represents a tic-tac-toe board (3 x 3). For example if,
board = [ ["X", ".", "O"],["O", "O", "."], ["X", "X", "X"]] , then player X won the
game by completing the bottom row. Each “.” represents an empty square. Write a function
called "allDone" which is passed a tic-tac-toe board and returns True if either player
won the game. If the game was a draw, the function returns False. A win consists of three
consecutive X’s or three consecutive O’s in a row, a column, or diagonal. Any method that
works is fine, but give it some thought before you jump in.
Explanation / Answer
def allDone(board):
for i in range(3):
if board[i][0] != '.' and board[i][0] == board[i][1] and board[i][1] == board[i][2]: # check row
return True
if board[0][i] != '.' and board[0][i] == board[1][i] and board[1][i] == board[2][i]: # check column
return True
if board[0][0] != '.' and board[0][0] == board[1][1] and board[1][1] == board[2][2]: # check first diagonal
return True
if board[0][2] != '.' and board[0][2] == board[1][1] and board[1][1] == board[2][0]: # check second diagonal
return True
return False
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.