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 currentPlayer = 1; 
int cursor = 0;
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() {
  if (gameon) {
    drawBoard();
    cursorcheck();

    if (digitalRead(button1_pin) == LOW) {
      Serial.println("button 1");
      handleInput();
      delay(250); // debounce delay
    }

    if (digitalRead(selection_button_pin) == LOW && board[cursor / 3][cursor % 3] == 0) {
      Serial.println("Selection");
      handleSelection();
      delay(250); // debounce delay
    }

    cursorblink(cursor);
    if (checkWin()) {
      gameon = 0;
      displayWin();
    }
  }
}

void drawBoard() {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; 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
      }
    }
  }
}

void handleInput() {
  cursor = (cursor + 1) % 9;
  while (board[cursor / 3][cursor % 3] != 0) {
    cursor = (cursor + 1) % 9;
  }
  Serial.println(cursor);
}

void handleSelection() {
  Serial.println("Selected!");
  board[cursor / 3][cursor % 3] = currentPlayer;
  currentPlayer = (currentPlayer == 1) ? 2 : 1;
  cursor = 0;
}

void cursorcheck() {
  if (board[cursor / 3][cursor % 3] != 0) {
    cursor = (cursor + 1) % 9;
  }
}

void cursorblink(int tempcursor) {
  digitalWrite(LED_pins[tempcursor], HIGH);
  delay(250);
  digitalWrite(LED_pins[tempcursor], LOW);
  delay(250);
}

bool checkWin() {
  int winPatterns[8][3][2] = {
    {{0, 0}, {0, 1}, {0, 2}}, // Row 1
    {{1, 0}, {1, 1}, {1, 2}}, // Row 2
    {{2, 0}, {2, 1}, {2, 2}}, // Row 3
    {{0, 0}, {1, 0}, {2, 0}}, // Column 1
    {{0, 1}, {1, 1}, {2, 1}}, // Column 2
    {{0, 2}, {1, 2}, {2, 2}}, // Column 3
    {{0, 0}, {1, 1}, {2, 2}}, // Diagonal 1
    {{0, 2}, {1, 1}, {2, 0}}  // Diagonal 2
  };

  for (int i = 0; i < 8; i++) {
    if (board[winPatterns[i][0][0]][winPatterns[i][0][1]] != 0 &&
        board[winPatterns[i][0][0]][winPatterns[i][0][1]] == board[winPatterns[i][1][0]][winPatterns[i][1][1]] &&
        board[winPatterns[i][0][0]][winPatterns[i][0][1]] == board[winPatterns[i][2][0]][winPatterns[i][2][1]]) {
      return true;
    }
  }

  return false;
}

void displayWin() {
  Serial.print("Player ");
  Serial.print((currentPlayer == 1) ? 2 : 1); // Because currentPlayer has already been toggled
  Serial.println(" wins!");
}
$abcdeabcde151015202530354045505560fghijfghij