Hangman显示发现了一封信

时间:2013-03-31 13:59:19

标签: java

我目前正在用java编写一个刽子手控制台游戏,到目前为止没有任何问题。

我现在遇到了一个似乎无法解决的问题。

我有不同单词的数组,简单,中等和硬,并根据所选择的难度随机得到一个单词。然后我将该单词转换为*,以便玩家无法看到这些字符。

然后向用户询问信件,并且程序通知用户他们是对还是错。但问题是,如果在单词中,我似乎无法将*改为字母。因此,例如,如果单词是“bee”并且玩家选择了b,我希望 *更改为b

有人能帮我指出正确的方向吗?

Hangman课程:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hangman;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author Adam2_000
 */
public class Hangman {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


        String player = "";
        String selection;
        int turn = 12;
        List<String> wordBox = new ArrayList<String>();
        boolean hangman = false;
        Scanner scan = new Scanner(System.in);

        //New instances of class and arrays
        words words = new words();
        String easyWords1[] = words.easyWords;
        String mediumWords1[] = words.mediumWords;
        String hardWords1[] = words.hardWords;
        Random random = new Random();


        System.out.println("Welcome to Hangman version 1");
        System.out.println("Please choose a difficulty");
        System.out.println("A: Easy");
        System.out.println("B: Medium");
        System.out.println("C: Hard");

        System.out.println(" _________     ");
        System.out.println("|         |    ");
        System.out.println("|         0    ");
        System.out.println("|        /|\\  ");
        System.out.println("|        / \\  ");
        System.out.println("|              ");
        System.out.println("|              ");


        char iChoice;

        do {
            selection = scan.nextLine().toUpperCase();
        } while (selection.isEmpty());
        iChoice = selection.charAt(0);
        if (iChoice != 'X') {
            switch (iChoice) {

                case 'A':
                    System.out.println("You have choosen easy:");
                    //Get and print random string from easyWords                   
                    int selectA = random.nextInt(easyWords1.length);
                    String replaceAllEasy = easyWords1[selectA].replaceAll("\\S", "*");
                    // Print the randomly selected word and its length
                    System.out.println("Random String selected: " + replaceAllEasy);
                    System.out.println("This word contains " + easyWords1[selectA].length() + " letters");

                    //while hangman == false continue to ask player for letters
                    while (hangman == false) {
                        System.out.println("Turns remaining: " + turn);
                        System.out.println("Please choose a letter A-Z :");
                        String easyChosenLetter = scan.next();

                        if (wordBox.contains(easyChosenLetter)) {
                            System.out.println("Letter alreay choosen please choose another letter");
                        } else {
                            wordBox.add(easyChosenLetter);
                        }

                        if (easyWords1[selectA].contains(easyChosenLetter)) {

                            System.out.println("Wordbox letters are: " + wordBox);
                            System.out.println("Yes!");
                            turn--;
                        } else {
                            System.out.println("Wordbox letters are: " + wordBox);
                            System.out.println("No!");
                            turn--;
                        }

                        if (turn == 0) {
                            hangman = true;
                        }

                        while (hangman == true) {
                            System.out.println("You lose!");
                            System.exit(0);
                        }

                    }
                    break;

                case 'B':
                    System.out.println("You have choosen Medium");
                    //Get and print random string from mediumWords
                    int selectB = random.nextInt(mediumWords1.length);
                    String replaceAllMedium = mediumWords1[selectB].replaceAll("\\S", "*");
                    // Print the randomly selected word and its length
                    System.out.println("Random String selected: " + replaceAllMedium);
                    System.out.println("This word contains " + mediumWords1[selectB].length() + " letters");

                    //while hangman == false continue to ask player for letters
                    while (hangman == false) {
                        System.out.println("Turns remaining: " + turn);
                        System.out.println("Please choose a letter A-Z :");
                        String mediumChosenLetter = scan.next();

                        if (wordBox.contains(mediumChosenLetter)) {
                            System.out.println("Letter alreay choosen please choose another letter");
                        } else {
                            wordBox.add(mediumChosenLetter);
                        }

                        wordBox.add(mediumChosenLetter);
                        if (easyWords1[selectB].contains(mediumChosenLetter)) {
                            System.out.println("Wordbox letters are: " + wordBox);
                            System.out.print("Yes!");
                            turn--;

                        } else {
                            System.out.println("Wordbox letters are: " + wordBox);
                            System.out.println("No!");
                            turn--;
                        }

                        if (turn == 0) {
                            hangman = true;
                        }

                        while (hangman == true) {
                            System.out.println("You lose!");
                            System.exit(0);
                        }

                    }
                    break;

                case 'C':
                    System.out.println("You have choosen Hard");
                    //Get and print random string from hardWords
                    int selectC = random.nextInt(hardWords1.length);
                    String replaceAllHard = hardWords1[selectC].replaceAll("\\S", "*");
                    // Print the randomly selected word and its length  
                    System.out.println("Random String selected: " + replaceAllHard);
                    System.out.println("This word contains " + hardWords1[selectC].length() + " letters");

                    //while hangman == false continue to ask player for letters
                    while (hangman == false) {
                        System.out.println("Turns remaining: " + turn);
                        System.out.println("Please choose a letter A-Z :");
                        String hardChosenLetter = scan.next();

                        if (wordBox.contains(hardChosenLetter)) {
                            System.out.println("Letter alreay choosen please choose another letter");
                        } else {
                            wordBox.add(hardChosenLetter);
                        }

                        wordBox.add(hardChosenLetter);
                        if (easyWords1[selectC].contains(hardChosenLetter)) {
                            System.out.println("Wordbox letters are: " + wordBox);
                            System.out.print("Yes!");
                            turn--;

                        } else {
                            System.out.println("Wordbox letters are: " + wordBox);
                            System.out.println("No!");
                            turn--;
                        }

                        if (turn == 0) {
                            hangman = true;
                        }

                        while (hangman == true) {
                            System.out.println("You lose!");
                            System.exit(0);
                        }


                    }
                    break;



            }

        }
    }
}

