-3
Hello! Facing the construction of an old game in Python, I’m doing a victory function, which will alert which of the players won the match. Below is the game algorithm under construction:
def players(n):
if n % 2 == 0:
return 'X'
else:
return 'O'
def play():
paper = [ ' ' ] * 9
draw = 0
qj = players(0)
n = 0
while draw != 9:
try:
pos = int(input('{} - Insira a posição que deseja jogar: '.format(qj)))
if 1 <= pos <= 9:
paper[pos - 1] = players(n)
n = 1 - n
draw += 1
for i in range(0,len(paper),3):
print(' {} | {} | {} '.format(paper[i],paper[i+1],paper[i+2]))
if i < 6:
print('-----------')
else:
print('O valor se encontra entre 1 e 9.')
except ValueError:
print('Valor inválido! Tente novamente.')
if draw == 9:
print('\nEmpate!')
And this is the win function (which does not go very well with software):
def winner(paper):
if (paper[0][0] == 'X' and paper[1][0] == 'X' and paper[2][0] == 'X') or (paper[3][0] == 'X' and paper[4][0] == 'X' and paper[5][0] == 'X') or (paper[6][0] == 'X' and paper[7][0] == 'X' and paper[8][0] == 'X'):
print('O cliente venceu!')
elif (paper[0][0] == 'X' and paper[3][0] == 'X' and paper[6][0] == 'X') or (paper[1][0] == 'X' and paper[4][0] == 'X' and paper[7][0] == 'X') or (paper[2][0] == 'X' and paper[5][0] == 'X' and paper[8][0] == 'X'):
print('O cliente venceu!')
elif (paper[0][0] == 'X' and paper[4][0] == 'X' and paper[8][0] == 'X') or (paper[2][0] == 'X' and paper[4][0] == 'X' and paper[6][0] == 'X'):
print('O cliente venceu!')
elif (paper[0][0] == 'O' and paper[1][0] == 'O' and paper[2][0] == 'O') or (paper[3][0] == 'O' and paper[4][0] == 'O' and paper[5][0] == 'O') or (paper[6][0] == 'O' and paper[7][0] == 'O' and paper[8][0] == 'O'):
print('O servidor venceu!')
elif (paper[0][0] == 'O' and paper[3][0] == 'O' and paper[6][0] == 'O') or (paper[1][0] == 'O' and paper[4][0] == 'O' and paper[7][0] == 'O') or (paper[2][0] == 'O' and paper[5][0] == 'O' and paper[8][0] == 'O'):
print('O servidor venceu!')
elif (paper[0][0] == 'O' and paper[4][0] == 'O' and paper[8][0] == 'O') or (paper[2][0] == 'O' and paper[4][0] == 'O' and paper[6][0] == 'O'):
print('O servidor venceu!')
else:
return False
Does anyone know how to solve?
Here there is already a way to do this (see function
jogo_terminou
, is basically that)– hkotsubo
Right. I took a look there (y)
– jmb2001