好的,这就是我现在所拥有的,而且我仍然无法让它工作:(。如果你输了就让它工作但是它仍然不会说你赢了,即使我添加了一个检查到的字符串看它是否等于你获胜的长度
static String[] words = new String[] { "turtle","hamster","rabbit","potato",
"carrot","mouse","dog","cat","applejuice","milk","cow"};
static char[] dashes;
static String randomWord;
static int wrongGuesses;
static int playLoop;
static int lettersRight;
static boolean gameWon = false;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out
.println("Do you want to play?, Enter 1 to play or 0 to quit");
playLoop = input.nextInt();
if (playLoop == 0) {
System.out.println(":'(, The console is sad :'(");
} else if (playLoop > 0) {
System.out.println("The Game Has Started!");
playLoop();
}
}
public static void hangMan() {
if (wrongGuesses == 0) {
System.out.println("----------");
System.out.println("| |");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (wrongGuesses == 1) {
System.out.println("----------");
System.out.println("| |");
System.out.println("| O");
System.out.println("|");
System.out.println("|");
} else if (wrongGuesses == 2) {
System.out.println("----------");
System.out.println("| |");
System.out.println("| O");
System.out.println("| |");
System.out.println("|");
} else if (wrongGuesses == 3) {
System.out.println("----------");
System.out.println("| |");
System.out.println("| O");
System.out.println("| --|");
System.out.println("|");
} else if (wrongGuesses == 4) {
System.out.println("----------");
System.out.println("| |");
System.out.println("| O");
System.out.println("| --|--");
System.out.println("|");
} else if (wrongGuesses == 5) {
System.out.println("----------");
System.out.println("| |");
System.out.println("| O");
System.out.println("| --|--");
System.out.println("| /");
} else if (wrongGuesses == 6) {
System.out.println("----------");
System.out.println("| |");
System.out.println("| O");
System.out.println("| --|--");
System.out.println("| / \\");
} else if (wrongGuesses == 7) {
System.out.println("----------");
System.out.println("| |");
System.out.println("| O");
System.out.println("| --|--");
System.out.println("| _/ \\");
}
}
private static void playLoop() {
randomWord = getWord();
int guessesLeft = 8;
System.out.println(randomWord);
for (int x = 0; x < randomWord.length(); x++) {
dashes[x] = '_';
}
wrongGuesses = 0;
String usedLetters = "";
while (wrongGuesses < 8) {
if (guessesLeft < 2) {
System.out.println("You have " + guessesLeft + " guess left.");
} else {
System.out
.println("You have " + guessesLeft + " guesses left.");
}
System.out.println("Guess a letter!");
for (int y = 0; y < randomWord.length(); y++) {
System.out.print(dashes[y] + " ");
}
System.out.println("");
char userGuess = input.next().charAt(0);
if (!checkGuess(userGuess)) {
guessesLeft--;
wrongGuesses++;
}
if (checkGuess(userGuess)){
lettersRight ++;
}
if (lettersRight == randomWord.length()) {
gameWon = true;
}
if (wrongGuesses == 8){
System.out.println("----------");
System.out.println("| |");
System.out.println("| O");
System.out.println("| --|--");
System.out.println("| _/ \\_");
System.out.println("Your friend is dead :(");
System.out.println();
main(new String[1]);
}
hangMan();
usedLetters += userGuess + " ";
System.out.println("Guessed Letters ---> "
+ usedLetters);
}
}
private static String getWord() {
int randomNum = (int) ((Math.random() * 9) + 1);
randomWord = words[randomNum];
dashes = new char[randomWord.length()];
return randomWord;
}
private static boolean checkGuess(char userGuess) {
boolean correct = false;
for (int z = 0; z < randomWord.length(); z++) {
if (userGuess == randomWord.charAt(z)) {
dashes[z] = userGuess;
correct = true;
}
}
return correct;
}
public static void win(){
if (gameWon) {
System.out.println("Congrats You Won The Game");
}
}
}
答案 0 :(得分:1)
至于让你的节目说'#34;恭喜&#34;,我没有看到任何逻辑来决定何时赢得比赛。看起来游戏将一直持续到你得到8个错误的猜测,即使在单词完成之后。添加该逻辑的一种简单方法是使用布尔值和计数器来跟踪已猜出的字母数。
建议:添加代码以在提示下一个猜测时显示错误的猜测。添加了代码,通过反复猜测同一个字母来防止玩家作弊。
static boolean gameWon = false;
static int lettersRight = 0;
static char[] wrongLetters = new char[8];
private static void playLoop() {
....
// include boolean in loop condition so it can exit when won
while (wrongGuesses < 8 && !gameWon) {
....
System.out.println("Guess a letter!");
for (int y = 0; y < randomWord.length(); y++) {
System.out.print(dashes[y] + " ");
}
// display previous guesses
System.out.println("Wrong guesses!");
for (int w = 0; w < wrongGuesses; w++) {
System.out.print(wrongLeters[w] + " ");
}
char userGuess = input.next().charAt(0);
// verify guess is a new letter
if (!alreadyGuessed(userGuess)) {
if (!checkGuess(userGuess)) {
guessesLeft--;
wrongGuesses++;
// add incorrect guesses to an array
wrongLetters[wrongGuesses] = userGuess;
}
else {
// check counter against length and set boolean
if (lettersRight == randomWord.length()) {
gameWon = true;
}
}
}
else {
System.out.println("No Cheating! That letter has already been used.");
}
....
// check if game was won or lost after the while loop exits
if (gameWon) {
System.out.println("Congrats You Won The Game");
}
else {
System.out.println("Your friend is dead :(");
}
....
}
private static boolean checkGuess(char userGuess) {
boolean correct = false;
for (int z = 0; z < randomWord.length(); z++) {
if (userGuess == randomWord.charAt(z)) {
dashes[z] = userGuess;
correct = true;
// increase counter inside for loop so any letters
// occurring more than once are all counted
letterRight++;
}
}
return correct;
}
// method to compare user input already correct letter guesses
// this prevents users from cheating by using the same letter repeatedly
private static boolean alreadyGuessed(char userGuess) {
boolean used= false;
for (int q = 0; q < randomWord.length(); q++) {
if (userGuess == dashes(q)) {
used = true;
}
}
return used;
}
答案 1 :(得分:0)
要让用户在猜测单词时打印出一条消息,每次在游戏循环结束时检查一下,看看他们是否正确猜对了,然后使用选择语句来处理它
无论如何,这是必要的,因为你不希望他们在获胜后继续猜测这个词。