-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPSWorld.java
executable file
·248 lines (213 loc) · 7.02 KB
/
RPSWorld.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import info.gridworld.actor.Actor;
import info.gridworld.actor.ActorWorld;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.HashMap;
public class RPSWorld extends ActorWorld
{
private static int day = 0;
private static boolean complete = false;
private static int[] populationRecord = new int[90];
private static HashMap<Class, ArrayList<RPSMatch>> matchRecord = new HashMap<Class, ArrayList<RPSMatch>>();
// constructors
public RPSWorld()
{
}
public RPSWorld(Grid<Actor> grid)
{
super(grid);
setMessage("Welcome to RPSWorld");
}
/**
* Getter method for the current day. Maybe you want to change
* your strategy as time passes.
*
* @return The current day in this RPSWorld
*/
public static int getDay()
{
return day;
}
/**
* This method can be called by an RPSCritter to get information
* about how a particular class of opponent has played in the past
*
* @param c - The class whose match record you would like
*
* @return An ArrayList containing RPSMatch objects that represent
* every match that an RPSCritter of class c has been part of
* during the entire history of this RPSWorld
*/
public static ArrayList<RPSMatch> getMatchRecord(Class c)
{
return matchRecord.get(c);
}
// boring implementation details
public void step()
{
if(complete) return;
System.out.println("\n> > > RPSWORLD -- Day " + day +" < < <");
setMessage("RPSWorld -- Day " + day);
// find all the RPSCritters
Grid<Actor> gr = getGrid();
ArrayList<RPSCritter> fighters = new ArrayList<RPSCritter>();
TreeSet<String> fighterClasses = new TreeSet<String>();
for (Location loc : gr.getOccupiedLocations())
{
if (gr.get(loc) instanceof RPSCritter)
{
RPSCritter r = (RPSCritter)gr.get(loc);
fighters.add(r);
fighterClasses.add(r.getClass().toString());
System.out.println(r.yourName() + " at " + r.getLocation());
}
}
setMessage(getMessage() + "\n" + fighterClasses.size() + " classes, " + fighters.size() + " individuals, ");
// check for winner
if(fighterClasses.size() == 1)
{
System.out.print("\tWe have a champion: "+ fighters.get(0).yourName());
System.out.println(" ("+fighters.get(0).getClass() +")");
complete = true;
return;
}
// record the current population
populationRecord[day%populationRecord.length] = fighters.size();
//System.out.println("RPSWorld population = " + Arrays.toString(populationRecord));
// if the world has had the same population for too long, add some new blood
boolean allSame = true;
for(int f=0; f<populationRecord.length-1; f++)
{
if(populationRecord[f] != populationRecord[f+1])
{
allSame = false;
break;
}
}
if(allSame)
{
// I wish there was a way to call World.frame.control.stop() here
/*
System.out.println("*** STAGNANT POPULATION -- ADDING MORE CRITTERS ***");
ArrayList<RPSCritter> newborns = new ArrayList<RPSCritter>();
RPSRunner.addOneOfEach(newborns);
addRandomly(newborns);
*/
System.out.println("*** STAGNANT POPULATION -- TIME FOR A SWEEP ***");
addSweep();
show();
}
int numFights = 0;
// neighbors always fight
for (RPSCritter r : fighters)
{
// maybe r was defeated by a previous RPSCritter
if (r.getGrid() == gr)
{
ArrayList<Location> battleGrounds = gr.getOccupiedAdjacentLocations(r.getLocation());
for(Location hotSpot : battleGrounds)
{
// maybe r lost to a previous neighbor
if (r.getGrid() != gr) break;
// if r is still alive, check for a fight
if(gr.get(hotSpot) instanceof RPSCritter)
{
playMatch(r, (RPSCritter)gr.get(hotSpot));
numFights++;
}
}
}
}
setMessage(getMessage() + numFights + " matches played today");
// then everyone gets to act
super.step();
day++;
}
public void addRandomly(ArrayList<RPSCritter> contestants)
{
Grid<Actor> gr = getGrid();
while(contestants.size() > 0)
{
int randIndex = (int)(Math.random()*contestants.size());
RPSCritter randCrit = contestants.get(randIndex);
Location randLoc = getRandomEmptyLocation();
while(gr.getEmptyAdjacentLocations(randLoc).size()<8)
{
randLoc = getRandomEmptyLocation();
}
add(randLoc, randCrit);
contestants.remove(randIndex);
}
}
private void addSweep()
{
Grid<Actor> gr = getGrid();
int s = -1;
double r = Math.random();
if (r<0.3333333)
s = RPSMatch.ROCK;
else if (r<0.666666)
s = RPSMatch.PAPER;
else
s = RPSMatch.SCISSORS;
int sweepDir = 90 * (int)(Math.random()*4);
int startSide = (int)(Math.random()*2);
if(sweepDir % 180 == 0) // moving up or down
{
for(int i=0; i<gr.getNumCols(); i++)
{
Sweeper x = new Sweeper(sweepDir, s);
Location loc = new Location(startSide*(gr.getNumRows()-1),i);
System.out.println("SWEEPER AT " + loc);
if(gr.get(loc) == null)
x.putSelfInGrid(gr, loc);
}
}
else // moving left or right
{
for(int i=0; i<gr.getNumRows(); i++)
{
Sweeper x = new Sweeper(sweepDir, s);
Location loc = new Location(i,startSide*(gr.getNumRows()-1));
System.out.println("SWEEPER AT " + loc);
if(gr.get(loc) == null)
x.putSelfInGrid(gr, loc);
}
}
}
public static void playMatch(RPSCritter one, RPSCritter two)
{
String oneId = one.yourName() + " (" + one.getClass() + ")";
String twoId = two.yourName() + " (" + two.getClass() + ")";
System.out.println("\n\tMatch between " + oneId + " and " + twoId);
RPSMatch m = new RPSMatch(one, two);
if (m.isDraw())
System.out.println("\t> draw, both shall live");
else if(m.getWinner() == one)
declareVictory(one, two);
else
declareVictory(two, one);
enterMatchInRecord(one, m);
enterMatchInRecord(two, m);
}
private static void enterMatchInRecord(RPSCritter c, RPSMatch m)
{
// lazy init for matchRecord ArrayLists
if(!matchRecord.containsKey(c.getClass()))
{
matchRecord.put(c.getClass(), new ArrayList<RPSMatch>() );
}
// put the match into the class arraylist
matchRecord.get(c.getClass()).add(m);
}
private static void declareVictory(RPSCritter winner, RPSCritter loser)
{
System.out.println("\t> " + winner.yourName() + " triumphs!");
System.out.println("\t> " + loser.yourName() + " shall perish");
if(loser.getGrid() != null)
loser.removeSelfFromGrid();
}
}