5
Enunciation
You must 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, must follow the winning strategy described above.
Be n the initial part number and m the maximum number of pieces it is possible to remove in a round. To ensure that the computer always wins, you need to consider the two possible scenarios for the start of 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 computer’s winning strategy is to always leave a number of tiles that is multiple of (m+1) to the player. If this is not possible, you should take as many pieces as possible.
Your job, then, will be to implement the Game and make the computer use the winning strategy.
Your Program
With the objective of the EP already defined, a doubt that should arise at this time is how to model the game so that it can be implemented in Python 3 corresponding rigorously to the specifications described so far.
To facilitate their 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, it is necessary to meet exactly what is set below for the automatic work correction to work properly.
The program must implement:
A function
computador_escolhe_jogada
which takes the numbers as parameters n and m described above and returns an integer corresponding to the next computer move according to the winning strategy.A function
usuario_escolhe_jogada
which receives the same parameters, prompts the player to inform his move and checks whether the given value is valid. If the given value is valid, the function must return it; otherwise, it must ask the user again to enter a valid move.A function
partida
which does not receive any parameter, prompts the user to enter the values of n and m and starts the game by switching between plays of 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 stated above. With each move, the current state of the game must be printed on the screen, that is, how many pieces were removed in the last move and how many remain on the table. When the last tile is removed, this function prints the message on the screen "The computer won!" or "You won!" as appropriate.
Note that in order for this to work, your program must always "remember" what the number of pieces is currently on the board and what is the maximum number of pieces to remove on each turn.
Championships
As we all know, a single round of a game is not enough to define who is the best player. So, once the function partida
is working, you must create another function called campeonato
. This new function must hold three consecutive matches of the game and, at the end, show the score of these three matches and indicate the winner of the championship. The scoreboard should be printed in 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 match, for example: if I put N=3 and M=2, the user must start, when user takes out the maximum parts (two), the computer also takes out two, getting a number of negative pieces on the table and ends the championship.
Not the same as: http://answall.com/questions/177176?
– Woss
It is the same, but I have been asked a new question. His game does not follow the requested parameters and information is missing when comparing the print, besides the issue is not solved.
– Mário Rodeghiero
Is that all the statement of the problem? What would be "take a piece"?
– Woss
The statement is huge, but in the link that Voce reported above, already contains a part. According to the code and the print, when starting the match, the game asks the "n" number of pieces and "m" maximum of pieces.
– Mário Rodeghiero
Great, but essensial to understand the problem. I do not know why removed from the other.
– Woss
I’ll try to edit and add to help.
– Mário Rodeghiero
I have already read the other, but in my view it is essential to have. I will analyze your code here.
– Woss
added the question.
– Mário Rodeghiero
Why are there several
return
in each function?– Woss
Teacher’s answer : You are not returning anything in the Xxx_choice move functions. You will need to change your program so that match() call the functions alternately by receiving from each the value of the current move.
– Mário Rodeghiero
Curiosity: What course is and what is your level of knowledge in programming?
– Woss
Coursera, Python from USP. I’m a beginner friend.
– Mário Rodeghiero
I edited the question to an acceptable format, within Sopt standards.
– Woss
Thanks for the support! I noticed that the calculation of the computer’s move is wrong, I put a print(n) at the beginning of the computer_chooses function and I realized that n ends up being negative, and this impacts on the result, regardless of who starts it.
– Mário Rodeghiero
I’m responding slowly, keep up with you.
– Woss
As a matter of curiosity, I started to check the possibility of whoever wins in a NIM match from a video (I think this one). For NIM with a stack and taking out up to three pieces, there is only one possibility that whoever wins will be second. I made this demonstration in Java, in this project here
– Jefferson Quesado