单词类:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hangman;

import java.lang.reflect.Array;
import java.util.Random;

/**
 *
 * @author Adam2_000
 */
public class words extends Hangman {

    String[] easyWords = {"Bee", "Car", "Fish", "Shed"};
    String[] mediumWords = {"House", "Sheep", "Castle", "Phone"};
    String[] hardWords = {"Octagon", "Crocodile", "Chocolate", "Motorbike"};

    public String[] getEasyWords() {
        return easyWords;
    }

    public void setEasyWords(String[] easyWords) {
        this.easyWords = easyWords;
    }

    public String[] getMediumWords() {
        return mediumWords;
    }

    public void setMediumWords(String[] mediumWords) {
        this.mediumWords = mediumWords;
    }

    public String[] getHardWords() {
        return hardWords;
    }

    public void setHardWords(String[] hardWords) {
        this.hardWords = hardWords;
    }

    @Override
    public String toString() {
        return "words{" + "easyWords=" + easyWords + ", mediumWords=" + mediumWords + ", hardWords=" + hardWords + '}';
    }
}

1 个答案:

答案 0 :(得分:1)

您将不得不将字符串“***”替换为“B **”,而不是更改字符串的第一个字符。 字符串在Java中是不可变的

String oldstr = "***";
String newstr = 'B'+oldstr.substring(1,2);

可能Strings不是此任务的正确数据结构。也许你想使用类似字符数组的东西并编写自己的print函数。

即使是包含这些字符数组的Word类也是一个好主意,因为每个对象都可以有自己的索引,告诉你有多少*'s可以猜测等等。


Word类的半伪。这实际上使用String s。未经测试。

public class Word{

  private String name;

  private String guessedSoFar;

  public String getGuessedSoFar(){
    return guessedSoFar;
  }

  public void changeIndexWith(String achar, int index){
     String temp = name.substring(0,index) + achar + name.substring(index+1, name.length();
  guessedSoFar = temp;
  }

  public void setName(String n){
    name = n;
  }

  public String getName(){
    return name;
  }
}