0

Ok I am making a simple text based game and I am unsure why and infinite loop is being created. Its not really infinite but I am unsure why the if statement is not evaluated every loop. Here is the whole program. The if Statement I need fixed is in the roomEight method which is at the end of the code.

//********************************
// A simple game that moves the
// player though the map
//********************************
import java.util.*;
import java.text.*;

public class mazegame
{
    private static Scanner scan = new Scanner (System.in); // starts scanner for the program
    public static Scanner scanS;
    // ScanS is a scanner for strings.
    // To call this variable type mazegame.scanP;
        public static int lifeCount = 15;
        public static int damage = 1;
        // imp stats
            public static int impAmount = 0;
            public static int impDamage = 1;
            public static int impLife = 1;
                // Low level monster stats
                // m followed by a number stands for monster then the level of monster
                public static int m1health = 5;
                public static int m1damage = 2;
                    // High level monster
                    public static int m2health = 10;
                    public static int m2damage = 2;
                        // Boss stats       
                        public static int bosshealth = 30;
                        public static int bossdamage = 10;
                            // Placement of player
                            public static int placement = 3;
                            public static String movement;
                            public static int scanVal; // holder a scanner value generic.
    public static void main(String[] args)
    {
        System.out.println("You wake up on a cold hard floor");
        time();
        System.out.println("you are unsure how you got there.");
        time();
        System.out.println("There is an opening a head");
        time();
        System.out.println("you walk forward into the opening the ground begins to tremble");
        time();
        System.out.println("the wall behind you closes you are trapped.");
        time();
        time();
        clear(); // clears screen for user.
        roomThree();

    }
    public static void timeHalfSec()
    {
        try
            {
                Thread.sleep(500);              //1000 milliseconds is one second.
            }catch(InterruptedException ex)
                {
                    Thread.currentThread().interrupt();
                }
    }
    public static void time()
    {
        try
            {
                Thread.sleep(1500);             //1000 milliseconds is one second.
            }catch(InterruptedException ex)
                {
                    Thread.currentThread().interrupt();
                }
    }
    public static void clear()
    {
        final String ANSI_CLS = "\u001b[2J";
        final String ANSI_HOME = "\u001b[H";
        System.out.print(ANSI_CLS + ANSI_HOME);
        System.out.flush();
    }
    public static void position(int placement)
    {
        switch( placement )
        {
            //********************************
            // For each room create a method and 
            // call it in this switch statement.
            //********************************
            case 1: 
                break;
            case 2: 
                break;
            case 3:
                break;
            case 4: 
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
            case 8: roomEight();
                break;
            case 9:
                break;
            case 10:
                break;
            case 11:
                break;
            case 12:
                break;
            case 13:
                break;
            case 14:
                break;
            case 15:
                break;
            case 16:
                break;
            case 17:
                break;
            case 18:
                break;
            case 19:
                break;
            case 20:
                break;
            case 21:
                break;
            case 22:
                break;
            case 23:
                break;
            case 24:
                break;
            case 25:
                break;
        }
    }
    public static void askMove()
    {
        System.out.println("You can walk forward, left , or right. Beware the imps.");
        System.out.println("Enter L for left, R for right, and F for forward.");
        time();
        System.out.print("Move:");
        movement = scan.nextLine();
    }
    public static void roomThree()
    {
        askMove();
        //--------------------------------
        // This switch stament is only for this room
        //--------------------------------
        switch ( movement )
        {
            case "l":
            case "L": 
                placement = 2;
                System.out.println("Changing rooms Please wait");
                time();
                clear();
                break;
            case "r":
            case "R": 
                placement = 4;
                System.out.println("Changing rooms Please wait");
                time();
                clear();
                break;
            case "f":
            case "F": 
                placement = 8;
                System.out.println("Changing rooms Please wait");
                time();
                clear();
                break;
        }
        // The switch statement changes position and position calls the next room method.
        position(placement);    
    }
    public static void roomEight()
    {
            System.out.print ("You have just entered a new room.");
            System.out.print ("There is an imp ahead would you like to see its stats? 1 for yes and 0 ");
            impAmount = 1;
            scanVal = scan.nextInt();
            if(scanVal == 1 )
            {
                impStats();
            }
            System.out.println("Would you like to hit the imp? 1 for yes and 0 for no.");
            scanVal = scan.nextInt();
            while (impAmount != 0)
            {
                if (scanVal == 1)
                {
                    impAmount = 0;
                    damage++;
                    lifeCount = 15;
                    System.out.println("You killed an imp! You found brass knuckles your damage increased by one. Here are your stats");
                    playerStats();

                }else{
                        lifeCount--;
                        System.out.println("The imp hit you. You took one damage");
                        playerStats();
                        timeHalfSec();
                        dead();
                    }
            }

    }
    public static void playerStats()
    {
        System.out.println("*----------------*");
        System.out.println("Your Hearts: " + lifeCount);
        System.out.println("Your Damage: " + damage);
        System.out.println("*----------------*");
    }
    public static void impStats()
    {
        System.out.println ("*----------------*");
        System.out.println("Amount of Imps: " + impAmount);
        System.out.println("Imp Health: 1");
        System.out.println("impDamage: 1");
        System.out.println("*----------------*");
    }
    public static void dead()
    {
        if(lifeCount < 1)
        {
            System.exit(0);
        }
    }



}



