#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set up the LCD display
LiquidCrystal_I2C lcd(0x27,16,2);
// Set the size of the game board
const int ROWS = 8;
const int COLS = 16;
// Set the initial state of the game board
int board[ROWS][COLS] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
// Update the game board based on the rules of the game of life
void updateBoard() {
int newBoard[ROWS][COLS];
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
int neighbors = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) {
continue;
}
int r = row + i;
int c = col + j;
if (r < 0) {
r = ROWS - 1;
} else if (r >= ROWS) {
r = 0;
}
if (c < 0) {
c = COLS - 1;
} else if (c >= COLS) {
c = 0;
}
neighbors += board[r][c];
}
}
if (board[row][col] == 1) {
if (neighbors < 2 || neighbors > 3) {
newBoard[row][col] = 0;
} else {
newBoard[row][col] = 1;
}
} else {
if (neighbors == 3) {
newBoard[row][col] = 1;
} else {
newBoard[row][col] = 0;
}
}
}
}
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
board[row][col] = newBoard[row][col];
}
}
}
// Display the game board on the LCD display
void displayBoard() {
lcd.setCursor(0, 0);
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (board[row][col] == 1) {
lcd.print("*");
} else {
lcd.print(" ");
}
}
lcd.setCursor(0, row + 1);
}
}
void setup() {
// Initialize the LCD display
lcd.init();
lcd.backlight();
// Set up the I2C bus
Wire.begin();
}
void loop() {
// Update and display the game board
updateBoard();
displayBoard();
delay(1000);
}
}