Created
March 6, 2023 16:56
-
-
Save cutalion/d7f4414b1364420474f3505ae79346f9 to your computer and use it in GitHub Desktop.
Game of life written by ChatGPT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt; | |
// Define the dimensions of the game board | |
const ROWS: usize = 20; | |
const COLS: usize = 20; | |
// Define the Cell struct | |
#[derive(Clone, Copy)] | |
struct Cell(bool); | |
impl fmt::Display for Cell { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
if self.0 { | |
write!(f, "■") | |
} else { | |
write!(f, "□") | |
} | |
} | |
} | |
// Define the Board struct | |
struct Board { | |
cells: [[Cell; COLS]; ROWS], | |
} | |
impl Board { | |
// Create a new empty board | |
fn new() -> Self { | |
Self { | |
cells: [[Cell(false); COLS]; ROWS], | |
} | |
} | |
// Set the state of a cell at a given row and column | |
fn set_cell(&mut self, row: usize, col: usize, state: bool) { | |
self.cells[row][col] = Cell(state); | |
} | |
// Get the state of a cell at a given row and column | |
fn get_cell(&self, row: usize, col: usize) -> bool { | |
self.cells[row][col].0 | |
} | |
// Update the board based on the rules of the Game of Life | |
fn update(&mut self) { | |
let mut new_cells = [[Cell(false); COLS]; ROWS]; | |
for row in 0..ROWS { | |
for col in 0..COLS { | |
let mut neighbors = 0; | |
// Check the eight neighbors around the current cell | |
for i in -1..=1 { | |
for j in -1..=1 { | |
if i == 0 && j == 0 { | |
continue; | |
} | |
let r = row as isize + i; | |
let c = col as isize + j; | |
// Make sure the neighbor is within the bounds of the board | |
if r >= 0 && r < ROWS as isize && c >= 0 && c < COLS as isize { | |
if self.cells[r as usize][c as usize].0 { | |
neighbors += 1; | |
} | |
} | |
} | |
} | |
// Apply the rules of the Game of Life | |
if self.cells[row][col].0 { | |
if neighbors < 2 || neighbors > 3 { | |
new_cells[row][col] = Cell(false); | |
} else { | |
new_cells[row][col] = Cell(true); | |
} | |
} else { | |
if neighbors == 3 { | |
new_cells[row][col] = Cell(true); | |
} else { | |
new_cells[row][col] = Cell(false); | |
} | |
} | |
} | |
} | |
self.cells = new_cells; | |
} | |
} | |
// Define the main function | |
fn main() { | |
let mut board = Board::new(); | |
// Set the initial state of the board | |
board.set_cell(5, 5, true); | |
board.set_cell(5, 6, true); | |
board.set_cell(5, 7, true); | |
board.set_cell(4, 7, true); | |
board.set_cell(3, 6, true); | |
// Print the initial state of the board | |
for row in 0..ROWS { | |
for col in 0..COLS { | |
print!("{}", board.cells[row][col]); | |
} | |
println!(); | |
} | |
// Update the board and print the new state | |
for _ in 0..10 { | |
println!("Press Enter to continue..."); | |
let mut input = String::new(); | |
std::io::stdin().read_line(&mut input).unwrap(); | |
board.update(); | |
for row in 0..ROWS { | |
for col in 0..COLS { | |
print!("{}", board.cells[row][col]); | |
} | |
println!(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment