工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Manufacturing Technology >> 製造プロセス

Arduino Due TIC TAC TOE with Touchscreen

コンポーネントと消耗品

>
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期限
  • SDO-> A.25
  • LED-> 3.3V
  • SCK-> A.27
  • SDI-> A.26
  • DC-> 9
  • リセット-> 8
  • CS-> 10
  • GND-> GND
  • VCC-> 3.3V

2017年8月22日

KIといくつかのコード最適化を変更しました。

2017年8月26日

いくつかのコードの最適化。

2017年8月27日

CPUに勝つことができますか?

2018年3月13日

AIのためのより良い方法を見つけました。

klauscamに感謝しますhttps://github.com/klauscam/ArduinoTicTacToe

コード

  • TIC TAC TOE
  • TicTacToe_new
TIC TAC TOE Arduino
コンピューターと対戦するシンプルなTICTACTOEゲーム
 //////////////////////////////////// ///////// // 2.8 "TOUCH SCREEN TIC TAC TOE // // // // http://www.educ8s.tv // // // // rom3によって08.2017を変更/// ///////////////////////////////////////////#include  #include  #include  #include  #include  #define TFT_RST 8#define TFT_DC 9#define TFT_CS 10#define TOUCH_ORIENTATION LANDSCAPEAdafruit_ILI9341 tft =Ada (TFT_CS、TFT_DC、TFT_RST); Adafruit_GFX_Buttonボタン[9]; URTouchmyTouch(30、28、26、24、22); unsigned short int move =1; unsigned short int winer =0; // 0 =描画、1 =Human、2 =CPUunsigned short int x、y; //位置touchunsignedshortintカーソル; //カーソルpositionunsignedshort int firstMoves [] ={0,2,6,8}; // CPUがunsignedで始まる場合、最初にこれらの位置を使用します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 again [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,0x 、0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x7,0xf8,0x0,0 、0x0,0x0,0x0,0x0,0x7f、0xff、0x80,0x0,0x0,0x0,0x0,0x0,0x3,0xff、0xff、0xf0,0x0,0x0,0x0,0x0,0x0,0xf、0xff、0 、0x0,0x0,0x0,0x0,0x0,0x1f、0xff、0xff、0xfe、0x0,0x0,0x0,0x0,0x0,0x7f、0xf0,0x3,0xff、0x80,0x0,0x0,0x0,0x0 、0x0,0x7f、0xc0,0x0,0x0,0x0,0x1,0xfe、0x0,0x0,0x1f、0xe0,0x0,0x0,0x0,0x3,0xf8,0x0,0x0,0x7,0xf0,0x0,0x0 、0xf0,0x0,0x0,0x3,0xf8,0x0,0x0,0x0,0xf、0xe0,0x0,0x0,0x1,0xfc、0x0,0x0,0x0,0xf、0xc0,0x0,0x0,0x0,0xfc、0 、0x0,0x1f、0x80,0x0,0x0,0x0,0x7e、0x0,0x0,0x0,0x3f、0x0,0x0,0x0,0x0,0x3f、0x0,0x0,0x0,0x3e、0x0,0x0,0 、0x0,0x0,0x0,0x7e、0x0,0x0,0x0,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0,0x0,0x1f、 0xe0,0x0,0x0,0x0,0x0,0xff、0x80,0x0,0x7f、0xc0,0x0,0x0,0x0,0x0,0x7f、0xf0,0x3,0xff、0x80,0x0,0x0,0x0,0x0 0xff、0xfe、0x0,0x0,0x0,0x0,0x0,0xf、0xff、0xff、0xfc、0x0,0x0,0x0,0x0,0x0,0x3,0xff、0xff、0xf0,0x0,0x0,0x0,0x0 0x0,0x7f、0xff、0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x7,0xf8,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,0 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}; const unsigned char x_bitmap [] PROGMEM ={ 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,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,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,0x0,0x0,0x0,0x0,0x3,0xff、0xff、0xc0,0x1,0xff、0xff、0xe0,0x0,0x1,0xff、0xff、0xe0,0x1,0xff、0xff、0xc0,0x0,0x1 0xff、0xff、0xf0,0x3,0xff、0xff、0xc0,0x0,0x0,0xf f、0xff、0xf0,0x7,0xff、0xff、0x80,0x0,0x0,0xff、0xff、0xf8,0x7,0xff、0xff、0x80,0x0,0x0,0x7f、0xff、0xf8,0xf、0xff、0x 0x0,0x0,0x3f、0xff、0xfc、0xf、0xff、0xfe、0x0,0x0,0x0,0x3f、0xff、0xfe、0x1f、0xff、0xfe、0x0,0x0,0x0,0x1f、0xff、0xfe、0 0xfc、0x0,0x0,0x0,0x1f、0xff、0xff、0x3f、0xff、0xfc、0x0,0x0,0x0,0xf、0xff、0xff、0x7f、0xff、0xf8,0x0,0x0,0x0,0xf、0 0xff、0xff、0xf8,0x0,0x0,0x0,0x7,0xff、0xff、0xff、0xff、0xf0,0x0,0x0,0x0,0x3,0xff、0xff、0xff、0xff、0xe0,0x0,0x0,0x 0xff、0xff、0xff、0xff、0xe0,0x0,0x0,0x0,0x1,0xff、0xff、0xff、0xff、0xc0,0x0,0x0,0x0,0x1,0xff、0xff、0xff、0xff、0xc0,0x0,0x 0x0,0x0,0xff、0xff、0xff、0xff、0x80,0x0,0x0,0x0,0x0,0xff、0xff、0xff、0xff、0x80,0x0,0x0,0x0,0x0,0x7f、0xff、0xff、0xff 0x0,0x0,0x0,0x0,0x3f、0xff、0xff、0xfe、0x0,0x0,0x0,0x0,0x0,0x3f、0xff、0xff、0xfe、0x0,0x0,0x0,0x0,0x0,0x1f、0 0xfc、0x0,0x0,0x0,0x0,0x0,0x1f、0xff、0xff、0xfc、0x0,0x0,0x0,0x0,0x0,0x1f、0xff、0xff、0xfc、0x0,0x0,0x0,0x0,0x 0xff、0xff、0xfe、0x0,0x0,0x0,0x0,0x0,0x3f、0xff、0xff、0xfe、0x0,0x0,0x0,0x0,0x0,0x7f、0xff、0xff、0xf f、0x0,0x0,0x0,0x0,0x0,0xff、0xff、0xff、0xff、0x80,0x0,0x0,0x0,0x0,0xff、0xff、0xff、0xff、0x80,0x0,0x0,0x0,0x1,0xff 0xff、0xff、0xff、0xc0,0x0,0x0,0x0,0x1,0xff、0xff、0xff、0xff、0xc0,0x0,0x0,0x0,0x3,0xff、0xff、0xff、0xff、0xe0,0x0,0x0 0x3,0xff、0xff、0xff、0xff、0xe0,0x0,0x0,0x0,0x7,0xff、0xff、0xff、0xff、0xf0,0x0,0x0,0x0,0xf、0xff、0xff、0xff、0xff、0x 0x0,0x0,0xf、0xff、0xff、0xff、0xff、0xf8,0x0,0x0,0x0,0x1f、0xff、0xff、0x7f、0xff、0xfc、0x0,0x0,0x0,0x1f、0xff、0xfe、0 0xfc、0x0,0x0,0x0,0x3f、0xff、0xfc、0x3f、0xff、0xfe、0x0,0x0,0x0,0x3f、0xff、0xfc、0x1f、0xff、0xfe、0x0,0x0,0x0,0x7f 0x1f、0xff、0xff、0x0,0x0,0x0,0xff、0xff、0xf8,0xf、0xff、0xff、0x80,0x0,0x0,0xff、0xff、0xf0,0x7,0xff、0xff、0x80,0x0,0 0xff、0xe0,0x7,0xff、0xff、0xc0,0x0,0x1,0xff、0xff、0xe0,0x3,0xff、0xff、0xc0,0x0,0x3,0xff、0xff、0xc0,0x3,0xff、0xff、0x 0x3,0xff、0xff、0xc0,0x1,0xff、0xff、0xe0,0x0,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,0x 、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"); //「TicTacToe」テキストを印刷しますtft.setCursor(30,100); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(4); tft.print( "Tic Tac Toe"); //赤いボタンを作成//ボタンを作成// x、y、w、h、アウトライン、塗りつぶし、テキストボタン[0] .initButton(&tft、160,200,200,40、ILI9341_WHITE、ILI9341_RED、ILI9341_WHITE、start、2); buttons [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); //「TicTacToe」テキストを印刷しますtft.setCursor(30,30); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(4); tft.print( "Tic Tac Toe"); //「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); buttons [0] .drawButton(); //ボタンを作成します// x、y、w、h、アウトライン、塗りつぶし、テキストボタン[1] .initButton(&tft、200,200,80,40、ILI9341_WHITE、ILI9341_RED、ILI9341_WHITE、cpu、2); buttons [1] .drawButton(); b =1; do {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( "OK、始めます。"); delay(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( "OK、CPUが始まります。"); delay(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 || move%2 ==0 &&player ==true){cursor + =10; tft.setCursor(240、cursor); tft.setTextSize(1); tft.println( "私が描く"); arduinoMove(); checkWinner(); } else {カーソル+ =10; tft.setCursor(240、cursor); tft.setTextSize(1); tft.println( "あなたの動き"); playerMove(); checkWinner(); } delay(1000);移動++; } while(winner ==0 &&moves <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){// "HUMAN WINS!"を印刷しますテキスト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、outline、fill、text buttons [0] .initButton(&tft、160,200,200,40、ILI9341_WHITE、ILI9341_RED、ILI9341_WHITE、again 、2); buttons [0] .drawButton(); do {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 error(); } else if((x> 85 &&x <155)&&(y> 15 &&y <85))// 1 {if(board [1] ==0){board [1] =1; drawX(85,15); } else error(); } if((x> 155 &&x <225)&&(y> 15 &&y <85))// 2 {if(board [2] ==0){board [2] =1; drawX(155,15); } else error(); } if((x> 15 &&x <85)&&(y> 85 &&y <155))// 3 {if(board [3] ==0){board [3] =1; drawX(15,85); } else error(); } if((x> 85 &&x <155)&&(y> 85 &&y <155))// 4 {if(board [4] ==0){board [4] =1; drawX(85,85); } else error(); } if((x> 155 &&x <225)&&(y> 85 &&y <155))// 5 {if(board [5] ==0){board [5] =1; drawX(155,85); } else error(); } if((x> 15 &&x <85)&&(y> 155 &&y <225))// 6 {if(board [6] ==0){board [6] =1; drawX(15,155); } else error(); } if((x> 85 &&x <155)&&(y> 155 &&y <225))// 7 {if(board [7] ==0){board [7] =1; drawX(85,155); } else error(); } if((x> 155 &&x <225)&&(y> 155 &&y <225))// 8 {if(board [8] ==0){board [8] =1; drawX(155,155); } else error(); }} void error(){cursor + =10; tft.setCursor(240、cursor); tft.setTextSize(1); tft.println( "エラー"); delay(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){return 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; } else return 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; } else return 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; } else return 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; } else return 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]);戻る; }} while(moves <2); } if(moves <3){if(board [4] ==0){board [4] =2; drawCpuMove(4);戻る; }} unsigned short int twoPlayer =checkOpponent(2,0); // CPU 2列? if(twoPlayer <9){board [twoPlayer] =2; drawCpuMove(twoPlayer);戻る; } unsigned short int nextMove =checkOpponent(1,0); //プレーヤーが2列に並んでいますか? if(nextMove <9){board [nextMove] =2; drawCpuMove(nextMove);戻る; } unsigned short int next =checkOpponent(0,2); if(next <9){board [next] =2; drawCpuMove(next);戻る; } 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'に配置します{//勝てませんか? 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_new Arduino
