Components and supplies
Arduino Due
2,8 zoll TFT LCD with touchscreen
Jumper wires (generic)
Apps and platforms
Arduino IDE
Project description
Code
TicTacToe_new
arduino
An invincible Tic Tac Toe single player on Arduino Due
1 ///////////////////////////////////////////// 2 // 2.8" TOUCH SCREEN TIC TAC TOE // 3 // // 4 // http://www.educ8s.tv // 5 // // 6 // modified 08.2017 by rom3 // 7///////////////////////////////////////////// 8 9#include <SPI.h> 10#include <Adafruit_GFX.h> 11#include <Adafruit_ILI9341.h> 12#include <URTouch.h> 13 14#define TFT_RST 8 15#define TFT_DC 9 16#define TFT_CS 10 17#define TOUCH_ORIENTATION LANDSCAPE 18 19Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST); 20 21Adafruit_GFX_Button buttons[9]; 22 23URTouch myTouch(30, 28, 26, 24, 22); 24 25char start[10] = {"Gamestart"}; 26char you[4] = {"YOU"}; 27char cpu[4] = {"CPU"}; 28char again[7] = {"Again?"}; 29int player; 30int difficulty=8; 31unsigned short int x,y,b; // position touch 32 33#define BLACK 0x0000 34#define BLUE 0x001F 35#define RED 0xF800 36#define GREEN 0x07E0 37#define CYAN 0x07FF 38#define MAGENTA 0xF81F 39#define YELLOW 0xFFE0 40#define WHITE 0xFFFF 41 42int board[]={0,0,0,0,0,0,0,0,0};// holds position data 0 is blank, 1 human, 2 is computer 43unsigned turn; 44 45const unsigned char circle[]PROGMEM={ 460b0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 470x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x7,0xf8,0x0,0x0,0x0,0x0,0x0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0x0,0x0,0xff,0xff,0xf8,0xf,0xff,0xff,0x80,0x0,0x0,0xff,0xff,0xf0,0x7,0xff,0xff,0x80,0x0,0x1,0xff,0xff,0xe0,0x7,0xff,0xff,0xc0,0x0,0x1,0xff,0xff,0xe0,0x3,0xff,0xff,0xc0,0x0, 720x3,0xff,0xff,0xc0,0x3,0xff,0xff,0xe0,0x0,0x3,0xff,0xff,0xc0,0x1,0xff,0xff,0xe0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 730x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 74}; 75 76 77void setup() 78{ 79myTouch.InitTouch(); 80 myTouch.setPrecision(PREC_HI); 81 tft.begin(); 82 tft.setRotation(3); 83} 84 85void loop() 86{ 87 int board[9] = {0,0,0,0,0,0,0,0,0}; 88 89 drawStartScreen(); 90 do{ 91 myTouch.read(); 92 x = myTouch.getX(); //Get touch point 93 y = myTouch.getY(); 94 delay(100); 95 }while(!(buttons[0].contains(x,y))); 96 97 tft.fillScreen(BLACK); 98 //Draw white frame 99 tft.drawRect(0,0,320,240,WHITE); 100 //Print "Tic Tac Toe" Text 101 tft.setCursor(30,30); 102 tft.setTextColor(WHITE); 103 tft.setTextSize(4); 104 tft.print("Tic Tac Toe"); 105 //Print "YouTube!" text 106 tft.setCursor(30,100); 107 tft.setTextColor(GREEN); 108 tft.setTextSize(4); 109 tft.print("Who begins"); 110 //Create Red Button 111 // create buttons // x, y, w, h, outline, fill, text 112 buttons[0].initButton(&tft, 100,200,80,40,WHITE, RED, WHITE, 113 you, 2); 114 buttons[0].drawButton(); 115 // create buttons // x, y, w, h, outline, fill, text 116 buttons[1].initButton(&tft, 200,200,80,40,WHITE, RED, WHITE, 117 cpu, 2); 118 buttons[1].drawButton(); 119 b=1; 120 do{ 121 myTouch.read(); 122 x = myTouch.getX(); //Get touch point 123 y = myTouch.getY(); 124 if (buttons[0].contains(x,y)) 125 { 126 b=0; 127 player = 1; 128 tft.fillScreen(BLACK); 129 //Draw white frame 130 tft.drawRect(0,0,320,240,WHITE); 131 tft.setCursor(30,30); 132 tft.setTextColor(WHITE); 133 tft.setTextSize(2); 134 tft.print("Ok, you begin."); 135 delay(2000); 136 } 137 if(buttons[1].contains(x,y)) 138 { 139 b=0; 140 player = 2; 141 tft.fillScreen(BLACK); 142 //Draw white frame 143 tft.drawRect(0,0,320,240,WHITE); 144 tft.setCursor(30,30); 145 tft.setTextColor(WHITE); 146 tft.setTextSize(2); 147 tft.print("Ok, CPU begins."); 148 delay(2000); 149 } 150 }while(b); 151 152 tft.fillScreen(BLACK); 153 tft.drawRect(0,0,319,240,WHITE); 154 155 drawVerticalLine(125); 156 drawVerticalLine(195); 157 drawHorizontalLine(80); 158 drawHorizontalLine(150); 159 160 for(turn = 0; turn < 9 && win(board) == 0; ++turn) { 161 if((turn+player) % 2 == 0) 162 computerMove(board); 163 else { 164 //draw(board); 165 playerMove(board); 166 } 167 } 168 switch(win(board)) { 169 case 0: 170 Serial.println("It's a draw.\ 171"); 172 break; 173 case 1: 174 //draw(board); 175 Serial.println("You lose.\ 176"); 177 break; 178 case -1: 179 Serial.println("You win!\ 180"); 181 break; 182 } 183 do{}while(myTouch.dataAvailable()==false); 184 //x = map(tp.y, 930, 88, 0, 480); 185 //y = map(tp.x, 908, 125, 0, 320); 186 //delay(250);} 187 } 188void drawHorizontalLine(int y) 189{ 190 int i=0; 191 for(i=0;i<7;i++) 192 { 193 tft.drawLine(60,y+i,270,y+i,WHITE); 194 } 195} 196 197void drawVerticalLine(int x) 198{ 199 int i=0; 200 for(i=0;i<7;i++) 201 { 202 tft.drawLine(x+i,20,x+i,220,WHITE); 203 } 204} 205void drawStartScreen() 206{ 207 tft.fillScreen(BLACK); 208 209 //Draw white frame 210 tft.drawRect(0,0,319,240,WHITE); 211 212 //Print "Tic Tac Toe" Text 213 tft.setCursor(30,100); 214 tft.setTextColor(WHITE); 215 tft.setTextSize(4); 216 tft.print("Tic Tac Toe"); 217 218 //Print "YouTube!" text 219 tft.setCursor(80,30); 220 tft.setTextColor(GREEN); 221 tft.setTextSize(4); 222 tft.print("Arduino"); 223 224 buttons[0].initButton(&tft, 160,200,200,40,WHITE, RED, WHITE, start, 2); 225 buttons[0].drawButton(); 226 227} 228 229void drawCircle(int x, int y) 230{ 231 drawBitmap(x,y,circle,65,65,RED); 232} 233 234void drawX(int x, int y) 235{ 236 drawBitmap(x,y,x_bitmap,65,65,BLUE); 237} 238void drawBitmap(int16_t x, int16_t y, 239 const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) { 240 241 int16_t i, j, byteWidth = (w + 7) / 8; 242 uint8_t byte; 243 244 for(j=0; j<h; j++) { 245 for(i=0; i<w; i++) { 246 if(i & 7) byte <<= 1; 247 else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8); 248 if(byte & 0x80) tft.drawPixel(x+i, y+j, color); 249 } 250 } 251} 252void computerMove(int board[9]) { 253 int move = -1; 254 int score = -2; 255 int i; 256 for(i = 0; i < 9; ++i) { 257 if(board[i] == 0) { 258 board[i] = 1; 259 int tempScore = -minimax(board, -1, 0); 260 board[i] = 0; 261 if(tempScore > score) { 262 score = tempScore; 263 move = i; 264 } 265 } 266 } 267 //returns a score based on minimax tree at a given node. 268 board[move] = 1; 269 drawCpuMove(move); 270 delay(100); 271} 272 273void playerMove(int board[9]) { 274 int move = 0; 275 b = 1; 276 do{ 277 if (myTouch.dataAvailable() == true) 278 { 279 myTouch.read(); 280 x = myTouch.getX(); //Get touch point 281 y = myTouch.getY(); 282 283 if((x>55 && x<125)&& (y>0 && y<80)) //0 284 { 285 if(board[0]==0) 286 { 287 move=0; 288 b=0; 289 } 290 } 291 else if((x>125 && x<195) && (y>0 && y<80)) //1 292 { 293 294 if(board[1]==0) 295 { 296 move=1; 297 b=0; 298 } 299 } 300 else if((x>195&&x<265)&& (y>0 && y<80)) //2 301 { 302 if(board[2]==0) 303 { 304 move=2; 305 b=0; 306 } 307 } 308 309 else if((x>55 && x<125)&& (y>80 && y<155)) //3 310 { 311 if(board[3]==0) 312 { 313 move=3; 314 b=0; 315 } 316 } 317 318 else if((x>125 && x<195)&& (y>80 && y<155)) //4 319 { 320 if(board[4]==0) 321 { 322 move=4; 323 b=0; 324 } 325 } 326 327 else if((x>195&&x<265)&& (y>80 && y<155)) //5 328 { 329 if(board[5]==0) 330 { 331 move=5; 332 b=0; 333 } 334 } 335 336 else if((x>55 && x<125)&& (y>155 && y<235)) //6 337 { 338 if(board[6]==0) 339 { 340 move=6; 341 b=0; 342 } 343 } 344 345 else if((x>125 && x<195)&& (y>155 && y<235)) //7 346 { 347 if(board[7]==0) 348 { 349 move=7; 350 b=0; 351 } 352 } 353 354 else if((x>195&&x<265)&& (y>155 && y<235)) //8 355 { 356 if(board[8]==0) 357 { 358 move=8; 359 b=0; 360 } 361 } 362 363 } 364 }while(b); 365 board[move] = -1; 366 drawPlayerMove(move); 367 delay(100); 368} 369int win(const int board[9]) { 370 //list of possible winning positions 371 unsigned wins[8][3] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}}; 372 int winPos; 373 for(winPos = 0; winPos < 8; ++winPos) { 374 if(board[wins[winPos][0]] != 0 && board[wins[winPos][0]] == board[wins[winPos][1]] && board[wins[winPos][0]] == board[wins[winPos][2]]) 375 return board[wins[winPos][2]]; 376 } 377 return 0; 378} 379 380int minimax(int board[9], int player, int depth) { 381 //check the positions for players 382 int winner = win(board); 383 if(winner != 0) return winner*player; 384 385 int move = -1; 386 int score = -2; 387 int i; 388 for(i = 0; i < 9; ++i) { 389 390 if(board[i] == 0) { 391 board[i] = player; 392 int thisScore=0; 393 if (depth<difficulty){ 394 thisScore = -minimax(board, player*-1,depth+1); 395 } 396 397 if(thisScore > score) { 398 score = thisScore; 399 move = i; 400 } 401 //choose the worst move for opponent 402 board[i] = 0; 403 } 404 } 405 if(move == -1) return 0; 406 return score; 407} 408void drawCpuMove(int move) 409{ 410 switch(move) 411 { 412 case 0: drawCircle(55,15); break; 413 case 1: drawCircle(130,15); break; 414 case 2: drawCircle(205,15); break; 415 case 3: drawCircle(55,85); break; 416 case 4: drawCircle(130,85); break; 417 case 5: drawCircle(205,85); break; 418 case 6: drawCircle(55,155); break; 419 case 7: drawCircle(130,155);break; 420 case 8: drawCircle(205,155);break; 421 } 422} 423 424void drawPlayerMove(int move) 425{ 426 switch(move) 427 { 428 case 0: drawX(55,15); break; 429 case 1: drawX(130,15); break; 430 case 2: drawX(205,15); break; 431 case 3: drawX(55,85); break; 432 case 4: drawX(130,85); break; 433 case 5: drawX(205,85); break; 434 case 6: drawX(55,155); break; 435 case 7: drawX(130,155);break; 436 case 8: drawX(205,155);break; 437 } 438} 439 440
TIC TAC TOE
arduino
A simple TIC TAC TOE game to play against the computer
1 ///////////////////////////////////////////// 2 // 2.8" TOUCH SCREEN TIC TAC TOE // 3 // // 4 // http://www.educ8s.tv // 5 // // 6 // modified 08.2017 by rom3 // 7///////////////////////////////////////////// 8 9#include <SPI.h> 10#include <Adafruit_GFX.h> 11#include <Adafruit_ILI9341.h> 12#include <URTouch.h> 13#include <avr/pgmspace.h> 14 15#define TFT_RST 8 16#define TFT_DC 9 17#define TFT_CS 10 18#define TOUCH_ORIENTATION LANDSCAPE 19 20Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST); 21 22Adafruit_GFX_Button buttons[9]; 23 24URTouch myTouch(30, 28, 26, 24, 22); 25 26unsigned short int moves = 1; 27unsigned short int winner = 0; //0 = Draw, 1 = Human, 2 = CPU 28unsigned short int x,y; // position touch 29unsigned short int cursor; // Cursor position 30unsigned short int firstMoves[]={0,2,6,8}; // will use these positions first, if CPU begins 31unsigned short int board[] = {0,0,0,0,0,0,0,0,0};// holds position data 0 is blank, 1 human, 2 is computer 32bool player; // true=YOU, false=CPU 33bool b=1; 34 35char start[10] = {"Gamestart"}; 36char you[4] = {"YOU"}; 37char cpu[4] = {"CPU"}; 38char again[7] = {"Again?"}; 39 40const unsigned char circle[]PROGMEM={ 410b0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 420x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x7,0xf8,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x7f,0xff,0x80,0x0,0x0,0x0,0x0,0x0,0x3,0xff,0xff,0xf0,0x0,0x0,0x0, 430x0,0x0,0xf,0xff,0xff,0xfc,0x0,0x0,0x0,0x0,0x0,0x1f,0xff,0xff,0xfe,0x0,0x0,0x0,0x0,0x0,0x7f,0xf0,0x3,0xff,0x80,0x0,0x0,0x0,0x0,0xff,0x80,0x0,0x7f,0xc0,0x0,0x0,0x0,0x1,0xfe,0x0,0x0,0x1f,0xe0,0x0,0x0, 440x0,0x3,0xf8,0x0,0x0,0x7,0xf0,0x0,0x0,0x0,0x7,0xf0,0x0,0x0,0x3,0xf8,0x0,0x0,0x0,0xf,0xe0,0x0,0x0,0x1,0xfc,0x0,0x0,0x0,0xf,0xc0,0x0,0x0,0x0,0xfc,0x0,0x0,0x0,0x1f,0x80,0x0,0x0,0x0,0x7e,0x0,0x0, 450x0,0x3f,0x0,0x0,0x0,0x0,0x3f,0x0,0x0,0x0,0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,0x0,0x0,0x0,0x0,0x0,0x0 70}; 71 72void setup() { 73 myTouch.InitTouch(); 74 myTouch.setPrecision(PREC_HI); 75 tft.begin(); 76 tft.setRotation(3); 77 tft.fillScreen(ILI9341_BLACK); 78 //Draw white frame 79 tft.drawRect(0,0,320,240,ILI9341_WHITE); 80 //Print "Arduino" text 81 tft.setCursor(80,30); 82 tft.setTextColor(ILI9341_GREEN); 83 tft.setTextSize(4); 84 tft.print("Arduino"); 85 //Print "Tic Tac Toe" Text 86 tft.setCursor(30,100); 87 tft.setTextColor(ILI9341_WHITE); 88 tft.setTextSize(4); 89 tft.print("Tic Tac Toe"); 90 //Create Red Button 91 // create buttons // x, y, w, h, outline, fill, text 92 buttons[0].initButton(&tft, 160,200,200,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, 93 start, 2); 94 buttons[0].drawButton(); 95do{ 96 waitonetouch(&x,&y); 97 if (buttons[0].contains(x,y)) 98 b=0; 99}while(b>0); 100} 101void loop() 102{ 103 for(unsigned short int i=0;i<9;i++) 104 { 105 board[i]=0; 106 } 107 tft.fillScreen(ILI9341_BLACK); 108 //Draw white frame 109 tft.drawRect(0,0,320,240,ILI9341_WHITE); 110 //Print "Tic Tac Toe" Text 111 tft.setCursor(30,30); 112 tft.setTextColor(ILI9341_WHITE); 113 tft.setTextSize(4); 114 tft.print("Tic Tac Toe"); 115 //Print "YouTube!" text 116 tft.setCursor(30,100); 117 tft.setTextColor(ILI9341_GREEN); 118 tft.setTextSize(4); 119 tft.print("Who begins"); 120 //Create Red Button 121 // create buttons // x, y, w, h, outline, fill, text 122 buttons[0].initButton(&tft, 100,200,80,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, 123 you, 2); 124 buttons[0].drawButton(); 125 // create buttons // x, y, w, h, outline, fill, text 126 buttons[1].initButton(&tft, 200,200,80,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, 127 cpu, 2); 128 buttons[1].drawButton(); 129 b=1; 130 do{ 131 waitonetouch(&x,&y); 132 if (buttons[0].contains(x,y)) 133 { 134 b=0; 135 player = 1; 136 tft.fillScreen(ILI9341_BLACK); 137 //Draw white frame 138 tft.drawRect(0,0,320,240,ILI9341_WHITE); 139 tft.setCursor(30,30); 140 tft.setTextColor(ILI9341_WHITE); 141 tft.setTextSize(2); 142 tft.print("Ok, you begin."); 143 delay(2000); 144 } 145 if(buttons[1].contains(x,y)) 146 { 147 b=0; 148 player = 0; 149 tft.fillScreen(ILI9341_BLACK); 150 //Draw white frame 151 tft.drawRect(0,0,320,240,ILI9341_WHITE); 152 tft.setCursor(30,30); 153 tft.setTextColor(ILI9341_WHITE); 154 tft.setTextSize(2); 155 tft.print("Ok, CPU begins."); 156 delay(2000); 157 } 158 }while(b>0); 159 b=1; 160 moves = 1; 161 winner = 0; 162 cursor = 10; 163 164 tft.fillScreen(ILI9341_BLACK); 165 166 //Draw frame 167 tft.drawRect(0,0,320,240,ILI9341_WHITE); 168 //Draw vertical line 169 tft.fillRect(80,15,5,205,ILI9341_WHITE); 170 tft.fillRect(150,15,5,205,ILI9341_WHITE); 171 //Draw horizontal line 172 tft.fillRect(15,80,205,5,ILI9341_WHITE); 173 tft.fillRect(15,150,205,5,ILI9341_WHITE); 174 tft.drawRect(230,10,80,220,ILI9341_WHITE); 175 { 176 do 177 { 178 if(moves%2==1 && player==false || moves%2==0 && player==true) 179 { 180 cursor+=10; 181 tft.setCursor(240,cursor); 182 tft.setTextSize(1); 183 tft.println("I draw"); 184 arduinoMove(); 185 checkWinner(); 186 }else 187 { 188 cursor+=10; 189 tft.setCursor(240,cursor); 190 tft.setTextSize(1); 191 tft.println("Your move"); 192 playerMove(); 193 checkWinner(); 194 } 195 delay(1000); 196 moves++; 197 }while (winner==0 && moves<10); 198 waitonetouch(&x,&y); 199 tft.fillScreen(ILI9341_BLACK); 200 201 //Draw frame 202 tft.drawRect(0,0,320,240,ILI9341_WHITE); 203 204 //Print "Game Over" Text 205 tft.setCursor(50,30); 206 tft.setTextColor(ILI9341_WHITE); 207 tft.setTextSize(4); 208 tft.print("GAME OVER"); 209 210 if(winner == 0) 211{ 212 //Print "DRAW!" text 213 tft.setCursor(110,100); 214 tft.setTextColor(ILI9341_YELLOW); 215 tft.setTextSize(4); 216 tft.print("DRAW"); 217} 218 if(winner == 1) 219{ 220 //Print "HUMAN WINS!" text 221 tft.setCursor(40,100); 222 tft.setTextColor(ILI9341_BLUE); 223 tft.setTextSize(4); 224 tft.print("HUMAN WINS"); 225} 226 227 if(winner == 2) 228{ 229 //Print "CPU WINS!" text 230 tft.setCursor(60,100); 231 tft.setTextColor(ILI9341_RED); 232 tft.setTextSize(4); 233 tft.print("CPU WINS"); 234} 235 // create buttons // x, y, w, h, outline, fill, text 236 buttons[0].initButton(&tft, 160,200,200,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, 237 again, 2); 238 buttons[0].drawButton(); 239 do{ 240 waitonetouch(&x,&y); 241 if (buttons[0].contains(x,y)) 242 b=0; 243 }while(b>0); 244 } 245} 246 247void playerMove() 248{ 249 waitonetouch(&x,&y); 250 if((x>15 && x<85) && (y>15 && y<85)) //0 251 { 252 if(board[0]==0) 253 { 254 board[0]=1; 255 drawX(15,15); 256 } 257 else error(); 258 } 259 else if((x>85 && x<155)&& (y>15 && y<85)) //1 260 { 261 if(board[1]==0) 262 { 263 board[1]=1; 264 drawX(85,15); 265 } 266 else error(); 267 } 268 if((x>155 && x<225)&& (y>15 && y<85)) //2 269 { 270 if(board[2]==0) 271 { 272 board[2]=1; 273 drawX(155,15); 274 } 275 else error(); 276 } 277 if((x>15 && x<85)&& (y>85 && y<155)) //3 278 { 279 if(board[3]==0) 280 { 281 board[3]=1; 282 drawX(15,85); 283 } 284 else error(); 285 } 286 if((x>85 && x<155)&& (y>85 && y<155)) //4 287 { 288 if(board[4]==0) 289 { 290 board[4]=1; 291 drawX(85,85); 292 } 293 else error(); 294 } 295 if((x>155 && x<225)&& (y>85 && y<155)) //5 296 { 297 if(board[5]==0) 298 { 299 board[5]=1; 300 drawX(155,85); 301 } 302 else error(); 303 } 304 if((x>15 && x<85)&& (y>155 && y<225)) //6 305 { 306 if(board[6]==0) 307 { 308 board[6]=1; 309 drawX(15,155); 310 } 311 else error(); 312 } 313 if((x>85 && x<155)&& (y>155 && y<225)) //7 314 { 315 if(board[7]==0) 316 { 317 board[7]=1; 318 drawX(85,155); 319 } 320 else error(); 321 } 322 if((x>155 && x<225)&& (y>155 && y<225)) //8 323 { 324 if(board[8]==0) 325 { 326 board[8]=1; 327 drawX(155,155); 328 } 329 else error(); 330 } 331} 332 333void error(){ 334 cursor+=10; 335 tft.setCursor(240,cursor); 336 tft.setTextSize(1); 337 tft.println("Error"); 338 delay(1000); 339 cursor+=10; 340 tft.setCursor(240,cursor); 341 tft.println("Try again."); 342 playerMove(); 343} 344 345unsigned short int checkOpponent(unsigned short int x, unsigned short int y) 346{ 347 if(board[1]==x && board[2]==x && board[0]==y){ 348 if(y==0){ 349 return 0; 350 } 351 else return 1; 352 } 353 if(board[3]==x && board[6]==x && board[0]==y){ 354 if(y==0){ 355 return 0; 356 } 357 else return 6; 358 } 359 if(board[0]==y && board[4]==x && board[8]==x){ 360 if(y==0){ 361 return 0; 362 } 363 else return 8; 364 } 365 if(board[0]==x && board[1]==y && board[2]==x){ 366 if(y==0){ 367 return 1; 368 } 369 else return 0; 370 } 371 if(board[4]==x && board[7]==x && board[1]==y){ 372 if(y==0){ 373 return 1; 374 } 375 else return 7; 376 } 377 if(board[0]==x && board[1]==x && board[2]==y){ 378 if(y==0){ 379 return 2; 380 } 381 else return 0; 382 } 383 if(board[5]==x && board[8]==x && board[2]==y){ 384 if(y==0){ 385 return 2; 386 } 387 else return 8; 388 } 389 if(board[4]==x && board[6]==x && board[2]==y){ 390 if(y==0){ 391 return 2; 392 } 393 else return 6; 394 } 395 if(board[4]==x && board[5]==x && board[3]==y){ 396 if(y==0){ 397 return 3; 398 } 399 else return 5; 400 } 401 if(board[0]==x && board[6]==x && board[3]==y){ 402 if(y==0){ 403 return 3; 404 } 405 else return 6; 406 } 407 if(board[3]==x && board[4]==y && board[5]==x){ 408 if(y==0){ 409 return 4; 410 } 411 else return 3; 412 } 413 if(board[1]==x && board[7]==x && board[4]==y){ 414 if(y==0){ 415 return 4; 416 } 417 else return 1; 418 } 419 if(board[0]==x && board[8]==x && board[4]==y){ 420 if(y==0){ 421 return 4; 422 } 423 else return 0; 424 } 425 if(board[2]==x && board[6]==x && board[4]==y){ 426 if(y==0){ 427 return 4; 428 } 429 else return 2; 430 } 431 if(board[3]==x && board[4]==x && board[5]==y){ 432 if(y==0){ 433 return 5; 434 } 435 else return 3; 436 } 437 if(board[2]==x && board[8]==x && board[5]==y){ 438 if(y==0){ 439 return 5; 440 } 441 else return 2; 442 } 443 if(board[7]==x && board[8]==x && board[6]==y){ 444 if(y==0){ 445 return 6; 446 } 447 else return 8; 448 } 449 if(board[0]==x && board[3]==x && board[6]==y){ 450 if(y==0){ 451 return 6; 452 } 453 else return 0; 454 } 455 if(board[4]==x && board[2]==x && board[6]==y){ 456 if(y==0){ 457 return 6; 458 } 459 else return 2; 460 } 461 if(board[1]==x && board[4]==x && board[7]==y){ 462 if(y==0){ 463 return 7; 464 } 465 else return 1; 466 } 467 if(board[6]==x && board[8]==x && board[7]==y){ 468 if(y==0){ 469 return 7; 470 } 471 else return 6; 472 } 473 if(board[6]==x && board[7]==x && board[8]==y){ 474 if(y==0){ 475 return 8; 476 } 477 else return 6; 478 } 479 if(board[5]==x && board[2]==x && board[8]==y){ 480 if(y==0){ 481 return 8; 482 } 483 else return 2; 484 } 485 if(board[4]==x && board[0]==x && board[8]==y){ 486 if(y==0){ 487 return 8; 488 } 489 else return 0; 490 } 491 else 492 return 100; 493} 494 495void arduinoMove() 496{ 497 if(moves<2) 498 { 499 do{ 500 unsigned short int randomMove =random(4); 501 unsigned short int c=firstMoves[randomMove]; 502 if(board[c]==0){ 503 board[c]=2; 504 drawCpuMove(firstMoves[randomMove]); 505 return; 506 } 507 }while(moves<2); 508 } 509 if(moves<3) 510 { 511 if(board[4]==0) 512 { 513 board[4]=2; 514 drawCpuMove(4); 515 return; 516 } 517 } 518 unsigned short int twoPlayer = checkOpponent(2,0);//CPU two in a row? 519 if(twoPlayer < 9) 520 { 521 board[twoPlayer]=2; 522 drawCpuMove(twoPlayer); 523 return; 524 } 525 unsigned short int nextMove = checkOpponent(1,0);//Player two in a row? 526 if(nextMove < 9) 527 { 528 board[nextMove]=2; 529 drawCpuMove(nextMove); 530 return; 531 } 532 unsigned short int next = checkOpponent(0,2); 533 if(next < 9) 534 { 535 board[next]=2; 536 drawCpuMove(next); 537 return; 538 } 539 do 540 { 541 unsigned short int randomMove =random(9); 542 if (board[randomMove]==0) 543 { 544 board[randomMove]=2; 545 drawCpuMove(randomMove); 546 return; 547 } 548 }while(nextMove == 100); 549} 550 551void drawCircle(unsigned short int x, unsigned short int y) 552{ 553 tft.drawBitmap(x,y,circle,65,65,ILI9341_RED); 554} 555 556void drawX(unsigned short int x, unsigned short int y) 557{ 558 tft.drawBitmap(x,y,x_bitmap,65,65,ILI9341_BLUE); 559} 560 561void drawCpuMove(unsigned short int move) 562{ 563 switch(move) 564 { 565 case 0: drawCircle(15,15); break; 566 case 1: drawCircle(85,15); break; 567 case 2: drawCircle(155,15); break; 568 case 3: drawCircle(15,85); break; 569 case 4: drawCircle(85,85); break; 570 case 5: drawCircle(155,85); break; 571 case 6: drawCircle(15,155); break; 572 case 7: drawCircle(85,155);break; 573 case 8: drawCircle(155,155);break; 574 } 575} 576 577void checkWinner() 578// checks board to see if there is a winner 579// places result in the global variable 'winner' 580{ 581 // noughts win? 582 if (board[0]>0 && board[1]==board[0] && board[2]==board[0]) { 583 winner=board[0]; 584 tft.fillRect(15, 45, 205, 5, ILI9341_WHITE); 585 } 586 if (board[3]>0 && board[4]==board[3] && board[5]==board[3]) { 587 winner=board[3]; 588 tft.fillRect(15, 115, 205, 5, ILI9341_WHITE); 589 } 590 if (board[6]>0 && board[7]==board[6] && board[8]==board[6]) { 591 winner=board[6]; 592 tft.fillRect(15, 185, 205, 5, ILI9341_WHITE); 593 } 594 if (board[0]>0 && board[3]==board[0] && board[6]==board[0]) { 595 winner=board[0]; 596 tft.fillRect(45, 15, 5, 205, ILI9341_WHITE); 597 } 598 if (board[1]>0 && board[4]==board[1] && board[7]==board[1]) { 599 winner=board[1]; 600 tft.fillRect(115, 15, 5, 205, ILI9341_WHITE); 601 } 602 if (board[2]>0 && board[5]==board[2] && board[8]==board[2]) { 603 winner=board[2]; 604 tft.fillRect(185, 15, 5, 205, ILI9341_WHITE); 605 } 606 if (board[0]>0 && board[4]==board[0] && board[8]==board[0]) { 607 winner=board[0]; 608 for(unsigned short int xx=0;xx<6;xx++){ 609 tft.drawLine(20+xx, 20, 210+xx, 210, ILI9341_WHITE); 610 } 611 } 612 if (board[2]>0 && board[4]==board[2] && board[6]==board[2]) { 613 winner=board[2]; 614 for(unsigned short int xx=0;xx<6;xx++){ 615 tft.drawLine(210+xx, 20, 20+xx, 210, ILI9341_WHITE); 616 } 617 } 618} 619void waitonetouch(unsigned short int *x,unsigned short int *y){ 620 do 621 { 622 delay(10); 623 if (myTouch.dataAvailable() == true) 624 { 625 myTouch.read(); 626 *x = myTouch.getX(); //Get touch point 627 *y = myTouch.getY(); 628 return; 629 } 630 }while(myTouch.dataAvailable()==false); 631} 632 633
TicTacToe_new
arduino
An invincible Tic Tac Toe single player on Arduino Due
1 ///////////////////////////////////////////// 2 // 2.8" TOUCH SCREEN TIC TAC TOE // 3 // // 4 // http://www.educ8s.tv // 5 // // 6 // modified 08.2017 by rom3 // 7///////////////////////////////////////////// 8 9#include <SPI.h> 10#include <Adafruit_GFX.h> 11#include <Adafruit_ILI9341.h> 12#include <URTouch.h> 13 14#define TFT_RST 8 15#define TFT_DC 9 16#define TFT_CS 10 17#define TOUCH_ORIENTATION LANDSCAPE 18 19Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST); 20 21Adafruit_GFX_Button buttons[9]; 22 23URTouch myTouch(30, 28, 26, 24, 22); 24 25char start[10] = {"Gamestart"}; 26char you[4] = {"YOU"}; 27char cpu[4] = {"CPU"}; 28char again[7] = {"Again?"}; 29int player; 30int difficulty=8; 31unsigned short int x,y,b; // position touch 32 33#define BLACK 0x0000 34#define BLUE 0x001F 35#define RED 0xF800 36#define GREEN 0x07E0 37#define CYAN 0x07FF 38#define MAGENTA 0xF81F 39#define YELLOW 0xFFE0 40#define WHITE 0xFFFF 41 42int board[]={0,0,0,0,0,0,0,0,0};// holds position data 0 is blank, 1 human, 2 is computer 43unsigned turn; 44 45const unsigned char circle[]PROGMEM={ 460b0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 470x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x7,0xf8,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x7f,0xff,0x80,0x0,0x0,0x0,0x0,0x0,0x3,0xff,0xff,0xf0,0x0,0x0,0x0, 480x0,0x0,0xf,0xff,0xff,0xfc,0x0,0x0,0x0,0x0,0x0,0x1f,0xff,0xff,0xfe,0x0,0x0,0x0,0x0,0x0,0x7f,0xf0,0x3,0xff,0x80,0x0,0x0,0x0,0x0,0xff,0x80,0x0,0x7f,0xc0,0x0,0x0,0x0,0x1,0xfe,0x0,0x0,0x1f,0xe0,0x0,0x0, 490x0,0x3,0xf8,0x0,0x0,0x7,0xf0,0x0,0x0,0x0,0x7,0xf0,0x0,0x0,0x3,0xf8,0x0,0x0,0x0,0xf,0xe0,0x0,0x0,0x1,0xfc,0x0,0x0,0x0,0xf,0xc0,0x0,0x0,0x0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x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 74}; 75 76 77void setup() 78{ 79myTouch.InitTouch(); 80 myTouch.setPrecision(PREC_HI); 81 tft.begin(); 82 tft.setRotation(3); 83} 84 85void loop() 86{ 87 int board[9] = {0,0,0,0,0,0,0,0,0}; 88 89 drawStartScreen(); 90 do{ 91 myTouch.read(); 92 x = myTouch.getX(); //Get touch point 93 y = myTouch.getY(); 94 delay(100); 95 }while(!(buttons[0].contains(x,y))); 96 97 tft.fillScreen(BLACK); 98 //Draw white frame 99 tft.drawRect(0,0,320,240,WHITE); 100 //Print "Tic Tac Toe" Text 101 tft.setCursor(30,30); 102 tft.setTextColor(WHITE); 103 tft.setTextSize(4); 104 tft.print("Tic Tac Toe"); 105 //Print "YouTube!" text 106 tft.setCursor(30,100); 107 tft.setTextColor(GREEN); 108 tft.setTextSize(4); 109 tft.print("Who begins"); 110 //Create Red Button 111 // create buttons // x, y, w, h, outline, fill, text 112 buttons[0].initButton(&tft, 100,200,80,40,WHITE, RED, WHITE, 113 you, 2); 114 buttons[0].drawButton(); 115 // create buttons // x, y, w, h, outline, fill, text 116 buttons[1].initButton(&tft, 200,200,80,40,WHITE, RED, WHITE, 117 cpu, 2); 118 buttons[1].drawButton(); 119 b=1; 120 do{ 121 myTouch.read(); 122 x = myTouch.getX(); //Get touch point 123 y = myTouch.getY(); 124 if (buttons[0].contains(x,y)) 125 { 126 b=0; 127 player = 1; 128 tft.fillScreen(BLACK); 129 //Draw white frame 130 tft.drawRect(0,0,320,240,WHITE); 131 tft.setCursor(30,30); 132 tft.setTextColor(WHITE); 133 tft.setTextSize(2); 134 tft.print("Ok, you begin."); 135 delay(2000); 136 } 137 if(buttons[1].contains(x,y)) 138 { 139 b=0; 140 player = 2; 141 tft.fillScreen(BLACK); 142 //Draw white frame 143 tft.drawRect(0,0,320,240,WHITE); 144 tft.setCursor(30,30); 145 tft.setTextColor(WHITE); 146 tft.setTextSize(2); 147 tft.print("Ok, CPU begins."); 148 delay(2000); 149 } 150 }while(b); 151 152 tft.fillScreen(BLACK); 153 tft.drawRect(0,0,319,240,WHITE); 154 155 drawVerticalLine(125); 156 drawVerticalLine(195); 157 drawHorizontalLine(80); 158 drawHorizontalLine(150); 159 160 for(turn = 0; turn < 9 && win(board) == 0; ++turn) { 161 if((turn+player) % 2 == 0) 162 computerMove(board); 163 else { 164 //draw(board); 165 playerMove(board); 166 } 167 } 168 switch(win(board)) { 169 case 0: 170 Serial.println("It's a draw.\ 171"); 172 break; 173 case 1: 174 //draw(board); 175 Serial.println("You lose.\ 176"); 177 break; 178 case -1: 179 Serial.println("You win!\ 180"); 181 break; 182 } 183 do{}while(myTouch.dataAvailable()==false); 184 //x = map(tp.y, 930, 88, 0, 480); 185 //y = map(tp.x, 908, 125, 0, 320); 186 //delay(250);} 187 } 188void drawHorizontalLine(int y) 189{ 190 int i=0; 191 for(i=0;i<7;i++) 192 { 193 tft.drawLine(60,y+i,270,y+i,WHITE); 194 } 195} 196 197void drawVerticalLine(int x) 198{ 199 int i=0; 200 for(i=0;i<7;i++) 201 { 202 tft.drawLine(x+i,20,x+i,220,WHITE); 203 } 204} 205void drawStartScreen() 206{ 207 tft.fillScreen(BLACK); 208 209 //Draw white frame 210 tft.drawRect(0,0,319,240,WHITE); 211 212 //Print "Tic Tac Toe" Text 213 tft.setCursor(30,100); 214 tft.setTextColor(WHITE); 215 tft.setTextSize(4); 216 tft.print("Tic Tac Toe"); 217 218 //Print "YouTube!" text 219 tft.setCursor(80,30); 220 tft.setTextColor(GREEN); 221 tft.setTextSize(4); 222 tft.print("Arduino"); 223 224 buttons[0].initButton(&tft, 160,200,200,40,WHITE, RED, WHITE, start, 2); 225 buttons[0].drawButton(); 226 227} 228 229void drawCircle(int x, int y) 230{ 231 drawBitmap(x,y,circle,65,65,RED); 232} 233 234void drawX(int x, int y) 235{ 236 drawBitmap(x,y,x_bitmap,65,65,BLUE); 237} 238void drawBitmap(int16_t x, int16_t y, 239 const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) { 240 241 int16_t i, j, byteWidth = (w + 7) / 8; 242 uint8_t byte; 243 244 for(j=0; j<h; j++) { 245 for(i=0; i<w; i++) { 246 if(i & 7) byte <<= 1; 247 else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8); 248 if(byte & 0x80) tft.drawPixel(x+i, y+j, color); 249 } 250 } 251} 252void computerMove(int board[9]) { 253 int move = -1; 254 int score = -2; 255 int i; 256 for(i = 0; i < 9; ++i) { 257 if(board[i] == 0) { 258 board[i] = 1; 259 int tempScore = -minimax(board, -1, 0); 260 board[i] = 0; 261 if(tempScore > score) { 262 score = tempScore; 263 move = i; 264 } 265 } 266 } 267 //returns a score based on minimax tree at a given node. 268 board[move] = 1; 269 drawCpuMove(move); 270 delay(100); 271} 272 273void playerMove(int board[9]) { 274 int move = 0; 275 b = 1; 276 do{ 277 if (myTouch.dataAvailable() == true) 278 { 279 myTouch.read(); 280 x = myTouch.getX(); //Get touch point 281 y = myTouch.getY(); 282 283 if((x>55 && x<125)&& (y>0 && y<80)) //0 284 { 285 if(board[0]==0) 286 { 287 move=0; 288 b=0; 289 } 290 } 291 else if((x>125 && x<195) && (y>0 && y<80)) //1 292 { 293 294 if(board[1]==0) 295 { 296 move=1; 297 b=0; 298 } 299 } 300 else if((x>195&&x<265)&& (y>0 && y<80)) //2 301 { 302 if(board[2]==0) 303 { 304 move=2; 305 b=0; 306 } 307 } 308 309 else if((x>55 && x<125)&& (y>80 && y<155)) //3 310 { 311 if(board[3]==0) 312 { 313 move=3; 314 b=0; 315 } 316 } 317 318 else if((x>125 && x<195)&& (y>80 && y<155)) //4 319 { 320 if(board[4]==0) 321 { 322 move=4; 323 b=0; 324 } 325 } 326 327 else if((x>195&&x<265)&& (y>80 && y<155)) //5 328 { 329 if(board[5]==0) 330 { 331 move=5; 332 b=0; 333 } 334 } 335 336 else if((x>55 && x<125)&& (y>155 && y<235)) //6 337 { 338 if(board[6]==0) 339 { 340 move=6; 341 b=0; 342 } 343 } 344 345 else if((x>125 && x<195)&& (y>155 && y<235)) //7 346 { 347 if(board[7]==0) 348 { 349 move=7; 350 b=0; 351 } 352 } 353 354 else if((x>195&&x<265)&& (y>155 && y<235)) //8 355 { 356 if(board[8]==0) 357 { 358 move=8; 359 b=0; 360 } 361 } 362 363 } 364 }while(b); 365 board[move] = -1; 366 drawPlayerMove(move); 367 delay(100); 368} 369int win(const int board[9]) { 370 //list of possible winning positions 371 unsigned wins[8][3] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}}; 372 int winPos; 373 for(winPos = 0; winPos < 8; ++winPos) { 374 if(board[wins[winPos][0]] != 0 && board[wins[winPos][0]] == board[wins[winPos][1]] && board[wins[winPos][0]] == board[wins[winPos][2]]) 375 return board[wins[winPos][2]]; 376 } 377 return 0; 378} 379 380int minimax(int board[9], int player, int depth) { 381 //check the positions for players 382 int winner = win(board); 383 if(winner != 0) return winner*player; 384 385 int move = -1; 386 int score = -2; 387 int i; 388 for(i = 0; i < 9; ++i) { 389 390 if(board[i] == 0) { 391 board[i] = player; 392 int thisScore=0; 393 if (depth<difficulty){ 394 thisScore = -minimax(board, player*-1,depth+1); 395 } 396 397 if(thisScore > score) { 398 score = thisScore; 399 move = i; 400 } 401 //choose the worst move for opponent 402 board[i] = 0; 403 } 404 } 405 if(move == -1) return 0; 406 return score; 407} 408void drawCpuMove(int move) 409{ 410 switch(move) 411 { 412 case 0: drawCircle(55,15); break; 413 case 1: drawCircle(130,15); break; 414 case 2: drawCircle(205,15); break; 415 case 3: drawCircle(55,85); break; 416 case 4: drawCircle(130,85); break; 417 case 5: drawCircle(205,85); break; 418 case 6: drawCircle(55,155); break; 419 case 7: drawCircle(130,155);break; 420 case 8: drawCircle(205,155);break; 421 } 422} 423 424void drawPlayerMove(int move) 425{ 426 switch(move) 427 { 428 case 0: drawX(55,15); break; 429 case 1: drawX(130,15); break; 430 case 2: drawX(205,15); break; 431 case 3: drawX(55,85); break; 432 case 4: drawX(130,85); break; 433 case 5: drawX(205,85); break; 434 case 6: drawX(55,155); break; 435 case 7: drawX(130,155);break; 436 case 8: drawX(205,155);break; 437 } 438} 439 440
Downloadable files
TicTacToe
Wiring
TicTacToe
TicTacToe
Wiring
TicTacToe
Comments
Only logged in users can leave comments