I am suppose was to incorporate arrays in my NIM game java code, so that we can allow the user to decide on how many heaps are in play. I am stuck and have no clue how to do so.
Heap Class
public class Heap {
private int heapSize;
public Heap(int size)
{
heapSize=size;
}
public boolean remove(int size)
{
boolean removed;
if(size<1 || size>heapSize)
{
System.out.println("Pick a number between 1 and "+heapSize);
removed = false;
} else {
heapSize-=size;
removed = true;
}
return removed;
}
public boolean isEmpty()
{
return heapSize==0;
}
public void displaySize()
{
System.out.print("\t"+heapSize);
}
public int getSize()
{
return heapSize;
}
}
Nim Class
import java.util.Random;
import java.util.Scanner;
public class Nim {
private Heap heapA;
private Heap heapB;
private Heap heapC;
private Random randomNumbers;
public Nim()
{
heapA=new Heap(10);
heapB=new Heap(10);
heapC=new Heap(10);
randomNumbers = new Random ();
}
public boolean playerMove()
{
System.out.print("\nSelect a heap: ");
char heapLetter = input.next().charAt(0);
Heap tempHeap;
switch(heapLetter)
{
case 'a':
case 'A':
tempHeap=heapA;
break;
case 'b':
case 'B':
tempHeap=heapB;
break;
case 'c':
case 'C':
tempHeap=heapC;
break;
default:
System.out.println("Invalid heap letter, select a, b or c");
return false;
}
if(tempHeap.isEmpty())
{
System.out.println("Heap "+heapLetter+" is empty, pick another");
return false;
}
System.out.print("How many do you want to remove? ");
int number = input.nextInt();
return tempHeap.remove(number);
}
public void gameMove()
{
Heap tempHeap;
boolean valid;
char select;
do {
int heapId = randomNumbers.nextInt(3);
switch(heapId)
{
case 0:
tempHeap=heapA;
select='A';
break;
case 1:
tempHeap=heapB;
select='B';
break;
default:
tempHeap=heapC;
select='C';
break;
}
if(tempHeap.isEmpty())
{
valid=false;
} else {
valid=true;
int size=randomNumbers.nextInt(tempHeap.getSize())+1;
tempHeap.remove(size);
System.out.println("Computer takes "+size+" from heap "+select);
}
} while(!valid);
}
public void printHeaps()
{
System.out.println("\tA\tB\tC");
heapA.displaySize();
heapB.displaySize();
heapC.displaySize();
System.out.println();
}
public boolean gameDone()
{
return heapA.isEmpty() && heapB.isEmpty() && heapC.isEmpty();
}
}
Client Class
public class Assign3 {
public static void main(String[] args)
{
System.out.println("Welcome to the NIM cup\nWe play by the normal play rules");
Nim nimGame = new Nim();
boolean validMove;
boolean playersTurn;
do {
playersTurn=true;
nimGame.printHeaps();
validMove=nimGame.playerMove();
if(validMove && !nimGame.gameDone())
{
playersTurn=false;
nimGame.printHeaps();
nimGame.gameMove();
}
} while(!nimGame.gameDone());
if(playersTurn)
{
System.out.println("Congrats, you win");
} else {
System.out.println("Sorry, you lose");
}
}
}
It is suppose to look like something along the lines of this:
Welcome to the NIM extreme challenge
We play by the normal play rules
How many heaps do you want to have? 100
Error: pick a number between 1 and 26: 0
Error: pick a number between 1 and 26: 3
A B C
10 10 10
Select a heap: c
How many do you want to remove? 10
A B C
10 10 0
Computer takes 6 from heap A
A B C
4 10 0
Select a heap: b
How many do you want to remove? 5
A B C
4 5 0
Computer takes 4 from heap B
A B C
4 1 0
Select a heap: A
How many do you want to remove? 1
A B C
3 1 0
Computer takes 1 from heap B
A B C
3 0 0
Select a heap: A
How many do you want to remove? 2
A B C
1 0 0
Computer takes 1 from heap A
Sorry: you lose
Good bye, thank you for playing