ArduinoDueの無敵のTicTacToeシングルプレイヤー
 ///////////////////////////////////// //////// // 2.8 "TOUCH SCREEN TIC TAC TOE // // // // http://www.educ8s.tv // // // // rom3によって08.2017を変更//// //////////////////////////////////////////#include  #include  #include  #include  #define TFT_RST 8#define TFT_DC 9#define TFT_CS 10#define TOUCH_ORIENTATION LANDSCAPEAdafruit_ILI9341 tft =Adafruit_ILI9341(TFT_CS [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難易度=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,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0,0x0,0x0,0 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}; const unsigned char x_bitmap [] PROGMEM ={ 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,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,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,0x0,0x0,0x0,0x0,0x3,0xff、0xff、0xc0,0x1,0xff、0xff、0xe0,0x0,0x1,0xff、0xff、0xe0,0x1,0xff、0xff、0xc0,0x0,0x1 0xff、0xff、0xf0,0x3,0xff、0xff、0xc0,0x0,0x0,0xff 、0xff、0xf0,0x7,0xff、0xff、0x80,0x0,0x0,0xff、0xff、0xf8,0x7,0xff、0xff、0x80,0x0,0x0,0x7f、0xff、0xf8,0xf、0xff、0x 、0x0,0x3f、0xff、0xfc、0xf、0xff、0xfe、0x0,0x0,0x0,0x3f、0xff、0xfe、0x1f、0xff、0xfe、0x0,0x0,0x0,0x1f、0xff、0xfe、0x1 、0x0,0x0,0x0,0x1f、0xff、0xff、0x3f、0xff、0xfc、0x0,0x0,0x0,0xf、0xff、0xff、0x7f、0xff、0xf8,0x0,0x0,0x0,0xf、0xff 、0xff、0xf8,0x0,0x0,0x0,0x7,0xff、0xff、0xff、0xff、0xf0,0x0,0x0,0x0,0x3,0xff、0xff、0xff、0xff、0xe0,0x0,0x0,0x0 、0xff、0xff、0xff、0xe0,0x0,0x0,0x0,0x1,0xff、0xff、0xff、0xff、0xc0,0x0,0x0,0x0,0x1,0xff、0xff、0xff、0xff、0xc0,0x0,0x0 、0x0,0xff、0xff、0xff、0xff、0x80,0x0,0x0,0x0,0x0,0xff、0xff、0xff、0xff、0x80,0x0,0x0,0x0,0x0,0x7f、0xff、0xff、0xff、0 、0x0,0x0,0x0,0x3f、0xff、0xff、0xfe、0x0,0x0,0x0,0x0,0x0,0x3f、0xff、0xff、0xfe、0x0,0x0,0x0,0x0,0x0,0x1f、0xff 、0x0,0x0,0x0,0x0,0x0,0x1f、0xff、0xff、0xfc、0x0,0x0,0x0,0x0,0x0,0x1f、0xff、0xff、0xfc、0x0,0x0,0x0,0x0,0x0,0 、0xff、0xfe、0x0,0x0,0x0,0x0,0x0,0x3f、0xff、0xff、0xfe、0x0,0x0,0x0,0x0,0x0,0x7f、0xff、0xff、0xff 、0x0,0x0,0x0,0x0,0x0,0xff、0xff、0xff、0xff、0x80,0x0,0x0,0x0,0x0,0xff、0xff、0xff、0xff、0x80,0x0,0x0,0x0,0x1,0xff、0 、0xff、0xff、0xc0,0x0,0x0,0x0,0x1,0xff、0xff、0xff、0xff、0xc0,0x0,0x0,0x0,0x3,0xff、0xff、0xff、0xff、0xe0,0x0,0x0,0 、0xff、0xff、0xff、0xff、0xe0,0x0,0x0,0x0,0x7,0xff、0xff、0xff、0xff、0xf0,0x0,0x0,0x0,0xf、0xff、0xff、0xff、0xff、0xf8 、0x0,0xf、0xff、0xff、0xff、0xff、0xf8,0x0,0x0,0x0,0x1f、0xff、0xff、0x7f、0xff、0xfc、0x0,0x0,0x0,0x1f、0xff、0xfe、0x3 、0x0,0x0,0x0,0x3f、0xff、0xfc、0x3f、0xff、0xfe、0x0,0x0,0x0,0x3f、0xff、0xfc、0x1f、0xff、0xfe、0x0,0x0,0x0,0x7f、0 、0xff、0xff、0x0,0x0,0x0,0xff、0xff、0xf8,0xf、0xff、0xff、0x80,0x0,0x0,0xff、0xff、0xf0,0x7,0xff、0xff、0x80,0x0,0x1,0x 、0xe0,0x7,0xff、0xff、0xc0,0x0,0x1,0xff、0xff、0xe0,0x3,0xff、0xff、0xc0,0x0,0x3,0xff、0xff、0xc0,0x3,0xff、0xff、0xe0 、0xff、0xff、0xc0,0x1,0xff、0xff、0xe0,0x0,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,0 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(); do {myTouch.read(); x =myTouch.getX(); //タッチポイントを取得y =myTouch.getY(); delay(100); } while(!(buttons [0] .contains(x、y))); tft.fillScreen(BLACK); //白いフレームを描画しますtft.drawRect(0,0,320,240、WHITE); //「TicTacToe」テキストを印刷しますtft.setCursor(30,30); tft.setTextColor(WHITE); tft.setTextSize(4); tft.print( "Tic Tac Toe"); //「YouTube!」を印刷しますテキストtft.setCursor(30,100); tft.setTextColor(GREEN); tft.setTextSize(4); tft.print( "誰が始めるか"); //赤いボタンを作成//ボタンを作成// x、y、w、h、アウトライン、塗りつぶし、テキストbuttons [0] .initButton(&tft、100,200,80,40、WHITE、RED、WHITE、you、2); buttons [0] .drawButton(); //ボタンを作成します// x、y、w、h、アウトライン、塗りつぶし、テキストボタン[1] .initButton(&tft、200,200,80,40、WHITE、RED、WHITE、cpu、2); buttons [1] .drawButton(); b =1; do {myTouch.read(); x =myTouch.getX(); //タッチポイントを取得y =myTouch.getY(); if(buttons [0] .contains(x、y)){b =0;プレーヤー=1; tft.fillScreen(BLACK); //白いフレームを描画しますtft.drawRect(0,0,320,240、WHITE); tft.setCursor(30,30); tft.setTextColor(WHITE); tft.setTextSize(2); tft.print( "OK、始めます。"); delay(2000); } if(buttons [1] .contains(x、y)){b =0;プレーヤー=2; tft.fillScreen(BLACK); //白いフレームを描画しますtft.drawRect(0,0,320,240、WHITE); tft.setCursor(30,30); tft.setTextColor(WHITE); tft.setTextSize(2); tft.print( "OK、CPUが始まります。"); delay(2000); }} while(b); tft.fillScreen(BLACK); tft.drawRect(0,0,319,240、WHITE); drawVerticalLine(125); drawVerticalLine(195); drawHorizo​​ntalLine(80); drawHorizo​​ntalLine(150); for(turn =0; turn <9 &&win(board)==0; ++ turn){if((turn + player)%2 ==0)computerMove(board); else {// draw(board); playerMove(ボード); }} switch(win(board)){ケース0:Serial.println( "これは引き分けです。\ n");壊す;ケース1:// draw(board); Serial.println( "あなたは負けます。\ n");壊す;ケース-1:Serial.println( "You win!\ 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); //「TicTacToe」テキストを印刷しますtft.setCursor(30,100); tft.setTextColor(WHITE); tft.setTextSize(4); tft.print( "Tic Tac Toe"); //「YouTube!」を印刷しますテキストtft.setCursor(80,30); tft.setTextColor(GREEN); tft.setTextSize(4); tft.print( "Arduino"); buttons [0] .initButton(&tft、160,200,200,40、WHITE、RED、WHITE、start、2); buttons [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 スコア){スコア=tempScore;移動=i; }}} //指定されたノードのミニマックスツリーに基づくスコアを返します。 board [move] =1; drawCpuMove(move); delay(100);} void playerMove(int board [9]){int move =0; b =1; do {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); board [move] =-1; drawPlayerMove(move); 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、intdepth){//プレーヤーの位置を確認しますint winer =win(board); if(winner!=0)return winer * player; int move =-1; intスコア=-2; int i; for(i =0; i <9; ++ i){if(board [i] ==0){board [i] =プレーヤー; int thisScore =0; if(depth スコア){スコア=thisScore;移動=i; } //対戦相手のボードの最悪の動きを選択[i] =0; }} if(move ==-1)return 0;スコアを返す;} void drawCpuMove(int move){switch(move){case 0:drawCircle(55,15);壊す;ケース1: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);壊す;ケース1: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センサーによるCO2モニタリング
  3. 1Sheeld / Arduinoとの盲ろう者のコミュニケーション
  4. Arduinoでコインアクセプターを制御する
  5. LEDを制御するBluetoothを搭載したArduino!
  6. ArduinoまたはESP8266を搭載した静電容量式指紋センサー
  7. Nextionディスプレイで遊ぶ
  8. Nunchuk制御ロボットアーム(Arduino付き)
  9. Arduinoで日射量を測定する
  10. Arduinoを搭載したミニレーダー
  11. Arduinoで音楽を作る方法