-
Notifications
You must be signed in to change notification settings - Fork 0
/
Puzzle.java
153 lines (140 loc) · 3.96 KB
/
Puzzle.java
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
import java.util.Random;
import java.util.Scanner;
public class Puzzle {
private Node[] correctNodes = new Node[6];
private Node[] resetNodes;
private String[] colorOptions = {"Red", "Orange", "Yellow", "Blue", "Green", "Purple"};
private Node[] guessNodes = new Node[6];
private int[] correctTally = new int[6];
private Scanner scan = new Scanner(System.in);
/*
* randomly selects 6 of the colors from the options of color
* to be the correct answer in the set order
*
* makes resetNodes, a copy of correctNodes that is reused every time correctNodes
* is altered by other functions
*/
public Puzzle() {
Random r = new Random();
for (int i = 0; i < correctNodes.length; i++) {
String color = colorOptions[r.nextInt(colorOptions.length)];
correctNodes[i] = new Node(color);
}
resetNodes = correctNodes;
}
/*
* @param n
* the node we want to see if present in correctNodes
*
* checks through correct nodes to determine if a certain node color is within the solution.
* Makes sure the node doesn't make a false positive for a node that already has a correct node
*
* @return index of node with same color
*/
private int contains(Node n) {
for (int i = 0; i < correctNodes.length; i++) {
if (correctNodes[i].equals(n) && !correctNodes[i].equals(guessNodes[i])) {
Node e = new Node("Empty");
correctNodes[i] = e;
return i;
}
}
return -1;
}
/*
* @param n
* the node we want to compare to the correct array
*
* @param index
* the index of the node so we can compare it to its corresponding node in correct array
*/
private boolean equals(Node n, int index) {
if (correctNodes[index].equals(n)) {
Node e = new Node("Empty");
correctNodes[index] = e;
return true;
}
return false;
}
/*
*
* asks for six colors, then compares to see if any are in right spot or if the color is present in code
*
* depending on the answer the tally array keeps track of correct colors and locations, correct colors but wrong locations,
* and all other cases
*
* then uses print function to display these amounts
*/
public boolean playRound() {
int count = 0;
System.out.println("Pick 6 colors: Red, Orange, Yellow, Green, Blue, or Purple.\n");
while (count < 6) {
System.out.print("Pick a color (" + (count+1) + "/6): ");
String color = scan.next();
boolean valid = false;
for (String c: colorOptions) {
if (c.substring(0,1).equals(color.substring(0,1))) {
valid = true;
Node n = new Node(c);
guessNodes[count] = n;
break;
}
}
if (valid) {
count++;
}
else {
System.out.println("Invalid color.");
}
}
return checkGuess();
}
private boolean checkGuess() {
int correctCount = 0;
for (int i = 0; i < correctNodes.length; i++) {
if (equals(guessNodes[i], i)) {
correctTally[i] = 2;
correctCount++;
}
else {
int index = contains(guessNodes[i]);
if (index >= 0) {
correctTally[i] = 1;
}
else {
correctTally[i] = 0;
}
}
}
correctNodes = resetNodes;
System.out.println(this);
if (correctCount == correctNodes.length) {
return true;
}
return false;
}
/*
* loops through the tally array counting correct spots and colors (2), correct colors but wrong spots (1), or incorrect (-1)
*/
public String toString() {
String response = "";
response += "Previous attempt: \n";
for (Node n: guessNodes) {
response += n.getColor() + " ";
}
response += "\n";
int correct = 0;
int wrongSpot = 0;
for (int i = 0; i < correctTally.length; i++) {
if (correctTally[i] == 2) {
correct++;
}
if (correctTally[i] == 1) {
wrongSpot++;
}
}
response += correct + " pegs in the right spot.\n";
response += wrongSpot + " pegs in the puzzle, but in incorrect spot.\n";
return response;
}
}