Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java刽子手游戏中保持总分 import java.util.Scanner; 导入javax.swing.JOptionPane; 公共级刽子手{ 公共静态void main(字符串[]args){ String playAgainMsg=“您想再次_Java - Fatal编程技术网

在Java刽子手游戏中保持总分 import java.util.Scanner; 导入javax.swing.JOptionPane; 公共级刽子手{ 公共静态void main(字符串[]args){ String playAgainMsg=“您想再次

在Java刽子手游戏中保持总分 import java.util.Scanner; 导入javax.swing.JOptionPane; 公共级刽子手{ 公共静态void main(字符串[]args){ String playAgainMsg=“您想再次,java,Java,在Java刽子手游戏中保持总分 import java.util.Scanner; 导入javax.swing.JOptionPane; 公共级刽子手{ 公共静态void main(字符串[]args){ String playAgainMsg=“您想再次播放吗?”; String PickCategorySg=“您已经尝试了此类别中的所有单词!\n是否要选择其他类别?”; int winCounter=0,loseCounter=0,分数=0; 字符串[]个单词; int=0; 字符串字串;

在Java刽子手游戏中保持总分
import java.util.Scanner;
导入javax.swing.JOptionPane;
公共级刽子手{
公共静态void main(字符串[]args){
String playAgainMsg=“您想再次播放吗?”;
String PickCategorySg=“您已经尝试了此类别中的所有单词!\n是否要选择其他类别?”;
int winCounter=0,loseCounter=0,分数=0;
字符串[]个单词;
int=0;
字符串字串;
布尔playCategory=true,playGame=true;
int totalCounter=0,计数器;
while(游戏类别和游戏)
{
while(游戏类别和游戏){
words=getWords();
计数器=0;
while(游戏和计数器1&&guess.equals(wordToGuess)){
韩元=真;
}else if(wordToGuess.indexOf(guess)!=-1){
布尔破折号=假;
for(int i=0;iimport java.util.Scanner;
import javax.swing.JOptionPane;
public class Hangman {

    public static void main(String[] args) {
        String playAgainMsg = "Would you like to play again?";
        String pickCategoryMsg = "You've tried all the words in this category!\nWould you like to choose another category?";
        int winCounter = 0, loseCounter = 0, score = 0;
        String[] words;
        int attempts = 0;
        String wordToGuess;
        boolean playCategory = true, playGame = true;
        int totalCounter = 0, counter;
        while (playCategory && playGame)
        {
        while (playCategory && playGame) {
            words = getWords();
            counter = 0;
            while (playGame && counter < words.length) {

                wordToGuess = words[counter++];
                if (playHangman(wordToGuess)) {

                    winCounter++;
                    System.out.println("You win! You have won " + winCounter + " game(s)." + " You have lost " + loseCounter + " game(s).");

                } else {
                    loseCounter++;
                    System.out.println("You lose! You have lost " + loseCounter + " game(s)." + " You have won " + winCounter + " game(s).");

                }

                if (counter < words.length) playGame = askYesNoQuestion(playAgainMsg);
            }
            if (playGame) playCategory = askYesNoQuestion(pickCategoryMsg);
        }
    }
    }


    public static boolean playHangman(String wordToGuess) {
        String[] computerWord = new String[wordToGuess.length()];
        String[] wordWithDashes = new String[wordToGuess.length()];
        for (int i = 0; i < computerWord.length; i++) {
            computerWord[i] = wordToGuess.substring(i, i+1);
            wordWithDashes[i] = "_";
        }

        Scanner in = new Scanner(System.in);
        int attempts = 0, maxAttempts = 7;
        boolean won = false;
        int points = 0;
        while (attempts < maxAttempts && !won) {
            String displayWord = "";
            for (String s : wordWithDashes) displayWord += " " + s;
            System.out.println("\nWord is:" + displayWord);
            System.out.print("\nEnter a letter or guess the whole word: ");
            String guess = in.nextLine().toLowerCase();
            if (guess.length() > 1 && guess.equals(wordToGuess)) {
                won = true;
            } else if (wordToGuess.indexOf(guess) != -1) {
                boolean dashes = false;
                for (int i = 0; i < computerWord.length; i++) {
                    if (computerWord[i].equals(guess)) wordWithDashes[i] = guess;
                    else if (wordWithDashes[i].equals("_")) dashes = true;
                }
                won = !dashes; // If there are no dashes left, the whole word has been guessed
            } else {
                drawHangmanDiagram(attempts);
                System.out.println("You've used " + ++attempts + " out of " + maxAttempts + " attempts.");
            }

        }
        int score = 0;
        score = scoreGame(attempts);
        System.out.println("Your score is: " + score);
        return won;

    }

    //should take in a failure int from the main method that increments after every failed attempt
    public static void drawHangmanDiagram(int failure)
    {
        if (failure == 0)
            System.out.println("\t+--+\n\t|  |\n\t|\n\t|\n\t|\n\t|\n\t|\n\t|\n\t+--");
        else if (failure == 1)
            System.out.println("\t+--+\n\t|  |\n\t|  @\n\t|\n\t|\n\t|\n\t|\n\t|\n\t+--");
        else if (failure == 2)
            System.out.println("\t+--+\n\t|  |\n\t|  @\n\t| /\n\t|\n\t|\n\t|\n\t|\n\t+--");
        else if (failure == 3)
            System.out.println("\t+--+\n\t|  |\n\t|  @\n\t| / \\\n\t|\n\t|\n\t|\n\t|\n\t+--");
        else if (failure == 4)
            System.out.println("\t+--+\n\t|  |\n\t|  @\n\t| /|\\\n\t|  |\n\t|\n\t|\n\t|\n\t+--");
        else if (failure == 5)
            System.out.println("\t+--+\n\t|  |\n\t|  @\n\t| /|\\\n\t|  |\n\t| /\n\t|\n\t|\n\t+--");
        else if (failure == 6)
            System.out.println("\t+--+\n\t|  |\n\t|  @\n\t| /|\\\n\t|  |\n\t| / \\\n\t|\n\t|\n\t+--");
    }

    // Asks user a yes/no question, ensures valid input
    public static boolean askYesNoQuestion(String message) {
        Scanner in = new Scanner(System.in);
        boolean validAnswer = false;
        String answer;
        do {
            System.out.println(message + " (Y/N)");
            answer = in.nextLine().toLowerCase();
            if (answer.matches("[yn]")) validAnswer = true;
            else System.out.println("Invalid input! Enter 'Y' or 'N'.");
        } while (!validAnswer);
        return answer.equals("y");
    }

    public static boolean askForCategory(int category) {
        Scanner in = new Scanner(System.in);
        boolean validAnswer = false;
        String answer;
        do {
            System.out.println("\nWould you like to play again? (Y/N)");
            answer = in.nextLine().toLowerCase();
            if (answer.matches("[yn]")) validAnswer = true;
            else System.out.println("Invalid input! Enter 'Y' or 'N'.");
        } while (!validAnswer);
        return answer.equals("y");
    }

    // Asks the user to pick a category
    public static String[] getWords() {
        String[] programming = {"java", "pascal", "python", "javascript", "fortran", "cobol"};
        String[] sports = {"gymnastics", "badminton", "athletics", "soccer", "curling", "snooker", "hurling", "gaelic", "football", "darts"};
        String[] result = {""};

        Scanner in = new Scanner(System.in);
        boolean validAnswer = false;
        String answer;
        do {
            System.out.println("Pick a category:\n1. Programming\n2. Sports");
            answer = in.nextLine().toLowerCase();
            if (answer.matches("[1-2]")) validAnswer = true;
            else System.out.println("Invalid input! Enter the number of the category you want.");
        } while (!validAnswer);

        int selection = Integer.parseInt(answer);
        switch (selection) {
            case 1: result = randomOrder(programming); break;
            case 2: result = randomOrder(sports); break;
        }
        return result;
    }

    // Sorts a String array in random order
    public static String[] randomOrder(String[] array) {
        int[] order = uniqueRandoms(array.length);
        String[] result = new String[array.length];
        for (int i = 0; i < order.length; i++) {
            result[i] = array[order[i]];
        }
        return result;
    }

    // Generates an array of n random numbers from 0 to n-1
    public static int[] uniqueRandoms(int n) {
        int[] array = new int[n];
        int random, duplicateIndex;
        for (int i = 0; i < n; ) {
            random = (int) (Math.random() * n);
            array[i] = random;
            for (duplicateIndex = 0; array[duplicateIndex] != random; duplicateIndex++);
            if (duplicateIndex == i) i++;
        }
        return array;
    }

    public static int scoreGame(int attempts)
    {
        int score = 0;
        switch (attempts)
        {
            case 0: score = 70; break;
            case 1: score = 60; break;   
            case 2: score = 50; break;
            case 3: score = 40; break;
            case 4: score = 30; break;
            case 5: score = 20; break;
            case 6: score = 10; break;
            case 7: score = 0;  break;

        }
        return score;
    }
}
score = scoreGame(attempts);
totalScore += score;
System.out.println("Your score is: " + score);