-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository
/
Copy pathconnect4.ino
More file actions
More file actions
Latest commit
558 lines (501 loc) · 13 KB
/
connect4.ino
File metadata and controls
558 lines (501 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include Adafruit_ST7735.h>gt;
// define pins for screen
#define TFT_SCLK 2
#define TFT_RST 4
#define TFT_CS 5
#define TFT_MOSI 13
#define TFT_DC 16
// define pins for buttons
#define RIGHTBUTTON 12
#define MIDDLEBUTTON 0
#define LEFTBUTTON 14
//define the arrow bitmap
const unsigned char arrow[] PROGMEM = {
0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x01, 0xe0, 0x00,
0x01, 0xe0, 0x00,
0x01, 0xe0, 0x00,
0x01, 0xe0, 0x00,
0x01, 0xe0, 0x00,
0x01, 0xe0, 0x00,
0x01, 0xe0, 0x00,
0x1f, 0xfe, 0x00,
0x0f, 0xfc, 0x00,
0x07, 0xf8, 0x00,
0x03, 0xf0, 0x00,
0x01, 0xe0, 0x00,
0x00, 0xc0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00
};
//define the circle bitmap
const unsigned char circle[] PROGMEM = {
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x07, 0x80,
0x1f, 0xe0,
0x1f, 0xe0,
0x3f, 0xf0,
0x3f, 0xf0,
0x3f, 0xf0,
0x3f, 0xf0,
0x1f, 0xe0,
0x1f, 0xe0,
0x07, 0x80,
0x00, 0x00,
0x00, 0x00
};
//create and initialize variables + array
int gameBoard[6][7];
int playerTurn = 1;
int lastSlotX = 0;
int lastSlotY = 0;
int chainLength = 1;
int winner = 0;
int currentSlot = 0;
int lastSlot = -1;
boolean startUI = true;
boolean drawWinScreen = true;
// create the screen object
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
void setup() {
// start serial monitor, tft, and initalize tft
Serial.begin(9600);
tft.initR(INITR_BLACKTAB);
tft.setRotation(3);
tft.fillScreen(ST77XX_BLACK);
Serial.println("tft set");
//set pin modes for buttons to be inputs
pinMode(RIGHTBUTTON, INPUT_PULLUP);
pinMode(MIDDLEBUTTON, INPUT_PULLUP);
pinMode(LEFTBUTTON, INPUT_PULLUP);
// create and initialize the board on the tft
drawBoard();
createBoard();
}
void loop() {
// always run
while (true) {
// only run when in game, not win screen
while (playerTurn != 0) {
// player 1 turn
if (playerTurn == 1) {
// first turn start
if (startUI) {
// fill border with respective colour and prompt user
tft.fillRect(23, 17, 120, 18, ST77XX_BLACK);
tft.fillRect(20, 3, 130, 8, ST77XX_BLACK);
drawBorder(playerTurn);
tft.setCursor(50, 3);
tft.write("Player 1! ");
tft.setCursor(15, 10);
tft.write("Please select a slot:");
startUI = false;
currentSlot = 0;
drawArrow();
drawWinScreen = true;
}
// check if the right button was pressed
if (digitalRead(RIGHTBUTTON) == LOW) {
// check if current slot is outside of bounds
if (currentSlot < t; 6) {
// change slot index and draw the new arrow
currentSlot++;
drawArrow();
}
}
// down right LAIEJDFHOLKASEJGFIOASJFGOIJSDOFGASDOFAOSIJF
x = lastSlotX + 1;
y = lastSlotY + 1;
while (x < t; 7 && y < mp; y < 6 && gameBoard[y][x] == playerTurn) {rTurn) {
chainLength++;
x++;
y++;
}
Serial.print("Diagonal \ chain length: ");
Serial.println(chainLength);
if (chainLength >= ;= 4) {
winner = playerTurn;
Serial.println("Win detected!");
playerTurn = 0;
return;
}
chainLength = 1;
//check diag two
x = lastSlotX - 1;
y = lastSlotY + 1;
while (x >= ;= 0 && y < mp; y < 6 && gameBoard[y][x] == playerTurn) {rTurn) {
chainLength++;
x--;
y++;
}
x = lastSlotX + 1;
y = lastSlotY - 1;
while (x < t; 7 && y >= p; y >= 0 && gameBoard[y][x] == playerTurn) {rTurn) {
chainLength++;
x++;
y--;
}
Serial.print("Diagonal / chain length: ");
Serial.println(chainLength);
if (chainLength >= ;= 4) {
winner = playerTurn;
Serial.println("Win detected!");
playerTurn = 0;
return;
}
}
void drawBoard() {
tft.fillRect(25, 35, 105, 1, ST77XX_WHITE);
tft.fillRect(25, 35, 1, 90, ST77XX_WHITE);
tft.fillRect(25, 50, 105, 1, ST77XX_WHITE);
tft.fillRect(40, 35, 1, 90, ST77XX_WHITE);
tft.fillRect(25, 65, 105, 1, ST77XX_WHITE);
tft.fillRect(55, 35, 1, 90, ST77XX_WHITE);
tft.fillRect(25, 80, 105, 1, ST77XX_WHITE);
tft.fillRect(70, 35, 1, 90, ST77XX_WHITE);
tft.fillRect(25, 95, 105, 1, ST77XX_WHITE);
tft.fillRect(85, 35, 1, 90, ST77XX_WHITE);
tft.fillRect(25, 110, 105, 1, ST77XX_WHITE);
tft.fillRect(100, 35, 1, 90, ST77XX_WHITE);
tft.fillRect(25, 125, 105, 1, ST77XX_WHITE);
tft.fillRect(115, 35, 1, 90, ST77XX_WHITE);
tft.fillRect(130, 35, 1, 91, ST77XX_WHITE);
}