-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
132 lines (107 loc) · 3.15 KB
/
Driver.java
File metadata and controls
132 lines (107 loc) · 3.15 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
/**
* Driver.java - The driver for the console version of the mastermind game
* @author Aidan St. George
* @version 1.2.1 - 12/19/20
*/
package application;
import java.util.Scanner;
public class Driver {
/**
* The Main method for this class. Sets up the main interactions with the user.
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Mastermind!");
boolean inGame = true;
do {
// Number of Players
int players = 0;
do {
System.out.print("Enter number of players (1-2): ");
try {
players = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Must be an integer!");
}
} while (players != 1 && players != 2);
// Set Pattern
Game game = null;
if (players == 1) {
game = new Game();
} else {
do {
try {
char[] pattern = getUserPattern(scanner);
game = new Game(pattern);
} catch (Exception e) {
System.out.println(e.getMessage());
}
} while (game == null);
}
boolean win = false;
// Guessing
while (!win && game.getCurrentGuessNumber() < 10) {
System.out.print("Guess #" + (game.getCurrentGuessNumber() + 1) + ", ");
boolean validInput = false;
do {
try {
win = game.checkGuess(getUserPattern(scanner));
validInput = true;
} catch (Exception e) {
System.out.println(e.getMessage());
}
} while (!validInput);
Guess guess = game.getCurrentGuess();
System.out.println("\t\t\t\tBlack: " + guess.getBlack() + ", White: " + guess.getWhite());
}
System.out.println("Win: " + win + ", Score: " + (11 - game.getCurrentGuessNumber()) + "\n");
// Check for restart
int action = -1;
do {
System.out.print("Enter 0 to quit, 1 to restart: ");
try {
action = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Must be an integer!");
}
} while (action != 0 && action != 1);
if (action == 0) {
inGame = false;
}
} while (inGame);
scanner.close();
}
/**
* Helper method that gets the users input for a pattern. Checks the validity of the pattern and if necessary asks for another pattern.
* @param scanner the scanner that should be used, just to avoid having multiple scanners
* @return the input pattern as an array of characters
*/
public static char[] getUserPattern(Scanner scanner) {
boolean validPattern = false;
char[] pattern;
do {
System.out.print("Enter a pattern: ");
String input = scanner.nextLine();
pattern = new char[4];
int colorNumber = 0;
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) != ' ') {
try {
pattern[colorNumber] = Character.toLowerCase(input.charAt(i));
} catch (Exception e) {
colorNumber++; // So validPattern is not set true
break;
}
colorNumber++;
}
}
if (colorNumber == 4) {
validPattern = true;
} else {
System.out.println("Pattern must be four colors long!");
}
} while (!validPattern);
return pattern;
}
}