0

How do I write a program with multiple functions that makes the outcome look like the following:


Let's play NIM! NIM_State {a:10, b:10, c:10}

your move: b 10 removing 10 from b gives NIM State {a:10, b:0, c:10}

computer move: c 5 removing 5 from c gives NIM State {a:10, b:0, c:5}

your move: c 5 removing 5 from c gives NIM State {a:10, b:0, c:0}

computer move: a 8 removing 8 from a gives NIM State {a:2, b:0, c:0}

your move: a 1 removing 1 from a gives NIM State {a:1, b:0, c:0}

computer move: a 1 removing 1 from a gives NIM State {a:0, b:0, c:0}

You win! Computer loses.


I've tried to build up a structure, but the nim state is resuming back to a:10, b:10, c:10, and I don't know how to fix that.

'''play the game NIM against a user'''
import random

user = 1
computer = 2

# A function for players to make a move
def make_move(whoseturn, a, b, c):
    letter = ['a','b','c']
    user_choice = input(str('your move:'))
    user_letter_choice = user_choice[0]
    user_number_choice = int(user_choice[2:])

    residual = 10-user_number_choice

    total = [a, b, c]
    n = len(total)
    for i in range(n):
        if total[i]>=0:
            if whoseturn == user:
                print('removing',user_number_choice, 'from', user_letter_choice, 'gives')
                if user_letter_choice == 'a':
                    a = a-user_number_choice
                elif user_letter_choice == 'b':
                    b = b-user_number_choice
                else:
                    c = c-user_number_choice
                nim_state='a:{la},b:{lb},c:{lc}'.format(la = a, lb = b, lc =c)
                return nim_state

            elif whoseturn == computer:
                if user_letter_choice == 'a' and residual > 0:
                    computer_letter_choice = random.choice(letter)
                    computer_number_choice = random.randint(1,residual)
                    print('computer move:', computer_letter_choice,computer_number_choice)
                    print('removing',computer_number_choice,'from',computer_letter_choice,'gives')
                    if computer_letter_choice == 'a':
                        a = a-computer_number_choice
                    elif computer_letter_choice == 'b':
                        b = b-computer_number_choice
                    else:
                        c = c-computer_number_choice

                elif user_letter_choice == 'a' and residual==0:
                    letter = ['b','c']
                    computer_letter_choice = random.choice(letter)
                    computer_number_choice = random.randint(1,10)
                    print('computer move:', computer_letter_choice,computer_number_choice)
                    print('removing',computer_number_choice,'from',computer_letter_choice,'gives')
                    if computer_letter_choice == 'a':
                        a = a-computer_number_choice
                    elif computer_letter_choice == 'b':
                        b = b-computer_number_choice
                    else:
                        c = c-computer_number_choice

                elif user_letter_choice == 'b' and residual > 0:
                    computer_letter_choice = random.choice(letter)
                    computer_number_choice = random.randint(1,residual)
                    print('computer move:', computer_letter_choice,computer_number_choice)
                    print('removing',computer_number_choice,'from',computer_letter_choice,'gives')
                    if computer_letter_choice == 'a':
                        a = a-computer_number_choice
                    elif computer_letter_choice == 'b':
                        b = b-computer_number_choice
                    else:
                        c = c-computer_number_choice

                elif user_letter_choice == 'b' and residual==0:
                    letter = ['a','c']
                    computer_letter_choice = random.choice(letter)
                    computer_number_choice = random.randint(1,10)
                    print('computer move:', computer_letter_choice,computer_number_choice)
                    print('removing',computer_number_choice,'from',computer_letter_choice,'gives')
                    if computer_letter_choice == 'a':
                        a = a-computer_number_choice
                    elif computer_letter_choice == 'b':
                        b = b-computer_number_choice
                    else:
                        c = c-computer_number_choice

                elif user_letter_choice == 'c' and residual > 0:
                    computer_letter_choice = random.choice(letter)
                    computer_number_choice = random.randint(1,residual)
                    print('computer move:', computer_letter_choice,computer_number_choice)
                    print('removing',computer_number_choice,'from',computer_letter_choice,'gives')
                    if computer_letter_choice == 'a':
                        a = a-computer_number_choice
                    elif computer_letter_choice == 'b':
                        b = b-computer_number_choice
                    else:
                        c = c-computer_number_choice

                elif user_letter_choice == 'c' and residual==0:
                    letter = ['a','b']
                    computer_letter_choice = random.choice(letter)
                    computer_number_choice = random.randint(1,10)
                    print('computer move:', computer_letter_choice,computer_number_choice)
                    print('removing',computer_number_choice,'from',computer_letter_choice,'gives')
                    if computer_letter_choice == 'a':
                        a = a-computer_number_choice
                    elif computer_letter_choice == 'b':
                        b = b-computer_number_choice
                    else:
                        c = c-computer_number_choice
                nim_state='a:{la},b:{lb},c:{lc}'.format(la = a, lb = b, lc =c)
                return nim_state
    return False

# A function to declare the winner of the game
def declare_winner(whoseturn):
    if (whoseturn == computer):
        print("You win! Computer loses.")
    else:
        print("You lose! Computer wins.")
    return

# A function to run the game
def play_game(whoseturn, a, b, c):
    print("-" * 25)
    print('Let\'s play NIM!')
    print("NIM_State NIM_State {a:10, b:10, c:10}")
    while make_move!=False: 
        if whoseturn == user: 
            print(make_move(user, a, b, c))
            print()
            print(make_move(computer, a, b, c))
            print()
        else:
            print(make_move(computer, a, b, c))
            print()
            print(make_move(user, a, b, c))
            print()
    declare_winner(whoseturn)
    print("-" * 25)

play_game(user, 10, 10, 10)

Can someome help me with this? Thank you!

0 Answers0