1

I've recently decided that I want to make a program that plays a game called "Nim," which is a game in which you start with a predetermined amount of "sticks" and each player takes turns removing between 1 and 3 sticks. Whoever removes the last stick loses.

Anyway, I have written my program and it compiles and runs almost flawlessly. There's only one small problem. After the game is over, it shows the "good game" screen twice, with the game's very first line appearing in the middle (I'll post screenshots at the end here). It's very strange, and I was just wondering if you guys could give it a look.

I'm cutting a chunk of the program out (only one class, named Cup()), because it's somewhat long, so if you see a class you don't recognize then just ignore it. It's pretty self explanatory what the class does in the program, and it's not where the error is occurring. Here's the code.

class SticksGame
{
    public static void main(String[] args) throws InputMismatchException
    {
    Random r = new Random();
    int score1 = 0, score2 = 0;
    Cup c = new Cup();
    int j = 0, d = 0, i = 0, k = 0;
    boolean b = true;
    String exit = "default";
    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to the Sticks Game! Last Stick loses! Must pick 1 - 3 sticks.");
    System.out.println();
    do
    {
        i = r.nextInt(15) + 9;
        System.out.println("We begin with " + i + " sticks");
        System.out.println();
        while (b == true)
        {
            System.out.println("Your move");
            k = input.nextInt();
            if (k > 3)
            {
                System.out.println("You must select between 1 and 3 sticks");
                k = input.nextInt();
            }
            else if (k < 1) 
            {
                System.out.println("You must select between 1 and 3 sticks");
                k = input.nextInt();
            }
            else
            {
                j = i;
                i = i - k;  
                if (i <= 0)
                {
                    System.out.println("Computer wins!");
                    score2 = (score2 + 1);
                    b = false;
                }
                else
                {
                    System.out.println("We now have " + i + " sticks.");            
                }
                d = c.select();
                System.out.println("Computer removes " + d + " sticks");
                i = i - d;
                System.out.println("We now have " + i + " sticks");
                if (i <= 0)
                {
                    System.out.println("You Win!");
                    score1 = (score1 + 1);
                    b = false;
                }
            }
        }
        System.out.println();
        System.out.println("Good game!");
        System.out.println("Your score: " + score1 + "     Computer's Score: " + score2);
        System.out.println("Press enter if you'd like to play again. Otherwise, type \"quit\"");
        exit = input.nextLine();
        b = true;
    }   
    while(!"quit".equals(exit));
}

}

Any helps are appreciated! Thanks :)

~Andrew

CODE EDITED FOR JANOS

The Output at the end of the game

A little late, I know, but here is the FULL GAME for anyone who wants to play! feel free to copy and paste it into your notepad and execute using cmd(YOU MUST KEEP MY NAME AS A COMMENT ON TOP!) :)

//Andrew Mancinelli: 2015
import java.util.*;
import java.io.*;
class Cup
{
    private ArrayList<Integer> c = new ArrayList<Integer>();
    public Cup()
    {
        c.add(1);
        c.add(2);
        c.add(3);
    }
    public int count()
    {
        return c.size();
    }
    public int select()
    {
        int index = (int)(c.size() * Math.random());
        return c.get(index);
    }
    public void remove(Integer move)
        {
        c.remove(move);
    }
}
class SticksGame
{
    public static void help()
    {
        System.out.println();
        System.out.println("Okay, so here's how it works... The object of the game is to NOT have the last stick. Whoever ends up with the very last stick loses.");
        System.out.println();
        System.out.println("Rule 1: You will each take turns removing sticks. you may only remove 1, 2, or 3 sticks in a turn");
        System.out.println();
        System.out.println("Rule 2: The beginning number of sticks is always random between 9 and 24 sticks");
        System.out.println();
        System.out.println("Rule 3: Whoever chooses the last stick, LOSES!");
        System.out.println();
        System.out.println("And that's it! Simple, right?");
}
    public static void main(String[] args) throws InputMismatchException
    {
        Random r = new Random();
        int score1 = 0, score2 = 0;
        Cup c = new Cup();
        int j = 0, d = 0, i = 0, k = 0;
        boolean b = true;
        String exit = "default", inst = "default";
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to the Sticks Game! Last Stick loses!");
        System.out.println();
        System.out.println("Need some instructions? Type \"help\" now to see the instructions. Otherwise, press enter to play!");
        inst = input.nextLine();
        if (inst.equals("help"))
        {
            help();
            System.out.println();
            System.out.println("press \"enter\" to begin!");
            inst = input.nextLine();
        }
        do
        {
            i = r.nextInt(15) + 9;
            System.out.println();
            System.out.println("We begin with " + i + " sticks");
            System.out.println();
            while (b == true)
            {
                System.out.println("Your move");
                k = input.nextInt();
                if (k > 3)
                {
                    System.out.println("You must select between 1 and 3 sticks");
                    k = input.nextInt();
                }
                else if (k < 1) 
                {
                    System.out.println("You must select between 1 and 3 sticks");
                    k = input.nextInt();
                }
                else
                {
                    j = i;
                    i = i - k;  
                    if (i <= 0)
                    {
                        System.out.println("Computer wins!");
                        score2 = (score2 + 1);
                        b = false;
                        break;
                    }
                    else
                    {
                        System.out.println("We now have " + i + " sticks.");            
                    }
                    d = c.select();
                    i = i - d;
                    if (i >= 0)
                    {
                        System.out.println("Computer removes " + d + " sticks");
                        System.out.println("We now have " + i + " sticks");
                    }
                    if (i <= 0)
                    {
                        System.out.println("You Win!");
                        score1 = (score1 + 1);
                        b = false;
                        break;
                    }
                }
            }
            System.out.println();
            System.out.println("Good game!");
            System.out.println("Your score: " + score1 + "     Computer's Score: " + score2);
            System.out.println("Press enter if you'd like to play again. Otherwise, type \"quit\"");
            input.nextLine();
            exit = input.nextLine();
            b = true;
        }   
        while(!"quit".equals(exit));
    }
}
Andrew M
  • 93
  • 8
  • Thanks for the other link Skeptic! Only half the problem, However. I edited the queston. – Andrew M Oct 29 '15 at 22:37
  • @RealSkeptic Could you remove the Duplicate mark? I edited the question to suit a different need. – Andrew M Oct 29 '15 at 22:45
  • Now it's a duplicate of [this question](http://stackoverflow.com/q/13102045/4125191) - but somebody else will have to do the closing. – RealSkeptic Oct 29 '15 at 22:48

2 Answers2

2

The problem is that this condition is always true:

while (exit != "quit");

Because != means "not identical", and the exit variable and "quit" are not identical. Use the equals method for checking logical equality.

In this example, change the loop condition to this instead:

while (!"quit".equals(exit));

For your other problem of not properly starting a second game, you need to reinitialize the state variables, for example reset b = true.

Lastly, note that input.nextInt() doesn't read the newline character that you pressed when entering a number. So when exit = input.nextLine() runs, it reads that newline character, and doesn't actually give you a chance to type "quit". To solve this, add input.nextLine(); right before exit = input.nextLine();

janos
  • 109,862
  • 22
  • 193
  • 214
1

The unexpected retry was because of the use of input.nextLine(); the program assumed that you already pressed [enter].

From previous work, the two options is to insert one more input.nextline();

input.nextLine();  
exit = input.nextLine();

Or use input.next(); instead, although enter will not work for this method so you may need to enter any key or "quit" to exit;

exit = input.next();
Jay
  • 46
  • 3