//********************************************************************************************************************************
// Places to look for code and things to look up.
// Lookup: .equalsIgnoreCase, global scanner.
// Places to look for code: 
// http://stackoverflow.com/questions/23586732/how-to-make-a-one-static-scanner-global-variable-without-closing-scan-constantly
// https://www.youtube.com/watch?v=zijvKOjnmwY
// http://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement
// http://www.csci.csusb.edu/dick/samples/java.classes.html#System
// http://stackoverflow.com/questions/22452930/terminating-a-java-program
// 
// 
// 
//********************************************************************************************************************************

2 Answers2

0

You'd probably want to move the following two lines into the loop :

System.out.println("Would you like to hit the imp? 1 for yes and 0 for no.");
scanVal = scan.nextInt();
Jérémie B
  • 10,611
  • 1
  • 26
  • 43
  • Even if I put this in the loop it shouldn't do anything. If you could compile and run the code you will see what is wrong. If you select the else option to hit the imp that is when it starts repeating until player life is less then one. But for some reason I am unsure why the if statement is not evaluated each loop. – WalterTheSoyBoy Feb 25 '16 at 13:40
  • omg wow that is such a simple mistake that I should have seen thanks for the help – WalterTheSoyBoy Feb 25 '16 at 16:43
0

As I can see, you scan the nextInt that a user enter, you do stuff with it and then you re-scan the nextInt. The problem with that is when you use the scanner and ask for a single int, there is still the new-line char in the scanner ('\n'). Thus, when you ask a second time for the int, this will return the new line char. My understanding of all this is not on point, but what you have to do is one of those solution :

  1. Use nextLine instead of nextInt and parse the string value into an Int. This will clear the buffer, and you will be able to validate if the user entered a valid Int. You'd do it like this :

    String scanVal = scan.nextLine();
    //You can add some try catch in order to validate the int being parsed
    int choice = Integer.parseInt(scanVal); 
    
  2. Or you can clear the buffer after you have scanned your int by calling scan.nextLine()after scan.nextInt()

Hope this helps!

RaphBlanchet
  • 575
  • 6
  • 23
  • *"The problem with that is when you use the scanner and ask for a single int, there is still the new-line char in the scanner ('\n'). Thus, when you ask a second time for the int, this will return the new line char."* No, this is not correct. You might want to read this: [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/q/7056749) – Tom Feb 24 '16 at 18:50
  • Well pretty close to what I said, I thought nextInt was being affected by that remaining newline char, apologies.. – RaphBlanchet Feb 24 '16 at 19:21