MPMC Project
MPMC Project
Fun and Entertainment: To create a simple, engaging project that can be enjoyed
by individuals of all ages, promoting the understanding and enjoyment of basic
game development and electronic circuits.
Thus, by designing Tic Tac Toe using Arduino, individuals can gain practical
experience in programming, hardware integration, and electronic circuits while
creating an enjoyable and interactive gaming experience.
INDEX
SERIAL NO. TITLE PAGE NO.
INTRODUCTION
Tic Tac Toe, often referred to as Xs and Os, is a classic and simple game
enjoyed by people of all ages. In this project, we aim to bring this
timeless game to life using the Arduino platform, combining the
principles of electronics and programming to create an interactive
gaming experience.
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.
CIRCUIT DIAGRAM
Serial.begin(9600);
}
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;
}
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);
}
}
RESULTS
CONCLUSION
In conclusion, the development of the Tic Tac Toe game using the
Arduino platform has proven to be an insightful and enriching
journey. Through this project, we have successfully demonstrated the
integration of hardware components with software logic, creating an
interactive gaming experience that combines the principles of
electronics and programming.
We hope that this project has not only inspired a deeper appreciation
for the application of Arduino in gaming but has also sparked a
curiosity and passion for further exploration and innovation in the
exciting field of electronics and programming.