背景:我正在通过赫尔辛基大学的大规模开放在线课程(MOOC)计划学习Java。目标之一是建立一个基于文本的子手游戏。这里是更多参考:https://materiaalit.github.io/2013-oo-programming/part1/week-2/
我找出了大部分代码。但是,游戏执行笨拙。要进行扩展,游戏应该一直运行直到猜中或猜中的数目达到7。因此,while循环将继续工作直到达到7或正确猜出单词为止。
问题:我的猜测是丰田。 printWord()方法通过执行以下操作隐藏单词:******。每次用户猜字母时,都会删除星号。因此,如果我猜对了字母,应该这样做:* o ** o。但是,当用户输入下一个字母时,它会忘记猜测为“ o”。因此,它目前的工作方式如下:
User: o
printWord(): *o*o**
User: T
printWord(): T*****
这是我的主班:
public class main {
public static void main(String[] args) {
String word = "Toyota";
int count = 0;
Hangman hangman = new Hangman();
System.out.println("************");
System.out.println("* Hangman *");
System.out.println("************");
System.out.println("");
hangman.printMenu();
System.out.println("");
Scanner reader = new Scanner (System.in);
String input = reader.nextLine();
if (input.equals("quits")){
System.out.println("Thank you for playing!");
}else{
while (count < 7){
char c = reader.next().charAt(0);
hangman.printWord(c);
hangman.printMan(count);
hangman.printStatus(c);
count++;
}
}
}
}
这是子手班:
public class Hangman {
public static void printMenu(){
System.out.println(" * menu *");
System.out.println("game on - starts the game.");
System.out.println("quit - quits the game");
System.out.println("status - prints the game status");
System.out.println("a single letter uses the letter as a guess");
System.out.println("an empty line prints this menu");
}
public static void printMan(int count){
String word = "Toyota";
if (count == 1) {
System.out.println("Wrong guess, try again");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("___|___");
System.out.println();
}
if (count == 2) {
System.out.println("Wrong guess, try again");
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 (count == 3) {
System.out.println("Wrong guess, try again");
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 (count == 4) {
System.out.println("Wrong guess, try again");
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 (count == 5) {
System.out.println("Wrong guess, try again");
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 (count == 6) {
System.out.println("Wrong guess, try again");
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 (count == 7) {
System.out.println("GAME OVER!");
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! The word was " + word);
}
}
public static void printStatus(char c){
String mystring = "abcdefghijklmnopqrstuvwxyz";
int count = 0;
int sum = 0;
StringBuilder sb = new StringBuilder();
sb.append(mystring);
for (int i = 0; i < sb.length(); i++){
if (c ==sb.charAt(i)){
sb.deleteCharAt(i);
}
}
count ++;
sum = sum + count;
System.out.println(sb.toString());
System.out.println("The number of guesses are: " + sum);
}
public void printWord(char c){
String word = "Toyota";
int count = 0;
char [] blank = new char [word.length()];
for (int i =0; i < word.length(); i++){
blank[i] = '*';
}
if (word.contains(c + "")){
for (int j = 0; j <word.length(); j++ ){
if (word.charAt(j) ==c){
blank[j] = c;
}
count++;
}
}else {
count++;
printMan(count);
System.out.println("Wrong guess");
}
if (word.equals(String.valueOf(blank))){
System.out.println(blank);
System.out.println("You won");
}
System.out.println(blank);
}
public static void gameOn(){
int count = 0;
if (count < 7){
System.out.println("Keep Trying. You can get this");
System.out.println(count);
}else {
System.out.println("Game Over bud");
System.out.println(count);
}
}
}
我假设将方法printWord()设为静态是个问题,因为使该方法的新实例会重置所有内容。但是,删除该关键字无效。由于某些原因,printStatus可以跟踪使用的字母,但不能跟踪实际计数。
任何帮助总是值得赞赏的。谢谢!
答案 0 :(得分:1)
制作单词和空白全局变量,以及在调用构造函数时设置变量的构造函数。如下所示。
将在printWord()
中创建空白char数组的行移至构造函数。因为每次调用printWord
都是在创建一个新的空白数组或替换旧的空白数组,这就是打印空白的原因。您只想设置一次空白char数组,构造Hangman()
将是最好的选择,因为在创建hangman对象(Hangman hangman = new Hangman();
)时仅调用一次。我希望这是有道理的。
让我知道这是否解决了您的问题。
添加/更新代码
public class Hangman {
private String word;
private char[] blank;
private int guesses;
public Hangman() {
word = "Toyota";
blank = new char [word.length()];
for (int i =0; i < word.length(); i++){
blank[i] = '*';
}
guesses = 0;
}
// Optional Constructor - Hangman hangman = new Hangman("Toyota");
public Hangman(String word) {
this.word = word;
blank = new char [word.length()];
for (int i =0; i < word.length(); i++){
blank[i] = '*';
}
guesses = 0;
}
/**
* Rest of your code
*/
}
删除代码-删除通过printWord()
方法标记的注释行
//String word = "Toyota";
int count = 0;
//char [] blank = new char [word.length()];
//for (int i =0; i < word.length(); i++){
// blank[i] = '*';
//}
已更新-printStatus
public static void printStatus(char c){
String mystring = "abcdefghijklmnopqrstuvwxyz";
int count = 0;
StringBuilder sb = new StringBuilder();
sb.append(mystring);
for (int i = 0; i < sb.length(); i++){
if (c ==sb.charAt(i)){
sb.deleteCharAt(i);
}
}
guesses++;
System.out.println(sb.toString());
System.out.println("The number of guesses are: " + guesses);
}