Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Nim game

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone help me out with the isValidMove method, how can I make sure numbers from both users and computer are not more than what is remaining. An invalid move is a number less than one, a number greater than three, or anumber that is larger than the number of marbles on the table. Use a privatestatic method called isValidMove() to make this determination. This method returns a boolean value.

 
B Carter
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I make sure computer doesnt choose a number greater than the marbles remaining
 
Saloon Keeper
Posts: 8434
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way would be to pass "remaining" into getUserSelection() that then passes it into isValidMove() that uses it in an "if" statement.
 
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you really been told to make that method static?
 
Campbell Ritchie
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't keep creating Scanners to read from System.in. You only need one Scanner reading System.in; if you put it into a utility class, you can use it for everything in the same application.
 
B Carter
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the requirement for the isValidMove is for it to be static.
I have updated my code where I now monitor each players turn as below. But I run into the problem where if there are less marble than 3 the computer sometimes takes more than whats left which sometimes lets the user win


public static void main(String[] args)
   {
       System.out.println("************** Welcome to the game of Nim *******************");
       System.out.println("The object of this game is to make me take the last marble off the table");
       System.out.println("You must take at least 1 and you can take up to 3 marbles on your turn");
       System.out.println("You can go first");

       Scanner scan = new Scanner(System.in);
       String input;
       boolean userTurn = true;
       int remainingMarbles = 13;

       while (remainingMarbles > 0) {
           while (remainingMarbles > 0) {
               remainingMarbles -= getUserSelection();
               userTurn = !userTurn;
               System.out.println("There are " + (remainingMarbles -= Math.min(getComputerSelection(), remainingMarbles)) + " marbles on the table");
               userTurn = userTurn;
           }
       }
       if (userTurn){
           System.out.println("Congratulations! you won!");
       } else {
           System.out.println("Hard luck you lose!");
       }
   }
 
Sheriff
Posts: 16564
272
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When solving a problem, it helps to have some kind of mental model to guide you in organizing your thoughts.

For example, you already are employing something of a mental model by using different methods to do specific jobs.

There are a number of mental models that strategy could be related to. Here are just some examples:
1. Assemblies in a car - each assembly has its own job to do.
2. Rooms in a house - each room has its own purpose and use
3. Parts of your body - each body part has its own purpose

See how it works? Now, lets talk about a mental model for that job of checking if the number of marbles chosen is valid.

I've been watching Downton Abbey lately so I'll use the mental model of a butler. Imagine your method is a butler. Just for giggles, let's say your butler's name is Carson. You might have this kind of conversation:



(after you've briefed Carson on the general rules of the game)

You: "Carson, now that you know the rules, would you mind helping us judge if the number of marbles taken on each turn was allowed? We're terribly bad at this sort of thing, you see."

Carson: "Beg your pardon, M'Lord, but I am very busy and cannot stand around watching two people play a silly game. I must get on with my other duties."

You: "Oh, don't worry about that! You needn't stand around watching, you just need to be in earshot and we'll tell you how many marbles we intend to take on each turn. With your brilliant mathematical mind, you can tell us if that would be a legal move."

Carson: "Well, as I understand it, the number of marbles remaining will also be changing, isn't that correct, M'Lord?"

You: "Oh, of course, you're right! Very well then, we'll tell you how many marbles are remaining and how many we intend to take. Then you can do the math and tell us if that would be a legal move. How does that sound?"

Carson: "Very good, M'Lord. I'll leave you to it then. Just shout out those numbers, I'll just be around the corner tending to my other duties."


In this analogy, you are the main method and Carson is obviously the method that checks if a move is legal. OK, admittedly that was taking the looong way to the village square but the idea is you have to give the method all the information it needs to do its calculations. That is, you need to pass in the number of marbles remaining and how many marbles you intend to take.


 
Junilu Lacar
Sheriff
Posts: 16564
272
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:give the method all the information it needs to do its calculations. That is, you need to pass in the number of marbles remaining and how many marbles you intend to take.


I suggest this method signature:

(This also shows an example of how you would write a JavaDoc comment)
 
Marshal
Posts: 8050
569
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP

I did not go into details with your code, but one thing stood out very quickly.

Variable


If you and couple of your teammates (let's say Allen and Bob) playing cards and chitchatting so you forgot whose turn is next.

And somebody raises a question: "Whose turn is next?"

What would be your answer if you were know its Rob's turn next?
 
Junilu Lacar
Sheriff
Posts: 16564
272
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
And somebody raises a question: "Whose turn is next?"

What would be your answer if you were know its Rob's turn next?


Never mind that, think how nonsensical this conversation would be:

Person 1: Whose turn is it?

Person 2: Yes! (or No!)

Observer:  

(Cue Abbott & Costello's "Who's on First?" bit)
 
Junilu Lacar
Sheriff
Posts: 16564
272
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This program is perfect for demo on Test-Driven Development.

I've started a thread here for questions and discussion: https://coderanch.com/t/745113/engineering/Test-driving-game-Nim

Didn't take me long really, although I've been popping back and forth between working on this and working on my actual work. My local history shows I created the Nim.java file at 3:43pm ET and it's now 6:09pm ET. So I couldn't have been working on it for more than 90 minutes since I had a couple of short meetings in that interval.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic