const int LED_pins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10}; // LED pins
const int moveButton_pin = 12; // Move cursor button pin
const int selectButton_pin = 11; // Select position button pin
int board[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; // Tic Tac Toe board
int currentPlayer = 1;
int cursor = 0;
bool gameOn = true;
// Variables for debouncing
unsigned long lastButtonPress = 0;
const unsigned long debounceDelay = 50; // 50 milliseconds debounce time
// Variables for scoring and rounds
int player1Score = 0;
int player2Score = 0;
int currentRound = 1; // Use a different name for the round counter
void setup() {
Serial.begin(9600);
for (int i = 0; i < 9; i++) {
pinMode(LED_pins[i], OUTPUT); // set LED pins as outputs
}
pinMode(moveButton_pin, INPUT_PULLUP); // set Move button pin as input with pull-up resistor
pinMode(selectButton_pin, INPUT_PULLUP); // set Select button pin as input with pull-up resistor
Serial.print("Game Start! Round: ");
Serial.println(currentRound);
}
void loop() {
if (!gameOn) {
delay(1000); // Wait for a second before resetting
resetGame(); // Reset game for a new round
return; // Exit loop if the game is over
}
drawBoard();
checkButtons();
delay(100); // Main loop delay
}
void drawBoard() {
for (int i = 0; i < 9; i++) {
if (board[i / 3][i % 3] == 1) {
digitalWrite(LED_pins[i], HIGH); // turn on LED for X
} else if (board[i / 3][i % 3] == 2) {
digitalWrite(LED_pins[i], HIGH);
delay(100); // turn on LED for O with a delay
digitalWrite(LED_pins[i], LOW);
} else {
digitalWrite(LED_pins[i], LOW); // turn off LED if no X or O
}
}
}
void checkButtons() {
unsigned long currentMillis = millis();
// Handle move button
if (digitalRead(moveButton_pin) == LOW && (currentMillis - lastButtonPress) > debounceDelay) {
lastButtonPress = currentMillis;
cursor = (cursor + 1) % 9; // Move cursor
Serial.print("Cursor: ");
Serial.println(cursor);
}
// Handle select button
if (digitalRead(selectButton_pin) == LOW && (currentMillis - lastButtonPress) > debounceDelay) {
lastButtonPress = currentMillis;
if (board[cursor / 3][cursor % 3] == 0) {
board[cursor / 3][cursor % 3] = currentPlayer;
checkWin();
if (gameOn) {
currentPlayer = (currentPlayer == 1) ? 2 : 1; // Switch player
}
}
}
}
void checkWin() {
int win = 0;
// Check rows, columns, and diagonals for a win
for (int i = 0; i < 3; i++) {
if (board[i][0] != 0 && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
win = board[i][0];
}
if (board[0][i] != 0 && board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
win = board[0][i];
}
}
if (board[0][0] != 0 && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
win = board[0][0];
}
if (board[0][2] != 0 && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
win = board[0][2];
}
if (win != 0) {
Serial.print("Player ");
Serial.print(win);
Serial.println(" wins!");
// Update scores
if (win == 1) {
player1Score++;
} else {
player2Score++;
}
// Display scores
Serial.print("Scores - Player 1: ");
Serial.print(player1Score);
Serial.print(" | Player 2: ");
Serial.println(player2Score);
// End the game and prepare for a new round
gameOn = false;
}
}
void resetGame() {
// Reset the board and game status
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = 0;
}
}
currentPlayer = 1; // Reset to Player 1
cursor = 0;
gameOn = true; // Start a new game
currentRound++; // Increment round
Serial.print("Round ");
Serial.println(currentRound);
}