Created
June 15, 2025 15:43
-
-
Save Sudha247/b8f5d3988cd91373214f347c74dfd248 to your computer and use it in GitHub Desktop.
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
| package main | |
| import ( | |
| "fmt" | |
| "math/rand" | |
| "os" | |
| "strconv" | |
| "time" | |
| ) | |
| func initialiseBoard(matrix [][]int) { | |
| for i := range matrix { | |
| for j := range matrix[i] { | |
| matrix[i][j] = rand.Intn(2) | |
| } | |
| } | |
| } | |
| func getCell(matrix [][]int, x, y int) int { | |
| rows := len(matrix) | |
| cols := len(matrix[0]) | |
| if x < 0 || x >= rows || y < 0 || y >= cols { | |
| return 0 // Out of bounds, treat as dead cell | |
| } | |
| return matrix[x][y] | |
| } | |
| func neighbourhood(matrix [][]int, x, y int) int { | |
| // Calculate the sum of the 8 neighbours | |
| return (getCell(matrix, x-1, y-1) + // Top-left | |
| getCell(matrix, x-1, y) + // Top | |
| getCell(matrix, x-1, y+1) + // Top-right | |
| getCell(matrix, x, y-1) + // Left | |
| getCell(matrix, x, y+1) + // Right | |
| getCell(matrix, x+1, y-1) + // Bottom-left | |
| getCell(matrix, x+1, y) + // Bottom | |
| getCell(matrix, x+1, y+1)) // Bottom-right | |
| } | |
| func next(matrix [][]int, x, y int) int { | |
| // Iteration of Conway's Game of Life | |
| cell := matrix[x][y] | |
| neighbours := neighbourhood(matrix, x, y) | |
| // Rule 1: Any live cell with fewer than two live neighbours dies, as if caused by under-population. | |
| if cell == 1 && neighbours < 2 { | |
| return 0 | |
| } | |
| // Rule 2: Any live cell with two or three live neighbours lives on to the next generation. | |
| if cell == 1 && neighbours <= 3 { | |
| return 1 | |
| } | |
| // Rule 3: Any live cell with more than three live neighbours dies, as if by over-population. | |
| if cell == 1 && neighbours > 3 { | |
| return 0 | |
| } | |
| // Rule 4: Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. | |
| if cell == 0 && neighbours == 3 { | |
| return 1 | |
| } | |
| return 0 | |
| } | |
| func printBoard(matrix [][]int) { | |
| for _, row := range matrix { | |
| for _, cell := range row { | |
| if cell == 1 { | |
| fmt.Print("\u2588 ") // "0 " | |
| } else { | |
| fmt.Print(". ") // ". " | |
| } | |
| } | |
| fmt.Println() | |
| } | |
| fmt.Println() | |
| } | |
| func runGeneration(matrix [][]int) { | |
| newMatrix := make([][]int, len(matrix)) | |
| for i := range newMatrix { | |
| newMatrix[i] = make([]int, len(matrix[i])) | |
| } | |
| for i := range matrix { | |
| for j := range matrix[i] { | |
| newMatrix[i][j] = next(matrix, i, j) | |
| } | |
| } | |
| copy(matrix, newMatrix) | |
| printBoard(matrix) | |
| } | |
| func main() { | |
| rand.Seed(time.Now().UnixNano()) | |
| args := os.Args | |
| board_size, err := strconv.Atoi(args[1]) | |
| number_of_times, _ := strconv.Atoi(args[2]) | |
| if err != nil { | |
| fmt.Println("Board size must be an integer:", err) | |
| return | |
| } | |
| if board_size <= 0 { | |
| fmt.Println("Board size must be a positive integer") | |
| return | |
| } | |
| board := make([][]int, board_size) | |
| for i := range board { | |
| board[i] = make([]int, board_size) | |
| } | |
| initialiseBoard(board) | |
| printBoard(board) | |
| for i := 0; i < number_of_times; i++ { | |
| time.Sleep(500 * time.Millisecond) | |
| fmt.Print("\033[H\033[2J") // Clear the console | |
| runGeneration(board) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run the program --