2 Replies - 256 Views - Last Post: 03 November 2017 - 05:39 AM Rate Topic: -----

#1 a.palazzo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-November 17

The game of nim stick version with three piles

Posted 02 November 2017 - 05:21 PM

I'm having trouble with coding my game, in the game there are three piles that all have a different number 3,5, and 7 when you play the game and you pick a pile and subtract the sticks you chose to remove from it you should expect to get the right number if you pick pile 3 and remove 2 you should get 5 but instead you get 1 meaning that the coding only recognizes pile 1 and doesn't register the other two piles, another problem I have is with [def humanLegalMove():] reason being I can't get the coding for player 2 to subtract from the net number after player 1 choses and get it to repeat with both players until one player wins.

import random
import time
print("""************ NIM GAME   ***********
************ Game Start ***********
************ The rules  ***********
-----------------------------------------------------
There are three piles of sticks each a different number
You can remove 1 to 3 sticks a turn from one pile.
The player that removes the final stick is the winner.
-----------------------------------------------------""")




Player1 = str(input("Enter your name player1: "))
Player2 = str(input("Enter your name player2: "))
A = 3
B = 5
C = 7
howMany = 0
gameover = False


def stickpile(A,B,C):
   stickpile = (A,B,C)
   return stickpile


def howMany():
    howMany = int(input("How many sticks do you want to remove from the pile (1 to 3): "))
def choosePile():
   choosePile = str(input("Please choose which pile you want to pick A, B, or C: "))
    


def removingsticksPlayer1():
    global stickpile
    global choosePile
    stickpile = choosePile
    return stickpile
    return choosePile

def removingsticksPlayer2():
    global stickpile
    global choosePile
    stickpile = choosePile
    return stickpile
    return choosePile

[def humanLegalMove():
    global howMany
    global choosePile
    legalMove = False
    while not legalMove:
        print("It's your turn, ",Player1)
        choosePile = str(input("Pick which pile you want to remove sticks from A,B,C: "))
        if choosePile == A:
            print(A)
        elif choosePile == B:
            print(B)/>/>
        elif choosePile == C:
            print(C)
        howMany = int(input("How many sticks do you want to remove?(from 1 to 3) "))
        if howMany>3 or howMany<1:
            print("Enter a number between 1 and 3.")
        if A - howMany:
            print("sticks left: ", A - howMany)
        elif B - howMany:
            print("sticks left: ", B - howMany)
        elif C - howMany:
            print("sticks left: ", C - howMany)]

        else: legalMove = True
    
        [print("It's your turn, ",Player2)
        choosePile = str(input("Pick which pile you want to remove sticks from A,B,C: "))
        if choosePile == A:
            print(A)
        elif choosePile == B:
            print(B)/>/>
        elif choosePile == C:
            print(C)
        howMany = int(input("How many sticks do you want to remove?(from 1 to 3) "))
        if howMany > 3 or howMany < 1:
            print("Enter a number between 1 and 3.")
        elif
        if A - howMany:
            print("sticks left: ", A - howMany)
        elif B - howMany:
            print("sticks left: ", B - howMany)
        elif C - howMany:
            print("sticks left: ", C - howMany)]
            

def checkWinner(Player1):
    if stickpile == 1:
        print(Player1,"wins.")
        global gameover
        gameover = True
        return gameover

def resetGameover():
    global gameover
    gameover = False
    return gameover

def game():
    while gameover==False:
        print("It's ",Player1,"turn. The number of straws left: ",removingsticksPlayer1())
        checkWinner(Player1)
        if gameover==True:
            break
        humanLegalMove()        
        print("The number of straws left: ",removingsticksPlayer2())
        checkWinner(Player2)

def playAgain():
    answer = input("Do you want to play again?(y/n)")
    resetGameover()
    while answer == "y":
        game()
    else:
        print("Thanks for playing the game")

game()
playAgain()


This post has been edited by baavgai: 03 November 2017 - 05:06 AM
Reason for edit:: tagged


Is This A Good Question/Topic? 0
  • +

Replies To: The game of nim stick version with three piles

#2 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 320
  • View blog
  • Posts: 1,056
  • Joined: 27-December 13

Re: The game of nim stick version with three piles

Posted 03 November 2017 - 01:55 AM

Please place your code between code tags, icon "CODE" is found right above the input window.

Early in your code you do:
def stickpile(A,B,C):
    stickpile = (A,B,C)
    return stickpile

and later:
global stickplie

It seems to me that you use same name for more than one purpose (and the purpose of the three-line function completely eludes me).
I see more potential issues, but it is difficult to be sure when indentation is not shown; thus, use code tags.

This post has been edited by DK3250: 03 November 2017 - 01:57 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6979
  • View blog
  • Posts: 14,602
  • Joined: 16-October 07

Re: The game of nim stick version with three piles

Posted 03 November 2017 - 05:39 AM

Ok, stop right now. Get rid of all globals. Start again.

I'd begin like:
import random
import time

# you have many functions to define up here
# no globals!!!

def play_again():
    answer = input("Do you want to play again?(y/n)")
    # no resetGameover()
    return answer == "y":
        # no, no, f no game()
    # print("Thanks for playing the game")

def play_one_game(player1, player2):
    pile = init_stickpile()
    current_player = player1
    gameover = False
    while not gameover:
        print("It's ",current_player,"turn.")
        if current_player == player1:
            pile = user_choose(pile, current_player)
        else:
            pile = computer_choose(pile, current_player)
        show_pile(pile)
        gameover = check_for_win(pile, current_player)
        # if we're still going, change current player
        if not gameover:
            if current_player == player1:
                current_player = player2
            else:
                current_player = player1

def game():
    show_intro()
    player1, player2 = init_players()
    gameover = False
    while not gameover:
        play_one_game(player1, player2)
        gameover = not play_again()
    print("Thanks for playing the game")

game()



The only real game state you have in this is the current pile and the current player. So, you pass those to functions that rely on or affect such values.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1