Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

closed as off-topic by Marc-Andre, Quill, Mat's Mug Apr 14 at 20:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Marc-Andre, Quill, Mat's Mug
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Welcome to Code Review! I voted to close your question as off-topic since you request to add a functionality in your code. Please see the help center for more information. – Marc-Andre Apr 14 at 19:30
up vote 0 down vote accepted

At the beginning of your code you have

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 ();
}

I believe you would accomplish what you want if you request how many heaps the user wants and create an ArrayList of objects. Array sizes must be dictated at compile time, but ArrayLists are auto-resizing.

You want something like...

ArrayList<Heap> heapList = new ArrayList<Heap>();
heapList.add(new Heap(10));

I'm a little rusty and have never used these, myself. So this is all theory for me. You will need to account for and control how many heaps you make, etc.

I recommend you google "java arraylist" and look at several examples.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.