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

So.. would it work if I added another if-statement under the current elses so that if (newNStones > 3) go do
public static int computerMove(int nStones) again, or if it is <= then the computer wins. But, I don't know how to use public static int computerMove(int nStones) again

Can you explain what you want the code in the main method to do so that the game continues until the end of the game? You need to first decide on what the logic of the program is before you try to add if statements or any other code.
Basically you need a loop to keep the game going.

Edited 4 Years Ago by NormR1

Basically, we each keep taking a number of stones from the pile (1-3), and the person who takes the last amount of stones wins. So, if there are 4 and I take 1, 3 are left, and the computer ones. If there are 5 and the computer takes 2, then I would win.. If no one is able to win, it just keeps running through the code.

That is the overall program logic. What is the logic neede for the main method? Currently this is what you have:
computer plays
human plays

You need to put that in a loop so each player takes his turn:
begin loop
computer plays
human plays
end loop

The problem now is there needs to be a way to exit the loop. When would you want to exit the loop? What values do the methods return that you can use to make that decision?

This article has been dead for over six months. Start a new discussion instead.