Statement
You should write a program in the Python language, version 3, which will 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 part number and m for the maximum number of pieces that can be removed in a round. To ensure that the computer always wins, consider the two possible scenarios for starting the game:
If n is multiple of (m + 1) , the computer must 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 strategy of the computer to win is to always leave a number of pieces that are multiple of (m + 1) to the player. If this is not possible, you should take out as many pieces as possible.
Your job, then, is to implement the Game and get the computer to use the winning strategy.
Your Program
With the purpose of the already defined EP, a doubt that must arise at the moment is how to model the game in a way that can be implemented in Python 3, corresponding strictly to the specifications described so far.
In order to facilitate its work and allow the automatic correction of the exercise, we present below a model, that is, a general description of a set of functions that solves the problem proposed in this EP. Although other approaches are possible, you must meet exactly what is defined below for automatic job matching to work correctly.
The program should implement:
-
A
computador_escolhe_jogada
function that receives as parameters the n and m numbers described above and returns an integer corresponding to the next move of the computer according to with the winning strategy. -
A
usuario_escolhe_jogada
function that receives the same parameters, prompts the player to report his move and check if the value entered is valid. If the value entered is valid, the function should return it; otherwise, you must prompt the user again to enter a valid move. -
A
partida
function that does not receive any parameters prompts the user to enter the n and m values and starts the game by switching between plays the computer and the user (that is, calls to the two previous functions). The choice of the initial move must be made according to the winning strategy, as said before. At each play, the current state of play must be printed on the screen, ie how many pieces were removed on the last play and how many are left on the table. When the last part is removed, this function prints the message " The computer won!" Or " You won! " as the case may be.
Note that for this to work, your program should always "remember" what is the number of pieces currently on the board and what is the maximum number of pieces to take out on each move.
Championships
As we all know, a single round of a game is not enough to define who is the best player. So, since the partida
function is working, you should create another function called campeonato
. This new role must carry out three consecutive games of the game and, at the end, show the score of those three matches and indicate the winner of the championship. The scoreboard should be printed on the form
Code
computador = 0
usuario = 0
rodada = 0
def computador_escolhe_jogada(n, m):
global computador
n = n - m
if (n == 1):
print(" ")
print("O computador tirou %s peça." % n)
print("Agora restam %s peças no tabuleiro." % n)
print(" ")
if (n == 0):
print ("Fim do jogo! O computador ganhou!")
partida()
else:
print(" ")
print("O computador tirou %s peça." % m)
print("Agora restam %s peças no tabuleiro." % n)
print(" ")
if (n == 0):
print ("Fim do jogo! O computador ganhou!")
partida()
return n
return m
def usuario_escolhe_jogada(n, m):
global usuario
print(" ")
n_user = int(input("Quantas peças você vai tirar? "))
print("Voce tirou %s peças." % n_user)
if (n_user <= m):
n = n - m
print(" ")
print("Agora restam apenas %s peças no tabuleiro." % n)
else:
while (n_user > m):
print("Oops! Jogada inválida! Tente de novo.")
print(" ")
n_user = int(input("Quantas peças você vai tirar? "))
if (n == 0):
print ("Vitoria do usuario")
return n_user
return n
return m
def partida():
global computador
global usuario
global rodada
while(rodada <= 3):
rodada = rodada + 1
if (rodada <= 3 ):
print(" ")
print("**** Rodada %s ****" % rodada)
print(" ")
n = int(input("Quantas peças? "))
m = int(input("Limite de peças por jogada? "))
if (((n )%(m + 1)) == 0):
while (n > 0):
print(" ")
print("Voce começa!")
usuario_escolhe_jogada(n,m)
if n > 0:
n = n - m
computador_escolhe_jogada(n,m)
n = n - m
computador = computador + 1
else:
print(" ")
print("Computador Começa!!")
while( n > 0):
computador_escolhe_jogada(n,m)
computador = computador + 1
n = n - m
if n > 0:
usuario_escolhe_jogada(n,m)
n = n - m
else:
print("**** Final do campeonato! ****")
print(" ")
print("Fim de Campeonato: Computador %s x 0 Usuario " % computador)
break
print("Bem-vindo ao jogo do NIM! Escolha:")
print(" ")
print("1 - para jogar uma partida isolada ")
tipo_jogo = int(input("2 - para jogar um campeonato "))
print(" ")
if ( tipo_jogo == 1 ):
print("Voce escolheu partida isolada!")
if ( tipo_jogo == 2):
print("Voce escolheu um campeonato!")
partida()
else:
pass
My problem is when the user starts the game, for example: if I put N = 3 and M = 2 , the user should start, maximum of pieces (two), the computer also removes two, leaving a number of negative pieces on the table and closes the championship.