The Game of Nim is a well-known game with a number of variants. One particularly
ID: 3649607 • Letter: T
Question
The Game of Nim is a well-known game with a number of variants. One particularly interesting version plays like this: Assume you have a large pile of marbles. Two players alternate taking marbles from the pile. In each move, a player is allowed to take between 1 and half of the total marbles. So, for instance, if there are 64 marbles in the pile, any number between and including 1 and 32 is a legal move. Whichever player takes the last marble loses.Write a program called Nim.java in which a human player plays against the computer. The program should first ask for how many marbles are in the starting pile. Then the program should ask for a character for who will start the game ('p' for player, 'c' for computer). The game will then alternate between the computer and the player, asking the player for how many marbles they want to take (making sure they take a positive integer between 1 and half the pile and making them pick another number if they do it wrong).
The computer should always take enough marbles to make the size of the pile a power of 2 minus 1 - such as 3, 7, 15, 31, 63, etc. If that's not possible, the computer will take one marble.
You should find that the computer, if it gets to go first, will always win. Similarly, if you go first and know the strategy, you'll always win!
INPUT: An integer representing the size of the pile of marbles, a char representing who goes first ('p' or 'c'), then an undetermined number of integers from the player representing how many marbles the player is taking.
OUTPUT: The steps of the game and the size of the pile will be output. At the end of the game, the winner is announced.
Here is an example :
Nim.java:
The Game of Nim
Number of marbles are in the pile: 100
Who will start? (p or c): c
The pile has 100 marbles in it.
The computer takes 37 marble(s).
The pile has 63 marbles in it.
How many marbles do you want to take? (1-31): 4
The pile has 59 marbles in it.
The computer takes 28 marble(s).
The pile has 31 marbles in it.
How many marbles do you want to take? (1-15): 13
The pile has 18 marbles in it.
The computer takes 3 marble(s).
The pile has 15 marbles in it.
How many marbles do you want to take? (1-7): 6
The pile has 9 marbles in it.
The computer takes 2 marble(s).
The pile has 7 marbles in it.
How many marbles do you want to take? (1-3): 2
The pile has 5 marbles in it.
The computer takes 2 marble(s).
The pile has 3 marbles in it.
How many marbles do you want to take? (1-1): 1
The pile has 2 marbles in it.
The computer takes 1 marble(s).
The pile has 1 marbles in it.
How many marbles do you want to take? (1-1): 1
The computer wins!
Please make sure that all the variables are defined and the code works .please Thanks
Explanation / Answer
import java.util.Random; import javax.swing.JOptionPane; /** * Represents a game of Nim, where players take turns removing marbles from a pile. * The player that takes the last marble wins the game. */ public class Nim { public static int COMPUTER = 0; public static int HUMAN = 1; private Player computer; private Player human; private int marbleCount; private int turn; // whose turn it is private int winner; /** * Constructs a Nim game. There are two players, one is the computer and the other is human. * The player who gets to go first is chosen at random. The number of marbles in the pile is * generated randomly between 10 and 100. A message is displayed showing how many marbles the * starts with. The contructor calls the play() method to start the game. */ public Nim() { Random rand = new Random(); // assign values for all instance fields before play() is called computer = new Player(COMPUTER); human = new Player (HUMAN); marbleCount = rand.nextInt(91) + 10; turn = rand.nextInt(2); winner = HUMAN; // JOptionPane - Show number of marbles JOptionPane.showInputDialog(marbleCount); play(); } /** * The play method continues game play until the pile is reduced to 0. Whoever takes the last marble * loses the game. At the beginning of each play, the number of marbles left is displayed. The player * whose turn it is calls the Player play method, sending it the number of marbles left. When the player * takes marbles, the marbleCount is decreased. When the marbles are gone, the winner of the game is * displayed. */ public void play() // while there are marbles left // if else for turn // if its the computers turn you are going to call the computer's play method, // last method in the player file // call computer;s play method, send it marbleCount, and // reduce marblecount by the amount the computer takes // Then you need to check for a winner // switch the turn // else human's turn //call the human's play method - send it marbleCount // reduce marbleCount by the human's take // check for a winner // switch the turn { while(marbleCount >= 0) JOptionPane.showMessageDialog(null,"There are " + marbleCount + "marbles left"); if(turn = 0) turn = 1; else turn = 0; // At the end of play (when the loop exits), the winner is displayed if (winner == COMPUTER) JOptionPane.showMessageDialog(null,"Computer wins!"); else JOptionPane.showMessageDialog(null,"Human wins!"); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.