我是java的新手,我正在尝试创建一个基本的刽子手游戏,我已经创建了该轮的方法,但我不得不创建一个布尔值,将游戏转向下一轮(玩家)如果他写了错误的信,就会失去一次猜测信件;他只会错误10次;如果猜测,他只会继续游戏)。 到目前为止我有我的代码,我甚至尝试用if / else语句排除boolean,但对我来说这似乎很愚蠢。任何帮助都会有用。谢谢。
import java.util.Random;
import java.util.Scanner;
public class assignmenTwo {
public static void main(String[] args) {
welcomeMessage();
String myWord = randomizeWord();
char[] hiddenWord = hideWord(myWord);
printArr(hiddenWord);
char guess = userLine();
game(guess, myWord);
//
// indexOfGuessed(guess,myWord,hiddenWord);
// printArr(hiddenWord);
}
public static void game (char userGuess, String word) {
int attempts = 10;
while (attempts > 0) {
round();
for (int i = 0; i < word.length(); i++) {
if (userGuess != word.charAt(i)) {
attempts--;
System.out.println("You have"+attempts+"attempts. Try again!");
}
}
}
}
public static void round () {
String myWord = randomizeWord();
char[] hiddenWord = hideWord(myWord);
printArr(hiddenWord);
char guess = userLine();
indexOfGuessed(guess,myWord,hiddenWord);
printArr(hiddenWord);
return;
}
public static void welcomeMessage() {
String welcome = "Welcome";
System.out.println(welcome);
}// say hi
public static String randomizeWord() {
Random r = new Random();
String[] dictionary = {"hello", "dear", "Universe"};
int wordIndex = r.nextInt(dictionary.length);
String myWord = dictionary[wordIndex];
return myWord;
}
public static char[] hideWord(String myString) {
char[] hidWord = new char[myString.length()];
for (int i = 0; i < myString.length(); i++) {
hidWord[i] = '*';
}
return hidWord;
}
public static char userLine() {
Scanner in = new Scanner(System.in);
System.out.println("\nYour word is above. Guess a letter!");
char userInput = in.next().charAt(0);
return userInput;
}
public static String stars(String word) {
String stars = "";
for (int i = 0; i < word.length(); i++) {
stars += '*';
}
return stars;
}
public static void printArr (char [] arr) {
System.out.println();
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
public static void indexOfGuessed(char userGuess, String word, char[] hiddenWord) {
for (int i = 0; i < word.length(); i++) {
if (userGuess == word.charAt(i)) {
hiddenWord[i] = userGuess;
}
}
return;
}
}
答案 0 :(得分:0)
您可以像这样更改IndexOfGuessed
功能,这样它将返回搜索状态:
public static void findGuessed(char userGuess, String word, char[] hiddenWord) {
boolean found = false;
for (int i = 0; i < word.length(); i++) {
if (userGuess == word.charAt(i)) {
hiddenWord[i] = userGuess;
found = true;
}
}
return found;
}
然后你可以让round
也返回一个布尔值,表示它是否赢了:
public static void round () {
String myWord = randomizeWord();
char[] hiddenWord = hideWord(myWord);
printArr(hiddenWord);
char guess = userLine();
return findGuessed(guess, myWord, hiddenWord);
}
然后在game
中你可以检查一轮:
boolean won = round();
if (won) {
...
}
答案 1 :(得分:0)
而不是:
while (attempts > 0) {
round();
for (int i = 0; i < word.length(); i++) {
if (userGuess != word.charAt(i)) {
attempts--;
System.out.println("You have"+attempts+"attempts. Try again!");
}
}
}
您应该使用布尔值创建一个唯一的循环,该布尔值定义游戏是否结束:
boolean over = false;
while(!over){
over = round();
}
其中round()
将调用捕获用户建议的函数,将其与秘密字进行比较并递减attempts
的值(可以是类的属性或变量)传递参数)并在将结果提示给用户后,如果游戏结束则返回。
离开循环时,您可以询问用户是否希望再次尝试并调用设置功能重新启动游戏。
答案 2 :(得分:0)
另一件事(因为你的布尔返回值的问题似乎得到了解答):
每次调用round()
时,您都会创建一个新的String myWord
。
此字符串仅对round()
方法可见,与您在main方法中初始化的String myWord
不同。
我建议您只需将myWord
设为全局变量,设置一次,如果需要,让其他方法引用此变量。
就像现在一样,每次拨打round()
只会从词典中获取一个新的随机词。
我还说如果你没有让一切变得静止,你会更好,因为eclipse(或其他一些IDE)告诉你,你不能在静态主方法中做出非静态引用:
String myWord;
public static void main(String[] args) {
AssignmenTwo a2 = new AssignmenTwo();
a2.runGame();
//
// indexOfGuessed(guess,myWord,hiddenWord);
// printArr(hiddenWord);
}
public void runGame(){
welcomeMessage();
myWord = randomizeWord();
char[] hiddenWord = hideWord(myWord);
printArr(hiddenWord);
char guess = userLine();
game(guess, myWord);
}
public void game(char userGuess, String word) {
int attempts = 10;
while (attempts > 0) {
round();
for (int i = 0; i < word.length(); i++) {
if (userGuess != word.charAt(i)) {
attempts--;
System.out.println("You have" + attempts + "attempts. Try again!");
}
}
}
}
public void round() {
char[] hiddenWord = hideWord(myWord);
printArr(hiddenWord);
char guess = userLine();
indexOfGuessed(guess, myWord, hiddenWord);
printArr(hiddenWord);
return;
} ...