-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (66 loc) · 1.6 KB
/
Copy pathmain.go
File metadata and controls
79 lines (66 loc) · 1.6 KB
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
package main
import (
"fmt"
"math/rand"
"os"
"os/exec"
"strings"
"strconv"
"time"
"github.com/whage/simforest/simforest"
)
const (
gameLoopInterval = 50
)
func main() {
rand.Seed(time.Now().UnixNano())
environment := NewEnvironment()
population := simforest.InitPopulation(environment)
for {
drawWorld(population, environment)
time.Sleep(gameLoopInterval * time.Millisecond)
population = simforest.Tick(population, environment)
// https://stackoverflow.com/a/33509850/1772429
fmt.Printf("\033[0;0H")
}
}
func NewEnvironment() *simforest.Environment {
sttyCommand := exec.Command("stty", "size")
sttyCommand.Stdin = os.Stdin
out, err := sttyCommand.Output()
if err != nil {
fmt.Println("Error when running `stty size`: ", err)
os.Exit(1)
}
terminalSize := strings.Split(strings.Trim(string(out), "\n"), " ")
w, _ := strconv.Atoi(terminalSize[1])
h, _ := strconv.Atoi(terminalSize[0])
return simforest.CreateEnvironment(w, h)
}
func drawWorld(population []simforest.Entity, e *simforest.Environment) {
for y := 0; y < e.Height(); y++ {
for x := 0; x < e.Width(); x++ {
fmt.Print(getMarker(x, y, population))
}
}
//fmt.Println(len(population))
}
func GetMarker(c simforest.Entity) string {
marker := c.Render()
if !c.IsAlive() {
return " "
}
if c.IsAdult() {
return fmt.Sprintf(marker.Color, strings.ToUpper(marker.Character))
} else {
return fmt.Sprintf(marker.Color, marker.Character)
}
}
func getMarker(x, y int, population []simforest.Entity) string {
for _, c := range population {
if c.Pos().X == x && c.Pos().Y == y {
return GetMarker(c)
}
}
return " "
}