Win function in Tic-tac-toe - Python

Asked

Viewed 136 times

-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)

  • Right. I took a look there (y)

2 answers

0

I did so:

def winner(paper):

    wins = [    [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 pos1, pos2, pos3 in wins:
        if paper[pos1] == 'X' and paper[pos2] == 'X' and paper[pos3] == 'X':
            print('O Cliente venceu! Parabéns!')
        elif paper[pos1] == 'O' and paper[pos2] == 'O' and paper[pos3] == 'O':
            print('O Servidor venceu! Parabéns!')
    return False

0

Your win function could check if one of the teams (X or O) was the winner, see only:

def winner(paper, team):
    for i in range(3):

        # Horizontal
        if paper[0][i] == paper[1][i] == paper[2][i] == team:
            return True

        # Vertical
        if paper[i][0] == paper[i][1] == paper[i][2] == team:
            return True

    # Diagonal
    if paper[0][0] == paper[1][1] == paper[2][2] == team:
        return True

    # Diagonal
    if paper[0][2] == paper[1][1] == paper[2][0] == team:
        return True

    return False

Now it’s easy to determine the winner:

paper = [
    ['O','X','X'],
    ['X','O','X'],
    ['X','O','O']
]

if winner(paper, 'O'):
    print('O Servidor Venceu!')
elif winner(paper, 'X'):
    print('O Cliente Venceu!')
else:
    print('Ninguem Venceu!')
  • Okay. Thank you! (y)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.