0

I'm creating a hangman game. Everything works fine but i am trying to implement a high score list. The score for each person works but when i ask the user to input their name at end of game it just skips it and goes back to original line. This is where i try get the users name.

if(highScore(score))
{
    System.out.print("Enter your name: ");
    name[position]=scan.nextLine();
}

Here is the highscore method.

//checks if you made it to highscore list
    static boolean highScore(int score)
    {
        for (int i=0;i<high.length;i++)
        {
            if(score>high[i])
            {
                if(i==0)
                {
                    //moving scores down
                    high[2]=high[1];
                    high[1]=high[0];
                    high[0]=score;
                    //moves names down
                    name[2]=name[1];
                    name[1]=name[0];
                    System.out.println("Congratulations you are in first place");
                }
                else if(i==1)
                {
                    //moves scores down
                    high[2]=high[1];
                    high[1]=score;
                    //moves names down
                    name[2]=name[1];
                    System.out.println("Congratulations you are in second place");
                }
                else
                {
                    //replaces score
                    high[2]=score;
                    System.out.println("Congratulations you are in third place");
                }

                position=i;
                return true;
            }
        }
        //did not make it 
        return false;
}

This is the whole code.its kind of long

import java.util.Random;
import java.util.Scanner;

public class hangman
{
    static int lives=6;//lives at start
    static String secret="";//secret word(filled with asterix'*')
    static String pastGuesses="";//previous incorect attempts
    static int score=0;//starting score
    static int high[]=new int[3];//top 3~~high score for this sitting
    static String name[]=new String[3];//top3~~names of high score holders
    static int position=0;//position for scanning in name
    static boolean playAgain=true;//if true~~have option to continue with game

    //print board
    static void show()
    {
        System.out.println("~~~~~~~~~~~~~~");
        System.out.println(secret);
        if(!(pastGuesses.equals(""))^checkwin())
            System.out.println("Previous incorrect answer: "+pastGuesses);
    }

    //making secret word(Filling with correct numer of asterix'*')
    static void makeSecret(String word)
    {
        for(int i=0;i<word.length();i++)
        {
            secret+="*";
        }
    }

    //checks for win
    static boolean checkwin()
    {
        int count=0;

        for (int i=0;i<secret.length();i++)
        {
            if(secret.charAt(i)=='*')
                count++;
        }
        if(count==0)
        {
            score+=10;
            return true;
        }
        else
            return false;
    }

    //checks for letter in word(lives=0 also in here)
    static void checkLetter(char guess,String word)
    {
        int count=0;
        char[] secretChars = secret.toCharArray();//convert secret to array of chars

        for(int i=0;i<word.length();i++)
        {
            if (word.charAt(i) == guess) 
            {
                secretChars[i] = guess;
                count++;
            }
        }
        secret = new String(secretChars);//back to string

        if(count==0)
        {
            pastGuesses+=guess+" ";
            lives--;
            if(lives==0)
            {
                System.out.println("~~Out of lives, you lose.~~");
                System.out.println("The word was: "+word);
                System.out.println("Your final score was "+score);
                System.out.println("You guessed "+score/20+" words correctly.");
                playAgain=false;
            }
            else
                System.out.println("Incorrect.\nYou have "+lives+" lives left");
        }
    }

    //checks if you made it to highscore list
    static boolean highScore(int score)
    {
        for (int i=0;i<high.length;i++)
        {
            if(score>high[i])
            {
                if(i==0)
                {
                    //moving scores down
                    high[2]=high[1];
                    high[1]=high[0];
                    high[0]=score;
                    //moves names down
                    name[2]=name[1];
                    name[1]=name[0];
                    System.out.println("Congratulations you are in first place");
                }
                else if(i==1)
                {
                    //moves scores down
                    high[2]=high[1];
                    high[1]=score;
                    //moves names down
                    name[2]=name[1];
                    System.out.println("Congratulations you are in second place");
                }
                else
                {
                    //replaces score
                    high[2]=score;
                    System.out.println("Congratulations you are in third place");
                }

                position=i;
                return true;
            }
        }
        //did not make it 
        return false;
    }

