Nim

Assignment

These are my requirement.

  1. Please do not use array
  2. Please do not modify Nim.java class and methods in other class( You can modify those methods little bit)
  3. The tester shows that I should enter 1 for stupid mode, and 2 for smart. Do notmake a random choice, as the book says.
  4. please write the comment for codes
  5. Please do not use Ioexception and JOptionPane

Computer.java

public class Computer{

private int mode;

private int choice;

public Computer(int m){

mode = m;

choice = -1;

}

public void move(int marblesLeft){

// your code here

}

public int getChoice(){

return choice;

}

} 

Game (3).java

public class Game{

private int marbles;

private Human humanPlayer;

private Computer computerPlayer;

public Game(int difficulty){

// your code here

}

public void play(){

// your code here

}

// you may wish to add more methods here

}

Human.java

import java.util.Scanner;

public class Human{

private int choice;

private Scanner input;

public Human(){

input=new Scanner(System.in);

choice = -1;

}

public void move(){

// your code here

}

public int getChoice(){

return choice;

}

} 

Nim.java

import java.util.Scanner;

public class Nim{

public static void main(String[] args){

System.out.println(“Welcome to Nim death match!”);

Scanner input = new Scanner(System.in);

System.out.println(“What difficulty level do you want to playt 1/2?”);

int level = input.nextInt();

Game g = new Game(level);

g.play();

System.out.println(“Thanks for playing!”);

}

}

Solution

 Computer.java

 import java.util.Random;

public class Computer{

private int mode;

private int choice;

public Computer(int m){

mode = m;

choice = -1;

}

public void move(int marblesLeft){

if (marblesLeft == 1) {

choice = 1; // the only choice

return;

}

if (mode == 2) {

// smart mode

// take off enough to make the left be power of 2 minus 1

int k = 1;

while (k <= marblesLeft) {

k = k * 2;

}

if (marblesLeft != k – 1) {

choice = marblesLeft – k / 2 + 1;

return;

}

// otherwise, choose a random one

}

// take a random number between 1 and marblesLeft / 2

Random r = new Random();

choice = r.nextInt(marblesLeft / 2) + 1;

}

public int getChoice(){

return choice;

}

} 

Game.java 

import java.util.Random;

public class Game{

private int marbles;

private Human humanPlayer;

private Computer computerPlayer;

public Game(int difficulty){

// generate the initial size of the pipe

Random r = new Random();

marbles = r.nextInt(91) + 10; // between 10 and 100

// initialize the players

humanPlayer = new Human();

computerPlayer = new Computer(difficulty);

}

public void play(){

Random r = new Random();

// determine which player goes first

// the human and computer take turns until no more marbles left

int value = r.nextInt(2);

if (value == 0) { // computer goes first

while (!computerTurn() && !humanTurn());

} else { // human first

while (!humanTurn() && !computerTurn());

}

}

private boolean computerTurn() { // computer’s turn, return true if lose

System.out.println(“Remaining marbles: ” + marbles);

computerPlayer.move(marbles);

int choice = computerPlayer.getChoice();

System.out.println(“Computer takes ” + choice + ” marble(s)!”);

marbles -= choice;

// check if computer lost

if (marbles == 0) {

System.out.println(“Computer took the last marble. Computer lost!”);

return true;

}

return false;

}

private boolean humanTurn() { // human’s turn, return true if lose

System.out.println(“Remaining marbles: ” + marbles);

humanPlayer.move(marbles);

int choice = humanPlayer.getChoice();

marbles -= choice;

// check if human lost

if (marbles == 0) {

System.out.println(“Human took the last marble. Human lost!”);

return true;

}

return false;

}

} 

Human.java

import java.util.Scanner;

public class Human{

private int choice;

private Scanner input;

public Human(){

input=new Scanner(System.in);

choice = -1;

}

public void move(int marblesLeft){

while (true) {

if (marblesLeft > 3) {

System.out.printf(“Choose [1-%d] marble(s) to take: “, marblesLeft / 2);

} else {

System.out.print(“You can only choose 1 marble to take: “);

}

choice = input.nextInt();

if (marblesLeft <= 2 && choice == 1) {

break;

}

// make sure the choice is at least 1 and at most half of the marbles

if (choice < 1 || choice > marblesLeft / 2) {

System.out.println(“Invalid choice…”);

} else {

break;

}

}

}

public int getChoice(){

return choice;

}

} 

Nim.java

import java.util.Scanner;

public class Nim{

public static void main(String[] args){

System.out.println(“Welcome to Nim death match!”);

Scanner input = new Scanner(System.in);

System.out.println(“What difficulty level do you want to playt 1/2?”);

int level = input.nextInt();

Game g = new Game(level);

g.play();

System.out.println(“Thanks for playing!”);

}

}