-
Notifications
You must be signed in to change notification settings - Fork 0
/
birdcage.ino
309 lines (262 loc) · 7.57 KB
/
birdcage.ino
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Shannon's Bird Cage
Here is the circuit used for the game.
This is an M=4 board. An M=3 board can be used too.
8 ● = ● = ● = ●
7 | | | |
6 ● - ● - ● - ●
5 | | | |
4 ● - ● - ● - ●
3 | | | |
2 ● - ● - ● - ●
1 | | | |
0 ● = ● = ● = ●
0 1 2 3 4 5 6
A B C D E F G
Single lines are resistors, double lines
are connections (with zero resistance).
Nodes are shown as circles. Valid moves
are shown as (single) lines. One player
(the machine) is called CUT and removes
lines. The other (human) is SHORT, and
short-circuits lines.
CUT wins if the top row is separated from
the bottom row. SHORT wins if the top
row is connected to the bottom (with
zero resistance).
The top row is connected to a 5V supply,
the bottom row is connected to ground via
a resistor (to avoid a real short circuit
if SHORT wins).
This program measures the voltage at the nodes
and uses Shannon's heuristic to choose the
next move - this is the one that has the maximum
voltage drop between adjoining nodes.
*/
// Size of game (can be 3 or 4)
// This is determined in setup
int M = 4;
// Pins used to read voltages using analogRead
byte nodePins[13] = {A13, A12, A11, A10, A9, A8, A6, A5, A4, A3, A2, A1, A15};
// Next move button
// Useful reference: http://www.gammon.com.au/switches
const byte switchPin = 8;
byte oldSwitchState = HIGH;
// LED digital pins
byte ledRowPins[7] = {51, 49, 47, 45, 43, 41, 39};
byte ledColumnPins[7] = {35, 33, 31, 29, 27, 25, 23};
// An array storing the state of the circuit.
// Nodes (where i and j are both even) store the voltages measured via analogRead.
// Moves (where i+j is odd) store the voltage differences between adjoining nodes.
int volts[9][7];
// An array storing which moves have already been made.
// A value of 1 in position i, j (where i+j is odd) means the move has been made already.
int moves[9][7];
const int maxV = 1023; // 5V represented in 10 bits
void setup() {
Serial.begin(9600);
Serial.println("Shannon's Bird Cage");
for (int i = 0; i < 13; i++) {
pinMode(nodePins[i], INPUT_PULLUP);
}
pinMode(switchPin, INPUT_PULLUP);
for (int i = 0; i < 7; i++) {
pinMode(ledColumnPins[i], OUTPUT);
pinMode(ledRowPins[i], OUTPUT);
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 7; j++) {
volts[i][j] = -1;
moves[i][j] = 0;
}
}
// top row is always at 5V
// (note bottom row is not at 0V, since there is a resistor to avoid shorting)
volts[8][0] = maxV;
volts[8][2] = maxV;
volts[8][4] = maxV;
volts[8][6] = maxV;
// work out if M=3 or M=4 by seeing if there is a jumper at A7
updateVoltages();
computeDifferences();
M = volts[7][0] == 0 ? 3 : 4;
Serial.print("M=");
Serial.println(M);
for (int i = 0; i < M * 2 - 1; i++) {
digitalWrite(ledColumnPins[i], HIGH);
digitalWrite(ledRowPins[i], HIGH);
}
}
void loop() {
byte switchState = digitalRead(switchPin);
if (switchState != oldSwitchState) {
oldSwitchState = switchState;
delay(10); // debounce time
if (switchState == LOW) {
bool gameOver = checkGameOver();
updateVoltages();
computeDifferences();
displayVoltages();
displayVoltageDifferences();
//displayMoves();
if (!gameOver) {
chooseMove();
}
}
}
}
// Return true if the position represents a node (where a voltage measurement
// is made).
boolean isNode(int i, int j) {
return (i % 2 == 0) && (j % 2 == 0);
}
// Return true if the position represents a move.
boolean isMove(int i, int j) {
return (i + j) % 2 == 1;
}
bool checkGameOver() {
bool gameOver = false;
// We know the game is over if the bottom row is either
// 0V (CUT wins) or 5V (SHORT wins).
// This is needed otherwise bottomRowVoltage may not be 0 (not sure why)
turnAllLedsOff();
// We need to temporarily disconnect the pull-up resistors,
// since they will alter the voltage readings.
for (int i = 0; i < 13; i++) {
pinMode(nodePins[i], INPUT);
}
int bottomRowVoltage = analogRead(A15);
if (bottomRowVoltage == 0) {
Serial.println("I win! (CUT)");
turnRowLedsOn();
gameOver = true;
} else if (bottomRowVoltage == maxV) {
Serial.println("You win! (SHORT)");
turnColumnLedsOn();
gameOver = true;
}
// Re-connect pull-up resistors
for (int i = 0; i < 13; i++) {
pinMode(nodePins[i], INPUT_PULLUP);
}
return gameOver;
}
// Read the voltages for all nodes
void updateVoltages() {
volts[6][0] = analogRead(nodePins[0]);
volts[4][0] = analogRead(nodePins[1]);
volts[2][0] = analogRead(nodePins[2]);
volts[2][2] = analogRead(nodePins[3]);
volts[4][2] = analogRead(nodePins[4]);
volts[6][2] = analogRead(nodePins[5]);
volts[6][4] = analogRead(nodePins[6]);
volts[4][4] = analogRead(nodePins[7]);
volts[2][4] = analogRead(nodePins[8]);
volts[2][6] = analogRead(nodePins[9]);
volts[4][6] = analogRead(nodePins[10]);
volts[6][6] = analogRead(nodePins[11]);
// bottom row
volts[0][0] = analogRead(nodePins[12]);
volts[0][2] = volts[0][0];
volts[0][4] = volts[0][0];
volts[0][6] = volts[0][0];
}
// Compute the voltage differences for all moves (differences between nodes).
void computeDifferences() {
for (int i = 7; i >= 1; i--) { // skip top and bottom rows
for (int j = 0; j < 7; j++) {
if ((i % 2 == 0) && (j % 2 == 1)) {
volts[i][j] = abs(volts[i][j - 1] - volts[i][j + 1]);
} else if ((i % 2 == 1) && (j % 2 == 0)) {
volts[i][j] = abs(volts[i - 1][j] - volts[i + 1][j]);
}
}
}
}
// Use the Shannon heuristic (largest voltage difference) to choose the next move.
void chooseMove() {
int maxVoltageDiff = -1;
int mi = -1;
int mj = -1;
for (int i = M * 2 - 1; i >= 1; i--) { // skip top and bottom rows
for (int j = 0; j < M * 2 - 1; j++) {
// check valid move, and hasn't already been played
if (isMove(i, j) && moves[i][j] == 0) {
if (volts[i][j] > maxVoltageDiff) {
maxVoltageDiff = volts[i][j];
mi = i;
mj = j;
}
}
}
}
moves[mi][mj] = 1;
Serial.print("Move: ");
Serial.print(mi);
Serial.print(", ");
Serial.println(mj);
turnAllLedsOff();
showMove(mi, mj);
}
void turnAllLedsOff() {
for (int i = 0; i < 7; i++) {
digitalWrite(ledRowPins[i], LOW);
digitalWrite(ledColumnPins[i], LOW);
}
}
void turnRowLedsOn() {
turnAllLedsOff();
for (int i = 0; i < M * 2 - 1; i++) {
digitalWrite(ledRowPins[i], HIGH);
}
}
void turnColumnLedsOn() {
turnAllLedsOff();
for (int i = 0; i < M * 2 - 1; i++) {
digitalWrite(ledColumnPins[i], HIGH);
}
}
void showMove(int i, int j) {
// need to subtract 1 from row, since bottom row has no LED
digitalWrite(ledRowPins[i - 1], HIGH);
digitalWrite(ledColumnPins[j], HIGH);
}
void displayVoltages() {
for (int i = 8; i >= 0; i--) {
Serial.print(i);
Serial.print("|");
for (int j = 0; j < 7; j++) {
if (isNode(i, j)) {
Serial.print(volts[i][j]);
}
Serial.print("\t");
}
Serial.println();
}
}
void displayVoltageDifferences() {
for (int i = 7; i >= 1; i--) {
Serial.print(i);
Serial.print("|");
for (int j = 0; j < 7; j++) {
if (isMove(i, j)) {
Serial.print(volts[i][j]);
}
Serial.print("\t");
}
Serial.println();
}
}
void displayMoves() {
for (int i = 7; i >= 1; i--) {
Serial.print(i);
Serial.print("|");
for (int j = 0; j < 7; j++) {
if (isMove(i, j)) {
Serial.print(moves[i][j]);
}
Serial.print("\t");
}
Serial.println();
}
}