Java replacement of specific characters

前端 未结 4 888
北海茫月
北海茫月 2021-01-23 18:32

This is my first question on this site so i\'ll try not to be a total noob..

I\'m currently creating hangman game in java. So my question to you is if we are given a wor

相关标签:
4条回答
  • 2021-01-23 18:49

    The easiest way to do that would be to use a Java regular expression on the target word. So whenever a user types in a letter you look for that letter in a regex match. That match will give you back the index of matching characters which you can then use to replace the letter in the __ version of the string.

    ...    
        listOfWords[9] = "zapping";
        Random generator = new Random();
        int lineNumber = generator.nextInt(9);
        disguisedWord = listOfWords[lineNumber];
        char[] hidden = disguisedWord.replaceAll("*","_").toCharArray();
    }
    
    public void makeGuess() throws IOException {
        System.out.println("Your word is " + disguisedWord.length() + " letters long.");
        System.out.println("Feel free to guess a letter.");
        String guess = System.in.read();
    
        Pattern pat = new Pattern(guess);
        Matcher mat = pat.matcher(disguisedWord);
        while (mat.find()) {
            int start = mat.start();
            hidden[start] = guess.toCharArray()[0];
        }
    }
    
    0 讨论(0)
  • 2021-01-23 18:52

    I think the most easy way would be storing the state in a variable and by looping over the real world replace the matching characters.

    private String word; 
    private char[] state;
    
    public void guess(char input){
        for (int i = 0; i < word.length(); i++) {
            if (word.toLowerCase().charAt(i) == input){
                state[i] = word.charAt(i);
            }
        }
    }
    

    afterwards you can print the contents of "state".

    0 讨论(0)
  • 2021-01-23 19:06
    String ghost = "ghost";
    String input = "o";
    for (int i = 0; i < ghost.length(); i++) {
        if (String.valueOf(ghost.charAt(i)).equalsIgnoreCase(input)) {
            System.out.print(input + " ");
        } else {
            System.out.print("_ ");
        }
    }
    

    You can simplify the if statement using the ternary operator:

    System.out.print(String.valueOf(ghost.charAt(i)).equalsIgnoreCase(input) ? input + " " : "_ ");
    
    0 讨论(0)
  • 2021-01-23 19:06

    Why not have another array of true/false values that mark what letters have been guessed and what letters are still unknown? I don't think using regex and string replaces is an easy way to go about getting hangman to work, especially when they guess another letter.

    Something like this perhaps.

    String answer = "someword";
    boolean[] knownLetters = new boolean[answer.length()];
    for (int i = 0; i < answer.length(); i++) {
        if(knownLetters[i]) {
            System.out.print(answer.charAt(i));
        } else {
            System.out.print("_");
        }
    }
    System.out.println("");
    
    0 讨论(0)
提交回复
热议问题