亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Manufacturing Technology >> 制造工艺

Arduino Due TIC TAC TOE 带触摸屏

组件和用品

Arduino Due
× 1
2,8 zoll TFT LCD 带触摸屏
× 1
跳线(通用)
× 1

应用和在线服务

Arduino IDE

关于这个项目

关于

你好,我想分享我在 nickthegreek82 项目上的工作:arduino-touch-tic-tac-toe-game。

你需要:

  • Adafruit 图书馆
  • GFX 库
  • 和 URTouch 库

接线

触摸屏接线如下:

  • 触摸 -> Arduino Due
  • T_IRQ -> 22
  • T_DO -> 24
  • T_DIN -> 26
  • T_CS -> 28
  • T_CLK -> 30

对于显示器:

  • 显示 -> Arduino Due
  • SDO -> A.25
  • LED -> 3.3V
  • SCK -> A.27
  • SDI -> A.26
  • DC -> 9
  • 重置 -> 8
  • CS -> 10
  • GND -> GND
  • VCC -> 3.3V

08.22.2017

修改了KI和一些代码优化。

08.26.2017

一些代码优化。

08.27.2017

你能战胜CPU吗?

03.13.2018

为人工智能找到了更好的方法

感谢 klauscam https://github.com/klauscam/ArduinoTicTacToe

代码

  • TIC TAC TOE
  • 井字游戏新版
