-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pony
287 lines (269 loc) · 7.73 KB
/
main.pony
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*********************************
Ponylang 2048 (ANSI terminal)
@author [email protected]
**********************************/
use "term"
use "random"
use "time"
// The 4 Edge Rows we merge toward
interface EdgeRow
fun val row() : Iterator[U32] ref
fun val inc() : I32
primitive TopRow is EdgeRow
fun row() : Iterator[U32] ref => let r : Array[U32] box = [0;1;2;3]
r.values()
fun inc() : I32 => 4
primitive LeftRow is EdgeRow
fun row() : Iterator[U32] ref => let r : Array[U32] box = [0;4;8;12]
r.values()
fun inc() : I32 => 1
primitive RightRow is EdgeRow
fun row() : Iterator[U32] ref => let r : Array[U32] box = [3;7;11;15]
r.values()
fun inc() : I32 => -1
primitive BottomRow is EdgeRow
fun row() : Iterator[U32] ref => let r : Array[U32] box = [12;13;14;15]
r.values()
fun inc() : I32 => -4
// Move defines for arrow keys
primitive LEFT
primitive RIGHT
primitive UP
primitive DOWN
type Move is (LEFT|RIGHT|UP|DOWN)
/**
* Called by ANSITerm on keypress. override arrows and apply for quit()
*/
class KeyboardHandler is ANSINotify
let _game : Game tag
new iso create(game : Game tag) => _game = game
fun ref apply(term: ANSITerm ref, input: U8 val) =>
if input == 113 then // q key
_game.quit()
term.dispose()
end
fun ref left(ctrl: Bool, alt: Bool, shift: Bool) => _game.move(LEFT)
fun ref down(ctrl: Bool, alt: Bool, shift: Bool) => _game.move(DOWN)
fun ref up(ctrl: Bool, alt: Bool, shift: Bool) => _game.move(UP)
fun ref right(ctrl: Bool, alt: Bool, shift: Bool) => _game.move(RIGHT)
// tuple for row data
type ROW is (U32,U32,U32,U32)
// namespace to handle merge matching
primitive Merger
fun tag apply(r : ROW) : ROW =>
match r
| (0,0,0,_) => (r._4,0,0,0)
| (0,0,_,r._3) => (r._3<<1,0,0,0)
| (0,0,_,_) => (r._3,r._4,0,0)
| (0,_,r._2,_) => (r._2<<1,r._4,0,0)
| (0,_,0,r._2) => (r._2<<1,0,0,0)
| (0,_,0,_) => (r._2,r._4,0,0)
| (0,_,_,r._3) => (r._2,r._3<<1,0,0)
| (0,_,_,_) => (r._2,r._3,r._4,0)
| (_, r._1, _, r._3) => (r._1<<1, r._3<<1, 0, 0)
| (_, r._1, 0, _) => (r._1<<1, r._4, 0, 0)
| (_, r._1, _, _) => (r._1<<1, r._3, r._4, 0)
| (_, 0,r._1, _) => (r._1<<1,r._4,0,0)
| (_, 0,0, r._1) => (r._1<<1,0,0,0)
| (_, 0,0, _) => (r._1,r._4,0,0)
| (_, 0,_, r._3) => (r._1, r._3<<1,0,0)
| (_, 0,_, _) => (r._1, r._3,r._4,0)
| (_,_,r._2,_) => (r._1, r._2<<1,r._4,0)
| (_,_,0,r._2) => (r._1, r._2<<1,0,0)
| (_,_,0,_) => (r._1, r._2,r._4,0)
| (_,_,_,r._3) => (r._1, r._2,r._3<<1,0)
else
r
end
/**
* Game actor
*/
actor Game
embed _grid : Array[U32] = Array[U32].init(0, 16)
let _rand : Random = MT(Time.millis())
let _env : Env
let _board : String ref = recover String(1024) end
new create(env: Env)=>
_env = env
_add_block()
_add_block()
_draw()
/**
* merge single row, return None if no merge or movement occurred
*/
fun _merge(start : U32, inc : I32) : (ROW | None) =>
var st = start.i32()
let rval : ROW = (_get(st), _get(st + inc),
_get(st + (inc * 2)), _get(st + (inc * 3)))
let rout = Merger(rval)
if rout is rval then None else rout end
/**
* merge and save single row, return true if row changed
*/
fun ref _update(start : U32, inc : I32) : Bool =>
match _merge(start, inc)
| let rout : ROW =>
var st = start.i32()
_set(st, rout._1)
_set(st + inc, rout._2)
_set(st + (inc * 2), rout._3)
_set(st + (inc * 3), rout._4)
true
else
false
end
/**
* merge and save all 4 rows, return true if any row changed
*/
fun ref _shift_to(edge : EdgeRow val) : Bool =>
var updated = false
for r in edge.row() do
if _update(r, edge.inc()) then
updated = true
end
end
updated
// return ANSI color string for value
fun _fmt(i : U32) : String =>
match i
| 0 => " __ "
| 2 => "\x1B[31m 2 \x1B[0m"
| 4 => "\x1B[32m 4 \x1B[0m"
| 8 => "\x1B[33m 8 \x1B[0m"
| 16 => "\x1B[34m 16 \x1B[0m"
| 32 => "\x1B[35m 32 \x1B[0m"
| 64 => "\x1B[36m 64 \x1B[0m"
| 128 => "\x1B[37m128 \x1B[0m"
| 256 => "\x1B[41m\x1B[37m256 \x1B[0m"
| 512 => "\x1B[42m\x1B[37m512 \x1B[0m"
| 1024 => "\x1B[43m\x1B[37m1024\x1B[0m"
| 2048 => "\x1B[47m\x1B[35m\x1B[1m\x1B[5m2048\x1B[0m"
else
i.string()
end
// draw the screen
fun ref _draw() =>
let s : String ref = _board
s.truncate(0)
var i : U32 = 0
repeat
if (i % 4) == 0 then
s.append("---------------------\n")
end
s.append(_fmt(_get(i)))
s.append(" ")
i = i + 1
if (i % 4) == 0 then
s.append("\n")
end
until i==16 end
_env.out.print("\x1B[2J")
_env.out.print(s.string())
_env.out.print("Arrow keys to move. Press (q)uit key to quit.")
// get single grid cell value
fun _get(i : (I32|U32)) : U32 => try _grid(i.usize())? else 0 end
// update single grid cell value
fun ref _set(i:(I32|U32), v : U32) =>
try
_grid.update(i.usize(),v)?
else
_env.out.print("cant update!")
end
// count non-zero cells
fun _count() : U64 =>
var c : U64 = 0
for v in _grid.values() do
c = c + if v == 0 then 0 else 1 end
end
c
// add new grid cell value if possible
fun ref _add_block() =>
let c = _count()
if c == 16 then return end
var hit = _rand.int(16 - c)
var i : U32 = 0
while i < 16 do
if (_get(i) == 0) then
if hit == 0 then
_set(i, if _rand.int(10) > 0 then 2 else 4 end)
break
end
hit = hit - 1
end
i = i + 1
end
// test win condition
fun _win() : Bool =>
for v in _grid.values() do
if v == 2048 then return true end
end
false
// returns false if any cell can be merged or moved
fun _no_moves(edge : EdgeRow val) : Bool =>
for r in edge.row() do
match _merge(r, edge.inc())
| let rout : ROW =>
if (rout._1 == 0) or (rout._2 == 0) or
(rout._3 == 0) or (rout._4 == 0) then
return false
end
end
end
true
// test if no more moves can be made
fun _lose() : Bool =>
(_grid.size() >= 16) and
_no_moves(LeftRow) and
_no_moves(RightRow) and
_no_moves(TopRow) and
_no_moves(BottomRow)
// dispose of input closes ANSI term
// (fails to close on bash on unbuntu on windows)
be quit()=>
_env.out.print("Exiting.. some terminals may require <ctrl-c>")
_env.exitcode(0)
_env.input.dispose()
// make a single move
be move(m: Move) =>
let updated =
match m
| LEFT => _shift_to(LeftRow)
| RIGHT => _shift_to(RightRow)
| UP => _shift_to(TopRow)
| DOWN => _shift_to(BottomRow)
end
if _win() then
_draw()
_env.out.print("You win :)")
quit()
else
if updated then
_add_block()
_draw()
end
if _lose() then
_env.out.print("You lose :(")
quit()
end
end
// Entry point
actor Main
new create(env: Env) =>
// unit test
ifdef "test" then
TestMain(env)
return
end
// else game
let input : Stdin tag = env.input
env.out.print("Welcome to ponylang-2048...")
let game = Game(env)
let term = ANSITerm(KeyboardHandler(game), input)
let notify : StdinNotify iso = object iso
let term: ANSITerm = term
let _in: Stdin tag = input
fun ref apply(data: Array[U8] iso) => term(consume data)
fun ref dispose() => _in.dispose()
end
// block until notify.dispose()
input(consume notify)