Home /
Expert Answers /
Computer Science /
use-python-to-solve-the-problem-def-is-won-board-player-tic-tac-toe-is-one-of-the-most-popular-34-pa322
(Solved): Use python to solve the
problem
def is_won(board, player):
Tic-Tac-Toe is one of the most popular " ...
Use python to solve the
problem
def is_won(board, player):
Tic-Tac-Toe is one of the most popular "pen and paper" games. It is also known as "Noughts and Crosses" or "Xs and Os". In the classic variation, two players take turns marking squares on a \( 3 \times 3 \) grid. One player (X-player) marks their squares with an ' \( x \) ', and the other player (O-player) marks their squares with an 'o'. A player who manages to mark a whole row or a whole column or one of the two diagonals wins, and shows it by crossing the winning squares. Here are a few examples. Define a function named is_won (board, player) which takes a list of 3 strings representing a Tic-Tac-Toe board, and a player string as parameters and returns True if the parameter player has won the game, and returns False otherwise. The parameters of the function are defined as follows: - The first parameter is called board. It is a list of length 3, where each entry is a string with 3 characters representing a row on a Tic-Tac-Toe board. Each character in a row represents a square on the Tic-Tac-Toe board, with the "-" character indicating an empty square. For example, an empty board looks like this: - The second parameter is called player. It is a single character indicating whether the player is " 0 " or " \( \mathrm{x} \) ". Hint: Defining helper functions to check the diagonals, rows, and columns may be helpful.
For example:
CODE SNIPPET main.py def is_won(board,player): # check row for row in board: if(row.count(player) == 3): return True # check columns for i in range(3): if ("".join(board[j][i] for j in range(3))).