在Java问题上的Hangman游戏有不正确的猜测

时间:2018-05-08 10:38:58

标签: java

我遇到了一个刽子手游戏的问题,我试图在java中完成,仅仅是为了我自己的学习。我的问题是,目前在我的代码中,无论你是否正确或错误,尝试次数总是倒数。所以基本上你只有10个去找出这个词然后游戏结束。

我想要的是,如果猜测是正确的,那么尝试次数将保持为当前数字,并且只有当我猜错时,它才会倒数1。

我想知道如何实现这一点,因为我正在努力转换我的代码来执行此操作:

以下是整个代码:

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

public class Game {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        String[] movieList = {"The Simpsons Movie", "Batman Begins", "Terminator"};

        boolean weArePlaying = true;
        while (weArePlaying) {
            System.out.println("Welcome to Guess The Movie Game");

            char[] randomWordToGuess = movieList[random.nextInt(movieList.length)].toLowerCase().toCharArray();
            int wordLength = randomWordToGuess.length;
            char[] playerGuess = new char[wordLength];

            for (int i = 0; i < playerGuess.length; i++) {
                    playerGuess[i] = '_';
            }

            for (int i = 0; i < randomWordToGuess.length; i++) {
                if (randomWordToGuess[i] == ' ') {
                    playerGuess[i] = ' ';
                }
            }

            boolean wordCompleted = false;
            int attempts = 10;

            while (!wordCompleted && attempts != 0) {
                System.out.println("Current guess: ");
                printArray(playerGuess);
                System.out.println("Number of attempts left: " + attempts);
                System.out.println("Enter a letter");
                char input = scanner.nextLine().charAt(0);
                attempts--;

                if (input == '-') {
                    weArePlaying = false;
                    wordCompleted = true;
                }

                else {
                        for (int i = 0; i < randomWordToGuess.length; i++) {
                            if (randomWordToGuess[i] == input) {
                                playerGuess[i] = input;
                            }
                        }


                    if (isTheWordCompleted(playerGuess)) {
                        wordCompleted = true;
                        System.out.println("You Won!!!");
                        System.out.println("Do you want to play again? (yes/no)");
                    }
                }

            }

            if (!wordCompleted) {
                System.out.println("You ran out of movieList. Game Over");
                System.out.println("Do you want to play again? (yes/no)");
                String anotherGame = scanner.nextLine();
                if (anotherGame.equals("no")) {
                    weArePlaying = false;
                } else if (anotherGame.equals("yes")) {
                    weArePlaying = true;
                } else {
                    System.out.println("Do you want to play again? (yes/no)");
                }
            }

        }

        System.out.println("Game Over");
    }

    public static void printArray(char[] array) {
        for (int i = 0; i < array.length; i++) {
           System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    public static boolean isTheWordCompleted(char[] array) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == '_') {
                return false;
            }
        }
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

只要有输入,您就会减少尝试次数:

List<TeamIndividual>

修改检查用户输入的部分:

char input = scanner.nextLine().charAt(0);
attempts--; //remove this line from here