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

标签 java

我需要一个简单的刽子手游戏的帮助。我正在运行游戏,我只需要知道在哪里以及如何将猜测的字母添加到数组的正确行中。还有我可以省略任何代码以使我的项目更具可读性吗?

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;

    }
}

最佳答案

猜测字母的输出应该是System.out.println("Guessed Letters are : "+ list.toString());

这样它会显示所有猜测的字母,而不仅仅是最后一个字母。

将单词显示为空白行,直到猜出每个字母为止,定义一个新的字符数组

    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] += '_';
    }

然后在正确时更新它(大约第 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;
    }

然后在 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;
    }

关于java - Hangman Java 帮助 - 如何替换猜测的字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27302064/

相关文章:

java - 如何使用 DOM Java 搜索 xml 中的子节点

java - PrintWriter与普通打印不同

java - Jmagick错误: magick. MagickException:没有可缩放的图像

java - 以下 Java 程序的输出是什么,其中 null 作为重载方法的参数传递?

java - 如何动态调用合格服务上的方法?

类插件架构的 Java 技巧

java - 具有多个 UDP 接收器的 GStreamer 管道(包含图像)

java - 在 Java 中,在内存方面使用私有(private)与公共(public)有区别吗?

java - Android 使用 Retrofit 和 OkHttp 获取 Endpoints

java - Maven 插件不使用 Eclipse 的代理设置