-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathWheelOfFortune.java
More file actions
209 lines (157 loc) · 4.88 KB
/
Copy pathWheelOfFortune.java
File metadata and controls
209 lines (157 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import java.util.*;
public class WheelOfFortune
{
//Creates a new board
private static GameBoard fortuneBoard = new GameBoard("ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"The Secret Life of Bees", "Movies");
public static void displayBoard()
{
//Display available letters
fortuneBoard.displayLetters();
//Update puzzle
fortuneBoard.updatePuzzle();
//Display updated puzzle
fortuneBoard.displayPuzzle();
}
public static void play (Player player)
{
Wheel fortuneWheel = new Wheel();
int playerChoice=-1;
boolean goAgain = true, puzzleSolved=false;
Scanner keyboard;
/*This while executes under the condition that it is still the player's turn AND
*the puzzle is not yet solved. It will not execute if either the players turn has ended OR
*the puzzle was solved during the players turn
*/
while (goAgain==true && puzzleSolved==false)
{
boolean wasGuessed=false;
/*This do-while loop prompts the player to spin (1) or guess(2) and continues to
* loop while the player's input is not either 1 or 2
*/
do
{
keyboard = new Scanner(System.in);
/*this try-catch block handles the InputMismatchException thrown by Scanner's
* nextInt() method
*/
try
{
//Prompts player to spin (1) or guess (2)
System.out.print("Player " + player.getPlayerNumber() + " - would you like to Spin (1)"
+ " or Guess (2) the puzzle? ");
//Assigns player's input to variable
playerChoice = keyboard.nextInt();
}
catch (InputMismatchException e)
{
//Assigns -1 to variable to prompt another loop iteration
playerChoice=-1;
}
}while(playerChoice != 1 && playerChoice!=2);
if(playerChoice == 1)
{
//Spins the wheel
fortuneWheel.spin();
/*This if-else block executes when the player lands on a wheel value
*greater than 0.0
*/
if (fortuneWheel.getWheelValue()>0.0)
{
/*This do-while loop will execute as long as the player is guessing
* a letter that has already been guessed
*/
do
{
//Prompts player to select a letter
System.out.print("Select your letter from the available letters from above: ");
//Gets player guess
player.setPlayerGuess(keyboard.next().charAt(0));
//Checks that the letter has not been guessed
wasGuessed = fortuneBoard.isLetterGuessed(player.getPlayerGuess());
}while (wasGuessed);
/* Play the current letter on the board.
* The validity of the letter has not been checked at this point
* Code has only checked that the letter is not a duplicate guess at this point
*/
fortuneBoard.setCharacter(player.getPlayerGuess());
System.out.println();
//Removes the current letter on the board from the list of available letters
fortuneBoard.updateAvailableLetters();
/* Now checking if current letter is in the puzzle
* If the player guesses a letter that is in the puzzle, player can go again
* else the player's turn terminates
*/
if (fortuneBoard.isLetterInPuzzle(player.getPlayerGuess())==true)
displayBoard();
else {
goAgain=false;
System.out.println("Incorrect!");
System.out.println();
}
}
else
{
//If wheel value is less than 0, turn terminates
goAgain = false;
}
}
else
{
System.out.print("Please Enter Your Guess: ");
keyboard = new Scanner(System.in);
String playerGuess = keyboard.next().trim();
if (!playerGuess.equalsIgnoreCase(fortuneBoard.getPuzzle()))
{
System.out.println("\nIncorrect!\n");
goAgain = false;
}
else {
fortuneBoard.setPendingPuzzle(playerGuess);
}
}
/*Checks whether the puzzle has been solved
*Prints congratulatory message if puzzle is solved and terminates turn
*/
if(checkSolved()==true)
{
goAgain=false;
System.out.println("Congratulations! You Solved the Puzzle!\n"+
"Player " + player.getPlayerNumber()+ " Wins!");
}
}
}//End Play
public static boolean checkSolved()
{
boolean solved;
if (fortuneBoard.getPendingPuzzle().equalsIgnoreCase(fortuneBoard.getPuzzle())) {
solved = true;
}
else
solved = false;
return solved;
}
public static void main (String [] args)
{
//Prints welcome message
System.out.println("Welcome to the Wheel of Fortune\n");
//Creates 3 new players
Player player1 = new Player(1);
Player player2 = new Player(2);
Player player3 = new Player(3);
do {
displayBoard();
play(player1);
if (checkSolved()==true)
break;
displayBoard();
play(player2);
if (checkSolved()==true)
break;
displayBoard();
play(player3);
if (checkSolved()==true)
break;
}while(checkSolved()==false);
}
}