Промышленное производство
Промышленный Интернет вещей | Промышленные материалы | Техническое обслуживание и ремонт оборудования | Промышленное программирование |
home  MfgRobots >> Промышленное производство >  >> Manufacturing Technology >> Производственный процесс

Arduino Due TIC TAC TOE с сенсорным экраном

Компоненты и расходные материалы

Arduino Due
× 1
2,8 zoll TFT ЖК-дисплей с сенсорным экраном
× 1
Перемычки (общие)
× 1

Приложения и онлайн-сервисы

IDE Arduino

Об этом проекте

О

Здравствуйте, хочу поделиться своей работой над проектом от nickthegreek82:arduino-touch-tic-tac-toe-game.

Вам необходимо:

  • Библиотека Adafruit
  • Библиотека GFX
  • и библиотека URTouch

Электропроводка

Схема подключения сенсорного экрана следующая:

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

Для дисплея:

  • Дисплей -> Arduino Due
  • SDO -> A.25
  • светодиод -> 3,3 В
  • SCK -> A.27
  • SDI -> A.26
  • DC -> 9
  • СБРОС -> 8
  • CS -> 10
  • GND -> GND
  • VCC -> 3,3 В

22.08.2017

Доработан KI и некоторая оптимизация кода.

26.08.2017

Некоторая оптимизация кода.

27.08.2017

Сможете ли вы победить ЦП?

13.03.2018

нашел лучший способ для искусственного интеллекта

спасибо klauscam https://github.com/klauscam/ArduinoTicTacToe

Код

  • Крестики-нолики
  • TicTacToe_new
