27.1k views
2 votes
Write a program that plays X/O game. The X/O game is played on a 3x3 grid. The game is played by two players, who take turns. each player is asked to enter the indices of the location to be filled with X or O mark. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark, change the players after every successful move, and pronounce the winner.

For

1 Answer

3 votes

A program in computing is a set of instructions that tells a computer what to do. It's like a recipe for the computer, telling it exactly what steps to take to complete a specific task.

def draw_board(board):

"""Prints the current state of the game board."""

print(board[0] + '|' + board[1] + '|' + board[2])

print('-+-+-')

print(board[3] + '|' + board[4] + '|' + board[5])

print('-+-+-')

print(board[6] + '|' + board[7] + '|' + board[8])

def

check_winner(board, player):

"""Checks if the given player has won."""

winning_combos = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]

for combo in winning_combos:

if all(board[i] == player for i in combo):

return True

return False

User Podshumok
by
8.0k points

No related questions found