I'm creating some code for a google challenge and I need to write the following function: Say you have a board game x places long, and you have a three sided dye with 'stay', 'right', 'and left'. I need to right a function that when given the amount of dice rolls and number of places and the board will return how many different ways you can get there in the correct amount of dice rolls. (Without going off the edge of the bored of course) Then Moodulas 123454321. (Don't ask me why I mod the answer to 123454321, I don't know). I have whipped up the following code, but it throws a null-pointer exception. I originally did it with plain integer lists. But they were to small and could not pass all tests. Can anyone help? Here is the code:
public static void main(String args[]) {
System.out.print(answer(100, 10));
}
public static int answer(int diceRolls, int numOfSquares) {
//Two many squares to be possible. Return 0 for 0 solutions.
if ((numOfSquares - diceRolls) > 1) {
return 0;
}
//If there are only two squares there is only One option. Roll a Right.
if (numOfSquares == 2) {
return diceRolls;
}
//If there are exactly as may dice rolls as there is squares.
//There are as much possibilities as there are squares
//Therefore return number of squares.
if (diceRolls == numOfSquares) {
return numOfSquares;
}
//Tests how many options there are to roll and get as many dice rolls as needed.
//While still getting the amount of squares needed to finish the board game.
BigInteger listOne[] = new BigInteger[numOfSquares]; //listOnes[current] holds the number valid paths to position 'current' using 'subCurrent-1' steps
BigInteger listTwo[] = new BigInteger[numOfSquares]; //put number of valid paths to position 'current' using 'subCurrent' steps into listTwo[current]
BigInteger total = new BigInteger("0");
listOne[0] = BigInteger.valueOf(1);
listOne[1] = BigInteger.valueOf(1);
int max = 1;
for (int index = 1; index < diceRolls; index++) {
listTwo = new BigInteger[numOfSquares]; //Resets listTwo
//Adds one to max until max is correctly fitted with number of squares
if (max < (numOfSquares - 1)) {
max++;
}
for (int subIndex = 0; subIndex < numOfSquares && subIndex < (max + 1); subIndex++) {
//If at first square
if (subIndex == 0) {
//Set listTwo's current value to lstOnes current value + the next value.
listTwo[subIndex] = listOne[subIndex].add(listOne[subIndex + 1]);
} else if (subIndex == max) {
//If at final square
if (subIndex == (numOfSquares - 1)) {
total.add(listOne[subIndex - 1]); //Adds listOnes last value to total.
} else {
listTwo[subIndex] = listOne[subIndex - 1];
} else {
//If subIndex is not yet at final or first Square
//Set current value of listTwo to listOne's last square + current square + nextSquare
listTwo[subIndex] = listOne[subInde add(listOne[subIndex].add(listOne[subIndex + 1]));
}
}
listOne = listTwo; //Sets listOne to the new value of listTwo
}
int answer = total.mod(BigInteger.valueOf(123454321)).intValue(); //For some reason question asks to return modulo 123454321 of the number
return answer;
}
Now here is the stack trace:
Exception in thread "main" java.lang.NullPointerException
at java.math.BigInteger.add(Unknown Source)
at arrays.MinionsBoredGames.answer(MinionsBoredGames.java:59)
at arrays.MinionsBoredGames.main(MinionsBoredGames.java:8)