#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0);
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define CELL_SIZE 20 // Size of each cell in pixels
#define GRID_SIZE 3 // Number of cells per row and column
#define EMPTY_CELL ' '
#define PLAYER_X 'X'
#define PLAYER_O 'O'
#define BUTTON_RIGHT_PIN 3
#define BUTTON_LEFT_PIN 2
#define BUTTON_UP_PIN 4
#define BUTTON_DOWN_PIN 5
#define BUTTON_ENTER_PIN 6
int gridWidth = GRID_SIZE * CELL_SIZE;
int gridHeight = GRID_SIZE * CELL_SIZE;
int offsetX = (OLED_WIDTH - gridWidth) / 2;
int offsetY = (OLED_HEIGHT - gridHeight) / 2;
int cursorX = 0; // Cursor position on the grid
int cursorY = 0;
char grid[GRID_SIZE][GRID_SIZE]; // 3x3 grid for the game
void setup() {
pinMode(BUTTON_RIGHT_PIN, INPUT_PULLUP);
pinMode(BUTTON_LEFT_PIN, INPUT_PULLUP);
pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
pinMode(BUTTON_ENTER_PIN, INPUT_PULLUP);
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB14_tr); // Choose a font
initializeGrid();
}
void loop() {
// Game logic goes here
handleInput();
u8g2.firstPage();
do {
drawGrid();
drawCursor(cursorX, cursorY);
} while (u8g2.nextPage());
}
void initializeGrid() {
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
grid[i][j] = EMPTY_CELL;
}
}
}
void handleInput() {
// Check for button presses and move the cursor accordingly
if (digitalRead(BUTTON_RIGHT_PIN) == LOW && cursorX < GRID_SIZE - 1) {
cursorX++;
delay(200); // Debounce delay
}
if (digitalRead(BUTTON_LEFT_PIN) == LOW && cursorX > 0) {
cursorX--;
delay(200); // Debounce delay
}
if (digitalRead(BUTTON_UP_PIN) == LOW && cursorY > 0) {
cursorY--;
delay(200); // Debounce delay
}
if (digitalRead(BUTTON_DOWN_PIN) == LOW && cursorY < GRID_SIZE - 1) {
cursorY++;
delay(200); // Debounce delay
}
if (digitalRead(BUTTON_ENTER_PIN) == LOW ) {//add && to check if space empty
if (grid[cursorX][cursorY] == EMPTY_CELL) {
grid[cursorX][cursorY] = 'X';
}
delay(200); // Debounce delay
}
}
void drawCursor(int x, int y) {
int cellCenterX = offsetX + x * CELL_SIZE + CELL_SIZE / 2;
int cellCenterY = offsetY + y * CELL_SIZE + CELL_SIZE / 2;
u8g2.drawPixel(cellCenterX, cellCenterY);
}
// void drawGrid() {
// // Draw horizontal lines
// for (int i = 0; i <= GRID_SIZE; i++) {
// u8g2.drawHLine(offsetX, offsetY + i * CELL_SIZE, gridWidth);
// }
// // Draw vertical lines
// for (int i = 0; i <= GRID_SIZE; i++) {
// u8g2.drawVLine(offsetX + i * CELL_SIZE, offsetY, gridHeight);
// }
// }
void drawGrid() {
// Draw horizontal lines
for (int i = 0; i <= GRID_SIZE; i++) {
u8g2.drawHLine(offsetX, offsetY + i * CELL_SIZE, gridWidth);
}
// Draw vertical lines
for (int i = 0; i <= GRID_SIZE; i++) {
u8g2.drawVLine(offsetX + i * CELL_SIZE, offsetY, gridHeight);
}
// Draw Xs on the grid
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
if (grid[i][j] == 'X') {
int cellCenterX = offsetX + i * CELL_SIZE + CELL_SIZE / 2 - 4; // Adjusted to center 'X' horizontally
int cellCenterY = offsetY + j * CELL_SIZE + CELL_SIZE / 2 + 2; // Adjusted
u8g2.drawStr(cellCenterX - 3, cellCenterY + 5, "X");
}
}
}
}
// void drawSymbol(int row, int col, char symbol) {
// u8g2.setCursor(col * CELL_SIZE + 3, (row + 1) * CELL_SIZE - 4);
// u8g2.print(symbol);
// }
// void placeSymbol(int row, int col, char symbol) {
// grid[row][col] = symbol;
// drawSymbol(row, col, symbol);
// }