产生另一个随机词

时间:2019-02-03 17:29:19

标签: java

我有这个程序,它从wordBank中随机选择一个单词来玩play子手游戏。游戏完全按照我的需要工作,但是当游戏结束时,我需要它随机选择另一个单词。

我知道这是它们初始化字符串和数组的方式。我不确定是否可以在gameStart方法中添加一些内容。但是我在移回它时遇到了麻烦。

import java.util.Scanner;

public class hangman 
{
    static String[] wordBank = {"appear", "ticket", "witness", "guerrilla", "command", "calendar", "illusion", "provoke", "secular", "pocket", "print", "wagon", "freedom", "umbrella"};

    static String chosenWord = wordBank[(int) (Math.random() *wordBank.length)];

    static char[] asterisk;
    static int count = 9;
    static char[] userGuess = new char[1];
    static char[] word = chosenWord.toCharArray();          
    //Converts string chosen word to an array of characters
    static String usedLetters = "";
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) 
    {
        for (int wordCount = 14; wordCount >0; wordCount++)
        {               
            if (count == 9)
            {
                gameStart ();   //Method called when starting a new game
            }

            else 
            {
                gameContinue();     //Method called when it is a continuation of a game.
            }
        }
    }

    private static void gameStart() 
    {       
        System.out.println("\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
        System.out.println("This is a game of Hangman. You have ");
        System.out.println("8 attempts to guess the word correct.");
        System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
        asterisk = new char[chosenWord.length()];       //Assigns asterisks for all the letters in the chosen word

        System.out.print("\nYour word has " + chosenWord.length() + " letters, ");

        for ( int i = 0; i < chosenWord.length(); i++ ) //Initializes 2nd array to show all asterisks.
        {
            asterisk[i] = '*';
        }
        System.out.println(asterisk);
        count--;
    }   

    private static void gameContinue() 
    {
        if (count > 0 && (contains('*', asterisk)))
        {           
            System.out.println("\nType a letter to guess.");
            String guess = input.nextLine();                    //Takes in the guess of the user

            letterCheck(guess);                     //Checks input letter to see if it has already been used.

            char[] userGuess = guess.toCharArray();         //Converts string of user input to array

            for(int i=0;i<userGuess.length;i++)
            {
                System.out.println("\nYour letter is \"" + userGuess[i] + "\".");
            }           

            match(word, userGuess);                                 //Calls method to see if guess is in the word
        }

        else 
        {
            System.out.println("\nYou have guessed too many times.\n");
            System.out.println("Lets try again.\n");
            count = 9;                                      //Resets count to 9 to start game over
        }       
    }   

    static void letterCheck(String guess) 
    {       
        if (usedLetters.contains(guess))       //Checks to see if letter has been used
        {
            System.out.println("\nYou have already guessed the letter " + guess + " before.");
        }
        else 
        {
            {
                usedLetters = usedLetters + guess.toString();     //Adds letter to sting.
            }
        }       
    }

    private static void match(char[] word, char[] userGuess) 
    {
        int j = 0;      

        for (int i = 0; i < word.length; i++) 
        {           
            if (userGuess[0] == word[i]) 
            {               
                asterisk[i] = userGuess[0];             
                j++;
            }   
        }

        if (j > 0)          //If there is a matching letter, it jumps in here.
        {
            System.out.println("There is " + j + " correct letters for your guess.\n");         
            if (!contains('*', asterisk))        //if the asterisk word does not contain any more
            {                                    //asterisks you jumps in here to show user has won
                System.out.println("\n****************************");
                System.out.println("*     YOU ARE A WINNER     *");
                System.out.println("****************************\n");
                System.out.println("The word you guessed was ");        
                count = 9;
            }   
        }

        else                            //If there is not match it comes down here
        {
            count--;
            System.out.println("There was not a match for that letter.\n");
            System.out.println("**************************************");
            System.out.println("You have " + count + " guesses left.\n");
        }
        System.out.println("---------------");
        System.out.println(asterisk);
        System.out.println("---------------");
    }

    private static boolean contains(char c, char[] asterisk) 
    {
        for (char x : asterisk)
        {
            if (x == c)
            {
                return true;
            }
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

您必须使用chosenWord方法初始化wordgameStart,以便每次开始新游戏时,都会再次随机选择一个单词。