-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlay.java
More file actions
176 lines (151 loc) · 7.16 KB
/
Play.java
File metadata and controls
176 lines (151 loc) · 7.16 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
import java.io.*;
import java.util.*;
class Play
{
// if showCode is true then the secret code is revealed
// before the game starts. This will be used by graders
// of the program, to test the feedback of guesses.
private boolean showCode;
private DifficultyLevel diffLevel;
private int numGuesses;
private int numPositions;
private int numColors;
public Play(boolean sc, DifficultyLevel dl)
{
showCode = sc;
diffLevel = dl;
}
public void printInstructions() throws Exception
{
String colors;
switch (diffLevel.getLevel())
{
case 'R':
numPositions = 4;
numColors = 6;
numGuesses = 12;
colors = "Violet, Indigo, Blue, Green, Yellow and Orange";
break;
case 'P':
numPositions = 4;
numColors = 7;
numGuesses = 10;
colors = "Violet, Indigo, Blue, Green, Yellow, Orange and Red";
break;
case 'E':
numPositions = 5;
numColors = 9;
numGuesses = 12;
colors = "Violet, Indigo, Blue, Green, Yellow, Orange, Red, Pink and Cyan";
break;
case 'G':
numPositions = 5;
numColors = 10;
numGuesses = 10;
colors = "Violet, Indigo, Blue, Green, Yellow, Orange, Red, Pink, Cyan and Maroon";
break;
default: // This should not happen. But if it happens, treat the unknown level as "Rookie" level.
numPositions = 4;
numColors = 6;
numGuesses = 12;
colors = "Violet, Blue, Green, Yellow, Orange and Red";
break;
}
System.out.println("\t\t\tWelcome to BRAINZILIA!\n\n\n\tWhere the brain is truely mightier than the brawn!\n");
System.out.println("This is a text version of the classic board game Mastermind.\n" +
"The computer will think of a secret code.\n" +
"Difficulty level is: " + diffLevel.getLevel() + "\n" +
"The code consists of " + numPositions + " colored pegs.\n" +
"The pegs may be one of " + numColors + " colors: " + colors + "\n" +
"You try to guess what colored pegs are in the code and what order they are in.\n" +
"After making a guess the feedback will be displayed.\n" +
"A feedback consists of a black peg for each peg you have exactly correct (color and position) in your guess.\n" +
"For each peg in the guess that is the correct color, but is out of position, you get a white peg.\n\n" +
"Only the first letter of the color is displayed. B for Blue, R for Red, and so forth.\n" +
"When entering guesses you only need to enter the first character of the color as a capital letter.\n"
);
}
public void runGames() throws Exception
{
printInstructions(); //prints the instructions for the game
System.out.println("You have " + numGuesses + " guesses to guess the correct answer.\n");
boolean anotherGame = true; //checks if another game will be played
while(anotherGame)
{
System.out.println("Generating secret code ....");
GameBoard gameBoard = new GameBoard(numGuesses); //creates the gameBoard object for the game
gameBoard.addPositions(); //adds the correct number of positions for the gameBoard object
Feedback feedback = new Feedback(); //the feedback object that will generate the feedback
Code compCode = new Code(numPositions, numColors); //the secret code to be used throughout the game
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
boolean canContinue = true; //checks if the game can continue playing
int n = gameBoard.getNumGuesses(); //resets the number of guesses back
while(canContinue)
{
//shows the secret code if showCode is true
if(showCode)
{
System.out.println("The secret code is: " + compCode);
}
System.out.println("You have " + n + " guesses left.\n");
boolean badCode = true; //checks if the user puts in a bad code
Code userCode = new Code(numPositions, numColors); //creates temporary userCode object
//runs while the user puts in a bad code
while(badCode)
{
badCode = false;
System.out.println("What is your next guess?");
System.out.println("Type in the characters for your guess and press enter.");
System.out.print("Enter guess: ");
String guess = br.readLine(); //gets the code from the user
try
{
userCode = new Code(numPositions, numColors, guess); //creates user code with correct input
}
catch(IllegalArgumentException e)
{
badCode = true; //sets badCode to true if the user's code was bad
System.out.println(e.getMessage());
}
}
boolean winner = feedback.getFeedback(compCode, userCode);
//boolean that stores if the user won; also gets the results for the round
//shows secret code is showCode is true
if(showCode)
{
System.out.println("The secret code is: " + compCode + "\n");
}
gameBoard.changeRow(userCode, feedback); //changes the row of the gameBoard
//if boolean winner is false, this will print out the gameBoard that winners see
//else it will print out the default gameBoard
if(winner)
{
System.out.println(gameBoard.winnerToString());
}
else
{
System.out.println(gameBoard);
}
n--;
if(n == 0 || winner)
{
canContinue = false;
if(winner)
{
System.out.println("You solved the puzzle! Good job.");
}
else //it has to be (n == 0)
{
System.out.println("You did not solve the puzzle. Too bad.");
}
System.out.print("Enter Y for another game or anything else to quit: ");
String input = br.readLine();
if(!input.equals("Y"))
{
anotherGame = false;
}
}
}
}
}
}