-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMastermind.java
More file actions
117 lines (84 loc) · 3.26 KB
/
Mastermind.java
File metadata and controls
117 lines (84 loc) · 3.26 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// Executable for Mastermind, game setup and initialization
// A mastermind Algorithm for variable # positions and variable # code tokens
public class Mastermind {
/**********************************************************************************************************/
/*******************************************MAIN / RUN THE GAME********************************************/
/**********************************************************************************************************/
public static void main(String[] args) throws IOException {
BufferedReader buffy = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Welcome to Mastermind! Enter the tokens you would like to use, separated by spaces:");
String[] derp = buffy.readLine().split("\\s+");
Token[] tokens = new Token[derp.length];
// generate the tokens to be used
for (int i=0; i<derp.length; i++) {
tokens[i] = new Token(derp[i]);
}
System.out.println("How many positions?");
int np = Integer.parseInt(buffy.readLine()); // read returns an wrong int, so use readLine and parse to cast
/**************************THE GAME LOOP****************************/
String response;
do {
System.out.println("Starting new game...");
MastermindGame game = new MastermindGame(tokens, np);
int blackPegs = -1;
int whitePegs = -1;
Token[] code = new Token[np];
do {
// set code = to the guess. the guess was made based on the last setting of black and white pegs
code = game.playGuess(blackPegs,whitePegs);
System.out.println("allPossibleCombos:");
print2DTokenArray(game.allPossibleCombos);
System.out.println("remainingCombos:");
print2DTokenArray(game.remainingCombos);
System.out.print("Guessing: ");
printTokenArray(code);
// get black and white peg counts
System.out.println("How many black pegs?");
blackPegs = Integer.parseInt(buffy.readLine());
if (blackPegs == np) {
break;
}
System.out.println("How many white pegs?");
whitePegs = Integer.parseInt(buffy.readLine());
} while (true);
System.out.print("Got it! The code is: ");
printTokenArray(code);
System.out.println("Play again? y/n");
response = buffy.readLine();
} while (response.equals("y"));
System.out.println("gg");
buffy.close();
}
/*********************************************************************************************/
/*******************************************GENERAL PURPOSE***********************************/
/*********************************************************************************************/
static void print2DTokenArray(Token[][] a) {
for (int i=0; i<a.length; i++) {
printTokenArray(a[i]);
}
}
static void printTokenArray(Token[] t) {
if (t == null) {
return;
}
for (int i=0; i<t.length; i++) {
System.out.print(t[i] + "\t");
}
System.out.println();
}
// compare two Token[]s
static boolean tokenArrayEquals(Token[] one, Token[] two) {
if (one.length != two.length) {
return false;
}
for (int i=0; i<one.length; i++) {
if (!one[i].equals(two[i])) {
return false;
}
}
return true;
}
}