From 30b9fd779c6d940ca11d0bf51438b81fc0cd8105 Mon Sep 17 00:00:00 2001 From: BigTire Date: Tue, 15 Nov 2022 09:28:08 +0100 Subject: [PATCH] Added files. --- Readme.md | 7 + .../tic_tac_toe_first_player.ino | 251 +++++++++++++++++ .../tic_tac_toe_second_player.ino | 254 ++++++++++++++++++ tic_tac_toe_random/tic_tac_toe_random.ino | 52 ++++ 4 files changed, 564 insertions(+) create mode 100644 Readme.md create mode 100644 tic_tac_toe_game/tic_tac_toe_first_player/tic_tac_toe_first_player.ino create mode 100644 tic_tac_toe_game/tic_tac_toe_second_player/tic_tac_toe_second_player.ino create mode 100644 tic_tac_toe_random/tic_tac_toe_random.ino diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..bc3dbb8 --- /dev/null +++ b/Readme.md @@ -0,0 +1,7 @@ +# Tic tac toe game for M5Core2 +- first player + - circles + - tic_tac_toe_first_player.ino +- second player + - crosses + - tic_tac_toe_second_player.ino \ No newline at end of file diff --git a/tic_tac_toe_game/tic_tac_toe_first_player/tic_tac_toe_first_player.ino b/tic_tac_toe_game/tic_tac_toe_first_player/tic_tac_toe_first_player.ino new file mode 100644 index 0000000..ec33a2f --- /dev/null +++ b/tic_tac_toe_game/tic_tac_toe_first_player/tic_tac_toe_first_player.ino @@ -0,0 +1,251 @@ +#include +#include + +#define RX_PIN 19 +#define TX_PIN 27 + +String stringReceived = ""; +bool playing = true; +int numberOfChecks = 0; + +int GAME_BOARD[3][3] = { + {NULL, NULL, NULL}, + {NULL, NULL, NULL}, + {NULL, NULL, NULL} +}; + +void setup() { + M5.begin(); + createGrid(); + Serial2.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); +} + +void createGrid() { + M5.Lcd.drawLine(0, 80, 320, 80, WHITE); + M5.Lcd.drawLine(0, 160, 320, 160, WHITE); + M5.Lcd.drawLine(106, 0, 106, 320, WHITE); + M5.Lcd.drawLine(212, 0, 212, 320, WHITE); +} + +void createCross(int coord_x, int coord_y) { + int COORDS[3][3][4] = { + {{30, 20, 70, 60}, {136, 20, 176, 60}, {242, 20, 282, 60}}, + {{30, 100, 70, 140}, {136, 100, 176, 140}, {242, 100, 282, 140}}, + {{30, 180, 70, 220}, {136, 180, 176, 220}, {242, 180, 282, 220}} + }; + + M5.Lcd.drawLine(COORDS[coord_x][coord_y][0],COORDS[coord_x][coord_y][1],COORDS[coord_x][coord_y][2],COORDS[coord_x][coord_y][3], WHITE); + M5.Lcd.drawLine(COORDS[coord_x][coord_y][0],COORDS[coord_x][coord_y][3],COORDS[coord_x][coord_y][2],COORDS[coord_x][coord_y][1], WHITE); + GAME_BOARD[coord_x][coord_y] = 2; + if (checkWin()) { + gameEnd(2); + } +} + +void createCircle(int coord_x, int coord_y) { + int COORDS[3][3][4] = { + {{30, 20, 70, 60}, {136, 20, 176, 60}, {242, 20, 282, 60}}, + {{30, 100, 70, 140}, {136, 100, 176, 140}, {242, 100, 282, 140}}, + {{30, 180, 70, 220}, {136, 180, 176, 220}, {242, 180, 282, 220}} + }; + + M5.Lcd.drawCircle((COORDS[coord_x][coord_y][0] + COORDS[coord_x][coord_y][2]) / 2, (COORDS[coord_x][coord_y][1] + COORDS[coord_x][coord_y][3]) / 2, 20, WHITE); + + GAME_BOARD[coord_x][coord_y] = 1; + if (checkWin()) { + gameEnd(1); + } + +} + +bool checkWin() { + int value = GAME_BOARD[0][0]; + int x; + numberOfChecks++; + + if (value > 0) { + if ((GAME_BOARD[1][1] == value) && (GAME_BOARD[2][2] == value)) { + return true; + } + } + + value = GAME_BOARD[0][2]; + if (value > 0) { + if ((GAME_BOARD[1][1] == value) && (GAME_BOARD[2][0] == value)) { + return true; + } + } + + for (x = 0; x < 3; x++) { + value = GAME_BOARD[x][0]; + if (value > 0) { + if ((GAME_BOARD[x][1] == value) && (GAME_BOARD[x][2] == value)) { + return true; + } + } + } + + int y; + for (y = 0; y < 3; y++) { + value = GAME_BOARD[0][y]; + if (value > 0) { + if ((GAME_BOARD[1][y] == value) && (GAME_BOARD[2][y] == value)) { + return true; + } + } + } + + if (numberOfChecks == 9) { + delay(1000); + int i; + for (i = 0; i < 3; i++) { + M5.Axp.SetLDOEnable(3, true); + delay(500); + M5.Axp.SetLDOEnable(3, false); + delay(250); + } + delay(1000); + M5.shutdown(3); + } + + return false; +} + +void gameEnd(int playerNumber) { + delay(1000); + M5.Lcd.clear(); + if (playerNumber == 1) { + int i; + for (i = 0; i < 3; i++) { + M5.Lcd.drawCircle(160, 120, 50, WHITE); + M5.Axp.SetLDOEnable(3, true); + delay(500); + M5.Lcd.clear(); + M5.Axp.SetLDOEnable(3, false); + delay(250); + } + M5.shutdown(3); + } else { + int i; + for (i = 0; i < 3; i++) { + M5.Lcd.drawLine(100, 60, 220, 160, WHITE); + M5.Lcd.drawLine(100, 160, 220, 60, WHITE); + M5.Axp.SetLDOEnable(3, true); + delay(500); + M5.Lcd.clear(); + M5.Axp.SetLDOEnable(3, false); + delay(250); + } + M5.shutdown(3); + } +} + +void loop() { + TouchPoint_t coordinate; + coordinate = M5.Touch.getPressPoint(); + if(Serial2.available()){ + char c = Serial2.read(); + stringReceived += c; + if (stringReceived.length() == 3 && (stringReceived.toInt() % 10) != 2) { + stringReceived = ""; + } + if (stringReceived.length() == 3 && ((stringReceived.toInt() % 10) == 2)) { + int value = stringReceived.toInt(); + createCross((int)(value / 100) % 10, (int)(value / 10) % 10); + stringReceived = ""; + playing = true; + } + } + + if (coordinate.x != -1){ + if ((coordinate.x > 0) && (coordinate.x < 106)) { + if ((coordinate.y > 0) && (coordinate.y < 80)) { + if (GAME_BOARD[0][0] == 0) { + if (playing) { + Serial2.print("001"); + createCircle(0, 0); + playing = false; + } + } + } + if ((coordinate.y > 80) && (coordinate.y < 160)) { + if (GAME_BOARD[1][0] == 0) { + if (playing) { + Serial2.print("101"); + createCircle(1, 0); + playing = false; + } + } + } + if ((coordinate.y > 160) && (coordinate.y < 240)) { + if (GAME_BOARD[2][0] == 0) { + if (playing) { + Serial2.print("201"); + createCircle(2, 0); + playing = false; + } + } + } + } + + if ((coordinate.x > 106) && (coordinate.x < 212)) { + if ((coordinate.y > 0) && (coordinate.y < 80)) { + if (GAME_BOARD[0][1] == 0) { + if (playing) { + Serial2.print("011"); + createCircle(0, 1); + playing = false; + } + } + } + if ((coordinate.y > 80) && (coordinate.y < 160)) { + if (GAME_BOARD[1][1] == 0) { + if (playing) { + Serial2.print("111"); + createCircle(1, 1); + playing = false; + } + } + } + if ((coordinate.y > 160) && (coordinate.y < 240)) { + if (GAME_BOARD[2][1] == 0) { + if (playing) { + Serial2.print("211"); + createCircle(2, 1); + playing = false; + } + } + } + } + + if ((coordinate.x > 212) && (coordinate.x < 320)) { + if ((coordinate.y > 0) && (coordinate.y < 80)) { + if (GAME_BOARD[0][2] == 0) { + if (playing) { + Serial2.print("021"); + createCircle(0, 2); + playing = false; + } + } + } + if ((coordinate.y > 80) && (coordinate.y < 160)) { + if (GAME_BOARD[1][2] == 0) { + if (playing) { + Serial2.print("121"); + createCircle(1, 2); + playing = false; + } + } + } + if ((coordinate.y > 160) && (coordinate.y < 240)) { + if (GAME_BOARD[2][2] == 0) { + if (playing) { + Serial2.print("221"); + createCircle(2, 2); + playing = false; + } + } + } + } + } +} \ No newline at end of file diff --git a/tic_tac_toe_game/tic_tac_toe_second_player/tic_tac_toe_second_player.ino b/tic_tac_toe_game/tic_tac_toe_second_player/tic_tac_toe_second_player.ino new file mode 100644 index 0000000..34ec4e4 --- /dev/null +++ b/tic_tac_toe_game/tic_tac_toe_second_player/tic_tac_toe_second_player.ino @@ -0,0 +1,254 @@ +#include +#include + +#define RX_PIN 19 +#define TX_PIN 27 + +String stringReceived = ""; +bool playing = false; +int numberOfChecks = 0; + +int GAME_BOARD[3][3] = { + {NULL, NULL, NULL}, + {NULL, NULL, NULL}, + {NULL, NULL, NULL} +}; + +void setup() { + M5.begin(); + createGrid(); + Serial2.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); +} + +void createGrid() { + M5.Lcd.drawLine(0, 80, 320, 80, WHITE); + M5.Lcd.drawLine(0, 160, 320, 160, WHITE); + M5.Lcd.drawLine(106, 0, 106, 320, WHITE); + M5.Lcd.drawLine(212, 0, 212, 320, WHITE); +} + +void createCross(int coord_x, int coord_y) { + int COORDS[3][3][4] = { + {{30, 20, 70, 60}, {136, 20, 176, 60}, {242, 20, 282, 60}}, + {{30, 100, 70, 140}, {136, 100, 176, 140}, {242, 100, 282, 140}}, + {{30, 180, 70, 220}, {136, 180, 176, 220}, {242, 180, 282, 220}} + }; + + M5.Lcd.drawLine(COORDS[coord_x][coord_y][0],COORDS[coord_x][coord_y][1],COORDS[coord_x][coord_y][2],COORDS[coord_x][coord_y][3], WHITE); + M5.Lcd.drawLine(COORDS[coord_x][coord_y][0],COORDS[coord_x][coord_y][3],COORDS[coord_x][coord_y][2],COORDS[coord_x][coord_y][1], WHITE); + GAME_BOARD[coord_x][coord_y] = 2; + if (checkWin()) { + gameEnd(2); + } +} + +void createCircle(int coord_x, int coord_y) { + int COORDS[3][3][4] = { + {{30, 20, 70, 60}, {136, 20, 176, 60}, {242, 20, 282, 60}}, + {{30, 100, 70, 140}, {136, 100, 176, 140}, {242, 100, 282, 140}}, + {{30, 180, 70, 220}, {136, 180, 176, 220}, {242, 180, 282, 220}} + }; + + M5.Lcd.drawCircle((COORDS[coord_x][coord_y][0] + COORDS[coord_x][coord_y][2]) / 2, (COORDS[coord_x][coord_y][1] + COORDS[coord_x][coord_y][3]) / 2, 20, WHITE); + + GAME_BOARD[coord_x][coord_y] = 1; + if (checkWin()) { + gameEnd(1); + } + +} + +bool checkWin() { + int value = GAME_BOARD[0][0]; + int x; + numberOfChecks++; + + if (value > 0) { + if ((GAME_BOARD[1][1] == value) && (GAME_BOARD[2][2] == value)) { + return true; + } + } + + value = GAME_BOARD[0][2]; + if (value > 0) { + if ((GAME_BOARD[1][1] == value) && (GAME_BOARD[2][0] == value)) { + return true; + } + } + + for (x = 0; x < 3; x++) { + value = GAME_BOARD[x][0]; + if (value > 0) { + if ((GAME_BOARD[x][1] == value) && (GAME_BOARD[x][2] == value)) { + return true; + } + } + } + + int y; + for (y = 0; y < 3; y++) { + value = GAME_BOARD[0][y]; + if (value > 0) { + if ((GAME_BOARD[1][y] == value) && (GAME_BOARD[2][y] == value)) { + return true; + } + } + } + + if (numberOfChecks == 9) { + delay(1000); + int i; + for (i = 0; i < 3; i++) { + M5.Axp.SetLDOEnable(3, true); + delay(500); + M5.Axp.SetLDOEnable(3, false); + delay(250); + } + delay(1000); + M5.shutdown(3); + } + + return false; +} + +void gameEnd(int playerNumber) { + delay(1000); + M5.Lcd.clear(); + if (playerNumber == 1) { + int i; + for (i = 0; i < 3; i++) { + M5.Lcd.drawCircle(160, 120, 50, WHITE); + M5.Axp.SetLDOEnable(3, true); + delay(500); + M5.Lcd.clear(); + M5.Axp.SetLDOEnable(3, false); + delay(250); + } + M5.shutdown(3); + } else { + int i; + for (i = 0; i < 3; i++) { + M5.Lcd.drawLine(100, 60, 220, 160, WHITE); + M5.Lcd.drawLine(100, 160, 220, 60, WHITE); + M5.Axp.SetLDOEnable(3, true); + delay(500); + M5.Lcd.clear(); + M5.Axp.SetLDOEnable(3, false); + delay(250); + } + M5.shutdown(3); + } +} + +void loop() { + TouchPoint_t coordinate; + coordinate = M5.Touch.getPressPoint(); + if(Serial2.available()){ + char c = Serial2.read(); + stringReceived += c; + if (stringReceived.length() == 3 && (stringReceived.toInt() % 10) != 1) { + stringReceived = ""; + } + Serial.println(stringReceived); + if (stringReceived.length() == 3 && ((stringReceived.toInt() % 10) == 1)) { + int value = stringReceived.toInt(); + Serial.println(stringReceived); + createCircle((int)(value / 100) % 10, (int)(value / 10) % 10); + stringReceived = ""; + playing = true; + } + } + + if (coordinate.x != -1){ + if ((coordinate.x > 0) && (coordinate.x < 106)) { + if ((coordinate.y > 0) && (coordinate.y < 80)) { + if (GAME_BOARD[0][0] == 0) { + if (playing) { + Serial2.print("002"); + createCross(0, 0); + playing = false; + } + } + } + if ((coordinate.y > 80) && (coordinate.y < 160)) { + if (GAME_BOARD[1][0] == 0) { + if (playing) { + Serial2.print("102"); + createCross(1, 0); + playing = false; + } + } + } + if ((coordinate.y > 160) && (coordinate.y < 240)) { + if (GAME_BOARD[2][0] == 0) { + if (playing) { + Serial2.print("202"); + createCross(2, 0); + playing = false; + } + } + } + } + + if ((coordinate.x > 106) && (coordinate.x < 212)) { + if ((coordinate.y > 0) && (coordinate.y < 80)) { + if (GAME_BOARD[0][1] == 0) { + if (playing) { + Serial2.print("012"); + createCross(0, 1); + playing = false; + } + } + } + if ((coordinate.y > 80) && (coordinate.y < 160)) { + if (GAME_BOARD[1][1] == 0) { + if (playing) { + Serial2.print("112"); + createCross(1, 1); + playing = false; + } + } + } + if ((coordinate.y > 160) && (coordinate.y < 240)) { + if (GAME_BOARD[2][1] == 0) { + if (playing) { + Serial2.print("212"); + createCross(2, 1); + playing = false; + } + } + } + } + + if ((coordinate.x > 212) && (coordinate.x < 320)) { + if ((coordinate.y > 0) && (coordinate.y < 80)) { + if (GAME_BOARD[0][2] == 0) { + if (playing) { + Serial2.print("022"); + createCross(0, 2); + playing = false; + } + } + } + if ((coordinate.y > 80) && (coordinate.y < 160)) { + if (GAME_BOARD[1][2] == 0) { + if (playing) { + createCross(1, 2); + playing = false; + Serial2.print("122"); + } + } + } + if ((coordinate.y > 160) && (coordinate.y < 240)) { + if (GAME_BOARD[2][2] == 0) { + if (playing) { + Serial2.print("222"); + createCross(2, 2); + playing = false; + + } + } + } + } + } +} \ No newline at end of file diff --git a/tic_tac_toe_random/tic_tac_toe_random.ino b/tic_tac_toe_random/tic_tac_toe_random.ino new file mode 100644 index 0000000..464ea94 --- /dev/null +++ b/tic_tac_toe_random/tic_tac_toe_random.ino @@ -0,0 +1,52 @@ +#include + +void setup() { + M5.begin(); + +} + +void createGrid() { + M5.Lcd.drawLine(0, 80, 320, 80, WHITE); + M5.Lcd.drawLine(0, 160, 320, 160, WHITE); + M5.Lcd.drawLine(106, 0, 106, 320, WHITE); + M5.Lcd.drawLine(212, 0, 212, 320, WHITE); +} + +void createCross(int coord_x, int coord_y) { + int COORDS[3][3][4] = { + {{30, 20, 70, 60}, {136, 20, 176, 60}, {242, 20, 282, 60}} , + {{30, 100, 70, 140}, {136, 100, 176, 140}, {242, 100, 282, 140}} , + {{30, 180, 70, 220}, {136, 180, 176, 220}, {242, 180, 282, 220}} + }; + + M5.Lcd.drawLine(COORDS[coord_x][coord_y][0],COORDS[coord_x][coord_y][1],COORDS[coord_x][coord_y][2],COORDS[coord_x][coord_y][3], WHITE); + M5.Lcd.drawLine(COORDS[coord_x][coord_y][0],COORDS[coord_x][coord_y][3],COORDS[coord_x][coord_y][2],COORDS[coord_x][coord_y][1], WHITE); +} + +void createCircle(int coord_x, int coord_y) { + int COORDS[3][3][4] = { + {{30, 20, 70, 60}, {136, 20, 176, 60}, {242, 20, 282, 60}} , + {{30, 100, 70, 140}, {136, 100, 176, 140}, {242, 100, 282, 140}} , + {{30, 180, 70, 220}, {136, 180, 176, 220}, {242, 180, 282, 220}} + }; + + M5.Lcd.drawCircle((COORDS[coord_x][coord_y][0] + COORDS[coord_x][coord_y][2]) / 2, (COORDS[coord_x][coord_y][1] + COORDS[coord_x][coord_y][3]) / 2, 20, WHITE); +} + +void loop() { + createGrid(); + int x; + int y; + for (x = 0; x < 3; x++) { + for (y = 0; y < 3; y++) { + if ((rand() % 2) == 0) { + createCross(x, y); + delay(500); + } else { + createCircle(x, y); + delay(500); + } + } + } + M5.Lcd.clear(); +} \ No newline at end of file