2048-ruby 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/game_2048/game.rb +26 -6
- data/lib/game_2048/render.rb +5 -1
- data/lib/game_2048/terminal.rb +17 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edcff0be5bfa1663ba3da973074496bd7d24eba83e7c4bb092eef6eaf593e2b5
|
4
|
+
data.tar.gz: 3446c4f1d961faee7105a2cbaaedf2d6b9e42d1cfa757e4c23ce39b4a858e2c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff183265592e526c7d534ff9ad2ec848c65f1884c6af218fc984ea7d1acf111bfce6146579ffdbe644ddb4f236bc9ff8bc0c79aa1aaec710a452eb0d436f2d6f
|
7
|
+
data.tar.gz: 96b2497fb4d441f3b11b23ab7218a1b75923f12317f43dc36828617c9db26706f1bc87cb7c108e93becefecc6ccb2f510c0d647a9ecf0cdde380d23de7e66098
|
data/lib/game_2048/game.rb
CHANGED
@@ -17,20 +17,28 @@ module Game2048
|
|
17
17
|
|
18
18
|
def run
|
19
19
|
@running = true
|
20
|
-
|
21
|
-
@terminal.hide_cursor
|
22
|
-
@render.refresh
|
20
|
+
terminal_init
|
23
21
|
|
24
22
|
Kernel.trap('SIGWINCH') do
|
25
23
|
@render.refresh
|
26
24
|
end
|
27
25
|
|
26
|
+
Kernel.trap('SIGCONT') do
|
27
|
+
terminal_init
|
28
|
+
end
|
29
|
+
|
28
30
|
while @running
|
29
31
|
@render.draw
|
30
32
|
key = @terminal.read
|
33
|
+
|
31
34
|
case key
|
32
|
-
when 'q'
|
35
|
+
when 'q', :ctrl_c
|
33
36
|
stop
|
37
|
+
when :ctrl_l
|
38
|
+
@render.refresh
|
39
|
+
when :ctrl_z
|
40
|
+
terminal_clear
|
41
|
+
Process.kill('SIGSTOP', Process.pid)
|
34
42
|
when 'r'
|
35
43
|
@tiles.reset
|
36
44
|
when '+'
|
@@ -57,10 +65,22 @@ module Game2048
|
|
57
65
|
end
|
58
66
|
end
|
59
67
|
|
68
|
+
terminal_clear
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def terminal_init
|
74
|
+
@terminal.raw_mode
|
75
|
+
@terminal.alt_screen_on
|
76
|
+
@terminal.hide_cursor
|
77
|
+
@render.refresh
|
78
|
+
end
|
79
|
+
|
80
|
+
def terminal_clear
|
60
81
|
@terminal.cooked_mode
|
61
|
-
@terminal.
|
82
|
+
@terminal.alt_screen_off
|
62
83
|
@terminal.show_cursor
|
63
|
-
@terminal.move_home
|
64
84
|
end
|
65
85
|
end
|
66
86
|
end
|
data/lib/game_2048/render.rb
CHANGED
@@ -166,7 +166,11 @@ module Game2048
|
|
166
166
|
end
|
167
167
|
|
168
168
|
def align
|
169
|
-
|
169
|
+
rows, cols = @terminal.display_size
|
170
|
+
return if rows.nil? || cols.nil?
|
171
|
+
|
172
|
+
@rows = rows
|
173
|
+
@cols = cols
|
170
174
|
n = Math.sqrt(Tiles::SIZE).to_i
|
171
175
|
@width = @size * 2 * n + n + 1
|
172
176
|
@height = @size * n + n + 1
|
data/lib/game_2048/terminal.rb
CHANGED
@@ -19,6 +19,8 @@ module Game2048
|
|
19
19
|
|
20
20
|
HIDE_CURSOR = '?25l'
|
21
21
|
SHOW_CURSOR = '?25h'
|
22
|
+
ALT_SCREEN_ON = '?1049h'
|
23
|
+
ALT_SCREEN_OFF = '?1049l'
|
22
24
|
|
23
25
|
RESET = 0
|
24
26
|
BOLD = 1
|
@@ -47,6 +49,12 @@ module Game2048
|
|
47
49
|
CUB => :left
|
48
50
|
}.freeze
|
49
51
|
|
52
|
+
CHARS_MAP = {
|
53
|
+
"\x03" => :ctrl_c,
|
54
|
+
"\x0c" => :ctrl_l,
|
55
|
+
"\x1a" => :ctrl_z
|
56
|
+
}.freeze
|
57
|
+
|
50
58
|
def initialize(input: $stdin, output: $stdout)
|
51
59
|
@input = input
|
52
60
|
@output = output
|
@@ -108,6 +116,14 @@ module Game2048
|
|
108
116
|
@output.write("#{CSI}#{SHOW_CURSOR}")
|
109
117
|
end
|
110
118
|
|
119
|
+
def alt_screen_on
|
120
|
+
@output.write("#{CSI}#{ALT_SCREEN_ON}")
|
121
|
+
end
|
122
|
+
|
123
|
+
def alt_screen_off
|
124
|
+
@output.write("#{CSI}#{ALT_SCREEN_OFF}")
|
125
|
+
end
|
126
|
+
|
111
127
|
def display_size
|
112
128
|
move_to(999, 999)
|
113
129
|
@output.write("#{CSI}#{CPR}")
|
@@ -145,7 +161,7 @@ module Game2048
|
|
145
161
|
end
|
146
162
|
end
|
147
163
|
|
148
|
-
data
|
164
|
+
CHARS_MAP.fetch(data, data)
|
149
165
|
end
|
150
166
|
|
151
167
|
def raw_mode
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: 2048-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- anim
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: me@telpart.ru
|