2
I’m implementing a game, against the computer, that follows this format:
Be
N
the initial part number andM
the maximum number of pieces that is possible withdraw in a round. To ensure that the computer wins always, it is necessary to consider the two possible scenarios for the start of the game:If
n
is multiple of(m+1)
, the computer should be "generous" and invite the player starting the match; otherwise the computer takes the start the game. Once the game has started, the strategy of the computer to win consists of always leaving a number of pieces multiple(m+1)
to the player. If this is not possible, should take as many pieces as possible.
As part of the exercise, the following functions must be implemented:
computador_escolhe_jogada
receivesN
andM
and returns an integer corresponding to the next computer move according to the winning strategy.usuario_escolhe_jogada
that receives the same parameters, asks the player to inform his move and checks its validity, returning the given value.partida
which does not receive any parameter, prompts the user to enter the values ofN
andM
and starts the game by switching between computer and user plays. 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 piece is removed, this function prints on the screen the message "The computer won!" or "You won!" as the case may be.
At the end of 3 matches, I need to show the result of the "championship", with the number of matches won by each player, but I cannot make the function work.
def computador_escolhe_jogada (n,m):
n = n - m
print ("O computador tirou ",m," peças")
print ("Agora restam",n," peças no tabuleiro.")
return n
return m
if n == 0:
print ("Vitoria do computador")
C = C + 1
def usuario_escolhe_jogada(n,m):
x = int(input("Digite quantas peças deseja retirar: "))
while not( x <= m ):
x = int(input("Digite quantas peças deseja retirar: "))
n = n - m
print ("Voce tirou ",m," peças")
print ("Agora restam",n," peças no tabuleiro.")
return n
return m
if n == 0:
print ("Vitoria do usuario")
U = U + 1
U = 0
C = 0
def partida():
n = int(input("Digite quantas peças: "))
m = int(input("Digite o limite de peças por jogada " ))
if n % (m + 1) != 0:
while (n > 0):
usuario_escolhe_jogada(n,m)
if n > 0:
n = n - m
computador_escolhe_jogada(n,m)
else:
while( n > 0):
computador_escolhe_jogada(n,m)
if n > 0:
n = n - m
usuario_escolhe_jogada(n,m)
Please edit the question and indent the code correctly. Remember that in Python, code with incorrect indentation is syntax error. Try pasting your code as it comes from the editor, and use the "format as code" button from the interface here. I have the impression that you pasted the code and only "pushed" the lines that started in the 0 column forward.
– jsbueno
@jsbueno is right... Since Python does not have code block markers, it is basically impossible for us to know where each
if
ordef
ends... I also recommend explaining the game - and the exact problem you are having - yourself, rather than pasting the statement. Much of what’s there is irrelevant to solving the exact problem you’re encountering.– Gabe
Your question is unclear. What exactly is your difficulty? You say you "can’t make the function work". Which function?
– Luiz Vieira
Possible duplicate of Python - NIM game
– Woss
@Andersoncarloswoss Actually this other one that’s a duplicate of this.
– Victor Stafusa
@Victorstafusa, yes, but it was edited erroneously and made the answer impossible. As there was answered, I marked this as duplicate to relate the questions.
– Woss