• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nim game logic  RSS feed

 
D Anthony
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I have setup a nim game which allows players to take a stone from three piles. I believe i have successfully setup the deduction from the first pile and have now extend this to the other piles, but i find when i'm deducting from pile one its also doing the same in the other piles and then goes into negative numbers which keep going on and on, am not sure if this this correct. Please see the codes below to assist me in what i'm not doing right.









Many thanks in advance
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see some while loops without any way for them to end once entered because the condition that allows execution to enter the loop is not changed inside the loop.

How are you trying to debug the code?  Add some print statements that print the values of variables as their values are changed an as they are used so you can see what the computer sees when the code executes.

How do you execute the code for testing?  I do not see a main() method.
 
D Anthony
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, yes, i do have a main() method

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The GetPlayer class is missing.

Make sure all the needed classes are posted.
 
D Anthony
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And i believe this is where i'm going wrong as not entirely sure how to constrcut the while loop to determine the winner!
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to constrcut the while loop 

Describe what the purpose of the loop is.
 
D Anthony
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:The GetPlayer class is missing.

Make sure all the needed classes are posted.


Ahh sorry please see GetPlayer class and ComputerPlayer class. Thanks




 
D Anthony
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
how to constrcut the while loop 

Describe what the purpose of the loop is.


The purpose of the loop is to determine number of stones left in a pile in order to determine the winner
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose of the loop is to determine number of stones left in a pile in order to determine the winner

Can you list the steps to take to solve that?


How does a user play the game?  There needs to be user instructions.
What buttons need to be pressed? What data entered???
 
D Anthony
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Human v Computer
Game initial state
Player move
Valid move (yes/no)
Invalid move go back to player
Valid move (yes, game over/declare winner)(no,new game state)

 
D Anthony
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
The purpose of the loop is to determine number of stones left in a pile in order to determine the winner

Can you list the steps to take to solve that?


How does a user play the game?  There needs to be user instructions.
What buttons need to be pressed? What data entered???


The GetPlayer class is providing option for the player to select game type
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does a user play the game with this program? 
What clicks need to be made?  What data entered?

What do you need to do to see the problem?


How are you trying to debug the program?
 
D Anthony
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:How does a user play the game with this program? 
What clicks need to be made?  What data entered?

What do you need to do to see the problem?


How are you trying to debug the program?


The main() method instantiate the the GUI class which provdes a JOption Message box asking the user to select human or computer
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
user to select human or computer

Ok, what's next?
 
D Anthony
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then the game initial state with 3 piles of random generated numbers between 1-100. The user then deduct from a choosen pile.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The user then deduct from a choosen pile.

How does a user chose a pile?
How is a deduction made from a pile?
 
D Anthony
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The three piles are visible in the gui so the user can choose from any and deduction is made by typing a new number less than the random generated numbers in the piles.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D Anthony wrote:
Norm Radder wrote:How does a user play the game with this program? 
What clicks need to be made?  What data entered?

What do you need to do to see the problem?


How are you trying to debug the program?


The main() method instantiate the the GUI class which provdes a JOption Message box asking the user to select human or computer

You haven't answered any of Norm's questions. The more effort you show the more likely you will get someone to volunteer time to help you.

"How are you trying to debug the program?"
  • Pencil and paper
  • Using a debugger such as the one provided with Eclipse
  • Riddling the code with print statements
  •  
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    deduction is made by typing a new number less than the random generated numbers in the piles.

    ok.    Note: The code needs to check that what the user has entered to be valid before using it.
    After changing a number in an input field and pressing Enter, what  happens?
    What should the user do next?
     
    D Anthony
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ahh, sorry, yes i'm using pencil and paper and a flowchart which i used to design the process, hope this helps
     
    D Anthony
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Norm Radder wrote:
    deduction is made by typing a new number less than the random generated numbers in the piles.

    ok.    Note: The code needs to check that what the user has entered to be valid before using it.
    After changing a number in an input field and pressing Enter, what  happens?
    What should the user do next?


    Yes, i believe this where i am getting confusing, checking the valid move.
    After changing the number in the input field then check if game over and declare winner
     
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    checking the valid move.

    What is a valid move?

    After changing a number in an input field and pressing Enter, 
    What should the user do next?
     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap. The goal of the game is to avoid being the player who must remove the last object.

    A small piece of the write up on Wikipedia/Nim. Further down the page is an example of a game being played. I found this helpful as I have not encountered the game of Nim before.
     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Here's one of your bugs: you use "firstPile.getText()" for all the piles.
     
    D Anthony
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    compute the nimSum of the piles int NimSum = p1^p2^p3;

    If NimSum is not zero, a smart strategy is possible
    compute the three numbers p1^NimSum,p2^NimSum,p3^NimSum

    Find one of these that is numerically smaller than the original pile number, pick this as your move, then use to update p1,p2,p3 as the case may be.
     
    D Anthony
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:
    Here's one of your bugs: you use "firstPile.getText()" for all the piles.


    Ahh, thanks for pointing out this
     
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Should the values from the text field be validated before being used?
     
    D Anthony
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The values from the text fields are valid as they are integers being passed in, the valid move is player can only take from one pile, if the nimSum is not zero, a smart strategy is possible but if the smart strategy is not possible(eg nimSum was zero) then do a random legal move like the computer- player. Hope this makes sense and answer your question.
     
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The posted code does not validate any of the user's input.  For example it accepts a negative number.
    The input should be validated before it is used.

    How does the program prevent (or at least detect if) the user from changing more than 1 text field?
     
    D Anthony
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, i believe this is where i'm struggling and need some guidiance as to how to implement this. I'm not sure if i understand how to implement the strategy to stop user from taking from more than text field and be validated before. The posted code is the closest i got to it.
     
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One approach would be to have a separate listener for each text field.
    Update the pile from the field associated with the listener and leave the piles for other fields unchanged.
    Before using the value from the text field, make sure it has a valid value: less than current pile and >= 0.
     
    D Anthony
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks, but i thougth i'm already adding each text field to separate listener as follow, or this is not what you meant?

     
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    this is not what you meant?

    No.  Those statements all use the same listener.  Each field should have its own listener.  You can Use an anonymous inner class to provide the separate listeners.
    https://docs.oracle.com/javase/tutorial/uiswing/events/generalrules.html#innerClasses
     
    Carey Brown
    Saloon Keeper
    Posts: 3329
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As you work through this I think you'll find that you'll be able to eliminate some redundant code if you use arrays instead of discrete variables. E.g.
     
    D Anthony
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you!
     
    D Anthony
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks, i learnt the basic of Java sometime ago and hadn't done anythng with it since am back doing this MS but i guess i will figure out as i go along:-)
     
    Norm Radder
    Rancher
    Posts: 2240
    28
     
    D Anthony
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, just trying to search for the most basic advice possible.
     
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just be sure to let everyone know so no one wastes time making responses that have already been made on another site.
     
    D Anthony
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I posted there after you have responded to me, i just felt like i needed some simplier answer hence why i have posted there after.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!