#include <iostream>
#include <vector>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
using namespace std;
// Define GPIO pins for LEDs and push buttons based on the JSON file
const uint LED_GREEN = 15; // Pico GP15 (Green LED)
const uint LED_RED = 14; // Pico GP14 (Red LED)
const uint BUTTON_1 = 16; // Pico GP16 (Button 1 - Player X)
const uint BUTTON_2 = 18; // Pico GP18 (Button 2 - Player O)
const uint BUTTON_3 = 20; // Pico GP20 (Button 3 - Reset)
// Function to print the Tic-Tac-Toe board
void printBoard(const vector<vector<char>>& board) {
cout << "Tic-Tac-Toe Board:" << endl;
for (const auto& row : board) {
for (char cell : row) {
cout << cell << " ";
}
cout << endl;
}
cout << endl;
}
// Function to check if a player has won
bool checkWin(const vector<vector<char>>& board, char player) {
// Check rows, columns, and diagonals for a win
for (int i = 0; i < 3; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true; // Horizontal win
}
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
return true; // Vertical win
}
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true; // Diagonal win (top-left to bottom-right)
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true; // Diagonal win (top-right to bottom-left)
}
return false;
}
// Function to check if the board is full (a draw)
bool isBoardFull(const vector<vector<char>>& board) {
for (const auto& row : board) {
for (char cell : row) {
if (cell == ' ') {
return false; // There is an empty cell, not a draw yet
}
}
}
return true; // All cells are filled, it's a draw
}
int main() {
stdio_init_all();
// Initialize LEDs as outputs
gpio_init(LED_GREEN);
gpio_set_dir(LED_GREEN, GPIO_OUT);
gpio_init(LED_RED);
gpio_set_dir(LED_RED, GPIO_OUT);
// Initialize buttons as inputs with pull-up resistors
gpio_init(BUTTON_1);
gpio_set_dir(BUTTON_1, GPIO_IN);
gpio_pull_up(BUTTON_1);
gpio_init(BUTTON_2);
gpio_set_dir(BUTTON_2, GPIO_IN);
gpio_pull_up(BUTTON_2);
gpio_init(BUTTON_3);
gpio_set_dir(BUTTON_3, GPIO_IN);
gpio_pull_up(BUTTON_3);
vector<vector<char>> board(3, vector<char>(3, ' ')); // Tic-Tac-Toe board
char currentPlayer = 'X'; // Player 'X' starts
while (true) {
// Print the current board
printBoard(board);
// Get the player's move via buttons
int row, col;
cout << "Player " << currentPlayer << ", press a button (1-3) to make a move or reset (3): ";
int buttonPressed = 0;
// Wait for a button press
while (buttonPressed == 0) {
if (gpio_get(BUTTON_1) == 0) {
buttonPressed = 1;
} else if (gpio_get(BUTTON_2) == 0) {
buttonPressed = 2;
} else if (gpio_get(BUTTON_3) == 0) {
buttonPressed = 3;
}
}
if (buttonPressed == 3) { // Reset button pressed
// Clear the board and reset player turn
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
currentPlayer = 'X';
continue; // Start a new game
}
// Calculate the row and column based on the button pressed
row = (buttonPressed - 1) / 3;
col = (buttonPressed - 1) % 3;
// Check if the move is valid
if (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' ') {
cout << "Invalid move. Try again." << endl;
continue;
}
// Place the player's symbol on the board
board[row][col] = currentPlayer;
// Check if the current player has won
if (checkWin(board, currentPlayer)) {
printBoard(board);
cout << "Player " << currentPlayer << " wins!" << endl;
break;
}
// Check if it's a draw
if (isBoardFull(board)) {
printBoard(board);
cout << "It's a draw!" << endl;
break;
}
// Switch to the next player
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
return 0;
}