编辑 - 当玩家正确输入短语时,游戏现在结束,需要在我的for循环中休息。但是,当打印获胜者时,它会在arraylist中打印姓氏,当我想要打印时,谁正确猜到了这句话。
到目前为止,其他所有内容都适用于我的代码,例如它列出了一个输入数组列表的玩家数量,列出了他们在一轮结束后的收入等等。但我现在要做的是列出名称猜到这句话的人每当我尝试打印猜对象的人的名字时,它会打印下一个玩家的名字,而不是获得该短语的人。我将发布我认为错误可能位于的代码的两部分。
第一个是显示统计数据的位置,第二个块是玩家一直猜到的地方,直到达到短语,第三个块是getname和getwinnings方法然而,游戏继续并让其他玩家再猜一遍。我希望它能打印出任何先得到这句话的人。
最后,最后一个块是游戏类的整个代码,而不是整个程序,但是发生错误的代码(错误就像不打印谁赢了)
如果猜测结束时拼图结束也可以解决这个问题,这应该在第二个区块中调用!puzzleSolved时发生,但是在所有玩家完成猜测之前它会继续存在。
if (guess.compareToIgnoreCase(phrase) == 0) {
System.out.println("Congratulations! You guessed it!");
System.out.println("Now pass it to the next person, they get one chance to guess the phrase!");
puzzleSolved = true;
// Round is over.
System.out.println("The player who won is " +players.getName());
System.out.println("Everyone's earnings are ");
for (Player earnings : Players){
System.out.println(earnings.getName());
System.out.println("$" +earnings.getWinnings());
}
public void playGame() throws InterruptedException {
// while the puzzle isn't solved, keep going
while (!puzzleSolved) {
// let the players take turns guessing
int guess = 0;
for (Player turn : Players) { // Enhanced (for each) look to cycle through players
Players.get(guess);
guess++;
playTurn(turn);
}
}
}
public int getWinnings() {
return winnings;
}
/**
* Adds amount to the player's winnings
* @param amount int money to add
*/
public void incrementScore(int amount) {
if (amount < 0) return;
this.winnings += amount;
}
/**
* Getter
* @return String player's name
*/
public String getName() {
return name;
}
public WofFortuneGame(Wheel wheel) throws InterruptedException {
// get the wheel
this.wheel = wheel;
// do all the initialization for the game
setUpGame();
}
/**
* Plays the game
*
* @throws InterruptedException
*/
public void playGame() throws InterruptedException {
// while the puzzle isn't solved, keep going
while (!puzzleSolved) {
// let the players take turns guessing
int guess = 0;
for (Player turn : Players) { // Enhanced (for each) look to cycle through players
Players.get(guess);
guess++;
playTurn(turn);
}
}
}
/**
* Sets up all necessary information to run the game
*/
private void setUpGame() {
addPhrase(); // Calling method to add more phrases to the game
// create a single player
player1 = new Player("Player1");
// ask how many people are going to play
System.out.println("How many players will be participating today?");
Scanner playerCount = new Scanner(System.in);
int player = playerCount.nextInt();
System.out.print(+player);
if (player == 0) {
System.out.println(" You can't play a game with 0 players!"); // "Easter Egg"
System.exit(0);
}
System.out.println(" players will be playing, please enter their names"); // ask for players names
for (int i = 0; i < player; i++) {
Scanner playerName = new Scanner(System.in);
String name = playerName.nextLine();
players = new Player(name);
Players.add(players);
} // add players to Players array list
// print out the rules
System.out.println("RULES!");
System.out.println("Each player gets to spin the wheel, to get a number value");
System.out.println("Each player then gets to guess a letter. If that letter is in the phrase, ");
System.out.println(" the player will get the amount from the wheel for each occurence of the letter");
System.out.println("If you have found a letter, you will also get a chance to guess at the phrase");
System.out.println("Each player only has three guesses, once you have used up your three guesses, ");
System.out.println("you can still guess letters, but no longer solve the puzzle.");
System.out.println();
// User can enter own phrase
System.out.println("Would you like to enter your own phrase? (Y/N)");
Scanner customPhrase = new Scanner(System.in); // Scanner for if yes or no to create custom phrase
char letter = customPhrase.next().charAt(0);
if ((letter == 'Y') || (letter == 'y')) {
System.out.println("Please enter your phrase!");
Scanner typePhrase = new Scanner(System.in); // Scanner to type custom phrase
String custom = typePhrase.nextLine();
phrase = custom;
System.out.println("Now let's play!");
} else {
Random randomPhrase = new Random();
phrase = Phrases.get(randomPhrase.nextInt(Phrases.size()));
}
// for each character in the phrase, create a letter and add to letters arraylist
for (int i = 0; i < phrase.length(); i++) {
// letter_array[i] = new Letter(phrase.charAt(i)); stores in standard array
Letters.add(new Letter(phrase.charAt(i))); // stores in array list
}
// setup done
}
/**
* Method to add a number of phrases to implement into the game. Using
* ArrayList, one could easily add more phrases if the game becomes boring.
*/
private void addPhrase() {
String phrase1 = "Where the Sun Dont Shine";
Phrases.add(phrase1);
String phrase2 = "High Noon";
Phrases.add(phrase2);
String phrase3 = "Fly Me to the Moon";
Phrases.add(phrase3);
String phrase4 = "Im Batman";
Phrases.add(phrase4);
String phrase5 = "Happy Halloween";
Phrases.add(phrase5);
String phrase6 = "Rock and Roll";
Phrases.add(phrase6);
String phrase7 = "Stay in School";
Phrases.add(phrase7);
String phrase8 = "There is no place like home";
Phrases.add(phrase8);
String phrase9 = "Survival of the Fittest";
Phrases.add(phrase9);
String phrase10 = "No Mans Sky";
Phrases.add(phrase10);
String phrase11 = "Offense is the Best Defense";
Phrases.add(phrase11);
String phrase12 = "I Play to Win";
Phrases.add(phrase12);
}
/**
* One player's turn in the game Spin wheel, pick a letter, choose to solve
* puzzle if letter found
*
* @param player
* @throws InterruptedException
*/
private void playTurn(Player player) throws InterruptedException {
int money = 0;
Scanner sc = new Scanner(System.in);
System.out.println(player.getName() + ", you have $" + player.getWinnings());
System.out.println("Spin the wheel! <press enter>");
sc.nextLine();
System.out.println("<SPINNING>");
Thread.sleep(200);
Wheel.WedgeType type = wheel.spin();
System.out.print("The wheel landed on: ");
switch (type) {
case MONEY:
money = wheel.getAmount();
System.out.println("$" + money);
break;
case LOSE_TURN:
System.out.println("LOSE A TURN");
System.out.println("So sorry, you lose a turn.");
return; // doesn't get to guess letter
case BANKRUPT:
System.out.println("BANKRUPT");
player.bankrupt();
return; // doesn't get to guess letter
default:
}
System.out.println("");
System.out.println("Here is the puzzle:");
showPuzzle();
System.out.println();
System.out.println(player.getName() + ", please guess a letter.");
//String guess = sc.next();
char letter = sc.next().charAt(0);
if (!Character.isAlphabetic(letter)) {
System.out.println("Sorry, but only alphabetic characters are allowed. You lose your turn.");
} else {
// search for letter to see if it is in
int numFound = 0;
for (Letter l : Letters) {
if ((l.getLetter() == letter) || (l.getLetter() == Character.toUpperCase(letter))) {
l.setFound();
numFound += 1;
}
}
if (numFound == 0) {
System.out.println("Sorry, but there are no " + letter + "'s.");
} else {
if (numFound == 1) {
System.out.println("Congrats! There is 1 letter " + letter + ":");
} else {
System.out.println("Congrats! There are " + numFound + " letter " + letter + "'s:");
}
System.out.println();
showPuzzle();
System.out.println();
player.incrementScore(numFound * money);
System.out.println("You earned $" + (numFound * money) + ", and you now have: $" + player.getWinnings());
System.out.println("Would you like to try to solve the puzzle? (Y/N)");
letter = sc.next().charAt(0);
System.out.println();
if ((letter == 'Y') || (letter == 'y')) {
solvePuzzleAttempt(player);
}
}
}
}
/**
* Logic for when user tries to solve the puzzle
*
* @param player
*/
private void solvePuzzleAttempt(Player player) {
if (player.getNumGuesses() >= 3) {
System.out.println("Sorry, but you have used up all your guesses.");
return;
}
player.incrementNumGuesses();
System.out.println("What is your solution?");
Scanner sc = new Scanner(System.in);
sc.useDelimiter("\n");
String guess = sc.next();
if (guess.compareToIgnoreCase(phrase) == 0) {
System.out.println("Congratulations! You guessed it!");
System.out.println("Now pass it to the next person, they get one chance to guess the phrase!");
puzzleSolved = true;
// Round is over.
System.out.println("The player who won is " +players.getName());
System.out.println("Everyone's earnings are ");
for (Player earnings : Players){
System.out.println(earnings.getName());
System.out.println("$" +earnings.getWinnings());
}
// TODO
} else {
System.out.println("Sorry, but that is not correct.");
}
}
/**
* Display the puzzle on the console
*/
private void showPuzzle() {
System.out.print("\t\t");
for (Letter l : Letters) {
if (l.isSpace()) {
System.out.print(" ");
} else if (l.isFound()) {
System.out.print(Character.toUpperCase(l.getLetter()) + " ");
} else {
System.out.print(" _ ");
}
}
System.out.println();
}
/**
* For a new game reset player's number of guesses to 0
*/
public void reset() {
players.reset();
}
}