NIM Game - PYTHON

0

My question was not so clear from the last time, so I decided to re-read it here, and I think I know where I'm going wrong, but I do not know what I'm missing.

In the question says:

  

"You should write a program in the Python language, version 3, which   allow a "victim" to play the NIM against the computer. The computer,   of course, should follow the winning strategy described above.

     

Let n be the starting number of pieces and m the maximum number of pieces that is   possible to withdraw in a round. To ensure that your computer   always consider the two possible scenarios for the beginning   of the game:

     

If n is a multiple of (m + 1), the computer should be "generous" and invite the player to start the game ;    Otherwise, the computer takes the initiative to start the game . Once the game is started, the computer's winning strategy consists   always leave a number of parts that is multiple of (m + 1) to the   player. If this is not possible, you should take the maximum number of   possible parts.

     

Your job will then be to implement the Game and make the computer use the winning strategy .

I have read the instructions several times, including discussions within the site explaining that I have to use exactly what is requested in the exercise, expressions, and the broker keeps talking that the computer should start the game, and when I test the the computer starts.

I made the code over the code of Anderson Carlos Woss ( link ) is this:

tipo_jogo = 0

def computador_escolhe_jogada(n, m):
    if n <= m:
        return n
    else:
        quantia = n % (m+1)
        if quantia > 0:
            return quantia
        return m

def usuario_escolhe_jogada(n, m):
    jogada = 0
    while jogada == 0:
        jogada = int(input("Quantas peças você vai tirar? "))
        if jogada > n or jogada < 1 or jogada > m:
            print("Oops! Jogada inválida! Tente de novo.")
            jogada = 0
    return jogada

def partida(): 
    print(" ")
    n = int(input("Quantas peças? "))
    m = int(input("Limite de peças por jogada? "))

    is_computer_turn = True
    if False:
        print("O computador começa!")
    if n % (m+1) == 0: is_computer_turn = False
    if True:
        print("Você começa!")

    while n > 0: 
        if is_computer_turn:
            jogada = computador_escolhe_jogada(n, m)
            is_computer_turn = False
            print("Computador tirou {} peças.".format(jogada))
        else:
            jogada = usuario_escolhe_jogada(n, m)
            is_computer_turn = True
            print("Você tirou {} peças.".format(jogada))
        n = n - jogada
        print("Agora restam {} peças no tabuleiro.\n".format(n))
    if is_computer_turn:
        print("Você ganhou!")
        return 1
    else:
        print("Fim de jogo! O computador ganhou!")
        return 0

def campeonato():
    usuario = 0
    computador = 0
    for _ in range(3):
        vencedor = partida()
        if vencedor == 1:
            usuario = usuario + 1
        else:
            computador = computador + 1
    print("******* Final do campeonato! *******")
    print("Placar: Você  {} x {}  Computador".format(usuario, computador))

while tipo_jogo == 0:
    print("Bem-vindo ao jogo do NIM! Escolha:")
    print(" ")
    print("1 - Para jogar uma partida isolada")
    print("2 - Para jogar um campeonato")

    tipo_jogo = int(input("Sua opção: "))
    print(" ")

    if tipo_jogo == 1:
        print("Voce escolheu partida isolada!")
        partida()
        break
    if tipo_jogo == 2:
        print("Voce escolheu um campeonato!")
        campeonato()
        break
    else:
        print("Opção inválida!")
        tipo_jogo = 0

Here's how the game should be printed:

$ > python3 jogo_nim.py

Bem-vindo ao jogo do NIM! Escolha:

1 - para jogar uma partida isolada
2 - para jogar um campeonato 2

Voce escolheu um campeonato!

**** Rodada 1 ****

Quantas peças? 3
Limite de peças por jogada? 1

Computador começa!

O computador tirou uma peça.
Agora restam 2 peças no tabuleiro.

Quantas peças você vai tirar? 2

Oops! Jogada inválida! Tente de novo.

Quantas peças você vai tirar? 1

Você tirou uma peça.
Agora resta apenas uma peça no tabuleiro.

O computador tirou uma peça.
Fim do jogo! O computador ganhou!

**** Rodada 2 ****

Quantas peças? 3
Limite de peças por jogada? 2

Voce começa!

Quantas peças você vai tirar? 2 
Voce tirou 2 peças.
Agora resta apenas uma peça no tabuleiro.

O computador tirou uma peça.
Fim do jogo! O computador ganhou!

**** Rodada 3 ****

Quantas peças? 4
Limite de peças por jogada? 3

Voce começa!

Quantas peças você vai tirar? 2
Voce tirou 2 peças.
Agora restam 2 peças no tabuleiro.

O computador tirou 2 peças.
Fim do jogo! O computador ganhou!

**** Final do campeonato! ****

Placar: Você 0 X 3 Computador

Are there any logic errors in what is requested? What could I do to fix it?

    
asked by anonymous 13.04.2017 / 22:09

0 answers