Unknown model· unknown
from NVIDIA HelpSteer3 Preference
write a code in python that checks if a tic-tac-toe game is finished, if yes who won, if…
- User
write a code in python that checks if a tic-tac-toe game is finished, if yes who won, if no - whose turn now
- Assistant
Here's a Python function that takes a 3x3 2D list representing a tic-tac-toe board (with 'X' and 'O' as players and '-' for empty cells) and returns:
- 'X' if X has won
- 'O' if O has won
- 'Draw' if it's a draw
- 'X_turn' if it's X's turn
- 'O_turn' if it's O's turn
pythondef check_tic_tac_toe(board): # Check rows, columns, and diagonals for a win for i in range(3): if (board[i][0] == board[i][1] == board[i][2] != '-' or board[0][i] == board[1][i] == board[2][i] != '-'): return board[i][0] if (board[0][0] == board[1][1] == board[2][2] != '-' or board[0][2] == board[1][1] == board[2][0] != '-'): return board[1][1] # Check for a draw if '-' not in [cell for row in board for cell in row]: return 'Draw' # Determine whose turn is next x_count = sum(row.count('X') for row in board) o_count = sum(row.count('O') for row in board) return 'X_turn' if x_count == o_count else 'O_turn'You can use this function like this:
pythonboard = [ ['X', '-', 'O'], ['-', 'X', '-'], ['O', '-', 'X'] ] print(check_tic_tac_toe(board)) # Output: 'X_turn'
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.