-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPins.java
More file actions
118 lines (97 loc) · 3.91 KB
/
Pins.java
File metadata and controls
118 lines (97 loc) · 3.91 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
package pins;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import javafx.scene.paint.Color;
public class Pins {
static private int numPins = 4;
static private String pinColours[] = {"Red", "Blue", "Yellow", "Green"};
static public HashMap<String, Color> colourOfPins = new HashMap<>();
static{
colourOfPins.put ("Red", Color.RED);
colourOfPins.put ("Blue", Color.BLUE);
colourOfPins.put ("Yellow", Color.YELLOW);
colourOfPins.put ("Green", Color.GREEN);
}
String[] pins;
public Pins(){
this.pins = new String[numPins];
for (int i = 0; i < numPins; i++){
pins[i] = "";
}
}
public static int randInt (int min, int max){
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static Pins generatePins (){
Pins pin = new Pins();
for (int i = 0; i < numPins; i++){
pin.pins[i] = pinColours[randInt(0,numPins-1)];
}
return pin;
}
public static int getNumPins(){
return numPins;
}
public static String[] getPinColours(){
return pinColours;
}
/*public static int[] evaluatePins(Pins guessedPins, Pins generatedPins){
//Initializes vars for the number of pins that are correct, are the right colour, and are wrong
int numRight = 0, numRightColour = 0, numWrong = 0;
//Initializes vars to count the number of each colour of pin that is not correct
HashMap<String, Integer> numColourGuessed = new HashMap<>();
HashMap<String, Integer> numColourGenerated = new HashMap<>();
for (int i = 0; i < Pins.getNumPins(); i++){
numColourGuessed.put(Pins.getPinColours()[i], 0);
numColourGenerated.put(Pins.getPinColours()[i], 0);
}
//Counts how many are correct & the number of each colour that are incorrect
for (int i = 0; i < Pins.getNumPins(); i++){
if (guessedPins.pins[i] == generatedPins.pins[i]) numRight++;
else {
numColourGuessed.put(guessedPins.pins[i], numColourGuessed.get(guessedPins.pins[i]) + 1);
numColourGenerated.put(generatedPins.pins[i], numColourGenerated.get(generatedPins.pins[i]) + 1);
}
}
//Counts the number that are the right colour but wrong place
for( String k: numColourGuessed.keySet() ){
numRightColour = numRightColour + Math.min(numColourGuessed.get(k), numColourGenerated.get(k));
}
//FInds the number that are wrong
numWrong = Pins.getNumPins() - numRight - numRightColour;
//Returns an array of three numbers (completely right, right colour, and completely wrong)
int[] a = {numRight, numRightColour, numWrong};
return a;
}*/
public int[] evaluatePins(Pins generatedPins){
//Initializes vars for the number of pins that are correct, are the right colour, and are wrong
int numRight = 0, numRightColour = 0, numWrong = 0;
//Initializes vars to count the number of each colour of pin that is not correct
HashMap<String, Integer> numColourGuessed = new HashMap<>();
HashMap<String, Integer> numColourGenerated = new HashMap<>();
for (int i = 0; i < Pins.getNumPins(); i++){
numColourGuessed.put(Pins.getPinColours()[i], 0);
numColourGenerated.put(Pins.getPinColours()[i], 0);
}
//Counts how many are correct & the number of each colour that are incorrect
for (int i = 0; i < Pins.getNumPins(); i++){
if (this.pins[i] == generatedPins.pins[i]) numRight++;
else {
numColourGuessed.put(this.pins[i], numColourGuessed.get(this.pins[i]) + 1);
numColourGenerated.put(generatedPins.pins[i], numColourGenerated.get(generatedPins.pins[i]) + 1);
}
}
//Counts the number that are the right colour but wrong place
for( String k: numColourGuessed.keySet() ){
numRightColour = numRightColour + Math.min(numColourGuessed.get(k), numColourGenerated.get(k));
}
//FInds the number that are wrong
numWrong = Pins.getNumPins() - numRight - numRightColour;
//Returns an array of three numbers (completely right, right colour, and completely wrong)
int[] a = {numRight, numRightColour, numWrong};
return a;
}
}