Difficulty in developing a game

1

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

  

Be N the starting part number and M the maximum number of parts 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 must be "generous" and invite   the player to start the match; Otherwise, the computer   begin the game. Once the game is started, the strategy of the   computer to win is to always leave a number of pieces   that is multiple of (m+1) to the player. If this is not possible,   you should take the maximum number of possible pieces.

As part of the exercise, it is mandatory to implement the following functions:

  • computador_escolhe_jogada receives N and M and returns an integer corresponding to the next move of the computer according to the winning strategy.

  • usuario_escolhe_jogada that receives the same parameters, asks the player to report his move and checks its validity, returning the value informed.

  • partida that does not receive any parameters, prompts the user to enter the values of N and M and start the game by switching between computer and user plays. 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.

At the end of 3 matches, I need to show the "championship" result, with the number of matches won by each player, but I can not make it 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)
    
asked by anonymous 13.01.2017 / 05:12

1 answer

1

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

global C
C = C + 1

So the function will understand that this is not a local variable that is not initialized. :)

    
14.01.2017 / 23:48