-
Notifications
You must be signed in to change notification settings - Fork 0
/
initializing_drawing.go
112 lines (99 loc) · 2.77 KB
/
initializing_drawing.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"bufio"
"image"
"log"
"os"
)
// InitBoard reads a file that represent the initial state of a board and returns a GameBoard
func InitBoard(filepath string) GameBoard {
// open the file
file, err := os.Open(filepath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// create an empty GameBoard
board := make(GameBoard, 0)
// read the file
scanner := bufio.NewScanner(file)
for scanner.Scan() {
currentLine := scanner.Text()
var currentCellArr []Cell
// if this line does not contain strategy elements (C or D), then we should not read anything from this line
hasStrat := string(currentLine[0]) == "C" || string(currentLine[0]) == "D"
if hasStrat {
for i := range currentLine {
currentCell := Cell{strategy: "", score: 0}
val := currentLine[i]
currentCell.strategy = string(val)
currentCellArr = append(currentCellArr, currentCell)
}
board = append(board, currentCellArr)
}
}
return board
}
// DrawGameBoard draws a single GameBoard
// adopted from the same function from drawing.go in CellularAutomata
func DrawGameBoard(board GameBoard, cellWidth int) image.Image {
height := len(board) * cellWidth
width := len(board[0]) * cellWidth
c := CreateNewPalettedCanvas(width, height, nil)
// declare colors
blue := MakeColor(80, 130, 255)
red := MakeColor(150, 40, 30)
// fill in colored squares
for i := range board {
for j := range board[i] {
if board[i][j].strategy == "C" {
c.SetFillColor(blue)
} else if board[i][j].strategy == "D" {
c.SetFillColor(red)
} else {
panic("what? I don't recognize this strategy.")
}
x := j * cellWidth
y := i * cellWidth
c.ClearRect(x, y, x+cellWidth, y+cellWidth)
c.Fill()
}
}
return GetImage(c)
}
// DrawGameBoards returns a list of boards; should use them to produce a gif
func DrawGameBoards(boards []GameBoard, cellWidth int) []image.Image {
numGenerations := len(boards)
imageList := make([]image.Image, numGenerations)
for i := range boards {
imageList[i] = DrawGameBoard(boards[i], cellWidth)
}
return imageList
}
// DrawAndSaveImgPNG draws and save the board to .png
func DrawAndSaveImgPNG(board GameBoard, cellWidth int) {
height := len(board) * cellWidth
width := len(board[0]) * cellWidth
c := CreateNewPalettedCanvas(width, height, nil)
// declare colors
blue := MakeColor(80, 130, 255)
red := MakeColor(150, 40, 30)
green := MakeColor(0, 255, 0)
// fill in colored squares
for i := range board {
for j := range board[i] {
if board[i][j].strategy == "C" {
c.SetFillColor(blue)
} else if board[i][j].strategy == "D" {
c.SetFillColor(red)
} else {
c.SetFillColor(green)
}
x := j * cellWidth
y := i * cellWidth
c.ClearRect(x, y, x+cellWidth, y+cellWidth)
c.Fill()
}
}
c.SaveToPNG("Prisoners.png")
}