    public static void main(String [] args)
    {
        //intialise random gen and scanner
        Scanner scan=new Scanner(System.in);
        Random rand=new Random();
        String answer="";//in game optons
        //word to guess
        String word="";
        int num;

        //list of categories
        String cartoon[]={"homer","bart","lisa","bob","tina","marge","louise","maggie",};
        String english[]={"goneril","hamlet","lear","oethello","shylock","antonio"};
        String clothes[]={"hat","gloves","trousers","jumper","shoes"};
        int play=0;//play again option

        while(true)
        {
            System.out.println("Type start to start game or type high to see highscore\nor type end to quit.");
            answer=scan.next();
            //game code
            if(answer.equalsIgnoreCase("start"))
            {
                //resets everything to original level to start game
                playAgain=true;
                play=0;
                secret="";
                pastGuesses="";
                score=0;

                while(playAgain)
                {
                    //option to play another word
                    while(playAgain)
                    {
                        //play again choice
                        if(play==1)
                        {
                            System.out.println("Do you want to play again?");
                            answer=scan.next();
                            if(answer.equalsIgnoreCase("yes"))
                            {
                                secret="";
                                pastGuesses="";
                                break;
                            }
                            else if(answer.equalsIgnoreCase("no"))
                            {
                                System.out.println("Your final score was "+score);
                                System.out.println("You guessed "+score/20+" word correct.");
                                if(highScore(score))
                                {
                                    System.out.print("Enter your name: ");
                                    name[position]=scan.nextLine();
                                }
                                playAgain=false;
                            }
                            else
                                System.out.println("Invalid choice");
                        }
                        else
                            break;
                    }
                    play=1;
                    //choosing a word at random from chosen category
                    while (playAgain)
                    {
                        System.out.println("Choose a category(type the corresponding number):\n1.Cartoon characters.\n2.English play characters.");
                        System.out.println("3.Items of clothing.");
                        int category=scan.nextInt();

                        if(category==1)
                        {
                            num=rand.nextInt(cartoon.length);
                            word=cartoon[num];
                            break;
                        }
                        else if(category==2)
                        {
                            num=rand.nextInt(english.length);
                            word=english[num];
                            break;
                        }
                        else if(category==3)
                        {
                            num=rand.nextInt(clothes.length);
                            word=clothes[num];
                            break;
                        }
                        else
                            System.out.println("invalid input.");
                    }
                    makeSecret(word);

                    //main game
                    while(lives>0&playAgain)
                    {
                        //printin board
                        show();

                        //making guesses
                        while(true)
                        {
                            //making choice of guessing letter or word
                            System.out.println("Do you want to(Enter corresponding number):\n1.Guess a letter.\n2.Guess the word.");
                            int guessChoice=scan.nextInt();
                            //letter guess
                            if(guessChoice==1)
                            {
                                System.out.print("Guess a letter: ");
                                String s1=scan.next();
                                char guess=s1.charAt(0);
                                //checking letter in word
                                checkLetter(guess,word);
                                //calls highscore
                                if(lives==0)
                                {
                                    if(highScore(score))
                                    {
                                        System.out.print("Enter your name: ");
                                        name[position]=scan.nextLine();
                                    }

                                }
                                break;
                            }
                            //word guess
                            else if(guessChoice==2)
                            {
                                System.out.print("Guess the word: ");
                                String s1=scan.next();
                                if(s1.equalsIgnoreCase(word))
                                    secret=word;
                                else
                                {
                                    checkLetter(' ',word);
                                    //calls high score
                                    if(lives==0)
                                    {
                                        if(highScore(score))
                                        {
                                            System.out.print("Enter your name: ");
                                            name[position]=scan.nextLine();
                                        }
                                    }
                                }
                                break;
                            }
                            else
                                System.out.println("Invalid choice.");
                        }

                        //winning statements
                        if(checkwin())
                        {
                            show();
                            System.out.println("Your current score is "+score);
                            System.out.println("~~Congratulations you won~~");
                            break;
                        }
                    }
                }
            }
            else if(answer.equalsIgnoreCase("high"))
            {
                for(int i=0;i<high.length;i++)
                {
                    System.out.println(name[i]+": "+high[i]);
                }
            }
            else if(answer.equalsIgnoreCase("end"))
                break;
        }
    } 
}

The scanner works fine in other parts of the code and yes the name of the scanner is scan. Any help is appreciated and if u know what is causing the can u explain it so i dont make it again. Thank you.

mikekane
  • 13
  • 8

0 Answers0