-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
43 lines (37 loc) · 885 Bytes
/
game.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
package main
import (
"fmt"
"github.com/olekukonko/tablewriter"
"os"
"os/exec"
"time"
)
type game struct {
board Board
generations int
intervalSec int
}
func NewGame(initialBoard Board, generations, intervalSecs int) *game {
return &game{board: initialBoard, generations: generations, intervalSec: intervalSecs}
}
func (g game) Run() {
for i := 0; i <= g.generations; i++ {
g.clearTerminal()
g.outputMatrix(i)
time.Sleep(time.Second * time.Duration(g.intervalSec))
g.board = g.board.NextGen()
}
}
func (g game) outputMatrix(generationNum int) {
table := tablewriter.NewWriter(os.Stdout)
table.SetBorder(true)
table.SetRowLine(true)
table.SetCaption(true, fmt.Sprintf("Generation %v", generationNum))
table.AppendBulk(g.board.TextBoard())
table.Render()
}
func (g game) clearTerminal() {
c := exec.Command("clear")
c.Stdout = os.Stdout
c.Run()
}