#define SIZE 3
#define LED1 11
#define LED2 12
int board[SIZE][SIZE];
int buttons[SIZE][SIZE] = {{2, 3, 4}, {5, 6, 7}, {8, 9, 10}};
int currentPlayer = 0;
void setup() {
for (int i = 2; i <= 10; i++) {
pinMode(i, INPUT_PULLUP);
}
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
resetBoard();
}
void loop() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (digitalRead(buttons[i][j]) == LOW && board[i][j] == -1) {
board[i][j] = currentPlayer;
delay(200);
if (checkWin(currentPlayer)) {
digitalWrite(currentPlayer == 0 ? LED1 : LED2, HIGH);
delay(5000);
digitalWrite(currentPlayer == 0 ? LED1 : LED2, LOW);
resetBoard();
} else {
currentPlayer = 1 - currentPlayer;
}
}
}
}
}
void resetBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = -1;
}
}
}
bool checkWin(int player) {
for (int i = 0; i < SIZE; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true;
}
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
return true;
}
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true;
}
return false;
}