Objective To be able to solve problems using while and do-while loops. Overview
ID: 664692 • Letter: O
Question
Objective
To be able to solve problems using while and do-while loops.
Overview
Complete Programming Project P6.5 The Game of Nim on page 302 of the textbook. You must implement the variant described in the textbook except that the client decides if the computer should play smart, and also decides who plays first.
Start-up code for your Nim class is provided. It includes the entire public interface, as well declarations of the instance variables and a stub for the helper method play(). The helper is invoked by the constructor to play the constructed game to completion. You must complete these methods and provide additional suitable helper methods.
Specific Requirements
The play() method must be implemented using a while loop. It must use two other helper methods, one for the computer’s play, and another for the human’s play. It must also maintain a current transcript of the game. For example, here’s the final transcript of a completed game:
NIM Initial Pile Size 15
CYBER plays SMART
HUMAN Plays First
HUMAN Takes 3 Pile Size 12
CYBER Takes 5 Pile Size 7
HUMAN Takes 3 Pile Size 4
CYBER Takes 1 Pile Size 3
HUMAN Takes 1 Pile Size 2
CYBER Takes 1 Pile Size 1
HUMAN Takes 1 Pile Size 0
CYBER Wins
2.The helper method for the human’s play must prompt the player for the number of marbles that the player wishes to take; use JOptionPane.showInputDialog().The input should repeated in a do-while loop until the player enters a legal move. The prompt must show sufficient information to enable the player to choose a winning move.
3.The helper method for the computer’s play should use two additional helpers: for random play, and for smart play.
4.Modify the client to prompt the player whether the computer should play smart, and if the player wishes to take the first turn; use JOptionPane.showConfirmDialog() calls.
5.Modify the client to allow several games to be played. Use a do-while loop that gives the player the option to play another game or to quit.
CODE FOR NIM CLASS:
//An instance of this class represents a game of Nim played
// between a human player (HUMAN) and the computer (CYBER)
import java.util.*;
import javax.swing.*;
public class Nim
{
//A Random-Number Generator, available to all methods
public static final Random generator = new Random();
//Instance Variables
private int pileSize; //# Marbles in the Pile
private String transcript; //Transcript of the game
private boolean playersTurn; //Indicates whose turn
private boolean smartPlay; //Indicates the game level
//Constructor
//Parameter computerSmart - true : optimal computer play
// false : random computer play
//Parameter humanGoesFirst- true : human plays first
// false : computer plays first
public Nim(boolean computerSmart, boolean humanGoesFirst)
{
this.play();
}
//Returns true if the human player has won, false otherwise
public boolean playerWon()
{
return generator.nextBoolean();
}
//Override: Returns a transcript of this game of Nim
public String toString()
{
return "Nim";
}
//============Helper Methods ====================================
//Mutator:
// Play a game of Nim to completion by alternating turns between
// the human player (HUMAN) and the computer player (CYBER)
//At each player's (HUMAN or CYBER) turn:
// -Player decides the number of marbles to take from the pile
// -The size of the pile is reduced by this number of marbles
// -The game transcript is updated
// -The turn is alternated
//The game ends when the number of marbles has been reduced to 0
private void play()
{
}
}
CODE FOR NIMCLIENT MAINCLASS:
import java.util.*;
import javax.swing.*;
public class NimClient
{
public static void main(String[] args)
{
//Decide randomly if the computer plays SMART
// and if the human player makes the 1st move
Random coinFlip = new Random();
boolean smart = coinFlip.nextBoolean();
boolean first = coinFlip.nextBoolean();
//Play a game of Nim
Nim game = new Nim(smart, first);
JOptionPane.showMessageDialog(null, game);
}
}
NIM Initial Pile Size 15
CYBER plays SMART
HUMAN Plays First
HUMAN Takes 3 Pile Size 12
CYBER Takes 5 Pile Size 7
HUMAN Takes 3 Pile Size 4
CYBER Takes 1 Pile Size 3
HUMAN Takes 1 Pile Size 2
CYBER Takes 1 Pile Size 1
HUMAN Takes 1 Pile Size 0
CYBER Wins
Explanation / Answer
Completed code:-
Nim.java
package com.tutai;
//An instance of this class represents a game of Nim played
//between a human player (HUMAN) and the computer (CYBER)
import java.util.*;
import javax.swing.*;
public class Nim
{
//A Random-Number Generator, available to all methods
public static final Random generator = new Random();
//Instance Variables
private int pileSize; //# Marbles in the Pile
private String transcript; //Transcript of the game
private boolean playersTurn; //Indicates whose turn
private boolean smartPlay; //Indicates the game level
//Constructor
//Parameter computerSmart - true : optimal computer play
// false : random computer play
//Parameter humanGoesFirst- true : human plays first
// false : computer plays first
public Nim(boolean computerSmart, boolean humanGoesFirst)
{
this.play();
}
//Returns true if the human player has won, false otherwise
public boolean playerWon()
{
return generator.nextBoolean();
}
//Override: Returns a transcript of this game of Nim
public String toString()
{
return "Nim";
}
public int getComputersMove(int left)
{
return (int)(Math.random()*2)+1;
}
//============Helper Methods ====================================
//Mutator:
// Play a game of Nim to completion by alternating turns between
// the human player (HUMAN) and the computer player (CYBER)
//At each player's (HUMAN or CYBER) turn:
// -Player decides the number of marbles to take from the pile
// -The size of the pile is reduced by this number of marbles
// -The game transcript is updated
// -The turn is alternated
//The game ends when the number of marbles has been reduced to 0
private void play()
{
String str = JOptionPane.showInputDialog(null, "Enter the number of elements to start.","Nim game", 1);
int left=0;
if(str!=null){
left = Integer.parseInt(str);
}
do
{
int computer=getComputersMove(left);
JOptionPane.showMessageDialog(null,"Computer has taken "+computer,"Nim game",1);
left-=computer;
JOptionPane.showMessageDialog(null,"Now we have "+left+" left.","Nim game",1);
if(left<=0)
{
JOptionPane.showMessageDialog(null,"Computer has won!","Nim game",1);
return;
}
str = JOptionPane.showInputDialog(null, "What is your move? [1 or 2]","Nim game", 1);
if(str!=null){
int person=Integer.parseInt(str);
while(person!=1 && person!=2)
{
str = JOptionPane.showInputDialog(null, person+" not allowed, choose either 1 or 2.","Nim game", 1);
person=Integer.parseInt(str);
}
left-=person;
JOptionPane.showMessageDialog(null,"Now we have "+left+" left.","Nim game",1);
if(left<=0)
{
JOptionPane.showMessageDialog(null,"You have won!","Nim game",1);
return;
}
}
}while(left>0);
}
}
NimClient.java
package com.tutai;
import java.util.*;
import javax.swing.*;
public class NimClient
{
public static void main(String[] args)
{
boolean smart,first;
int reply = JOptionPane.showConfirmDialog(null,"Shall Computer play SMART?","Nim Game",JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION){
smart = true;
}else{
smart=false;
}
reply = JOptionPane.showConfirmDialog(null,"Do you wish to take the 1st turn?","Nim Game",JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION){
first = true;
}else{
first=false;
}
//Play the game of Nim
Nim game = new Nim(smart, first);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.