-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
97 lines (82 loc) · 2.07 KB
/
options.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
package gol
import "fmt"
// Options represents all the options necessary to make
// a valid game
type Options struct {
X, Y int
Grid [][]uint8
RuleNumber int
Rules Rules
}
// MakeGame constructs a game from a given set of options,
// Which may be missing some options
func MakeGame(options Options) Game {
// Get/set rules amount if needed
if options.Rules.Array == nil {
if options.RuleNumber == 0 {
options.RuleNumber = randInt(4) + 2
}
options.Rules.Randomize(options.RuleNumber)
} else if options.RuleNumber == 0 {
options.RuleNumber = len(options.Rules.Array)
} else if options.RuleNumber != len(options.Rules.Array) {
panic(fmt.Sprintf("Rule number in options %d does not equal rules in array %d", options.RuleNumber, len(options.Rules.Array)))
}
// Grid check
if options.X < 0 || options.Y < 0 {
panic("X/Y values cannot be negative")
}
var setX, setY int
var field GridBuffers
// Make the proposed game x, y 50 by default
// or based on the length of the grid slices
// or by the x, y set by the grid
// or by the game options struct
if options.X == 0 {
if len(options.Grid) == 0 {
setX = 50
} else {
setX = len(options.Grid[0])
}
} else {
setX = options.X
}
if options.Y == 0 {
if len(options.Grid) == 0 {
setY = 50
} else {
setY = len(options.Grid)
}
} else {
setY = options.Y
}
options.Y = setY
options.X = setX
// Create the grid if it doesn't exist
// or validate the grid
if len(options.Grid) == 0 {
field = MakeGridBuffers(options.X, options.Y, false)
field.Randomize(options.RuleNumber)
} else {
field = MakeGridBuffers(options.X, options.Y, false)
field.Front = options.Grid
field.Validate()
}
// Make alive bool and counts arrays
alives := makeAlives(options.X, options.Y)
aliveCounts := MakeGridBuffers(options.X, options.Y, true)
// Create the game object
currentGame := Game{
options.X,
options.Y,
field,
options.Rules,
alives,
aliveCounts,
0}
// Ensure nothing mismatches
currentGame.Validate()
// Initialize the game
currentGame.init()
return currentGame
}