-
Notifications
You must be signed in to change notification settings - Fork 0
/
2048.go
190 lines (171 loc) · 4.11 KB
/
2048.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"math/rand"
"os"
"strconv"
)
func main() {
args := os.Args[1:]
// the dimensions for the grid / map
var height int
var width int
// the selected display strat
var displayerController DisplayerControllerInterface
numOfArgs := len(args)
if numOfArgs > 0 {
displayStrat := args[0]
switch displayStrat {
case "gtk":
displayerController = gtkDisplayerController{}
case "cli":
displayerController = cliDisplayer{}
default:
panic("Unknown display-controller ('gtk' or 'cli')")
}
} else {
height = 5
width = 5
displayerController = gtkDisplayerController{}
}
if numOfArgs > 1 {
if numOfArgs == 3 {
x, _ := strconv.Atoi(args[1])
y, _ := strconv.Atoi(args[2])
height = y
width = x
} else {
panic("wrong number of arguments. Should be\n'go-2048 display-controller height width\nor\n'go-2048 display-controller'")
}
} else {
height = 4
width = 4
}
// create the grid
var grid [][]int = createGrid(height, width)
createNewTiles(grid)
//var displayerController DisplayerControllerInterface = cliDisplayer{}
// the game loop
displayerController.init()
displayerController.startGameLoop(grid)
}
type DisplayerControllerInterface interface {
init()
startGameLoop(grid [][]int)
}
// randomly add some new 1's to the grid
func createNewTiles(grid [][]int) {
// get dimensions
height := len(grid)
width := len(grid[0])
for i := 0; i < width; i++ {
y := rand.Intn(height)
x := rand.Intn(width)
if grid[y][x] == 0 {
grid[y][x] = 1
}
}
}
// slightly overengineered method to move both up and down
func moveVertical(grid [][]int, up bool) {
// generic values to bet set depending on direction
var start, stop, move, reverseStop, reverseMove int
// generic function to compare two values
// for use in the boolean test in the for-loops
var compare greaterOrLesserTest
height := len(grid)
width := len(grid[0])
// set values depending movement direction
if up {
start = 0
stop = height
reverseStop = start
move = 1
compare = func(a, b int) bool {
return a < b
}
} else {
start = height - 1
stop = -1
reverseStop = start
move = -1
compare = func(a, b int) bool {
return a > b
}
}
reverseMove = move * (-1)
for i := 0; i < width; i++ {
for j := start; compare(j, stop); j = j + move {
// the currently examined cell's value
current := grid[j][i]
for reverse := j; compare(reverseStop, reverse); reverse = reverse + reverseMove {
if grid[reverse+reverseMove][i] == 0 {
grid[reverse+reverseMove][i] = current
grid[reverse][i] = 0
} else if grid[reverse+reverseMove][i] == current {
current = current * 2
grid[reverse+reverseMove][i] = current
grid[reverse][i] = 0
break
} else {
break
}
}
}
}
createNewTiles(grid)
}
// see moveVertical
func moveHorizontal(grid [][]int, left bool) {
var start, stop, move, reverseStop, reverseMove int
var compare greaterOrLesserTest
height := len(grid)
width := len(grid[0])
if left {
start = 0
stop = width
reverseStop = start
move = 1
compare = func(a, b int) bool {
return a < b
}
} else {
start = width - 1
stop = -1
reverseStop = start
move = -1
compare = func(a, b int) bool {
return a > b
}
}
reverseMove = move * (-1)
for i := 0; i < height; i++ {
for j := start; compare(j, stop); j = j + move {
// the currently examined cell's value
current := grid[i][j]
for reverse := j; compare(reverseStop, reverse); reverse = reverse + reverseMove {
if grid[i][reverse+reverseMove] == 0 {
grid[i][reverse+reverseMove] = current
grid[i][reverse] = 0
} else if grid[i][reverse+reverseMove] == current {
current = current * 2
grid[i][reverse+reverseMove] = current
grid[i][reverse] = 0
break
} else {
break
}
}
}
}
createNewTiles(grid)
}
// a type for use in the move methods, for generic comparations
type greaterOrLesserTest func(a, b int) bool
// for simply creating a grid, n times n in dimensions
func createGrid(height, width int) [][]int {
grid := make([][]int, height)
for i := 0; i < height; i++ {
grid[i] = make([]int, width)
}
return grid
}