简体   繁体   English

Hangman Java帮助-如何替换猜字母?

[英]Hangman Java Help - How do I replace guessed letters?

I Need help with a simple hangman game. 我需要一个简单的子手游戏的帮助。 I have the game running, I just need to know where and how to add a guessed letter into the correct lines of an array. 我正在运行游戏,我只需要知道在哪里以及如何将猜测的字母添加到数组的正确行中即可。 Also is there any code I can omit to make my project more readable? 还有什么我可以省略的代码可以使我的项目更具可读性?

public static void main(String[] args)
{

    //create scanner to read input
    Scanner keyboard = new Scanner(System.in);

    //create string name for player
    String name; 

    //get player's name
    System.out.println("What is you name?");
    name = keyboard.next();

    //Print welcome message
    System.out.println("Hello " + name + "! Welcome to a game of Hangman!");
    System.out.println("Guess letters until you correctly guess the word ");
    System.out.println("or you fully assemble a complete hangman.");
    System.out.println("You have a 3 letter word!");



    Random rand = new Random();
    String [] Word = {"hat"};

    String secretWord = Word[rand.nextInt(Word.length)];// word from the char array and stores it in a variable 
    char [] array = secretWord.toCharArray();   //converts the word from the array into a char array
    //char [] array = new char [secretWord.length()];
    //counter
    int i = 0;
    char input = 0;

    //counts wins
    int winCount = 0;

    //counts losses
    int tries = 20;
    int wrongCounter = 0;
    ArrayList<Character> list = new ArrayList<Character>();


    while(i<tries)
    {

     //allows player to guess a letter
        System.out.println("Guess a letter : ");        
        input = keyboard.next().charAt(0);
        list.add(input);

      //displays guessed letter
        System.out.println("Guessed Letters are : "+ input);

        for(int j = 0; j<notSame(array).length;j++)
        {

            if(findCorrect(array,input, list)>0)
            {
                j++;

                System.out.println("You got it right");


                winCount++;
                break;
            }
         else if(!(input ==notSame(array)[j]))
            {

               System.out.println("You got it wrong");

                wrongCounter++;
                break;


            }

            }

          //tests to see if the player won
            if(winCount == notSame(array).length)
            {
                System.out.println("You have Won!");
                System.out.println(secretWord + " is the correct word!");
                break;
            }
            else if(hangMan(wrongCounter)){
                break;
            }
        }

        System.out.println();
    }

    public static boolean hangMan(int wrongCounter) 
    {

        boolean isOver= false;
        System.out.println("------------|");

        switch(wrongCounter)
        {
        case 1:
            System.out.println("|           O");
            break;
        case 2 :
            System.out.println("|           O");
            System.out.println("|           |");
            break;
        case 3 :
            System.out.println("|           O");
            System.out.println("|          /|");
            break;
        case 4:
            System.out.println("|           O");
            System.out.println("|          /|\\");
            System.out.println();
            break;
        case 5:
            System.out.println("|           O");
            System.out.println("|          /|\\");
            System.out.println("|          /");
            break;
        case 6:
            System.out.println("|           O");
            System.out.println("|          /|\\");
            System.out.println("|          / \\");
            System.out.println("Sorry, you lost");
            System.out.println("hat is the correct word");
            isOver = true;
            break;
        }
        return isOver;

    }

    //removes all same letters from the array
    private static char[] notSame(char[] a) {
        HashSet<Integer> keys = new HashSet<Integer>();
        char[] result = new char[a.length];
        int j = 0;
        for (int i = 0 ; i < a.length; i++) 
        {

            if (keys.add((int) a[i])) 
            {
                result[j] = a[i];
                j++;
            }
        }
        return Arrays.copyOf(result, j);
    }
    //tests to see if the users input it equal to any of the letters in the array
    public static int findCorrect(char [] a,char Input,ArrayList list)
    {
        int count = 0;
        for(int i = 0;i<notSame(a).length;i++)
        {
            if(notSame(a)[i]==Input)
                count++;
        }
        return count;

    }
}

The output for the guessed letters should be System.out.println("Guessed Letters are : "+ list.toString()); 猜字母的输出应为System.out.println("Guessed Letters are : "+ list.toString());

That way it shows all of the letters guessed and not just the last one. 这样,它可以显示所有猜出的字母,而不仅仅是最后一个字母。

To display the word as blanked out lines until each letter is guessed define a new char array 要将单词显示为空白行,直到猜到每个字母,定义一个新的char数组

    String [] Word = {"hat"};

    String secretWord = Word[rand.nextInt(Word.length)];/

    // modified
    char [] displayArray = new char[secretWord.length()];
    for(int i=0; i < Word[0].length();i++){
      displayArray[i] += '_';
    }

Then update it when it is correct (around line 65 or so) 然后在正确时更新(大约65行左右)

    if(findCorrect(array,input, list)>0)
    {
         j++;            
         //modified
         int correctLocation = secretWord.indexOf(""+input);
         displayArray[correctLocation] = array[correctLocation];
         System.out.println("You got it right");

         winCount++;
         break;
    }

Then at the end of that for loop display it 然后在for循环的最后显示它

    //modified
    System.out.println("Word is : " + new String(displayArray));

    //tests to see if the player won
    if(winCount == notSame(array).length)
    {
        System.out.println("You have Won!");
        System.out.println(secretWord + " is the correct word!");
        break;
    }
    else if(hangMan(wrongCounter)){
        break;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM