Java - 使用 StringBuilder 的 Hangman 游戏。捕捉一个单词中的多个字母
Java - Hangman game with StringBuilder. Catching multiple letters in a word
我只是编码方面的初学者,并试图用几个 classes 制作我的第一个项目,这是一个刽子手游戏。我知道这里有很多关于该主题的线程,但无法找到适用于 StringBuilder 的解决方案。我坚持在单词中捕捉多个字母。任何人都可以帮忙吗?
有问题的方法是 checkAnswer,如下所示:
public void checkAnswer (){
if (logic.line.indexOf(answer)!= -1){
System.out.println ("You are right! The letter " + answer + " exists");
StringBuilder guessedChar = new StringBuilder(logic.newLine);
guessedChar.setCharAt(logic.line.indexOf(answer), answer);
lettersGuessed = answer +lettersGuessed;
score = score + 1;
System.out.println("Letters guessed: " + lettersGuessed);
System.out.println("Letters missed: " + lettersMissed);
System.out.println("Your score is:" + score);
logic.newLine = guessedChar.toString();
System.out.println(logic.newLine);
}}
完整代码如下:
主要 class
public class 主要 {
public static void main(String[] args) throws Exception {
Logic act = new Logic();
Game game = new Game();
act.getListOfMovies();
act.CodedLine();
do { game.input();
game.checkCondition();
game.checkAnswer();
game.checkScore();
} while (game.lettersGuessed.length() != act.line.length() && game.score!= -10);
}}
Class逻辑
public class Logic {
static String line;
static String newLine;
public String[] listOfMovies;
public Scanner fileScanner;
public Logic() {
}
public String getListOfMovies() throws Exception {
File fileWithMovies = new File("MoviesList.txt");//file access
Scanner fileScanner = new Scanner(fileWithMovies);//file scan
while (fileScanner.hasNext()) { // while there is a next line
line = fileScanner.nextLine();
String[] listOfMovies = new String[24]; //introduce array
for (int i = 0; i < listOfMovies.length; i++) { //
listOfMovies[i] = fileScanner.nextLine();
}
int random = (int) (Math.random() * listOfMovies.length); //get random number
for (int i = 0; i < line.length(); i++) { //get random movie
if (Character.isLetter(line.charAt(i))) {
line = listOfMovies[random];
System.out.println(line);
}
return line;
}
return line;
}return line;
}
public String CodedLine() {
newLine = line.replaceAll("[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]", "_");
System.out.println(newLine);
return newLine;
}}
Class 游戏
public class Game {
char answer;
Logic logic = new Logic();
String lettersGuessed = " ";
String lettersMissed = " ";
int score = 0;
public char input () {
Scanner inputScanner = new Scanner(System.in);
System.out.println("Type in your guess");
answer = inputScanner.next().charAt(0);
System.out.println("Your guess: " + answer);
return answer;
}
public void checkCondition (){
if (logic.line.indexOf(answer)==-1){
System.out.println("You are wrong. The letter " + answer + " does not exist");
lettersMissed = answer + lettersMissed;
score = score - 1;
System.out.println("Letters guessed: " + lettersGuessed);
System.out.println("Letters missed: " + lettersMissed);
System.out.println("Your score is:" + score);
}}
public void checkAnswer (){
if (logic.line.indexOf(answer)!= -1){
System.out.println ("You are right! The letter " + answer + " exists");
StringBuilder guessedChar = new StringBuilder(logic.newLine);
guessedChar.setCharAt(logic.line.indexOf(answer), answer);
lettersGuessed = answer +lettersGuessed;
score = score + 1;
System.out.println("Letters guessed: " + lettersGuessed);
System.out.println("Letters missed: " + lettersMissed);
System.out.println("Your score is:" + score);
logic.newLine = guessedChar.toString();
System.out.println(logic.newLine);
}}
public void checkScore (){
if (score == -10) {
System.out.println("The game is over! You lost..."); }}}
不会为你解决问题,不是这个网站的目的,而是建议:
- check this method - 基本上你告诉它
indexOf
应该从哪里开始搜索。首先你从 0
开始,然后如果你找到一个结果,你从 foundResultIndex + 1
开始,把它全部放在一个循环中,瞧。
word: batman, guess: a
int startAt = 0;
int foundResultIndex = "batman".indexOf('a', startAt) // 1
startAt = foundResultIndex + 1;
foundResultIndex = "batman".indexOf('a', startAt) // 4
...
不要在程序中保留太多状态,尤其是像 Logic
class 中那样的静态状态。你的 getListOfMovies
是一个完美的例子,你已经返回了一个结果,为什么要将它保存在一些 line
变量中?请改用返回的结果。
replaceAll
很有趣,改用这个 line.replaceAll("[a-z]", "_");
//Create list for index
ArrayList<Integer>answerIndexList=new ArrayList<Integer>();
//get char index
for (int i=0;i<logic.line.length();i++){
if(logic.line.charAt(i) == answer){
answerIndexList.add(i);
}
}
//replace index with answer
for(int i=0;i<answerIndexList.size();i++){
guessedChar.setCharAt(answerIndexList.get(i), answer);
}
logic.newLine = guessedChar.toString();
我只是编码方面的初学者,并试图用几个 classes 制作我的第一个项目,这是一个刽子手游戏。我知道这里有很多关于该主题的线程,但无法找到适用于 StringBuilder 的解决方案。我坚持在单词中捕捉多个字母。任何人都可以帮忙吗? 有问题的方法是 checkAnswer,如下所示:
public void checkAnswer (){
if (logic.line.indexOf(answer)!= -1){
System.out.println ("You are right! The letter " + answer + " exists");
StringBuilder guessedChar = new StringBuilder(logic.newLine);
guessedChar.setCharAt(logic.line.indexOf(answer), answer);
lettersGuessed = answer +lettersGuessed;
score = score + 1;
System.out.println("Letters guessed: " + lettersGuessed);
System.out.println("Letters missed: " + lettersMissed);
System.out.println("Your score is:" + score);
logic.newLine = guessedChar.toString();
System.out.println(logic.newLine);
}}
完整代码如下: 主要 class public class 主要 {
public static void main(String[] args) throws Exception {
Logic act = new Logic();
Game game = new Game();
act.getListOfMovies();
act.CodedLine();
do { game.input();
game.checkCondition();
game.checkAnswer();
game.checkScore();
} while (game.lettersGuessed.length() != act.line.length() && game.score!= -10);
}}
Class逻辑
public class Logic {
static String line;
static String newLine;
public String[] listOfMovies;
public Scanner fileScanner;
public Logic() {
}
public String getListOfMovies() throws Exception {
File fileWithMovies = new File("MoviesList.txt");//file access
Scanner fileScanner = new Scanner(fileWithMovies);//file scan
while (fileScanner.hasNext()) { // while there is a next line
line = fileScanner.nextLine();
String[] listOfMovies = new String[24]; //introduce array
for (int i = 0; i < listOfMovies.length; i++) { //
listOfMovies[i] = fileScanner.nextLine();
}
int random = (int) (Math.random() * listOfMovies.length); //get random number
for (int i = 0; i < line.length(); i++) { //get random movie
if (Character.isLetter(line.charAt(i))) {
line = listOfMovies[random];
System.out.println(line);
}
return line;
}
return line;
}return line;
}
public String CodedLine() {
newLine = line.replaceAll("[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]", "_");
System.out.println(newLine);
return newLine;
}}
Class 游戏
public class Game {
char answer;
Logic logic = new Logic();
String lettersGuessed = " ";
String lettersMissed = " ";
int score = 0;
public char input () {
Scanner inputScanner = new Scanner(System.in);
System.out.println("Type in your guess");
answer = inputScanner.next().charAt(0);
System.out.println("Your guess: " + answer);
return answer;
}
public void checkCondition (){
if (logic.line.indexOf(answer)==-1){
System.out.println("You are wrong. The letter " + answer + " does not exist");
lettersMissed = answer + lettersMissed;
score = score - 1;
System.out.println("Letters guessed: " + lettersGuessed);
System.out.println("Letters missed: " + lettersMissed);
System.out.println("Your score is:" + score);
}}
public void checkAnswer (){
if (logic.line.indexOf(answer)!= -1){
System.out.println ("You are right! The letter " + answer + " exists");
StringBuilder guessedChar = new StringBuilder(logic.newLine);
guessedChar.setCharAt(logic.line.indexOf(answer), answer);
lettersGuessed = answer +lettersGuessed;
score = score + 1;
System.out.println("Letters guessed: " + lettersGuessed);
System.out.println("Letters missed: " + lettersMissed);
System.out.println("Your score is:" + score);
logic.newLine = guessedChar.toString();
System.out.println(logic.newLine);
}}
public void checkScore (){
if (score == -10) {
System.out.println("The game is over! You lost..."); }}}
不会为你解决问题,不是这个网站的目的,而是建议:
- check this method - 基本上你告诉它
indexOf
应该从哪里开始搜索。首先你从0
开始,然后如果你找到一个结果,你从foundResultIndex + 1
开始,把它全部放在一个循环中,瞧。
word: batman, guess: a
int startAt = 0;
int foundResultIndex = "batman".indexOf('a', startAt) // 1
startAt = foundResultIndex + 1;
foundResultIndex = "batman".indexOf('a', startAt) // 4
...
不要在程序中保留太多状态,尤其是像
Logic
class 中那样的静态状态。你的getListOfMovies
是一个完美的例子,你已经返回了一个结果,为什么要将它保存在一些line
变量中?请改用返回的结果。replaceAll
很有趣,改用这个line.replaceAll("[a-z]", "_");
//Create list for index
ArrayList<Integer>answerIndexList=new ArrayList<Integer>();
//get char index
for (int i=0;i<logic.line.length();i++){
if(logic.line.charAt(i) == answer){
answerIndexList.add(i);
}
}
//replace index with answer
for(int i=0;i<answerIndexList.size();i++){
guessedChar.setCharAt(answerIndexList.get(i), answer);
}
logic.newLine = guessedChar.toString();