TIC TAC TOEArduino
与计算机对战的简单 TIC TAC TOE 游戏
 ////////////////////////////////// ///////// // 2.8" 触摸屏 TIC TAC TOE // // // // http://www.educ8s.tv // // // // 2017 年 8 月由 rom3 修改 // / /////////////////////////////////////////#include #include #include #include #include #define TFT_RST 8#define TFT_DC 9#define TFT_CS 10#define TOUCH_ORIENTATION LANDSCAPEAdafruit_ILI94dafruit_ILI934dafruit (TFT_CS, TFT_DC, TFT_RST);Adafruit_GFX_Button 按钮[9];URTouch myTouch(30, 28, 26, 24, 22);unsigned short int move =1; unsigned short int Winner =0; //0 =Draw, 1 =Human, 2 =CPUunsigned short int x,y; // 位置 touchunsigned short int cursor; // 光标位置unsigned short int firstMoves[]={0,2,6,8}; // 将首先使用这些位置,如果 CPU 开始无符号short int board[] ={0,0,0,0,0,0,0,0,0};// 持有位置数据 0 为空,1 个人,2 为计算机玩家;// true=YOU,false =CPUbo ol b=1; char start[10] ={"Gamestart"};char you[4] ={"YOU"};char cpu[4] ={"CPU"};char [7] ={"Again?"}; 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,0x7,0xf8,0x0,0x ,0x0,0x0,0x0,0x0,0x7f,0xff,0x80,0x0,0x0,0x0,0x0,0x0,0x3,0xff,0xff,0xf0,0x0,0x0,0x0,0x0,0x0,0xf,0xfc ,0x0,0x0,0x0,0x0,0x0,0x1f,0xff,0xff,0xfe,0x0,0x0,0x0,0x0,0x0,0x7f,0xf0,0x3,0xff,0x80,0x0,0x0,0x0,0x0x0,0x0,0x0x ,0x0,0x7f,0xc0,0x0,0x0,0x0,0x1,0xfe,0x0,0x0,0x1f,0xe0,0x0,0x0,0x0,0x3,0xf8,0x0,0x0,0x7,0xf0,0x0,0x0x ,0xf0,0x0,0x0,0x3,0xf8,0x0,0x0,0x0,0xf,0xe0,0x0,0x0,0x1,0xfc,0x0,0x0,0x0,0xf,0xc0,0x0,0x0,0x0,0xfc ,0x0,0x1f,0x80,0x0,0x0,0x0,0x7e,0x0,0x0,0x0,0x3f,0x0,0x0,0x0,0x0,0x3f,0x0,0x0,0x0,0x3e,0x0,0x0,0x0x ,0x0,0x0,0x0,0x7e,0x0,0x0,0x0,0x0,0x1f,0x80,0x0,0x0,0x7c,0x0,0x0,0x0,0x0,0xf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ff,0xff,0xff,0xc0,0x0,0x0,0x0,0x1,0xff,0xff,0xff,0xff,0xc0,0x0,0x0,0x0,0x3,0xff,0xff,0xff,0xff,0xe0,0,0x0,0x 0x3,0xff,0xff,0xff,0xff,0xe0,0x0,0x0,0x0,0x7,0xff,0xff,0xff,0xff,0xf0,0x0,0x0,0x0,0xf,0xff,0xff,0xff,0xff,0xf,0xff,0x0x 0x0,0x0,0xf,0xff,0xff,0xff,0xff,0xf8,0x0,0x0,0x0,0x1f,0xff,0xff,0x7f,0xff,0xfc,0x0,0x0,0x0,0x1f,0xff,0xf,0xff,0xf 0xfc,0x0,0x0,0x0,0x3f,0xff,0xfc,0x3f,0xff,0xfe,0x0,0x0,0x0,0x3f,0xff,0xfc,0x1f,0xff,0xfe,0x0,0x0,0x0,80xff 0x1f,0xff,0xff,0x0,0x0,0x0,0xff,0xff,0xf8,0xf,0xff,0xff,0x80,0x0,0x0,0xff,0xff,0xf0,0x7,0xff,0xff,0x80,1,0x0x 0xff,0xe0,0x7,0xff,0xff,0xc0,0x0,0x1,0xff,0xff,0xe0,0x3,0xff,0xff,0xc0,0x0,0x3,0xff,0xff,0xc0,0x3,0xff,0x0ff 0x3,0xff,0xff,0xc0,0x1,0xff,0xff,0xe0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 ,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 ,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};void setup() { myTouch.InitTouch(); myTouch.setPrecision(PREC_HI); tft.begin(); tft.setRotation(3); tft.fillScreen(ILI9341_BLACK); //绘制白框 tft.drawRect(0,0,320,240,ILI9341_WHITE); //打印“Arduino”文本 tft.setCursor(80,30); tft.setTextColor(ILI9341_GREEN); tft.setTextSize(4); tft.print("Arduino"); //打印“井字游戏”文本 tft.setCursor(30,100); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(4); tft.print("井字游戏"); //创建红色按钮 // 创建按钮 // x、y、w、h、轮廓、填充、文本按钮[0].initButton(&tft, 160,200,200,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, start, 2); button[0].drawButton();do{ waitonetouch(&x,&y); if (buttons[0].contains(x,y)) b=0;}while(b>0); }void loop() { for(unsigned short int i=0;i<9;i++) { board[i]=0; } tft.fillScreen(ILI9341_BLACK); //绘制白框 tft.drawRect(0,0,320,240,ILI9341_WHITE); //打印“井字游戏”文本 tft.setCursor(30,30); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(4); tft.print("井字游戏"); //打印“YouTube!”文本 tft.setCursor(30,100); tft.setTextColor(ILI9341_GREEN); tft.setTextSize(4); tft.print("谁开始"); //创建红色按钮 // 创建按钮 // x、y、w、h、轮廓、填充、文本按钮[0].initButton(&tft, 100,200,80,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, you, 2);按钮[0].drawButton(); // 创建按钮 // x、y、w、h、轮廓、填充、文本按钮[1].initButton(&tft, 200,200,80,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, cpu, 2);按钮[1].drawButton(); b=1;做{ waitonetouch(&x,&y); if (buttons[0].contains(x,y)) { b=0;玩家 =1; tft.fillScreen(ILI9341_BLACK); //绘制白框 tft.drawRect(0,0,320,240,ILI9341_WHITE); tft.setCursor(30,30); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2); tft.print("好的,开始吧。");延迟(2000); } if(buttons[1].contains(x,y)) { b=0;玩家 =0; tft.fillScreen(ILI9341_BLACK); //绘制白框 tft.drawRect(0,0,320,240,ILI9341_WHITE); tft.setCursor(30,30); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2); tft.print("好的,CPU 开始了。");延迟(2000); } }while(b>0); b=1;移动 =1;赢家 =0;光标 =10; tft.fillScreen(ILI9341_BLACK); //画框 tft.drawRect(0,0,320,240,ILI9341_WHITE); //画垂直线 tft.fillRect(80,15,5,205,ILI9341_WHITE); tft.fillRect(150,15,5,205,ILI9341_WHITE); //绘制水平线 tft.fillRect(15,80,205,5,ILI9341_WHITE); tft.fillRect(15,150,205,5,ILI9341_WHITE); tft.drawRect(230,10,80,220,ILI9341_WHITE); { do { if(moves%2==1 &&player==false || moves%2==0 &&player==true) { cursor+=10; tft.setCursor(240,cursor); tft.setTextSize(1); tft.println("我画了"); arduinoMove();检查赢家(); }else { 光标+=10; tft.setCursor(240,cursor); tft.setTextSize(1); tft.println("你的动作");播放器移动();检查赢家(); } 延迟(1000);移动++; }while (winner==0 &&move<10); waitonetouch(&x,&y); tft.fillScreen(ILI9341_BLACK); //画框 tft.drawRect(0,0,320,240,ILI9341_WHITE); //打印“游戏结束”文本 tft.setCursor(50,30); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(4); tft.print("游戏结束"); if(winner ==0){ //打印“DRAW!”文本 tft.setCursor(110,100); tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(4); tft.print("DRAW");} if(winner ==1){ //打印“人类获胜!”文本 tft.setCursor(40,100); tft.setTextColor(ILI9341_BLUE); tft.setTextSize(4); tft.print("HUMAN WINS");} if(winner ==2){ //打印"CPU WINS!"文本 tft.setCursor(60,100); tft.setTextColor(ILI9341_RED); tft.setTextSize(4); tft.print("CPU WINS");} // 创建按钮 // x、y、w、h、轮廓、填充、文本按钮[0].initButton(&tft, 160,200,200,40,ILI9341_WHITE, ILI9341_RED, ILI9341_WHITE, again , 2);按钮[0].drawButton();做{ waitonetouch(&x,&y); if (buttons[0].contains(x,y)) b=0; }while(b>0); }} void playerMove(){ waitonetouch(&x,&y); if((x>15 &&x<85) &&(y>15 &&y<85)) //0 { if(board[0]==0) { board[0]=1; drawX(15,15);其他错误(); } else if((x>85 &&x<155)&&(y>15 &&y<85)) //1 { if(board[1]==0) { board[1]=1; drawX(85,15);其他错误(); } if((x>155 &&x<225)&&(y>15 &&y<85)) //2 { if(board[2]==0) { board[2]=1; drawX(155,15);其他错误(); } if((x>15 &&x<85)&&(y>85 &&y<155)) //3 { if(board[3]==0) { board[3]=1; drawX(15,85);其他错误(); } if((x>85 &&x<155)&&(y>85 &&y<155)) //4 { if(board[4]==0) { board[4]=1; drawX(85,85);其他错误(); } if((x>155 &&x<225)&&(y>85 &&y<155)) //5 { if(board[5]==0) { board[5]=1; drawX(155,85);其他错误(); } if((x>15 &&x<85)&&(y>155 &&y<225)) //6 { if(board[6]==0) { board[6]=1; drawX(15,155);其他错误(); } if((x>85 &&x<155)&&(y>155 &&y<225)) //7 { if(board[7]==0) { board[7]=1; drawX(85,155);其他错误(); } if((x>155 &&x<225)&&(y>155 &&y<225)) //8 { if(board[8]==0) { board[8]=1; drawX(155,155);其他错误(); }}void error(){ cursor+=10; tft.setCursor(240,cursor); tft.setTextSize(1); tft.println("错误");延迟(1000);光标+=10; tft.setCursor(240,cursor); tft.println("再试一次。"); 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){ 返回 0; }否则返回1; } if(board[3]==x &&board[6]==x &&board[0]==y){ if(y==0){ return 0; }否则返回6; } if(board[0]==y &&board[4]==x &&board[8]==x){ if(y==0){ return 0; }否则返回8; } if(board[0]==x &&board[1]==y &&board[2]==x){ if(y==0){ return 1; }否则返回0; } if(board[4]==x &&board[7]==x &&board[1]==y){ if(y==0){ return 1; }否则返回7; } if(board[0]==x &&board[1]==x &&board[2]==y){ if(y==0){ return 2; }否则返回0; } if(board[5]==x &&board[8]==x &&board[2]==y){ if(y==0){ return 2; }否则返回8; } if(board[4]==x &&board[6]==x &&board[2]==y){ if(y==0){ return 2; }否则返回6; } if(board[4]==x &&board[5]==x &&board[3]==y){ if(y==0){ return 3; }否则返回5; } if(board[0]==x &&board[6]==x &&board[3]==y){ if(y==0){ return 3; }否则返回6; } if(board[3]==x &&board[4]==y &&board[5]==x){ if(y==0){ return 4;否则返回 3; } if(board[1]==x &&board[7]==x &&board[4]==y){ if(y==0){ return 4; }否则返回1; } if(board[0]==x &&board[8]==x &&board[4]==y){ if(y==0){ return 4; }否则返回0; } if(board[2]==x &&board[6]==x &&board[4]==y){ if(y==0){ return 4;否则返回 2; } if(board[3]==x &&board[4]==x &&board[5]==y){ if(y==0){ return 5;否则返回 3; } if(board[2]==x &&board[8]==x &&board[5]==y){ if(y==0){ return 5;否则返回 2; } if(board[7]==x &&board[8]==x &&board[6]==y){ if(y==0){ return 6; }否则返回8; } if(board[0]==x &&board[3]==x &&board[6]==y){ if(y==0){ return 6; }否则返回0; } if(board[4]==x &&board[2]==x &&board[6]==y){ if(y==0){ return 6;否则返回 2; } if(board[1]==x &&board[4]==x &&board[7]==y){ if(y==0){ return 7; }否则返回1; } if(board[6]==x &&board[8]==x &&board[7]==y){ if(y==0){ return 7; }否则返回6; } if(board[6]==x &&board[7]==x &&board[8]==y){ if(y==0){ return 8; }否则返回6; } if(board[5]==x &&board[2]==x &&board[8]==y){ if(y==0){ return 8;否则返回 2; } if(board[4]==x &&board[0]==x &&board[8]==y){ if(y==0){ return 8; }否则返回0; } else return 100;}void arduinoMove(){ if(moves<2) { do{ unsigned short int randomMove =random(4); unsigned short int c=firstMoves[randomMove]; if(board[c]==0){ board[c]=2; drawCpuMove(firstMoves[randomMove]);返回; } }同时(移动<2); } if(moves<3) { if(board[4]==0) { board[4]=2; drawCpuMove(4);返回; } } unsigned short int twoPlayer =checkOpponent(2,0);//CPU连续两个? if(twoPlayer <9) { board[twoPlayer]=2; drawCpuMove(twoPlayer);返回; } unsigned short int nextMove =checkOpponent(1,0);//连续播放两个? if(nextMove <9) { board[nextMove]=2; drawCpuMove(nextMove);返回; } unsigned short int next =checkOpponent(0,2); if(next <9) { board[next]=2; drawCpuMove(下一个);返回; } do { unsigned short int randomMove =random(9); if (board[randomMove]==0) { board[randomMove]=2; drawCpuMove(randomMove);返回; } }while(nextMove ==100);} void drawCircle(unsigned short int x, unsigned short int y){ tft.drawBitmap(x,y,circle,65,65,ILI9341_RED);}void drawX(unsigned short int 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);休息;情况 1:drawCircle(85,15);休息;情况 2:drawCircle(155,15);休息;情况 3:drawCircle(15,85);休息;情况 4:drawCircle(85,85);休息;情况 5:drawCircle(155,85);休息;情况 6:drawCircle(15,155);休息;案例 7:drawCircle(85,155);break;案例 8:drawCircle(155,155);break; }}void checkWinner() // 检查棋盘以查看是否有获胜者 // 将结果放入全局变量 'winner'{ // noughts win? if (board[0]>0 &&board[1]==board[0] &&board[2]==board[0]) {winner=board[0]; tft.fillRect(15, 45, 205, 5, ILI9341_WHITE); } if (board[3]>0 &&board[4]==board[3] &&board[5]==board[3]) {winner=board[3]; tft.fillRect(15, 115, 205, 5, ILI9341_WHITE); } if (board[6]>0 &&board[7]==board[6] &&board[8]==board[6]) {winner=board[6]; tft.fillRect(15, 185, 205, 5, ILI9341_WHITE); } if (board[0]>0 &&board[3]==board[0] &&board[6]==board[0]) {winner=board[0]; tft.fillRect(45, 15, 5, 205, ILI9341_WHITE); } if (board[1]>0 &&board[4]==board[1] &&board[7]==board[1]) {winner=board[1]; tft.fillRect(115, 15, 5, 205, ILI9341_WHITE); } if (board[2]>0 &&board[5]==board[2] &&board[8]==board[2]) {winner=board[2]; tft.fillRect(185, 15, 5, 205, ILI9341_WHITE); } if (board[0]>0 &&board[4]==board[0] &&board[8]==board[0]) {winner=board[0]; for(unsigned short int xx=0;xx<6;xx++){ tft.drawLine(20+xx, 20, 210+xx, 210, ILI9341_WHITE); } } if (board[2]>0 &&board[4]==board[2] &&board[6]==board[2]) {winner=board[2]; for(unsigned short int xx=0;xx<6;xx++){ tft.drawLine(210+xx, 20, 20+xx, 210, ILI9341_WHITE); } }}void waitonetouch(unsigned short int *x,unsigned short int *y){ do { delay(10); if (myTouch.dataAvailable() ==true) { myTouch.read(); *x =myTouch.getX(); //获取触摸点 *y =myTouch.getY();返回; } }while(myTouch.dataAvailable()==false); }
