forked from illacceptanything/illacceptanything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2048.cr
381 lines (317 loc) · 7.09 KB
/
2048.cr
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Based on 2048 by Gabriele Cirulli - gabrielecirulli.github.io/2048
require "io/console"
require "colorize"
module Screen
TILES = {
0 => {:white, :black},
2 => {:black, :white},
4 => {:blue, :white},
8 => {:black, :yellow},
16 => {:white, :red},
32 => {:black, :red},
64 => {:white, :magenta},
128 => {:red, :yellow},
256 => {:magenta, :yellow},
512 => {:white, :yellow},
1024 => {:white, :yellow},
2048 => {:white, :yellow},
4096 => {:white, :black},
8192 => {:white, :black},
16384 => {:white, :black},
32768 => {:white, :black},
65536 => {:white, :black}
}
def self.colorize_for(tile)
fg_color, bg_color = TILES[tile]
with_color(fg_color).on(bg_color).surround do
yield
end
end
def self.clear
print "\e[2J\e[1;1H"
end
def self.read_keypress
STDIN.raw do |io|
input = io.read 1
if input == "\e"
next_two_bytes = io.read_nonblock(2) rescue nil
third_byte = io.read_nonblock(1) rescue nil
input += next_two_bytes if next_two_bytes
input += third_byte if third_byte
end
case input
when "\e[A", "w"
:up
when "\e[B", "s"
:down
when "\e[C", "d"
:right
when "\e[D", "a"
:left
when "\e"
:escape
when "\u{3}"
:ctrl_c
when "q", "Q"
:q
else
:unknown
end
end
end
end
class Drawer
INNER_CELL_WIDTH = 16
INNER_CELL_HEIGHT = 6
def initialize
@n = 0
@grid = [] of Array(String)
@current_row = [] of String
@content_line = false
end
def set_current_row row
@current_row = row
end
def draw grid
@grid = grid
@n = @grid.size
Screen.clear
box
end
def box
top_border
(@n-1).times do |row|
tile row
mid_border
end
tile @n-1
bottom_border
end
def tile row
set_current_row @grid[row]
INNER_CELL_HEIGHT.times do |i|
if i == (@n / 2)+1
content_line
else
space_line
end
end
set_current_row [] of String
end
def space_line
line "│", " ", "│", "│"
end
def content_line
@content_line = true
space_line
@content_line = false
end
def top_border
line "┌", "─", "┬", "┐"
end
def mid_border
line "├", "─", "┼", "┤"
end
def bottom_border
line "└", "─", "┴", "┘"
end
def line left, fill, inner, right
print left
(@n-1).times do |cell|
cell_line fill, cell
print inner
end
cell_line fill, @n-1
puts right
end
def cell_line fill, cell
content = @current_row.at(cell) { "empty" }
tile_value = (content == "empty" ? 0 : content).to_i
content = "" if !@content_line || content == "empty"
fill_size = INNER_CELL_WIDTH / 2
fill_size -= content.length / 2
fill_size -= 2
print fill
Screen.colorize_for(tile_value) do
print fill*fill_size
print content
print fill*fill_size
print fill if content.length % 2 == 0
end
print fill
end
end
class Game
def initialize
@drawer = Drawer.new
@grid = [
[nil, nil, nil, nil] of Int32?,
[nil, nil, nil, nil] of Int32?,
[nil, nil, nil, nil] of Int32?,
[nil, nil, nil, nil] of Int32?,
]
insert_tile
insert_tile
end
def run
draw
until won? || lost?
if execute_action read_action
insert_tile
draw
end
end
if won?
end_game "You won!"
elsif lost?
end_game "You lost!"
else
raise "Game loop quitted unexpectedly"
end
end
def draw
@drawer.draw drawable_grid
end
def drawable_grid
@grid.map &.map(&.to_s)
end
def read_action
Screen.read_keypress
end
def insert_tile
value = rand > 0.8 ? 4 : 2
empty_cells = @grid.map(&.count &.nil?).sum
fill_cell = empty_cells > 1 ? rand(empty_cells-1)+1 : 1
empty_cell_count = 0
each_cell_with_index do |tile, row, col|
empty_cell_count += 1 unless tile
if empty_cell_count == fill_cell
@grid[row][col] = value
return
end
end
end
def each_cell_with_index
0.upto(@grid.size-1) do |row|
0.upto(@grid.size-1) do |col|
yield @grid[row][col], row, col
end
end
end
def execute_action action
if [:up, :down, :left, :right].includes? action
if can_move_in? action
shift_grid action
true
else
false
end
elsif [:ctrl_c, :escape, :q].includes? action
end_game "Bye"
elsif action == :unknown
false # ignore
else
raise ArgumentError.new "Unknown action: #{action}"
end
end
def shift_grid direction
drow, dcol = offsets_for direction
shift_tiles_to_empty_cells direction, drow, dcol
merge_tiles direction, drow, dcol
shift_tiles_to_empty_cells direction, drow, dcol
end
def shift_tiles_to_empty_cells direction, drow, dcol
modified = true
while modified
modified = false
movable_tiles(direction, drow, dcol) do |tile, row, col|
unless @grid[row+drow][col+dcol]
@grid[row+drow][col+dcol] = tile
@grid[row][col] = nil
modified = true
end
end
end
end
def merge_tiles direction, drow, dcol
movable_tiles(direction, drow, dcol) do |tile, row, col|
if @grid[row+drow][col+dcol] == tile
@grid[row][col] = nil
@grid[row+drow][col+dcol] = tile*2
end
end
end
def movable_tiles direction, drow, dcol
max = @grid.size-1
from_row, to_row, from_column, to_column =
case direction
when :up, :left
{0, max, 0, max}
when :down, :right
{max, 0, max, 0}
else
raise ArgumentError.new "Unknown direction #{direction}"
end
from_row.to(to_row) do |row|
from_column.to(to_column) do |col|
tile = @grid[row][col]
if tile && !to_border?(direction, row, col, drow, dcol)
yield tile, row, col
end
end
end
end
def can_move_in? direction
drow, dcol = offsets_for direction
movable_tiles(direction, drow, dcol) do |tile, row, col|
target_tile = @grid[row+drow][col+dcol]
return true if !target_tile || target_tile == tile
end
false
end
def offsets_for direction
drow = dcol = 0
case direction
when :up
drow = -1
when :down
drow = 1
when :left
dcol = -1
when :right
dcol = 1
else
raise ArgumentError.new "Unknown direction #{direction}"
end
{drow, dcol}
end
def to_border? direction, row, col, drow, dcol
case direction
when :up
row+drow < 0
when :down
row+drow >= @grid.size
when :left
col+dcol < 0
when :right
col+dcol >= @grid.size
else
false
end
end
def won?
@grid.any? &.any?(&.==(2048))
end
def lost?
!can_move?
end
def can_move?
can_move_in?(:up) || can_move_in?(:down) ||
can_move_in?(:left) || can_move_in?(:right)
end
def end_game msg
puts msg
exit
end
end
Game.new.run