-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpectedBrickValue.java
More file actions
182 lines (170 loc) · 4.78 KB
/
Copy pathExpectedBrickValue.java
File metadata and controls
182 lines (170 loc) · 4.78 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
177
178
179
180
181
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class ExpectedBrickValue {
public static void main(String args[]) {
if(args.length < 2) {
System.out.println("usage: java ExpectedBrickValue <decklist> <mulligan specifications>");
}
String decklist = new String(args[0]).trim();
String specs = new String(args[1]).trim();
ArrayList<String> deck = new ArrayList<String>();
ArrayList<Condition> conditions = new ArrayList<Condition>();
//Read decklist from file
//Format is CardName,Quantity
try {
FileReader fr = new FileReader(decklist);
BufferedReader br = new BufferedReader(fr);
String line = null;
while((line = br.readLine()) != null) {
// Split
String[] row = (new String(line)).trim().split(",");
String card = row[0];
int number = Integer.parseInt(row[1]);
for(int i = 0; i < number; i++) {
deck.add(card);
}
}
br.close();
}
catch(FileNotFoundException e) {
System.out.println("Unable to open file: " + decklist);
}
catch(IOException e) {
System.out.println("Error reading file: " + decklist);
}
try {
FileReader fr = new FileReader(specs);
BufferedReader br = new BufferedReader(fr);
String line = null;
while((line = br.readLine()) != null) {
// Split
String[] row = (new String(line)).trim().split(":");
String r = row[0];
String k = row[1];
String[] reqs = r.split(",");
String[] keep = k.split(",");
conditions.add(new Condition(reqs, keep));
}
br.close();
}
catch(FileNotFoundException e) {
System.out.println("Unable to open file: " + specs);
}
catch(IOException e) {
System.out.println("Error reading file: " + specs);
}
double sum = 0.0;
int runs = 100000;
for(int k = 0; k < runs; k++) {
sum += getEBV(deck, conditions);
}
System.out.println("EBV: " + (double)(sum / runs));
}
public static double getEBV(ArrayList<String> deck, ArrayList<Condition> conditions) {
ArrayList<String> shuffled = shuffle(deck); //Shuffle the deck
ArrayList<String> hand = new ArrayList<String>();
ArrayList<String> discard = new ArrayList<String>();
boolean[] keepCard = new boolean[3];
boolean first = false;
int coin = (int)(Math.random() * 2);
if(coin == 0) {
first = true;
}
//Draw initial hand of 3
while(hand.size() < 3) {
hand.add(shuffled.remove(0));
}
if(first) {
hand.add("FIRST");
}
else {
hand.add("SECOND");
}
//Figure out which cards to keep
for(int j = 0; j < hand.size() - 1; j++) {
keepCard[j] = shouldKeep(hand, hand.get(j), conditions);
}
//Mulligan selected cards and move mulliganed cards into discard since they are no longer relevant
for(int k = 2; k >= 0; k--) {
if(!keepCard[k]) {
discard.add(hand.remove(k));
}
}
//Draw cards from shuffled deck until hand has 3 cards
while(hand.size() < 4) {
hand.add(0, shuffled.remove(0));
}
//Add mulligan'd cards back to deck and reshuffle
while(!discard.isEmpty()) {
shuffled.add(discard.remove(0));
}
shuffled = shuffle(shuffled);
hand.add(0, shuffled.remove(0));
if(!first) {
hand.add(0, shuffled.remove(0));
}
//Score the hand!
//+1 point if you would keep the card, +0 otherwise
double EBV = 0.0;
for(int h = 0; h < hand.size() - 1; h++) {
if(shouldKeep(hand, hand.get(h), conditions)) {
EBV++;
}
}
return EBV;
}
public static boolean shouldKeep(ArrayList<String> hand, String card, ArrayList<Condition> conditions) {
for(int i = 0; i < conditions.size(); i++) {
Condition c = conditions.get(i);
if(contains(c.getReqs(), "KEEP") && contains(c.getKeep(), card)) {
return true;
}
boolean met = true;
if(contains(c.getKeep(), card)) {
for(int t = 0; t < c.getReqs().length; t++) {
if(!hand.contains(c.getReqs()[t])) {
met = false;
}
}
if(met) {
return met;
}
}
}
return false;
}
public static ArrayList<String> shuffle(ArrayList<String> deck) {
ArrayList<String> shuffled = new ArrayList<String>();
ArrayList<String> deckCopy = new ArrayList<String>(deck);
while(!deckCopy.isEmpty()) {
int index = (int)(Math.random() * deckCopy.size());
shuffled.add(deckCopy.remove(index));
}
return shuffled;
}
public static boolean contains(String[] list, String search) {
for(int i = 0; i < list.length; i++) {
if(list[i].equals(search)) {
return true;
}
}
return false;
}
}
class Condition {
String[] reqs;
String[] keep;
public Condition(String[] r, String[] k) {
reqs = r;
keep = k;
}
public String[] getReqs() {
return reqs;
}
public String[] getKeep() {
return keep;
}
}