Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

A função "change_player( )" não está funcionando

Estava funcionando normalmente, mas depois que refatorei o código parou de funcionar. Alguém tem alguma ideia? Aceito criticas construtivas ao código também.


from os import system

def display_game(game_board):
    print(f' {game_board[6]} | {game_board[7]} | {game_board[8]}')
    print('--- --- ---')
    print(f' {game_board[3]} | {game_board[4]} | {game_board[5]}')
    print('--- --- ---')
    print(f' {game_board[0]} |

 {game_board[1]} | {game_board[2]}\n')

def display_exemple():
    print(f'These are the spaces that the numbers represent')
    print(f' 7 | 8 | 9')
    print(f'--- --- ---')
    print(f' 4 | 5 | 6')
    print(f'--- --- ---')
    print(f' 1 | 2 | 3')

def change_player(player):

    if player == 1:
        print("Now it's O turn!")

    if player == -1:
        print("Now it's X turn")

    player = player * -1


def do_a_play(player, round_counter):

    display_exemple()
    get_play = input('Choose a position between 1 and 9 to play')
    play = int(get_play)

    if game_board[play - 1] == '.':
        if player == 1:
            game_board[play - 1] = 'X'

        if player == -1:
            game_board[play - 1] = 'O'

        round_counter += 1

    else:
        system('clear')
        print('You can not play there')
        do_a_play()

    system('clear')


def check_win(game_board):

    horizontal_win = game_board[0] == game_board[1] == game_board[2] != '.' or game_board[3] == game_board[4] == game_board[5] != '.' or game_board[6] == game_board[7] == game_board[8] != '.'
    vertical_win   = game_board[0] == game_board[3] == game_board[6] != '.' or game_board[1] == game_board[4] == game_board[7] != '.' or game_board[2] == game_board[5] == game_board[8] != '.'
    diagonal_win   = game_board[0] == game_board[4] == game_board[8] != '.' or game_board[2] == game_board[4] == game_board[6] != '.'

    win  = horizontal_win or vertical_win or diagonal_win
    draw = round_counter == 9

    if win:
        return 'winner'

    elif draw:
        return 'draw'

    else:
        return False

player = 1
round_counter = 0

while True:

    change_player(player)

    print(player)

    game_board = ['.','.','.',
                  '.','.','.',
                  '.','.','.']

    result = check_win(game_board)

    if result:
        print(f'Game over! We have a {result}')
        break

    do_a_play(player, round_counter)

    display_game(game_board)
2 respostas
solução!

Olá Lucas.

Na função do_a_play, na parte do else onde a função do_a_play é chamada, falou incluir os parâmetro, ficando assim do_a_play(player, round_counter).


def do_a_play(player, round_counter):

    display_exemple()
    get_play = input('Choose a position between 1 and 9 to play')
    play = int(get_play)

    if game_board[play - 1] == '.':
        if player == 1:
            game_board[play - 1] = 'X'

        if player == -1:
            game_board[play - 1] = 'O'

        round_counter += 1

    else:
        system('cls')
        print('You can not play there')
        do_a_play(player, round_counter)

    system('cls')

E também a declaração do game_board deve ficar fora do while, dessa maneira não perdemos as jogadas que já aconteceram.

player = 1
round_counter = 0
game_board = ['.','.','.',
                '.','.','.',
                '.','.','.']

while True:

    change_player(player)

    print(player)



    result = check_win(game_board)

    if result:
        print(f'Game over! We have a {result}')
        break

    do_a_play(player, round_counter)

    display_game(game_board)

Espero ter ajudado, mas se aparecer mais alguma questão não hesite em trazer. E não encontrei nenhum ponto de melhora, mas qualquer coisa eu aviso aqui, parabéns pelo código.

Bons Estudos.

Valeu pela dica! Consegui resolver o problema da função change_player também, mesmo não tendo entendido direito o porquê dela não estar funcionando kkkk

#! /usr/bin/python3

from os import system

def display_game(game_board):
    print(f' {game_board[6]} | {game_board[7]} | {game_board[8]}')
    print('--- --- ---')
    print(f' {game_board[3]} | {game_board[4]} | {game_board[5]}')
    print('--- --- ---')
    print(f' {game_board[0]} | {game_board[1]} | {game_board[2]}\n')


def display_exemple():
    print(f'These are the spaces that the numbers represent')
    print(f' 7 | 8 | 9')
    print(f'--- --- ---')
    print(f' 4 | 5 | 6')
    print(f'--- --- ---')
    print(f' 1 | 2 | 3')


def change_player(player):

    if player == 1:
        print("Now it's O turn!")

    if player == -1:
        print("Now it's X turn")

    return player * -1

def do_a_play(player, round_counter):

    display_exemple()
    get_play = input('Choose a position between 1 and 9 to play')
    play = int(get_play)

    if game_board[play - 1] == '.':
        if player == 1:
            game_board[play - 1] = 'X'

        if player == -1:
            game_board[play - 1] = 'O'

        round_counter += 1

    else:
        system('clear')
        print('You can not play there')
        do_a_play(player.round_counter)

    system('clear')


def check_win(game_board):

    horizontal_win = game_board[0] == game_board[1] == game_board[2] != '.' or game_board[3] == game_board[4] == game_board[5] != '.' or game_board[6] == game_board[7] == game_board[8] != '.'
    vertical_win   = game_board[0] == game_board[3] == game_board[6] != '.' or game_board[1] == game_board[4] == game_board[7] != '.' or game_board[2] == game_board[5] == game_board[8] != '.'
    diagonal_win   = game_board[0] == game_board[4] == game_board[8] != '.' or game_board[2] == game_board[4] == game_board[6] != '.'

    win  = horizontal_win or vertical_win or diagonal_win
    draw = round_counter == 9

    if win:
        return 'winner'

    elif draw:
        return 'draw'

    else:
        return False

player = 1
round_counter = 0

game_board = ['.','.','.',
              '.','.','.',
              '.','.','.']

while True:

    player = change_player(player)

    print(player)

    result = check_win(game_board)

    if result:
        print(f'Game over! We have a {result}')
        break

    do_a_play(player, round_counter)

    display_game(game_board)