169k views
4 votes
Write a nim game in java. Requirements:

1. The game should have 3 piles, each pile consist of at least 1 and at most 7 stones. At least one stone should be picked each round.

1 Answer

3 votes

Final answer:

To write a nim game in Java with the given requirements, you can create a class called NimGame. Inside this class, you can define three variables to represent the piles, and use a loop and conditional statements to handle the game logic.

Step-by-step explanation:

To write a nim game in Java with the given requirements, you can create a class called NimGame. Inside this class, you can define three variables to represent the piles, and use a loop and conditional statements to handle the game logic.

Here is an example code snippet to get you started:

class NimGame {
int pile1;
int pile2;
int pile3;

public NimGame() {
// initialize the piles with random values between 1 and 7
pile1 = (int) (Math.random() * 7) + 1;
pile2 = (int) (Math.random() * 7) + 1;
pile3 = (int) (Math.random() * 7) + 1;
}

public void playGame() {
// implement the game logic here
}

public static void main(String[] args) {
NimGame game = new NimGame();
game.playGame();
}
}

Remember to fill in the playGame() method to prompt user input, update the piles, and check for win conditions. You can use a Scanner object to read user input, and use if statements to ensure at least one stone is picked each round.

User Yogesh Jagdale
by
7.2k points