Difficulty in developing a little game

Asked

Viewed 1,093 times

2

I’m implementing a game, against the computer, that follows this format:

Be N the initial part number and M 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 receives N and M 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 of N and M 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)
  • 1

    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 is right... Since Python does not have code block markers, it is basically impossible for us to know where each if or def 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.

  • Your question is unclear. What exactly is your difficulty? You say you "can’t make the function work". Which function?

  • 4

    Possible duplicate of Python - NIM game

  • @Andersoncarloswoss Actually this other one that’s a duplicate of this.

  • @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.

Show 1 more comment

1 answer

1

Although you have made the C and U variables global Python requires you to identify as global within the function. It can be done like this:

global C
C = C + 1

Thus, the function will understand that it is not a local variable that is not initialized. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.