TIC TAC TOE Arduino
Простая игра TIC TAC TOE для игры против компьютера
 //////////////////////////////////// ///////// // 2.8 "СЕНСОРНЫЙ ЭКРАН TIC TAC TOE // // // // http://www.educ8s.tv // // // // изменено 08.2017 пользователем rom3 // / //////////////////////////////////////////// # include  #include  #include  #include  #include  #define TFT_RST 8 # define TFT_DC 9 #define TFT_CS 10 #defruit TOUCH_ORIENTATION_ORIENTATION1 (TFT_CS, TFT_DC, TFT_RST); Adafruit_GFX_Button buttons [9]; URTouch myTouch (30, 28, 26, 24, 22); unsigned short int перемещается =1; unsigned short int Winter =0; // 0 =ничья, 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 игрок computerbool; // true =YOU, false =CPUbo ол б =1; char start [10] ={"Gamestart"}; char you [4] ={"YOU"}; char cpu [4] ={"CPU"}; char again [7] ={"Опять?"}; 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 , 0x0,0x0,0x0,0x0,0x7f, 0xff, 0x80,0x0,0x0,0x0,0x0,0x0,0x3,0xff, 0xff, 0xf0,0x0,0x0,0x0,0x0,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,0x0,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,0x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xe0,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}; 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 («Ардуино»); // Печать текста "Крестики-нолики" 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); кнопки [0] .drawButton (); сделать {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 (); б =1; сделать {waitonetouch (&x, &y); if (buttons [0] .contains (x, y)) {b =0; player =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; player =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); б =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 (перемещает% 2 ==1 &&player ==false || перемещает% 2 ==0 &&player ==true) {cursor + =10; tft.setCursor (240, курсор); tft.setTextSize (1); tft.println («Рисую»); arduinoMove (); checkWinner (); } else {курсор + =10; tft.setCursor (240, курсор); tft.setTextSize (1); tft.println («Ваш ход»); playerMove (); checkWinner (); } задержка (1000); ходы ++; } while (победитель ==0 &&ходов <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) {// Вывести «НИЧЬЮ!» текст tft.setCursor (110,100); tft.setTextColor (ILI9341_YELLOW); tft.setTextSize (4); tft.print ("DRAW");} if (победитель ==1) {// Вывести «ЧЕЛОВЕЧЕСКИЕ ВЫИГРЫШИ!» текст tft.setCursor (40,100); tft.setTextColor (ILI9341_BLUE); tft.setTextSize (4); tft.print ("ЧЕЛОВЕЧЕСКИЕ ВЫИГРЫШИ");} if (победитель ==2) {// Вывести «ВЫИГРЫШИ ЦП!» текст 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, снова , 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 (15155); } еще ошибка (); } 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 () {курсор + =10; tft.setCursor (240, курсор); tft.setTextSize (1); tft.println («Ошибка»); задержка (1000); курсор + =10; tft.setCursor (240, курсор); 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 ( у ==0) {возврат 0; } else return 1; } if (board [3] ==x &&board [6] ==x &&board [0] ==y) {if (y ==0) {return 0; } else return 6; } if (board [0] ==y &&board [4] ==x &&board [8] ==x) {if (y ==0) {return 0; } else return 8; } if (доска [0] ==x &&доска [1] ==y &&доска [2] ==x) {if (y ==0) {return 1; } else return 0; } if (board [4] ==x &&board [7] ==x &&board [1] ==y) {if (y ==0) {return 1; } else return 7; } if (board [0] ==x &&board [1] ==x &&board [2] ==y) {if (y ==0) {return 2; } else return 0; } if (board [5] ==x &&board [8] ==x &&board [2] ==y) {if (y ==0) {return 2; } else return 8; } if (board [4] ==x &&board [6] ==x &&board [2] ==y) {if (y ==0) {return 2; } else return 6; } if (доска [4] ==x &&доска [5] ==x &&доска [3] ==y) {if (y ==0) {return 3; } else return 5; } if (доска [0] ==x &&доска [6] ==x &&доска [3] ==y) {if (y ==0) {return 3; } else return 6; } if (доска [3] ==x &&доска [4] ==y &&доска [5] ==x) {if (y ==0) {return 4; } else return 3; } if (доска [1] ==x &&доска [7] ==x &&доска [4] ==y) {if (y ==0) {return 4; } else return 1; } if (board [0] ==x &&board [8] ==x &&board [4] ==y) {if (y ==0) {return 4; } else return 0; } if (доска [2] ==x &&доска [6] ==x &&доска [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; } else return 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; } else return 8; } if (board [0] ==x &&board [3] ==x &&board [6] ==y) {if (y ==0) {return 6; } else return 0; } if (доска [4] ==x &&доска [2] ==x &&доска [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; } else return 1; } if (board [6] ==x &&board [8] ==x &&board [7] ==y) {if (y ==0) {return 7; } else return 6; } if (board [6] ==x &&board [7] ==x &&board [8] ==y) {if (y ==0) {return 8; } else return 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; } else return 0; } else return 100;} void arduinoMove () {if (move <2) {do {unsigned short int randomMove =random (4); беззнаковый короткий int c =firstMoves [randomMove]; если (доска [c] ==0) {доска [c] =2; drawCpuMove (firstMoves [randomMove]); возвращение; }} while (движется <2); } если (движется <3) {if (доска [4] ==0) {доска [4] =2; drawCpuMove (4); возвращение; }} unsigned short int twoPlayer =checkOpponent (2,0); // ЦП два подряд? если (twoPlayer <9) {доска [twoPlayer] =2; drawCpuMove (twoPlayer); возвращение; } unsigned short int nextMove =checkOpponent (1,0); // Игрок два подряд? если (nextMove <9) {доска [nextMove] =2; drawCpuMove (nextMove); возвращение; } unsigned short int next =checkOpponent (0,2); если (следующая <9) {доска [следующая] =2; drawCpuMove (далее); возвращение; } сделать {беззнаковый короткий int randomMove =random (9); если (доска [randomMove] ==0) {доска [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) {переключатель (перемещение) {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); ломать; case 7:drawCircle (85,155); разрыв; case 8:drawCircle (155,155); разрыв; }} void checkWinner () // проверяет доску, чтобы увидеть, есть ли победитель // размещает результат в глобальной переменной «победитель» {// ноль выигрывает? if (доска [0]> 0 &&доска [1] ==доска [0] &&доска [2] ==доска [0]) {победитель =доска [0]; tft.fillRect (15, 45, 205, 5, ILI9341_WHITE); } if (доска [3]> 0 &&доска [4] ==доска [3] &&доска [5] ==доска [3]) {победитель =доска [3]; tft.fillRect (15, 115, 205, 5, ILI9341_WHITE); } if (доска [6]> 0 &&доска [7] ==доска [6] &&доска [8] ==доска [6]) {победитель =доска [6]; tft.fillRect (15, 185, 205, 5, ILI9341_WHITE); } if (доска [0]> 0 &&доска [3] ==доска [0] &&доска [6] ==доска [0]) {победитель =доска [0]; tft.fillRect (45, 15, 5, 205, ILI9341_WHITE); } if (доска [1]> 0 &&доска [4] ==доска [1] &&доска [7] ==доска [1]) {победитель =доска [1]; tft.fillRect (115, 15, 5, 205, ILI9341_WHITE); } if (доска [2]> 0 &&доска [5] ==доска [2] &&доска [8] ==доска [2]) {победитель =доска [2]; tft.fillRect (185, 15, 5, 205, ILI9341_WHITE); } if (доска [0]> 0 &&доска [4] ==доска [0] &&доска [8] ==доска [0]) {победитель =доска [0]; for (unsigned short int xx =0; xx <6; xx ++) {tft.drawLine (20 + xx, 20, 210 + xx, 210, ILI9341_WHITE); }} if (доска [2]> 0 &&доска [4] ==доска [2] &&доска [6] ==доска [2]) {победитель =доска [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); если (myTouch.dataAvailable () ==true) {myTouch.read (); * х =myTouch.getX (); // Получение точки касания * y =myTouch.getY (); возвращение; }} while (myTouch.dataAvailable () ==false); } 