TicTacToe_newArduino
Arduino Due 上无敌的井字游戏单人游戏
 /////////////////////////////////// //////// // 2.8" 触摸屏 TIC TAC TOE // // // // http://www.educ8s.tv // // // // 2017 年 8 月由 rom3 修改 // // /////////////////////////////////////////#include  #include #include #include #define TFT_RST 8#define TFT_DC 9#define TFT_CS 10#define TOUCH_ORIENTATION LANDSCAPEAdafruit_ILI9341 tft =Adafruit_TFT_fruitAfruit_ILI9341 tft =Adafruit(TFTDC_fruitAfruit_ILI9341 [9];URTouch myTouch(30, 28, 26, 24, 22);char start[10] ={"Gamestart"};char you[4] ={"YOU"};char cpu[4] ={" CPU"};char again[7] ={"Again?"};int player;int differentity=8;unsigned short int x,y,b; // 位置 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};//保存位置数据0是空白的,1 个人,2 是计算机未签名的回合;c onst 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,0x7,0xf8,0x0,0x ,0x0,0x0,0x0,0x0,0x7f,0xff,0x80,0x0,0x0,0x0,0x0,0x0,0x3,0xff,0xff,0xf0,0x0,0x0,0x0,0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0xc0,0x0,0x1,0xff,0xff,0xe0,0x3,0xff,0xff,0xc0,0x0,0x3,0xff,0xff,0xc0,0x3,0xff,0xff,0x0xe ,0xff,0xff,0xc0,0x1,0xff,0xff,0xe0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0x0,0x0,0x0,0x0 ,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0x0,0x0,0,0x0 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};void setup(){myTouch.InitTouch(); myTouch.setPrecision(PREC_HI); tft.begin(); tft.setRotation(3);}void loop(){ int board[9] ={0,0,0,0,0,0,0,0,0}; drawStartScreen();做{ myTouch.read(); x =myTouch.getX(); //获取触摸点y =myTouch.getY();延迟(100); }while(!(buttons[0].contains(x,y))); tft.fillScreen(黑色); //绘制白框 tft.drawRect(0,0,320,240,WHITE); //打印“井字游戏”文本 tft.setCursor(30,30); tft.setTextColor(白色); tft.setTextSize(4); tft.print("井字游戏"); //打印“YouTube!”文本 tft.setCursor(30,100); tft.setTextColor(绿色); tft.setTextSize(4); tft.print("谁开始"); //创建红色按钮 // 创建按钮 // x, y, w, h, outline, fill, text buttons[0].initButton(&tft, 100,200,80,40,WHITE, RED, WHITE, you, 2);按钮[0].drawButton(); // 创建按钮 // x、y、w、h、轮廓、填充、文本按钮[1].initButton(&tft, 200,200,80,40,WHITE, RED, WHITE, cpu, 2);按钮[1].drawButton(); b=1;做{ myTouch.read(); x =myTouch.getX(); //获取触摸点y =myTouch.getY(); if (buttons[0].contains(x,y)) { b=0;玩家 =1; tft.fillScreen(黑色); //绘制白框 tft.drawRect(0,0,320,240,WHITE); tft.setCursor(30,30); tft.setTextColor(白色); tft.setTextSize(2); tft.print("好的,开始吧。");延迟(2000); } if(buttons[1].contains(x,y)) { b=0;玩家 =2; tft.fillScreen(黑色); //绘制白框 tft.drawRect(0,0,320,240,WHITE); tft.setCursor(30,30); tft.setTextColor(白色); tft.setTextSize(2); tft.print("好的,CPU 开始了。");延迟(2000); } }同时(b); tft.fillScreen(黑色); tft.drawRect(0,0,319,240,WHITE); drawVerticalLine(125); drawVerticalLine(195); drawHorizo​​ntalLine(80);绘制水平线(150); for(turn =0; turn <9 &&win(board) ==0; ++turn) { if((turn+player) % 2 ==0) computerMove(board); else { //画(板);玩家移动(棋盘); } } switch(win(board)) { case 0:Serial.println("这是平局。\n");休息;案例1://画(板); Serial.println("你输了。\n");休息; case -1:Serial.println("你赢了!\n");休息; } do{}while(myTouch.dataAvailable()==false); //x =map(tp.y, 930, 88, 0, 480); //y =map(tp.x, 908, 125, 0, 320); //delay(250);} }void drawHorizo​​ntalLine(int y){ int i=0; for(i=0;i<7;i++) { tft.drawLine(60,y+i,270,y+i,WHITE); }}void drawVerticalLine(int x){ int i=0; for(i=0;i<7;i++) { tft.drawLine(x+i,20,x+i,220,WHITE); }}void drawStartScreen(){ tft.fillScreen(BLACK); //绘制白框 tft.drawRect(0,0,319,240,WHITE); //打印“井字游戏”文本 tft.setCursor(30,100); tft.setTextColor(白色); tft.setTextSize(4); tft.print("井字游戏"); //打印“YouTube!”文本 tft.setCursor(80,30); tft.setTextColor(绿色); tft.setTextSize(4); tft.print("Arduino"); button[0].initButton(&tft, 160,200,200,40,WHITE, RED, WHITE, start, 2);按钮[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 字节; for(j=0; j score) { score =tempScore;移动 =我; } } } //返回基于给定节点的极小极大树的分数。板[移动] =1; drawCpuMove(移动); delay(100);}void playerMove(int board[9]) { int move =0; b =1;做{ if (myTouch.dataAvailable() ==true) { myTouch.read(); x =myTouch.getX(); //获取触摸点y =myTouch.getY(); if((x>55 &&x<125)&&(y>0 &&y<80)) //0 { if(board[0]==0) { move=0; b=0; } } else if((x>125 &&x<195) &&(y>0 &&y<80)) //1 { if(board[1]==0) { move=1; b=0; } } else if((x>195&&x<265)&&(y>0 &&y<80)) //2 { if(board[2]==0) { move=2; b=0; } } else if((x>55 &&x<125)&&(y>80 &&y<155)) //3 { if(board[3]==0) { move=3; b=0; } } else if((x>125 &&x<195)&&(y>80 &&y<155)) //4 { if(board[4]==0) { move=4; b=0; } } else if((x>195&&x<265)&&(y>80 &&y<155)) //5 { if(board[5]==0) { move=5; b=0; } } else if((x>55 &&x<125)&&(y>155 &&y<235)) //6 { if(board[6]==0) { move=6; b=0; } } else if((x>125 &&x<195)&&(y>155 &&y<235)) //7 { if(board[7]==0) { move=7; b=0; } } else if((x>195&&x<265)&&(y>155 &&y<235)) //8 { if(board[8]==0) { move=8; b=0; } } } }while(b);板[移动] =-1; drawPlayerMove(移动); delay(100);}int win(const int board[9]) { //可能的中奖位置列表 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}}; 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) { //检查球员的位置 intwinner =win(board); if(winner !=0) 返回赢家*玩家; int move =-1;整数分数 =-2;国际我; for(i =0; i <9; ++i) { if(board[i] ==0) { board[i] =player; int thisScore=0; if (depth score) { score =thisScore;移动 =我; } //选择对手棋盘最差的一步[i] =0; } } if(move ==-1) return 0; return score;}void drawCpuMove(int move){ switch(move) { case 0:drawCircle(55,15);休息;情况一:drawCircle(130,15);休息;情况2:drawCircle(205,15);休息;情况 3:drawCircle(55,85);休息;情况 4:drawCircle(130,85);休息;案例5:drawCircle(205,85);休息;情况 6:drawCircle(55,155);休息;情况 7:drawCircle(130,155);break;案例 8:drawCircle(205,155);break; }}void drawPlayerMove(int move){ switch(move) { case 0:drawX(55,15);休息;情况一:drawX(130,15);休息;情况 2:drawX(205,15);休息;情况 3:drawX(55,85);休息;情况 4:drawX(130,85);休息;情况 5:drawX(205,85);休息;情况 6:drawX(55,155);休息;情况 7:drawX(130,155);break;案例 8:drawX(205,155);break; }}

示意图

接线

制造工艺

  1. 井字游戏名片
  2. 使用 K30 传感器监测二氧化碳
  3. 聋盲通信与 1Sheeld/Arduino
  4. 使用 Arduino 控制硬币接收器
  5. Arduino 带蓝牙控制 LED!
  6. 带有 Arduino 或 ESP8266 的电容式指纹传感器
  7. 玩 Nextion Display
  8. Nunchuk 控制机械臂(使用 Arduino)
  9. 使用 Arduino 测量太阳辐射
  10. 带 Arduino 的迷你雷达
  11. 如何用 Arduino 制作音乐
  12. 在 Arduino 上使用 TinyML 进行咳嗽检测