-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe-ws2812.ino
281 lines (239 loc) · 7.31 KB
/
tictactoe-ws2812.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
/**********************************************************************************************
* TicTacToe - WS2812 Version 1.0
* by Christoph Khouri <christoph.khouri@gmail.com>
*
* Licensed under the MIT License
**********************************************************************************************/
/*
* Board logicaly set up as:
* 1 2 3
* 4 5 6
* 7 8 9
*
* Board technically set up as:
* 8 3 2
* 7 4 1
* 6 5 0
*
* 8 ways to win
* _#1__ _#2__ _#3__ _#4__
* 1 2 3 1 . . 1 . . . 2 .
* . . . . 5 . 4 . . . 5 .
* . . . . . 9 7 . . . 8 .
*
* _#5__ _#6__ _#7__ _#8__
* . . 3 . . 3 . . . . . .
* . 5 . . . 6 4 5 6 . . .
* 7 . . . . 9 . . . 7 8 9
*
*/
#include <Adafruit_NeoPixel.h>
#define LED_PIN D4
#define LED_COUNT 9
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Define possible colors on the board
uint32_t boardColor[] = {
strip.Color( 0, 0, 0), // [0] is black (off)
strip.Color(255, 0, 0), // [1] is red for "X"
strip.Color( 0, 0, 255), // [2] is blue for "O"
strip.Color( 0, 255, 0) // [3] is green for winning color
};
#define BOARD_SIZE 9
#define WINNING_FLASHES 5
int X = 1;
int O = 2;
int winner = 0; // Placeholder for winner
int turn = X; // X goes first
int totalMoves = 0; // Number of moves
String player_name[] = {"-", "X", "O"};
// Array to store state of board
int board[9]; // Values: 0=[No move yet], 1=X, 2=O
// Map Logical board to numbering of NeoPixels
// Note: Starts at index 1 to match logical layout
int boardMap[] = {0,8,3,2,7,4,1,6,5,0};
// Define the possible ways to win
int winningStrategy[][3] = {
{0,0,0}, // Tie game
{1,2,3},
{1,5,9},
{1,4,7},
{2,5,8},
{3,5,7},
{3,6,9},
{4,5,6},
{7,8,9}
};
void setup() {
strip.begin(); // Initialize NeoPixel strip
strip.show();
strip.setBrightness(50); // Set BRIGHTNESS (max = 255)
Serial.begin(115200); // Initialize the serial port for debugging
delay(200); // Quick nap
startGame(); // Let the games begin
}
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
// Initialize the board for a new game
void startGame() {
colorWipe(boardColor[1], 10); // Red
colorWipe(boardColor[2], 10); // Blue
colorWipe(boardColor[0], 10); // Wipe off with 'black'
delay(500);
Serial.println(".....................");
Serial.println("Shall we play a game?");
winner = 0;
turn = 1;
memset(board,0,sizeof(board));
totalMoves = 0;
debugBoard();
}
// Flash the board for a tie or winning game.
void showWinner(int strategy) {
if (strategy == 0) { // Tie
Serial.println("Tie game");
} else {
if (winner == 1) {
Serial.println("X is the WINNER!");
} else {
Serial.println("O is the WINNER!");
}
}
for (int i = 0; i < WINNING_FLASHES; i++) {
if (strategy == 0) { // Flash entire board for tie game
for (int j = 0; j < BOARD_SIZE; j++) {
strip.setPixelColor(j, boardColor[3]);
}
} else { // Just flash the winning spots
strip.setPixelColor(boardMap[winningStrategy[strategy][0]], boardColor[3]);
strip.setPixelColor(boardMap[winningStrategy[strategy][1]], boardColor[3]);
strip.setPixelColor(boardMap[winningStrategy[strategy][2]], boardColor[3]);
}
strip.show();
delay(350);
for(int i = 0; i < BOARD_SIZE; i++) { // Reset the board to it's original layout
strip.setPixelColor(i, boardColor[board[i]]);
}
strip.show();
delay(150);
}
}
// Validate the move against the current board
// and check for a winner or a tie game
void makeMove(int boardPosition) {
if (board[boardMap[boardPosition]] == turn) {
Serial.println("You already have that spot! Go Again.");
return;
} else if (board[boardMap[boardPosition]] == 0) { // Empty spot - Valid move
board[boardMap[boardPosition]] = turn;
strip.setPixelColor(boardMap[boardPosition], boardColor[turn]);
strip.show();
debugBoard();
totalMoves++;
Serial.println((String)"Total moves:"+totalMoves);
int winner = checkGame(turn);
if (winner == -1) { // Game still in progress
if (turn == X) {
turn = O;
Serial.println("O's turn");
} else {
turn = X;
Serial.println("X's turn");
}
} else {
showWinner(winner); // Flash board for winner
startGame(); // Start a new game
}
return;
} else {
Serial.println("Your opponent already has that spot! Try Again.");
return;
}
}
// Check if someone has won the game or there is a tie.
// Return the winning strategy (0 for tie) or -1 if game is still in progress.
int checkGame(int player) {
if (board[boardMap[1]] == player && board[boardMap[2]] == player && board[boardMap[3]] == player) { // 1
winner = player;
return 1;
}
if (board[boardMap[1]] == player && board[boardMap[5]] == player && board[boardMap[9]] == player) { // 2
winner = player;
return 2;
}
if (board[boardMap[1]] == player && board[boardMap[4]] == player && board[boardMap[7]] == player) { // 3
winner = player;
return 3;
}
if (board[boardMap[2]] == player && board[boardMap[5]] == player && board[boardMap[8]] == player) { // 4
winner = player;
return 4;
}
if (board[boardMap[3]] == player && board[boardMap[5]] == player && board[boardMap[7]] == player) { // 5
winner = player;
return 5;
}
if (board[boardMap[3]] == player && board[boardMap[6]] == player && board[boardMap[9]] == player) { // 6
winner = player;
return 6;
}
if (board[boardMap[4]] == player && board[boardMap[5]] == player && board[boardMap[6]] == player) {
winner = player;
return 7;
}
if (board[boardMap[7]] == player && board[boardMap[8]] == player && board[boardMap[9]] == player) {
winner = player;
return 8;
}
if (totalMoves == BOARD_SIZE) { // Tie game
return 0;
}
// No winner
return -1;
}
void debugBoard() {
Serial.print(" ");
Serial.print(player_name[board[boardMap[1]]]);
Serial.print(" ");
Serial.print(player_name[board[boardMap[2]]]);
Serial.print(" ");
Serial.print(player_name[board[boardMap[3]]]);
Serial.println();
Serial.print(" ");
Serial.print(player_name[board[boardMap[4]]]);
Serial.print(" ");
Serial.print(player_name[board[boardMap[5]]]);
Serial.print(" ");
Serial.print(player_name[board[boardMap[6]]]);
Serial.println();
Serial.print(" ");
Serial.print(player_name[board[boardMap[7]]]);
Serial.print(" ");
Serial.print(player_name[board[boardMap[8]]]);
Serial.print(" ");
Serial.print(player_name[board[boardMap[9]]]);
Serial.println();
}
void loop() {
int input;
if (Serial.available() > 0) { // is a character available?
input = Serial.readStringUntil('\n').toInt();
Serial.print(player_name[turn]);
Serial.print("'s move was at position:");
Serial.print(input);
Serial.println();
// check if a valid number was received
if ((input >= 1) && (input <= BOARD_SIZE)) {
makeMove(input);
} else if (input == 0) {
Serial.println("Restarting game");
startGame(); // Start a new game
} else {
Serial.println("Invalid move");
}
}
}