我正在使用 Java 编写一个刽子手游戏。我想让事情变得更困难,所以我添加了一个计时条件,让用户猜测这个词(例如:20 秒),现在我面临的问题是 while 循环不想在 20 秒完成后退出。它继续等待用户输入他们的猜测,然后更新他输了,这是不合逻辑的,因为如果你已经输了,为什么我还要继续等待你的答案?我尝试添加这一行来打破它,因为一旦时间到了,超时变量将自动更改为 true:
if (timeout) { break; }
但这不起作用!
这是我的代码:
package hangman;
import java.io.IOException;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class Essai {
private static String[] fruits = {"straweberry","bananas","orange","apple",
"watermelon","peach"};
private static String[] countries = {"tunisia","spain","japan","qatar",
"greece","palestine"};
private static String[] animals = {"shark","wolf","cat","cow","snake",
"horse"};
private static String[] languages = {"arabic","korean","english","russian",
"turkish","hindi"};
private static String[] occupations = {"doctor","engineer","teacher",
"baker","clown","pilot"};
private static Scanner scanner;
private static boolean timeout;
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_BLUE = "\u001B[34m";
public static void hangman(int tries, String word) {
if (tries == 1) {
System.out.println();
System.out.println();
System.out.println("___|___");
System.out.println();
}
if (tries == 2) {
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (tries == 3) {
System.out.println(" ____________");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" | ");
System.out.println("___|___");
}
if (tries == 4) {
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (tries == 5) {
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" |");
System.out.println("___|___");
}
if (tries == 6) {
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
}
if (tries == 7) {
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | _|_");
System.out.println(" | / | \\");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
System.out.println("Game over! It was: " + word);
}
}
public static void main(String[] args) throws IOException {
scanner = new Scanner(System.in);
int n = (int) Math.floor(Math.random() * (5)) + 1;
String word = null;
switch (n) {
case 1:
System.out
.println(ANSI_GREEN + "Category: fruits" + ANSI_RESET);
word = fruits[(int) (Math.random() * fruits.length)];
break;
case 2:
System.out.println(
ANSI_GREEN + "Category: countries" + ANSI_RESET);
word = countries[(int) (Math.random() * countries.length)];
break;
case 3:
System.out
.println(ANSI_GREEN + "Category: animals" + ANSI_RESET);
word = animals[(int) (Math.random() * countries.length)];
break;
case 4:
System.out.println(
ANSI_GREEN + "Category: languages" + ANSI_RESET);
word = languages[(int) (Math.random() * countries.length)];
break;
case 5:
System.out.println(
ANSI_GREEN + "Category: occupations" + ANSI_RESET);
word = occupations[(int) (Math.random() * countries.length)];
break;
}
timeout = false;
Timer timer = new Timer();
long startTime = System.currentTimeMillis();
timer.schedule(new TimerTask() {
public void run() {
timeout = true;
}
}, 20 * 1000);
// word="cat";
int nb = word.length();
String chl = word;
char[] ch = new char[nb];
String chf = "";
int i;
for (i = 0; i < nb; i++) {
ch[i] = '-';
chf = chf + '*';
}
System.out.println(
ANSI_BLUE + "Word contains " + nb + " letters: " + ANSI_RESET);
System.out.println(ANSI_RED + "You have 20 secs. Go!" + ANSI_RESET);
int tries = 0;
while (!timeout && tries < 7 && word.equals(chf) == false) {
System.out.println("Current progress: " + new String(ch));
// System.out.println(timeout);
if (timeout) {
break;
}
System.out.print(ANSI_RED + "\nGuess a letter: " + ANSI_RESET);
char letter = scanner.nextLine().charAt(0);
long remainingTime = (20 * 1000)
- (System.currentTimeMillis() - startTime);
System.out
.println("Time left: " + remainingTime / 1000 + " seconds");
int pos = word.indexOf(letter);
if (pos == -1) {
System.out.println("\nWrong Guess!");
tries++;
hangman(tries, chl);
} else {
while (pos != -1) {
ch[pos] = letter;
word = word.replaceFirst(String.valueOf(letter), "*");
pos = word.indexOf(letter);
}
}
}
timer.cancel();
if (timeout) {
System.out.println("Time's up! You took too long to guess.");
} else {
if (word.equals(chf)) {
System.out.print("Congratulations! You've guessed the word: "
+ chl + "!");
}
}
}
}
我引入了以下更改:
lock
用于超时线程和主线程之间同步的变量。其工作原理如下:
超时线程将
timeout
标志设置为 true 并使用 lock.notify()
通知主线程
主线程使用
lock.wait()
块中的 synchronized
等待此通知。仅当超时或游戏结束时才继续执行。
导入java.util.Scanner; 导入java.util.Timer; 导入 java.util.TimerTask;
公开课作文{ private static String[] 水果 = {"草莓", "香蕉", "橙子", "苹果", “西瓜”、“桃子”}; private static String[] states = {"突尼斯", "西班牙", "日本", "卡塔尔", “希腊”、“巴勒斯坦”}; private static String[] Animals = {"鲨鱼", "狼", "猫", "牛", "蛇", “马”}; private static String[] languages = {"阿拉伯语", "韩语", "英语", "俄语", “土耳其语”、“印地语”}; private static String[] 职业 = {"医生", "工程师", "教师", “面包师”、“小丑”、“飞行员”}; 私人静态扫描仪扫描仪; 私有静态布尔超时; 私有静态最终对象锁=新对象(); 公共静态最终字符串 ANSI_RESET = "\u001B[0m"; 公共静态最终字符串 ANSI_RED = "\u001B[31m"; 公共静态最终字符串 ANSI_GREEN = "\u001B[32m"; 公共静态最终字符串 ANSI_BLUE = "\u001B[34m";
public static void hangman(int tries, String word) {
// The implementation of the hangman method remains the same
}
public static void main(String[] args) {
scanner = new Scanner(System.in);
int n = (int) Math.floor(Math.random() * 5) + 1;
String word = null;
switch (n) {
case 1:
System.out.println(ANSI_GREEN + "Category: fruits" + ANSI_RESET);
word = fruits[(int) (Math.random() * fruits.length)];
break;
case 2:
System.out.println(ANSI_GREEN + "Category: countries" + ANSI_RESET);
word = countries[(int) (Math.random() * countries.length)];
break;
case 3:
System.out.println(ANSI_GREEN + "Category: animals" + ANSI_RESET);
word = animals[(int) (Math.random() * animals.length)];
break;
case 4:
System.out.println(ANSI_GREEN + "Category: languages" + ANSI_RESET);
word = languages[(int) (Math.random() * languages.length)];
break;
case 5:
System.out.println(ANSI_GREEN + "Category: occupations" + ANSI_RESET);
word = occupations[(int) (Math.random() * occupations.length)];
break;
}
timeout = false;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
synchronized (lock) {
timeout = true;
lock.notify(); // Notify the main thread about the timeout
}
}
}, 20 * 1000);
int tries = 0;
String chf = "";
for (int i = 0; i < word.length(); i++) {
chf += "*";
}
System.out.println(
ANSI_BLUE + "Word contains " + word.length() + " letters: " + ANSI_RESET);
System.out.println(ANSI_RED + "You have 20 secs. Go!" + ANSI_RESET);
while (!timeout && tries < 7 && !word.equals(chf)) {
System.out.println("Current progress: " + chf);
if (timeout) {
break;
}
System.out.print(ANSI_RED + "\nGuess a letter: " + ANSI_RESET);
char letter = scanner.nextLine().charAt(0);
int pos = word.indexOf(letter);
if (pos == -1) {
System.out.println("\nWrong Guess!");
tries++;
hangman(tries, word);
} else {
while (pos != -1) {
chf = chf.substring(0, pos) + letter + chf.substring(pos + 1);
pos = word.indexOf(letter, pos + 1);
}
}
}
timer.cancel();
if (timeout) {
System.out.println("Time's up! You took too long to guess.");
} else {
if (word.equals(chf)) {
System.out.print("Congratulations! You've guessed the word: " + word + "!");
}
}
}
}