Halo, saya ingin berbagi pekerjaan saya di proyek dari nickthegreek82:arduino-touch-tic-tac-toe-game.
Memodifikasi KI dan beberapa pengoptimalan kode.
Beberapa pengoptimalan kode.
menemukan cara yang lebih baik untuk A.I.
Kode
- TIC TAC TOE
- TicTacToe_new
TIC TAC TOEArduino
Gim TIC TAC TOE sederhana untuk dimainkan melawan komputer /////////////////////////////////////////////////// ///////// // 2.8" TOUCH SCREEN TIC TAC TOE // // // // http://www.educ8s.tv // // // // dimodifikasi 08.2017 oleh rom3 // / //////////////////////////////////////////#include #include #include #include #include #define TFT_RST 8#define TFT_DC 9#define TFT_CS 10#define TOUCH_ORIENTATION LANDSCAPEDAfruit_LI93LI9341Adafruit (TFT_CS, TFT_DC, TFT_RST);Tombol Adafruit_GFX_Button[9];URTouch myTouch(30, 28, 26, 24, 22); gerakan int pendek tanpa tanda =1; pemenang int pendek tanpa tanda =0; //0 =Seri, 1 =Manusia, 2 =CPUunsigned short int x,y; // posisi touchunsigned short int kursor; // Cursor positionunsigned short int firstMoves[]={0,2,6,8}; // akan menggunakan posisi ini terlebih dahulu, jika CPU mulai tidak ditandatangani short int board[] ={0,0,0,0,0,0,0,0,0};// menahan data posisi 0 kosong, 1 manusia, 2 pemain computerbool; // true=ANDA, false =CPUbo ol b=1; char start[10] ={"Gamestart"};char you[4] ={"ANDA"};char cpu[4] ={"CPU"};char again[7] ={"Lagi?"}; const unsigned char circle[]PROGMEM={0b0,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,0x0 ,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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0x0,0x7f,0xf,0xf 0x1f,0xff,0xff,0x0,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,0x3,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,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,0x0};pengaturan batal() { myTouch.InitTouch(); myTouch.setPrecision(PREC_HI); tft.mulai(); tft.setRotation(3); tft.fillScreen(ILI9341_BLACK); //Menggambar bingkai putih tft.drawRect(0,0,320,240,ILI9341_WHITE); //Mencetak teks "Arduino" tft.setCursor(80,30); tft.setTextColor(ILI9341_GREEN); tft.setTextSize(4); tft.print("Arduino"); //Cetak Teks "Tic Tac Toe" tft.setCursor(30.100); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(4); tft.print("Tic Tac Toe"); //Buat Tombol Merah // buat tombol // x, y, w, h, garis besar, isi, tombol teks[0].initButton(&tft, 160,200,200,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, start, 2); tombol[0].drawButton();lakukan{ waitonetouch(&x,&y); if (buttons[0].contains(x,y)) b=0;}sementara(b>0); }void loop() { for(int short int i=0;i<9;i++) { board[i]=0; } tft.fillScreen(ILI9341_BLACK); //Menggambar bingkai putih tft.drawRect(0,0,320,240,ILI9341_WHITE); //Mencetak Teks "Tic Tac Toe" tft.setCursor(30,30); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(4); tft.print("Tic Tac Toe"); //Cetak "YouTube!" teks tft.setCursor(30.100); tft.setTextColor(ILI9341_GREEN); tft.setTextSize(4); tft.print("Siapa yang memulai"); //Buat Tombol Merah // buat tombol // x, y, w, h, garis besar, isi, tombol teks[0].initButton(&tft, 100,200,80,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, you, 2); tombol[0].drawButton(); // buat tombol // x, y, w, h, garis besar, isi, tombol teks[1].initButton(&tft, 200,200,80,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, cpu, 2); tombol[1].drawButton(); b=1; lakukan{ waitonetouch(&x,&y); if (buttons[0].contains(x,y)) { b=0; pemain =1; tft.fillScreen(ILI9341_BLACK); //Menggambar bingkai putih tft.drawRect(0,0,320,240,ILI9341_WHITE); tft.setCursor(30,30); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2); tft.print("Oke, Anda mulai."); penundaan(2000); } if(buttons[1].contains(x,y)) { b=0; pemain =0; tft.fillScreen(ILI9341_BLACK); //Menggambar bingkai putih tft.drawRect(0,0,320,240,ILI9341_WHITE); tft.setCursor(30,30); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2); tft.print("Oke, CPU dimulai."); penundaan(2000); } }sementara(b>0); b=1; bergerak =1; pemenang =0; kursor =10; tft.fillScreen(ILI9341_BLACK); //Gambar bingkai tft.drawRect(0,0,320,240,ILI9341_WHITE); //Menggambar garis vertikal tft.fillRect(80,15,5,205,ILI9341_WHITE); tft.fillRect(150,15,5,205,ILI9341_WHITE); //Menggambar garis horizontal tft.fillRect(15,80,205,5,ILI9341_WHITE); tft.fillRect(15,150,205,5,ILI9341_WHITE); tft.drawRect(230,10,80,220,ILI9341_WHITE); { lakukan { if(moves%2==1 &&player==false || move%2==0 &&player==true) { kursor+=10; tft.setCursor(240,kursor); tft.setTextSize(1); tft.println("Saya menggambar"); arduinoMove(); cekPemenang(); }else { kursor+=10; tft.setCursor(240,kursor); tft.setTextSize(1); tft.println("Langkah Anda"); pemainBergerak(); cekPemenang(); } penundaan(1000); bergerak++; }sementara (pemenang==0 &&bergerak<10); waitonetouch(&x,&y); tft.fillScreen(ILI9341_BLACK); //Gambar bingkai tft.drawRect(0,0,320,240,ILI9341_WHITE); //Cetak Teks "Game Over" tft.setCursor(50,30); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(4); tft.print("PERMAINAN SELESAI"); if(pemenang ==0){ //Cetak "DRAW!" teks tft.setCursor(110.100); tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(4); tft.print("DRAW");} if(pemenang ==1){ //Cetak "MANUSIA MENANG!" teks tft.setCursor(40.100); tft.setTextColor(ILI9341_BLUE); tft.setTextSize(4); tft.print("MANUSIA MENANG");} if(pemenang ==2){ //Cetak "CPU MENANG!" teks tft.setCursor(60.100); tft.setTextColor(ILI9341_RED); tft.setTextSize(4); tft.print("CPU MENANG");} // buat tombol // x, y, w, h, garis besar, isi, tombol teks[0].initButton(&tft, 160,200,200,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, lagi , 2); tombol[0].drawButton(); lakukan{ waitonetouch(&x,&y); if (buttons[0].contains(x,y)) b=0; }sementara(b>0); }} void playerMove(){ waitonetouch(&x,&y); if((x>15 &&x<85) &&(y>15 &&y<85)) //0 { if(papan[0]==0) { papan[0]=1; drawX(15,15); } kesalahan lain(); } else if((x>85 &&x<155)&&(y>15 &&y<85)) ///1 { if(papan[1]==0) { papan[1]=1; drawX(85,15); } kesalahan lain(); } if((x>155 &&x<225)&&(y>15 &&y<85)) //2 { if(papan[2]==0) { papan[2]=1; drawX(155,15); } kesalahan lain(); } if((x>15 &&x<85)&&(y>85 &&y<155)) ///3 { if(papan[3]==0) { papan[3]=1; drawX(15,85); } kesalahan lain(); } if((x>85 &&x<155)&&(y>85 &&y<155)) //4 { if(papan[4]==0) { papan[4]=1; drawX(85,85); } kesalahan lain(); } if((x>155 &&x<225)&&(y>85 &&y<155)) //5 { if(papan[5]==0) { papan[5]=1; drawX(155,85); } kesalahan lain(); } if((x>15 &&x<85)&&(y>155 &&y<225)) //6 { if(papan[6]==0) { papan[6]=1; drawX(15.155); } kesalahan lain(); } if((x>85 &&x<155)&&(y>155 &&y<225)) //7 { if(papan[7]==0) { papan[7]=1; drawX(85.155); } kesalahan lain(); } if((x>155 &&x<225)&&(y>155 &&y<225)) //8 { if(papan[8]==0) { papan[8]=1; drawX(155.155); } kesalahan lain(); }}kesalahan batal(){ kursor+=10; tft.setCursor(240,kursor); tft.setTextSize(1); tft.println("Error"); penundaan (1000); kursor+=10; tft.setCursor(240,kursor); tft.println("Coba lagi."); playerMove();}unsigned short int checkOpponent(unsigned short int x, unsigned short int y){ if(board[1]==x &&board[2]==x &&board[0]==y){ if( y==0){ kembali 0; } lain kembali 1; } if(board[3]==x &&board[6]==x &&board[0]==y){ if(y==0){ return 0; } lain kembali 6; } if(board[0]==y &&board[4]==x &&board[8]==x){ if(y==0){ return 0; } lain kembali 8; } if(board[0]==x &&board[1]==y &&board[2]==x){ if(y==0){ return 1; } lain kembali 0; } if(board[4]==x &&board[7]==x &&board[1]==y){ if(y==0){ return 1; } lain kembali 7; } if(board[0]==x &&board[1]==x &&board[2]==y){ if(y==0){ return 2; } lain kembali 0; } if(board[5]==x &&board[8]==x &&board[2]==y){ if(y==0){ return 2; } lain kembali 8; } if(board[4]==x &&board[6]==x &&board[2]==y){ if(y==0){ return 2; } lain kembali 6; } if(board[4]==x &&board[5]==x &&board[3]==y){ if(y==0){ return 3; } lain kembali 5; } if(board[0]==x &&board[6]==x &&board[3]==y){ if(y==0){ return 3; } lain kembali 6; } if(board[3]==x &&board[4]==y &&board[5]==x){ if(y==0){ return 4; } lain kembali 3; } if(papan[1]==x &&papan[7]==x &&papan[4]==y){ if(y==0){ return 4; } lain kembali 1; } if(board[0]==x &&board[8]==x &&board[4]==y){ if(y==0){ return 4; } lain kembali 0; } if(board[2]==x &&board[6]==x &&board[4]==y){ if(y==0){ return 4; } lain kembali 2; } if(board[3]==x &&board[4]==x &&board[5]==y){ if(y==0){ return 5; } lain kembali 3; } if(board[2]==x &&board[8]==x &&board[5]==y){ if(y==0){ return 5; } lain kembali 2; } if(board[7]==x &&board[8]==x &&board[6]==y){ if(y==0){ return 6; } lain kembali 8; } if(board[0]==x &&board[3]==x &&board[6]==y){ if(y==0){ return 6; } lain kembali 0; } if(board[4]==x &&board[2]==x &&board[6]==y){ if(y==0){ return 6; } lain kembali 2; } if(board[1]==x &&board[4]==x &&board[7]==y){ if(y==0){ return 7; } lain kembali 1; } if(board[6]==x &&board[8]==x &&board[7]==y){ if(y==0){ return 7; } lain kembali 6; } if(board[6]==x &&board[7]==x &&board[8]==y){ if(y==0){ return 8; } lain kembali 6; } if(board[5]==x &&board[2]==x &&board[8]==y){ if(y==0){ return 8; } lain kembali 2; } if(board[4]==x &&board[0]==x &&board[8]==y){ if(y==0){ return 8; } lain kembali 0; } else return 100;}void arduinoMove(){ if(moves<2) { do{ unsigned short int randomMove =random(4); unsigned short int c=firstMoves[randomMove]; if(papan[c]==0){ papan[c]=2; drawCpuMove(FirstMoves[randomMove]); kembali; } }sementara(bergerak<2); } if(bergerak<3) { if(papan[4]==0) { papan[4]=2; drawCpuMove(4); kembali; } } unsigned short int twoPlayer =checkOpponent(2,0);//CPU dua berturut-turut? if(twoPlayer <9) { board[twoPlayer]=2; drawCpuMove(dua Pemain); kembali; } unsigned short int nextMove =checkOpponent(1,0);//Pemain dua berturut-turut? if(nextMove <9) { board[nextMove]=2; drawCpuMove(berikutnyaMove); kembali; } unsigned short int next =checkOpponent(0,2); if(berikutnya <9) { papan[berikutnya]=2; drawCpuMove(berikutnya); kembali; } lakukan { unsigned short int randomMove =random(9); if (board[randomMove]==0) { board[randomMove]=2; drawCpuMove(Pindahkan secara acak); kembali; } } while(nextMove ==100);} void drawCircle(int pendek tidak bertanda x, int pendek tidak bertanda y){ tft.drawBitmap(x,y,lingkaran,65,65,ILI9341_RED);}void drawX(int pendek tidak ditandatangani x , unsigned short int y){ tft.drawBitmap(x,y,x_bitmap,65,65,ILI9341_BLUE);}void drawCpuMove(unsigned short int move){ switch(move) { case 0:drawCircle(15,15); merusak; kasus 1:drawCircle(85,15); merusak; kasus 2:drawCircle(155,15); merusak; kasus 3:drawCircle(15,85); merusak; kasus 4:drawCircle(85,85); merusak; kasus 5:drawCircle(155,85); merusak; kasus 6:drawCircle(15.155); merusak; kasus 7:drawCircle(85.155);break; kasus 8:drawCircle(155.155);break; }}void checkWinner() // memeriksa papan untuk melihat apakah ada pemenang// tempat menghasilkan variabel global 'winner'{ // tidak menang? if (papan[0]>0 &&papan[1]==papan[0] &&papan[2]==papan[0]) { winner=papan[0]; tft.fillRect(15, 45, 205, 5, ILI9341_WHITE); } if (papan[3]>0 &&papan[4]==papan[3] &&papan[5]==papan[3]) { winner=papan[3]; tft.fillRect(15, 115, 205, 5, ILI9341_WHITE); } if (papan[6]>0 &&papan[7]==papan[6] &&papan[8]==papan[6]) { winner=papan[6]; tft.fillRect(15, 185, 205, 5, ILI9341_WHITE); } if (papan[0]>0 &&papan[3]==papan[0] &&papan[6]==papan[0]) { winner=papan[0]; tft.fillRect(45, 15, 5, 205, ILI9341_WHITE); } if (papan[1]>0 &&papan[4]==papan[1] &&papan[7]==papan[1]) { winner=papan[1]; tft.fillRect(115, 15, 5, 205, ILI9341_WHITE); } if (papan[2]>0 &&papan[5]==papan[2] &&papan[8]==papan[2]) { winner=papan[2]; tft.fillRect(185, 15, 5, 205, ILI9341_WHITE); } if (papan[0]>0 &&papan[4]==papan[0] &&papan[8]==papan[0]) { winner=papan[0]; for(int short int xx=0;xx<6;xx++){ tft.drawLine(20+xx, 20, 210+xx, 210, ILI9341_WHITE); } } if (papan[2]>0 &&papan[4]==papan[2] &&papan[6]==papan [2]) { winner=papan [2]; for(int short int xx=0;xx<6;xx++){ tft.drawLine(210+xx, 20, 20+xx, 210, ILI9341_WHITE); } }}void waitonetouch(int pendek tidak bertanda *x,int pendek tidak bertanda *y){ do { delay(10); if (myTouch.dataAvailable() ==true) { myTouch.read(); *x =myTouch.getX(); //Dapatkan titik sentuh *y =myTouch.getY(); kembali; } }sementara(myTouch.dataAvailable()==false); }
TicTacToe_newArduino
Pemain tunggal Tic Tac Toe yang tak terkalahkan di Arduino Due ////////////////////////////////////////////////////// //////// // 2.8" TOUCH SCREEN TIC TAC TOE // // // // http://www.educ8s.tv // // // // dimodifikasi 08.2017 oleh rom3 // // /////////////////////////////////////////#include #include #include #include #define TFT_RST 8#define TFT_DC 9#define TFT_CS 10#define TOUCH_ORIENTATION LANDSCAPEAdafruit_ILI9341 tft =Adafruit_FT_ILI9341(TFX_Butt_ILI9341) [9];URTouch myTouch(30, 28, 26, 24, 22);char start[10] ={"Gamestart"};char you[4] ={"ANDA"};char cpu[4] ={" CPU"};char again[7] ={"Lagi?"};int player;int kesulitan=8;int pendek yang tidak ditandatangani x,y,b; // position touch#define BLACK 0x0000#define BLUE 0x001F#define RED 0xF800 #define GREEN 0x07E0#define CYAN 0x07FF#define MAGENTA 0xF81F#define YELLOW 0xFFE0#define WHITE 0xFFFFint board[]={0,0,0,0,0,0,0,0,0};// menyimpan data posisi 0 kosong, 1 manusia, 2 giliran tanpa tanda komputer;c pada lingkaran karakter yang tidak ditandatangani[]PROGMEM={0b0,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,0x0 ,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 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 ,0xff,0xff,0x0,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,0x3,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,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,0x0};pengaturan batal(){myTouch.InitTouch(); myTouch.setPrecision(PREC_HI); tft.mulai(); tft.setRotation(3);}void loop(){ int board[9] ={0,0,0,0,0,0,0,0,0}; drawStartScreen(); lakukan{ myTouch.read(); x =myTouch.getX(); //Dapatkan titik sentuh y =myTouch.getY(); penundaan(100); } while(!(buttons[0].contains(x,y))); tft.fillScreen(HITAM); //Menggambar bingkai putih tft.drawRect(0,0,320,240,PUTIH); //Mencetak Teks "Tic Tac Toe" tft.setCursor(30,30); tft.setTextColor(PUTIH); tft.setTextSize(4); tft.print("Tic Tac Toe"); //Cetak "YouTube!" teks tft.setCursor(30.100); tft.setTextColor(HIJAU); tft.setTextSize(4); tft.print("Siapa yang memulai"); //Buat Tombol Merah // buat tombol // x, y, w, h, garis besar, isi, tombol teks[0].initButton(&tft, 100,200,80,40,WHITE, RED, WHITE, you, 2); tombol[0].drawButton(); // buat tombol // x, y, w, h, outline, fill, tombol teks[1].initButton(&tft, 200,200,80,40,WHITE, RED, WHITE, cpu, 2); tombol[1].drawButton(); b=1; lakukan{ myTouch.read(); x =myTouch.getX(); //Dapatkan titik sentuh y =myTouch.getY(); if (buttons[0].contains(x,y)) { b=0; pemain =1; tft.fillScreen(HITAM); //Menggambar bingkai putih tft.drawRect(0,0,320,240,PUTIH); tft.setCursor(30,30); tft.setTextColor(PUTIH); tft.setTextSize(2); tft.print("Oke, Anda mulai."); penundaan(2000); } if(buttons[1].contains(x,y)) { b=0; pemain =2; tft.fillScreen(HITAM); //Menggambar bingkai putih tft.drawRect(0,0,320,240,PUTIH); tft.setCursor(30,30); tft.setTextColor(PUTIH); tft.setTextSize(2); tft.print("Oke, CPU dimulai."); penundaan(2000); } }sementara(b); tft.fillScreen(HITAM); tft.drawRect(0,0,319,240,PUTIH); drawVerticalLine(125); drawVerticalLine(195); drawHorizontalLine(80); drawHorizontalLine(150); for(turn =0; turn <9 &&win(board) ==0; ++turn) { if((turn+player) % 2 ==0) computerMove(board); else { //gambar(papan); playerMove(papan); } } switch(win(board)) { kasus 0:Serial.println("Ini seri.\n"); merusak; kasus 1://draw(papan); Serial.println("Kamu kalah.\n"); merusak; case -1:Serial.println("Kamu menang!\n"); merusak; } lakukan{}sementara(myTouch.dataAvailable()==false); //x =peta(tp.y, 930, 88, 0, 480); //y =peta(tp.x, 908, 125, 0, 320); //delay(250);} }void drawHorizontalLine(int y){ int i=0; untuk(i=0;i<7;i++) { tft.drawLine(60,y+i,270,y+i,PUTIH); }}kosongkan drawVerticalLine(int x){ int i=0; for(i=0;i<7;i++) { tft.drawLine(x+i,20,x+i,220,PUTIH); }}void drawStartScreen(){ tft.fillScreen(HITAM); //Menggambar bingkai putih tft.drawRect(0,0,319,240,PUTIH); //Cetak Teks "Tic Tac Toe" tft.setCursor(30.100); tft.setTextColor(PUTIH); tft.setTextSize(4); tft.print("Tic Tac Toe"); //Cetak "YouTube!" teks tft.setCursor(80,30); tft.setTextColor(HIJAU); tft.setTextSize(4); tft.print("Arduino"); tombol[0].initButton(&tft, 160,200,200,40,PUTIH, MERAH, PUTIH, mulai, 2); tombol[0].drawButton(); }void drawCircle(int x, int y){ drawBitmap(x,y,circle,65,65,RED);}void drawX(int x, int y){ drawBitmap(x,y,x_bitmap,65,65,BLUE );}void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) { int16_t i, j, byteWidth =(w + 7) / 8; uint8_t byte; for(j=0; j skor) { skor =tempScore; bergerak =saya; } } } //mengembalikan skor berdasarkan pohon minimax pada node tertentu. papan[bergerak] =1; drawCpuMove(bergerak); delay(100);}void playerMove(int board[9]) { int move =0; b =1; lakukan{ if (myTouch.dataAvailable() ==true) { myTouch.read(); x =myTouch.getX(); //Dapatkan titik sentuh y =myTouch.getY(); if((x>55 &&x<125)&&(y>0 &&y<80)) //0 { if(papan[0]==0) { move=0; b=0; } } else if((x>125 &&x<195) &&(y>0 &&y<80)) //1 { if(papan[1]==0) { move=1; b=0; } } else if((x>195&&x<265)&&(y>0 &&y<80)) //2 { if(papan[2]==0) { move=2; b=0; } } else if((x>55 &&x<125)&&(y>80 &&y<155)) //3 { if(papan[3]==0) { move=3; b=0; } } else if((x>125 &&x<195)&&(y>80 &&y<155)) //4 { if(papan[4]==0) { pindah=4; b=0; } } else if((x>195&&x<265)&&(y>80 &&y<155)) //5 { if(papan[5]==0) { pindah=5; b=0; } } else if((x>55 &&x<125)&&(y>155 &&y<235)) //6 { if(papan[6]==0) { pindah=6; b=0; } } else if((x>125 &&x<195)&&(y>155 &&y<235)) //7 { if(papan[7]==0) { move=7; b=0; } } else if((x>195&&x<265)&&(y>155 &&y<235)) //8 { if(papan[8]==0) { pindah=8; b=0; } } } }sementara(b); papan[bergerak] =-1; drawPlayerMove(bergerak); delay(100);}int win(const int board[9]) { //daftar posisi pemenang yang mungkin menang tanpa tanda tangan[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}}; int winPos; for(winPos =0; winPos <8; ++winPos) { if(board[wins[winPos][0]] !=0 &&board[wins[winPos][0]] ==board[wins[winPos][ 1]] &&board[wins[winPos][0]] ==board[wins[winPos][2]]) return board[wins[winPos][2]]; } return 0;}int minimax(int board[9], int player, int depth) { //memeriksa posisi pemain int winner =win(board); if(pemenang !=0) mengembalikan pemenang*pemain; int bergerak =-1; int skor =-2; di aku; for(i =0; i <9; ++i) { if(papan[i] ==0) { papan[i] =pemain; int Skor ini=0; if (kedalaman skor) { skor =Skor ini; bergerak =saya; } //pilih langkah terburuk untuk papan lawan[i] =0; } } if(pindahkan ==-1) kembalikan 0; skor kembali;}void drawCpuMove(int move){ switch(move) { case 0:drawCircle(55,15); merusak; kasus 1:drawCircle(130,15); merusak; kasus 2:drawCircle (205,15); merusak; kasus 3:drawCircle(55,85); merusak; kasus 4:drawCircle(130,85); merusak; kasus 5:drawCircle(205.85); merusak; kasus 6:drawCircle(55.155); merusak; kasus 7:drawCircle(130,155);break; kasus 8:drawCircle(205.155);break; }}void drawPlayerMove(int move){ switch(move) { case 0:drawX(55,15); merusak; kasus 1:drawX(130,15); merusak; kasus 2:drawX (205,15); merusak; kasus 3:drawX(55,85); merusak; kasus 4:drawX(130,85); merusak; kasus 5:drawX(205.85); merusak; kasus 6:drawX(55.155); merusak; kasus 7:drawX(130,155);break; kasus 8:drawX(205.155);break; }}
Skema
pengkabelan