0% found this document useful (0 votes)
33 views4 pages

Python Jogo Do Galo

Como fazer o jogo do galo no python

Uploaded by

mmoreiraportela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Python Jogo Do Galo

Como fazer o jogo do galo no python

Uploaded by

mmoreiraportela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

board = [

['_', '_', '_'],

['_', '_', '_'],

['_', '_', '_'],

def draw_board(board):

for row in board:

print(' '.join(row))

def update_board(pos, icon, board):

row, col = pos

board[row][col] = icon

def get_available_spots(board):

available_spots = []

for row in range(3):

for col in range(3):

if board[row][col] == '_':

available_spots.append((row, col))

return available_spots

def check_win(icon, board):

# check across rows

for row in range(3):


if all(i == icon for i in board[row]):

return True

# check across columns

for col in range(3):

if all(board[i][col] == icon for i in range(3)):

return True

# check across left-right diagonal

if all(board[i][j] == icon for i,j in zip(range(3), range(3))):

return True

# check across right-left diagonal

if all(board[i][j] == icon for i,j in zip(range(3), range(3)[::-1])):

return True

return False

player = 'X'

while True:

# check for a stalemate

available_spots = get_available_spots(board)

if len(available_spots) == 0:
print('Stalemate. Restart the game')

break

print(f'Current player is: {player}')

draw_board(board)

# ask for position

while True:

try:

row = int(input('Row: '))

col = int(input('Col: '))

except ValueError:

continue

if (row, col) in available_spots:

update_board((row, col), player, board)

break

else:

print('Spot is already taken. Try choosing another spot.')

# check winning condition

if check_win(player, board):

print(f'Winner is {player}')

break
player = 'O' if player == 'X' else 'X'

print()

You might also like