0

I'm working on playing nim, where the person who takes the last object wins. My program is currently working, but it terminates after I only take one bunch of stones (You can take up to 3 at a time), no matter what the pile size is. Is there a way that I can recall the public static int computerMove(int nStones) method so that the game just doesn't terminate after one turn? I've tried a few ways, but they obviously haven't worked.

```
import java.util.Scanner;
import java.util.Random;
public class NIM
{
static Random randomGenerator = new Random();
static int nStones = randomGenerator.nextInt(10);
public static int computerStones;
public static int newNStones;
/*
* Returns the correct number of stones to take
* (according to the winning strategy) when nStones
* remain in the pile; if such a move is not possible,
* returns a random number of stones to take.
* Precondition: nStones > 0
*/
public static int computerMove(int nStones)
{
Random stones = new Random();
int random = stones.nextInt(3);
int randomStones = random + 1;
if ((nStones % 3 == 0 || nStones % 4 == 1) && nStones !=0)
{
computerStones = nStones - 3;
System.out.println("After the computer's move, there are" + " " + computerStones + " " + "left in the pile." );
return 3;
}
else if ( nStones % 4 == 2)
{
computerStones = nStones - 2;
System.out.println("After the computer's move, there are" + " " + computerStones + " " + "left in the pile." );
return 2;
}
else if ( nStones % 4 == 3)
{
computerStones = nStones - 3;
System.out.println("After the computer's move, there are" + " " + computerStones + " " + "left in the pile." );
return 2;
}
else if (nStones != 0)
{
computerStones = nStones - randomStones;
System.out.println("After the computer's move, there are" + " " + computerStones + " " + "left in the pile." );
return randomStones;
}
else
{
System.out.println("You have lost!");
return 0;
}
}
public static int humanMove(int computerStones)
{
if (computerStones > 0)
{
int newNStones;
System.out.println("How many stones do you take? ");
Scanner kboard = new Scanner(System.in);
int n = kboard.nextInt();
if (n > 0 && n < 4)
{
if (n == 1)
{
newNStones = computerStones - 1;
System.out.println("Now there are" + " " + newNStones + " " + " stones left in the pile.");
return 1;
}
else if (computerStones < n)
{
System.out.println("Can't take that many: only " + computerStones + " stone(s)left in the pile.");
return -1;
}
else if (n == 2)
{
newNStones = computerStones - 2;
System.out.println("Now there are" + " " + newNStones + " " + " stones left in the pile.");
return 2;
}
else if (n == 3)
{
newNStones = computerStones - 3;
System.out.println("Now there are" + " " + newNStones + " " + " stones left in the pile.");
return 3;
}
}
else
{
System.out.println("You are only allowed to take 1, 2, or 3 stones.");
return -1;
}
}
else
System.out.println("You have lost.");
return 0;
}
public static void main(String[] args)
{
Random randomGenerator = new Random();
int nStones = randomGenerator.nextInt(10);
System.out.println("There are" + " " + nStones + " " + "stones in the pile.");
computerMove(nStones);
humanMove(computerStones);
}
}
```

*Edited 4 Years Ago by polarbear125*