forked from PhiNotPi/NanoCoreWar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
251 lines (231 loc) · 7.75 KB
/
Game.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
249
250
251
import java.util.Random;
import java.util.ArrayList;
import java.util.Arrays;
import java.lang.IllegalStateException;
/**
* This runs a game of Core Wars between two players. It can be called mutiple times.
*
* @author PhiNotPi, Ilmari Karonen
* @version 3/17/15
*/
public class Game
{
final Player p1;
final Player p2;
final int coreSize;
final int coreSizeM1;
final int maxTime;
final int debug;
int offset1;
int offset2;
Random rand;
ArrayList<Instruction> p1code;
ArrayList<Instruction> p2code;
int p1size;
int p2size;
public Game(Player A, Player B, int coreSize, int maxTime, int debug)
{
p1 = A;
p2 = B;
coreSize--;
coreSize |= coreSize >> 1;
coreSize |= coreSize >> 2;
coreSize |= coreSize >> 4;
coreSize |= coreSize >> 8;
coreSize |= coreSize >> 16;
coreSize++;
this.coreSize = coreSize;
this.coreSizeM1 = coreSize - 1;
this.maxTime = maxTime / 2;
this.debug = debug;
rand = new Random();
p1code = p1.getCode();
p1size = p1code.size();
p2code = p2.getCode();
p2size = p2code.size();
}
public int runAll()
{
int sum = 0;
for(int i = 0; i < coreSize - p1size - p2size; i++)
{
sum += run(i) - 1;
}
return sum;
}
public int run()
{
return run(rand.nextInt(coreSize - p1size - p2size + 1));
}
public int run(int deltaOffset)
{
if(debug != 0)
{
System.out.println("New game between " + p1.getName() + " and " + p2.getName() + " with offset " + deltaOffset + ":");
}
int coreSize = this.coreSize, coreSizeM1 = coreSize-1;
Instruction[] core = new Instruction[coreSize];
for(int i = 0; i < coreSize; i++)
{
core[i] = Instruction.DAT00;
}
int offset1 = 0;
for(int i = 0; i != p1size; i++)
{
int loc = (offset1 + i) & coreSizeM1;
core[loc] = p1code.get(i);
}
int offset2 = offset1 + p1size + deltaOffset;
for(int i = 0; i != p2size; i++)
{
int loc = (offset2 + i) & coreSizeM1;
core[loc] = p2code.get(i);
}
int poffset = offset1 & coreSizeM1, ploc = poffset;
int xoffset = offset2 & coreSizeM1, xloc = xoffset;
int maxSteps = maxTime * 2;
for(int step = 0; step != maxSteps; step++)
{
if(debug != 0)
{
printCore(core, ploc, xloc, step);
}
Instruction curr = core[ploc];
int op = curr.packedOp;
if(op < Instruction.minValidOp)
{
return ((step & 1) == 0 ? 0 : 2);
}
int line1 = curr.field1, line2 = curr.field2;
int opcode = op >> (Instruction.modeBits*2), mode1 = (op >> Instruction.modeBits), mode2 = op;
mode1 &= Instruction.modeMask;
mode2 &= Instruction.modeMask;
switch(mode1)
{
case Instruction.MODE_IMM:
line1 += poffset;
break;
case Instruction.MODE_DIR:
line1 += ploc;
break;
case Instruction.MODE_IND:
line1 += ploc;
line1 += core[line1 & coreSizeM1].field1;
break;
default:
throw new IllegalStateException("invalid A addressing mode " + mode1 + " (decoded from " + op + ") on line " + ploc);
}
switch(mode2)
{
case Instruction.MODE_IMM:
line2 += poffset;
break;
case Instruction.MODE_DIR:
line2 += ploc;
break;
case Instruction.MODE_IND:
line2 += ploc;
line2 += core[line2 & coreSizeM1].field2;
break;
default:
throw new IllegalStateException("invalid B addressing mode " + mode2 + " (decoded from " + op + ") on line " + ploc);
}
line1 &= coreSizeM1;
line2 &= coreSizeM1;
Instruction i1, i2;
switch(opcode)
{
case Instruction.OP_MOV:
core[line2] = core[line1];
break;
case Instruction.OP_ADD:
i1 = core[line1]; i2 = core[line2];
core[line2] = new Instruction(i2.packedOp, i2.field1 + i1.field1, i2.field2 + i1.field2);
break;
case Instruction.OP_SUB:
i1 = core[line1]; i2 = core[line2];
core[line2] = new Instruction(i2.packedOp, i2.field1 - i1.field1, i2.field2 - i1.field2);
break;
case Instruction.OP_JMP:
ploc = line1 - 1; // will be incremented by one below
break;
case Instruction.OP_JMZ:
i2 = core[line2];
if(i2 == Instruction.DAT00 || (i2.field1 == 0 && i2.field2 == 0))
{
ploc = line1 - 1;
}
break;
case Instruction.OP_CMP:
i1 = core[line1]; i2 = core[line2];
if(i1 != i2 && (i1.field1 != i2.field1 || i1.field2 != i2.field2))
{
ploc++;
}
break;
default:
throw new IllegalStateException("invalid opcode " + opcode + " (decoded from " + op + ") on line " + ploc);
}
ploc++;
ploc &= coreSizeM1;
int tmpLoc = ploc; ploc = xloc; xloc = tmpLoc;
int tmpOffset = poffset; poffset = xoffset; xoffset = tmpOffset;
}
return 1;
}
public void printCore(Instruction[] core, int ploc, int xloc, int step)
{
int time = (step >> 1), turn = (step & 1);
System.out.println("---- cycle " + time + ", player " + (turn == 0 ? 1 : 2) +
" (" + (turn == 0 ? p1 : p2).getName() + ") about to execute: ----");
int dupCount = 0;
Instruction dupLine = Instruction.DAT00;
for(int i = 0; i < core.length; i++)
{
Instruction line = core[i];
if(line.equals(dupLine) && i != ploc && i != xloc)
{
if(dupCount == 0)
{
System.out.printf("%04d\t", i);
System.out.println(line);
}
dupCount++;
}
else
{
if(dupCount == 2)
{
System.out.printf("%04d\t", i-1);
System.out.println(dupLine);
}
else if(dupCount > 2)
{
System.out.println(" " + (dupCount - 1) + " lines skipped.");
}
System.out.printf("%04d\t", i);
System.out.print(line);
if(i == ploc)
{
System.out.print("\t<- " + (turn == 0 ? 1 : 2));
}
if(i == xloc)
{
System.out.print("\t<- " + (turn == 0 ? 2 : 1));
}
System.out.println();
dupLine = line;
dupCount = 1;
}
}
if(dupCount == 2)
{
System.out.printf("%04d\t", core.length-1);
System.out.println(dupLine);
}
else if(dupCount > 2)
{
System.out.println(" " + (dupCount - 1) + " lines skipped.");
}
}
}