TicTacToe_new Arduino
Непобедимый одиночный игрок Tic Tac Toe на Arduino Due
 ///////////////////////////////////// //////// // 2.8 "СЕНСОРНЫЙ ЭКРАН TIC TAC TOE // // // // http://www.educ8s.tv // // // // изменено 08.2017 пользователем rom3 // // /////////////////////////////////////////// # include  # include  #include  #include  #define TFT_RST 8 # определить TFT_DC 9 # определить TFT_CS 10 # определить TOUCH_ORIENTATION LANDSCAPEAdafruit_ILI9341 =Adafruit_ILI9341 TFT (TFT_CS, TFT_DC, TFT_RST); кнопки Adafruit_GFX_Button [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; // позиция касания # определить ЧЕРНЫЙ 0x0000 # определить СИНИЙ 0x001F # определить КРАСНЫЙ 0xF800 #define GREEN 0x07E0 #define CYAN 0x07FF #define MAGENTA 0xF81F # define ЖЕЛТЫЙ 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,0x0,0x7,0xf8,0x0,0x0,0x0 , 0x0,0x0,0x0,0x0,0x7f, 0xff, 0x80,0x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ff, 0xff, 0xf8,0x0,0x0,0x0,0x1f, 0xff, 0xff, 0x7f, 0xff, 0xfc, 0x0,0x0,0x0,0x1f, 0xff, 0xfe, 0x3f, 0xff, 0xff , 0x0,0x0,0x0,0x3f, 0xff, 0xfc, 0x3f, 0xff, 0xfe, 0x0,0x0,0x0,0x3f, 0xff, 0xfc, 0x1f, 0xff, 0xfe, 0x0,0x0,0x0,0x7f, 0xff, 0xf8,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}; 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 (); х =myTouch.getX (); // Получение точки касания y =myTouch.getY (); задержка (100); } while (! (buttons [0] .contains (x, y))); tft.fillScreen (ЧЕРНЫЙ); // Рисуем белую рамку tft.drawRect (0,0,320,240, БЕЛЫЙ); // Печать текста «Крестики-нолики» 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, контур, заливка, текстовые кнопки [0] .initButton (&tft, 100,200,80,40, БЕЛЫЙ, КРАСНЫЙ, БЕЛЫЙ, вы, 2); кнопки [0] .drawButton (); // создаем кнопки // x, y, w, h, контур, заливка, текстовые кнопки [1] .initButton (&tft, 200,200,80,40, БЕЛЫЙ, КРАСНЫЙ, БЕЛЫЙ, cpu, 2); кнопки [1] .drawButton (); б =1; сделать {myTouch.read (); х =myTouch.getX (); // Получение точки касания y =myTouch.getY (); if (buttons [0] .contains (x, y)) {b =0; player =1; tft.fillScreen (ЧЕРНЫЙ); // Рисуем белую рамку tft.drawRect (0,0,320,240, БЕЛЫЙ); tft.setCursor (30,30); tft.setTextColor (БЕЛЫЙ); tft.setTextSize (2); tft.print («Хорошо, начинаем.»); задержка (2000); } if (buttons [1] .contains (x, y)) {b =0; player =2; tft.fillScreen (ЧЕРНЫЙ); // Рисуем белую рамку tft.drawRect (0,0,320,240, БЕЛЫЙ); tft.setCursor (30,30); tft.setTextColor (БЕЛЫЙ); tft.setTextSize (2); tft.print («Хорошо, CPU запускается.»); задержка (2000); }} while (б); tft.fillScreen (ЧЕРНЫЙ); tft.drawRect (0,0,319,240, БЕЛЫЙ); 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 {// рисование (доска); playerMove (доска); }} switch (win (board)) {case 0:Serial.println ("Это ничья. \ n"); ломать; case 1:// рисовать (доска); Serial.println («Вы проиграли. \ N»); ломать; case -1:Serial.println («Вы выиграли! \ n»); ломать; } делать {} while (myTouch.dataAvailable () ==false); // x =map (tp.y, 930, 88, 0, 480); // y =map (tp.x, 908, 125, 0, 320); // задержка (250);}} void drawHorizontalLine (int y) {int i =0; для (я =0; я <7; я ++) {tft.drawLine (60, у + я, 270, у + я, БЕЛЫЙ); }} void drawVerticalLine (int x) {int я =0; for (i =0; i <7; i ++) {tft.drawLine (x + i, 20, x + i, 220, БЕЛЫЙ); }} void drawStartScreen () {tft.fillScreen (ЧЕРНЫЙ); // Рисуем белую рамку tft.drawRect (0,0,319,240, БЕЛЫЙ); // Печать текста "Крестики-нолики" tft.setCursor (30,100); tft.setTextColor (БЕЛЫЙ); tft.setTextSize (4); tft.print («Крестики-нолики»); // Распечатать "YouTube!" текст tft.setCursor (80,30); tft.setTextColor (ЗЕЛЕНЫЙ); tft.setTextSize (4); tft.print («Ардуино»); кнопки [0] .initButton (&tft, 160,200,200,40, БЕЛЫЙ, КРАСНЫЙ, БЕЛЫЙ, старт, 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, СИНИЙ );} 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  оценка) {оценка =tempScore; move =i; }}} // возвращает оценку, основанную на минимаксном дереве в данном узле. доска [движение] =1; drawCpuMove (переместить); delay (100);} void playerMove (int board [9]) {int move =0; б =1; сделать {если (myTouch.dataAvailable () ==true) {myTouch.read (); х =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 (б); доска [движение] =-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 (доска [выигрывает [winPos] [0]]! =0 &&доска [выигрывает [winPos] [0]] ==доска [выигрывает [winPos] [ 1]] &&доска [выигрыши [winPos] [0]] ==доска [выигрыши [winPos] [2]]) возврат доски [выигрыши [winPos] [2]]; } return 0;} int minimax (int board [9], int player, int depth) {// проверяем позиции игроков int Winner =win (board); если (победитель! =0) вернуть победителя * игрока; int move =-1; int score =-2; int i; for (i =0; i <9; ++ i) {if (board [i] ==0) {board [i] =player; int thisScore =0; если (глубина <сложность) {thisScore =-minimax (доска, игрок * -1, глубина + 1); } если (thisScore> оценка) {оценка =thisScore; move =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); ломать; case 7:drawCircle (130,155); разрыв; случай 8:drawCircle (205,155); разрыв; }} 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. Мониторинг CO2 с помощью датчика K30
  3. Слепоглухое общение с 1Sheeld / Arduino
  4. Управление монетоприемником с помощью Arduino
  5. Arduino с Bluetooth для управления светодиодом!
  6. Емкостной датчик отпечатков пальцев с Arduino или ESP8266
  7. Игра с дисплеем Nextion
  8. Роботизированная рука, управляемая нунчук (с Arduino)
  9. Измерение солнечной радиации с помощью Arduino
  10. Мини-радар с Arduino