-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (85 loc) · 2.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"flag"
"fmt"
"strings"
"time"
"github.com/manifoldco/promptui"
"github.com/tcluri/go-life/gameoflife"
)
var cellPatternsDir string = "./patterns"
var cellfileNameExtension string = ".cells"
func main() {
// Use flag to set the length of grid
size := flag.Int("grid_size", 15, "Enter the length of the grid(square) of board for the game of life")
// Use flag to get different types of grid and cell layout
sim := flag.String("sim", "select", "Enter the kind of simulation you want to run and see: Example - 'glider', 'gliders'(for two!)")
// Parse flags
flag.Parse()
var gridSize int
var populateFunc func(gameoflife.Grid) gameoflife.Grid
// Create grid and populate cells based on sim value
switch *sim {
case "select":
files := gameoflife.ListPatterns(cellPatternsDir, cellfileNameExtension)
prompt := promptui.Select{
Label: "Select GOL simulation you want to run:",
Items: files,
Searcher: func(input string, index int) bool {
option := files[index]
name := fmt.Sprintf("%s", option)
return strings.Contains(name, input)
},
StartInSearchMode: true,
}
_, selected, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
fmt.Printf("You choose %q\n", selected)
shape := gameoflife.LoadCellPatternFromFile(selected, cellPatternsDir, cellfileNameExtension)
gameoflife.AssignShape(shape)
shapeHeight, shapeWidth := shape.FindShapeHeightAndWidth()
fmt.Println("Shape height:", shapeHeight, "Shape width:", shapeWidth)
if shapeHeight >= shapeWidth {
gridSize = shapeHeight * 3
} else {
gridSize = shapeWidth * 3
}
fmt.Println("Grid size:", gridSize)
populateFunc = gameoflife.PopulateAndPlaceShapeAtCenter
case "random":
gridSize = *size
// Populate the grid cells with random cells
populateFunc = gameoflife.PopulateRandomCells
case "glider":
// Create an empty grid for the glider to go across smoothly
gridSize = 20
populateFunc = gameoflife.PopulateGlider
case "gliders":
gridSize = 20
populateFunc = gameoflife.PopulateTwoGliders
}
// Create an empty grid for the cells
initialGrid := gameoflife.CreateEmptyGrid(gridSize)
currentGeneration := populateFunc(initialGrid)
// Evolution!
generationNum := 1
for {
fmt.Printf("Generation #%d\n", generationNum)
aliveCells := gameoflife.CurrentAlive(currentGeneration)
if aliveCells == 0 {
fmt.Println("All cells have died")
return
} else {
fmt.Printf("Alive: %d\n", aliveCells)
}
gameoflife.PrintCellsInGrid(currentGeneration)
// Evolve the cell grid
currentGeneration = gameoflife.Evolution(currentGeneration)
time.Sleep(80 * time.Millisecond)
fmt.Print("\033[H\033[2J") // Clear screen
generationNum++
}
}