Question
Q.how to add in get winner code? get winner code error import random import os os.environ['PYTHONUNBUFFERED'] = '1' def create_board(size): Create a new game board
Q.how to add in get winner code? get winner code error
import random
import os
os.environ['PYTHONUNBUFFERED'] = '1'
def create_board(size):
"""Create a new game board with the specified size."""
board = []
for row in range(size):
board.append(['-'] * size)
return board
def print_board(board):
"""Print the current game board."""
size = len(board)
print(' ', end='')
for col in range(size):
print(col, end=' ')
print()
for row in range(size):
print(row, end=' ')
for col in range(size):
print(board[row][col], end=' ')
print()
def get_player_names():
"""Prompt the user for the names of the two players."""
player1 = input('Enter player 1 name: ')
player2 = input('Enter player 2 name: ')
return player1, player2
def get_board_size():
"""Prompt the user for the size of the game board."""
while True:
size = int(input('Size of game board: '))
if size % 2 == 0 or size < 3:
print('Invalid size! Please enter an odd number that is at least 3.')
else:
return size
def get_random_player():
"""Randomly choose which player goes first."""
return random.choice(['X', 'O'])
def get_tile_coordinates(board, player):
"""Prompt the user for the coordinates of the next tile to place."""
while True:
coordinates = input(player + ', place your ' + player + ' tile: ')
try:
row1, col1, row2, col2 = [int(c) for c in coordinates]
except:
print('Wrong input format! Please enter two pairs of row and column numbers separated by spaces.')
continue
if not (0 <= row1 < len(board) and 0 <= col1 < len(board) and 0 <= row2 < len(board) and 0 <= col2 < len(board)):
print('Invalid coordinates! Please enter two pairs of row and column numbers within the board.')
continue
if board[row1][col1] != '-' or board[row2][col2] != '-':
print('Square occupied!! Please re-enter.')
continue
if not ((row1 == row2 and abs(col1 - col2) == 1) or (col1 == col2 and abs(row1 - row2) == 1)):
print('Invalid tiles!! Please re-enter.')
continue
return row1, col1, row2, col2
def play_game():
"""Play a single game of XO domino-tile."""
player1, player2 = get_player_names()
size = get_board_size()
board = create_board(size)
current_player = get_random_player()
while True:
print_board(board)
row1, col1, row2, col2 = get_tile_coordinates(board, current_player)
board[row1][col1] = current_player
board[row2][col2] = current_player
if not is_possible_move(board):
break
current_player = 'X' if current_player == 'O' else 'O'
print_board(board)
winner = get_winner(board)
if winner is None:
print("It's a tie!! Rematch...")
play_game()
else:
print(winner + ' is the winner!!')
def is_possible_move(board):
"""Check if there is at least one possible move left on the board."""
for row in range(len(board)):
for col in range(len(board)):
if board[row][col] == '-':
# Check if there is an adjacent tile of the same symbol
if (row > 0 and board[row-1][col] == board[row][col]) \
or (col > 0 and board[row][col-1] == board[row][col]) \
or (row < len(board)-1 and board[row+1][col] == board[row][col]) \
or (col < len(board)-1 and board[row][col+1] == board[row][col]):
return True
return False
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started