TicTacToe Using Arduino Uno and Some LEDs
by Shenal in Circuits > Arduino
1611 Views, 0 Favorites, 0 Comments
TicTacToe Using Arduino Uno and Some LEDs
This project is a fun and interactive game of Tic Tac Toe that you can play using an Arduino Uno and a few electronic components. It features a 3x3 LED grid that serves as the game board and two push buttons for player input. The LED lights turn on to represent the moves made by the two players, with each player's selected position using different patterns of blinking.
The game has built-in logic to determine if a player has won, and the LEDs display different patterns to indicate the winning combination. Once a player wins, the game ends, and the board has to be reset for the next round.
This project is a great way to learn about Arduino programming and basic electronics while having fun with friends and family. With some basic coding and wiring skills, you can create your own version of this classic game and challenge your friends to a game of Tic Tac Toe.
Supplies
Hardware
- Arduino Uno
- 9 LEDs
- 9 220 ohm resisters
- 2 push buttons
- some jumper cables
- A bread board
Software
- Arduino IDE
Setup the Hardware
Connect the components as per the schematic provided in the image. The resistors are connected in series with the LEDs to limit the current flow. The push buttons are connected to the digital pins of the Arduino to detect player input.
Set Up the Code in Arduino IDE
- Open the Arduino IDE.
- Open the attached src.ino file by File > Open. If needed create a sketch folder by pressing "OK" in the next dialogue.
- Go to Tools > Board and select "Arduino Uno" as the board type.
- Go to Tools > Port and select the port to which the Arduino Uno is connected.
- Click on the "Upload" button to upload the code to the Arduino Uno board.
- Wait for the code to compile and upload to the board.
- Once the code is uploaded, the game of Tic Tac Toe can be played using the push buttons and LEDs connected to the Arduino Uno board.
The code is given here as well...
const int LED_pins[] = {2,3,4,5,6,7,8,9,10}; // LED pins
const int button1_pin = 12; // Player 1 button pin
const int button2_pin = 11; // Player 2 button pin
const int selection_button_pin = 13; // Selection button pin
int board[3][3] = {{0,0,0},{0,0,0},{0,0,0}}; // Tic Tac Toe board
int tempBoard[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
int currentPlayer = 1;
int cursor = 0;
int didwin[] = {0, 0, 0, 0, 0, 0, 0, 0};
int isai = 1;
int sum, sumh;
int gameon = 1;
void setup() {
Serial.begin(9600);
for (int i = 0; i < 9; i++) {
pinMode(LED_pins[i], OUTPUT); // set LED pins as outputs
}
pinMode(button1_pin, INPUT_PULLUP); // set Player 1 button pin as input with pull-up resistor
pinMode(button2_pin, INPUT_PULLUP); // set Player 2 button pin as input with pull-up resistor
pinMode(selection_button_pin, INPUT_PULLUP); // set Selection button pin as input with pull-up resistor
}
void loop() {
drawBoard();
cursorcheck();
if(digitalRead(button1_pin) == LOW) {
Serial.println("button 1");
handleInput(button1_pin);
delay(50);
}
if(digitalRead(selection_button_pin) == LOW && board[cursor/3][cursor%3] == 0) {
Serial.println("Selection");
handleSelec();
delay(50);
}
if(gameon == 1){
cursorblink(cursor);
}
checkWin();
displayWin();
}
void drawBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Serial.print(board[i][j]);
if (board[i][j] == 1) {
digitalWrite(LED_pins[i*3+j], HIGH); // turn on LED for X 100%
}
else if (board[i][j] == 2) {
digitalWrite(LED_pins[i*3+j], HIGH);
delay(1);
digitalWrite(LED_pins[i*3+j], LOW);
delay(1); // turn on LED for O 50%
}
else {
digitalWrite(LED_pins[i*3+j], LOW); // turn off LED if no X or O
}
}
}
Serial.println(cursor);
delay(500);
}
void handleInput(int buttonPin) {
// Player 1 button
//Serial.println("Pressed!");
if (cursor == 8){
cursor = 0;
}
else {
cursor = cursor+1;
}
Serial.println(cursor);
delay(250);
}
void handleSelec() {
Serial.println("Selected!");
board[cursor/3][cursor%3] = currentPlayer;
if(currentPlayer == 1) {
currentPlayer = 2;
}else {
currentPlayer = 1;
}
cursor = 0;
}
void cursorcheck() {
for(int q = 0; q < 9-cursor; q++){
if(board[cursor/3][cursor%3] != 0) {
cursor += 1;
}
}
}
void cursorblink(int tempcursor){
digitalWrite(LED_pins[tempcursor], HIGH);
delay(50);
digitalWrite(LED_pins[tempcursor], LOW);
delay(50);
}
void checkWin() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if(board[i][j]==0) {
tempBoard[i][j] = 9;
}else{tempBoard[i][j] = board[i][j];}
Serial.print(tempBoard[i][j]);
Serial.print("|");
}
}
Serial.println();
didwin[6] = 0;
didwin[7] = 0;
for (int i = 0; i < 3; i++) {
sum = 0;
sumh = 0;
didwin[6] += tempBoard[i][i];
didwin[7] += tempBoard[i][2-i];
for (int j = 0; j < 3; j++) {
sum += tempBoard[i][j];
sumh += tempBoard[j][i];
}
didwin[i] = sum;
didwin[3+i] = sumh;
}
for(int k = 0; k < 7; k++){
Serial.print(didwin[k]);
}
Serial.println();
}
void displayWin () {
for(int p = 0; p < 8; p++) {
if (didwin[p] == 3) {
gameon = 0;
int new_board[3][3] = {{1,1,0},{0,1,0},{1,1,1}};
memcpy(board, new_board, sizeof(new_board));
break;
}
if (didwin[p] == 6) {
gameon = 0;
int new_board[3][3] = {{1,1,1},{1,1,0},{1,1,1}};
memcpy(board, new_board, sizeof(new_board));
break;
}
delay(500);
}
}
Downloads
Play the Game
Now that you have set up the hardware and uploaded the code, it's time to play the game! Check out the embedded video below for a tutorial on how to play Tic Tac Toe on this setup.
In summary, each player takes turns pressing their button to move the cursor to the desired location on the board, and then presses the select button to place their mark. The game will automatically switch to the next player's turn. The LEDs will display the winning combination by showing which player won. To play again, press reset on Uno. Have fun playing!