text2048 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +9 -3
- data/lib/text2048/app.rb +3 -3
- data/lib/text2048/board.rb +6 -6
- data/lib/text2048/curses_view.rb +1 -1
- data/lib/text2048/curses_view/colorize.rb +2 -1
- data/lib/text2048/curses_view/tile.rb +5 -5
- data/lib/text2048/monkey_patch/array/board.rb +2 -2
- data/lib/text2048/monkey_patch/array/tile.rb +1 -1
- data/lib/text2048/tile.rb +3 -2
- data/lib/text2048/version.rb +1 -1
- data/spec/text2048/board_spec.rb +68 -68
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30b141b8b2c0dc4ac46adadb4c29b3bb9eb242a0
|
4
|
+
data.tar.gz: e975eab497eeb64b7d2cba4f5630f964b0291c15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6983d688faca6be66191813f72fa171e3746cad2ebe211db7106c0b21a72987a5b285cc69c2b16e7dd8e57e95ea9f816594dc86eea2b39d357c0f25d3d0f1142
|
7
|
+
data.tar.gz: 29b156dd1b9c2aee69a85ca739c9fc4bdb81eb389daf116133645739a79544cad080cf48c96e90100dbfdd62187db4031b4f01aece4c4a3cc217282a2899edd7
|
data/README.md
CHANGED
@@ -28,6 +28,12 @@ Installation
|
|
28
28
|
$ gem install text2048
|
29
29
|
```
|
30
30
|
|
31
|
+
To upgrade to the latest version, use:
|
32
|
+
|
33
|
+
```
|
34
|
+
$ gem update text2048
|
35
|
+
```
|
36
|
+
|
31
37
|
How to Play
|
32
38
|
-----------
|
33
39
|
|
@@ -35,9 +41,9 @@ How to Play
|
|
35
41
|
$ 2048
|
36
42
|
```
|
37
43
|
|
38
|
-
- Use your arrow keys or vi keys (
|
39
|
-
-
|
40
|
-
-
|
44
|
+
- Use your arrow keys or vi keys (h/j/k/l) to move the tiles.
|
45
|
+
- +/- to increase or decrease the size of the tiles displayed.
|
46
|
+
- q to quit.
|
41
47
|
|
42
48
|
Links
|
43
49
|
-----
|
data/lib/text2048/app.rb
CHANGED
@@ -21,10 +21,10 @@ module Text2048
|
|
21
21
|
def show_title
|
22
22
|
@view.high_score(@high_score)
|
23
23
|
@view.message = 'PRESS ANY KEY TO START'
|
24
|
-
@board = Board.new([[
|
24
|
+
@board = Board.new([[nil, nil, nil, nil],
|
25
25
|
[2, 0, 4, 8],
|
26
|
-
[
|
27
|
-
[
|
26
|
+
[nil, nil, nil, nil],
|
27
|
+
[nil, nil, nil, nil]])
|
28
28
|
@view.update(@board)
|
29
29
|
@view.wait_any_key
|
30
30
|
end
|
data/lib/text2048/board.rb
CHANGED
@@ -11,7 +11,7 @@ module Text2048
|
|
11
11
|
# @return [Number] returns the current score
|
12
12
|
attr_reader :score
|
13
13
|
|
14
|
-
def initialize(tiles = Array.new(4
|
14
|
+
def initialize(tiles = Array.new(4, Array.new(4)), score = 0)
|
15
15
|
@all_tiles = tiles.hashinize
|
16
16
|
@score = score
|
17
17
|
end
|
@@ -76,7 +76,7 @@ module Text2048
|
|
76
76
|
# @return [Board] a new board
|
77
77
|
def generate
|
78
78
|
tiles = @all_tiles.dup
|
79
|
-
tiles[
|
79
|
+
tiles[sample_empty_tile] = Tile.new(rand < 0.9 ? 2 : 4, :generated)
|
80
80
|
new_board(tiles, @score)
|
81
81
|
end
|
82
82
|
|
@@ -123,13 +123,13 @@ module Text2048
|
|
123
123
|
new_board(to_a.transpose, @score)
|
124
124
|
end
|
125
125
|
|
126
|
-
def
|
126
|
+
def empty_tiles
|
127
127
|
@all_tiles.select { |_key, each| each.to_i == 0 }
|
128
128
|
end
|
129
129
|
|
130
|
-
def
|
131
|
-
fail if
|
132
|
-
|
130
|
+
def sample_empty_tile
|
131
|
+
fail if empty_tiles.empty?
|
132
|
+
empty_tiles.keys.shuffle.first
|
133
133
|
end
|
134
134
|
|
135
135
|
def new_board(tiles, score)
|
data/lib/text2048/curses_view.rb
CHANGED
@@ -122,7 +122,7 @@ module Text2048
|
|
122
122
|
[0, 1, 2, 3].product([0, 1, 2, 3]).each do |row, col|
|
123
123
|
tile = tiles[row][col]
|
124
124
|
@tiles[[row, col]] =
|
125
|
-
Tile.new(tile, row, col, color(tile.
|
125
|
+
Tile.new(tile, row, col, color(tile.value), @scale).show
|
126
126
|
refresh
|
127
127
|
end
|
128
128
|
end
|
@@ -27,9 +27,9 @@ module Text2048
|
|
27
27
|
(DEFAULT_HEIGHT * scale).to_i
|
28
28
|
end
|
29
29
|
|
30
|
-
def initialize(
|
30
|
+
def initialize(tile, row, col, color, scale = 1)
|
31
31
|
klass = self.class
|
32
|
-
@value = value
|
32
|
+
@value = tile.value
|
33
33
|
@height = klass.height(scale)
|
34
34
|
@width = klass.width(scale)
|
35
35
|
@row = (@height + 1) * row + 2
|
@@ -70,7 +70,7 @@ module Text2048
|
|
70
70
|
end
|
71
71
|
|
72
72
|
def draw_number
|
73
|
-
return
|
73
|
+
return unless @value
|
74
74
|
if @scale >= 1
|
75
75
|
draw_lcd_number
|
76
76
|
else
|
@@ -91,7 +91,7 @@ module Text2048
|
|
91
91
|
end
|
92
92
|
|
93
93
|
def col_padded
|
94
|
-
num_length = @value.to_s.length
|
94
|
+
num_length = @value.to_i.to_s.length
|
95
95
|
@col + (@width - num_length * 4 + 1) / 2
|
96
96
|
end
|
97
97
|
|
@@ -151,7 +151,7 @@ module Text2048
|
|
151
151
|
def fill
|
152
152
|
(0..(@height - 1)).each do |each|
|
153
153
|
setpos(@row + each, @col)
|
154
|
-
if @value
|
154
|
+
if @value && each == @height / 2
|
155
155
|
addstr @value.to_s.center(@width)
|
156
156
|
else
|
157
157
|
addstr('.' * @width)
|
@@ -7,11 +7,11 @@ module Text2048
|
|
7
7
|
# 2048 related methods
|
8
8
|
module Board
|
9
9
|
def hashinize
|
10
|
-
[0, 1, 2, 3].product([0, 1, 2, 3])
|
10
|
+
col_row = [0, 1, 2, 3].product([0, 1, 2, 3])
|
11
|
+
col_row.each_with_object({}) do |(col, row), memo|
|
11
12
|
tile = self[col][row]
|
12
13
|
memo[[col, row]] =
|
13
14
|
tile.respond_to?(:status) ? tile : Text2048::Tile.new(tile)
|
14
|
-
memo
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
data/lib/text2048/tile.rb
CHANGED
@@ -6,12 +6,13 @@ require 'forwardable'
|
|
6
6
|
module Text2048
|
7
7
|
# 2048 tile
|
8
8
|
class Tile
|
9
|
+
attr_reader :value
|
9
10
|
attr_reader :status
|
10
11
|
|
11
12
|
extend Forwardable
|
12
13
|
|
13
14
|
def initialize(value, status = nil)
|
14
|
-
@value = value
|
15
|
+
@value = value
|
15
16
|
@status = status
|
16
17
|
end
|
17
18
|
|
@@ -25,7 +26,7 @@ module Text2048
|
|
25
26
|
end
|
26
27
|
|
27
28
|
def ==(other)
|
28
|
-
@value == other.to_i
|
29
|
+
@value.to_i == other.to_i
|
29
30
|
end
|
30
31
|
|
31
32
|
def_delegators :@value, :to_i, :to_s
|
data/lib/text2048/version.rb
CHANGED
data/spec/text2048/board_spec.rb
CHANGED
@@ -45,10 +45,10 @@ describe Text2048::Board do
|
|
45
45
|
When(:result) { board.right }
|
46
46
|
|
47
47
|
Then do
|
48
|
-
result.to_a == [[
|
49
|
-
[
|
50
|
-
[
|
51
|
-
[
|
48
|
+
result.to_a == [[nil, nil, nil, nil],
|
49
|
+
[nil, nil, nil, nil],
|
50
|
+
[nil, nil, nil, nil],
|
51
|
+
[nil, nil, nil, nil]]
|
52
52
|
end
|
53
53
|
And { result.score == 0 }
|
54
54
|
end
|
@@ -57,10 +57,10 @@ describe Text2048::Board do
|
|
57
57
|
When(:result) { board.left }
|
58
58
|
|
59
59
|
Then do
|
60
|
-
result.to_a == [[
|
61
|
-
[
|
62
|
-
[
|
63
|
-
[
|
60
|
+
result.to_a == [[nil, nil, nil, nil],
|
61
|
+
[nil, nil, nil, nil],
|
62
|
+
[nil, nil, nil, nil],
|
63
|
+
[nil, nil, nil, nil]]
|
64
64
|
end
|
65
65
|
And { result.score == 0 }
|
66
66
|
end
|
@@ -69,10 +69,10 @@ describe Text2048::Board do
|
|
69
69
|
When(:result) { board.up }
|
70
70
|
|
71
71
|
Then do
|
72
|
-
result.to_a == [[
|
73
|
-
[
|
74
|
-
[
|
75
|
-
[
|
72
|
+
result.to_a == [[nil, nil, nil, nil],
|
73
|
+
[nil, nil, nil, nil],
|
74
|
+
[nil, nil, nil, nil],
|
75
|
+
[nil, nil, nil, nil]]
|
76
76
|
end
|
77
77
|
And { result.score == 0 }
|
78
78
|
end
|
@@ -81,10 +81,10 @@ describe Text2048::Board do
|
|
81
81
|
When(:result) { board.down }
|
82
82
|
|
83
83
|
Then do
|
84
|
-
result.to_a == [[
|
85
|
-
[
|
86
|
-
[
|
87
|
-
[
|
84
|
+
result.to_a == [[nil, nil, nil, nil],
|
85
|
+
[nil, nil, nil, nil],
|
86
|
+
[nil, nil, nil, nil],
|
87
|
+
[nil, nil, nil, nil]]
|
88
88
|
end
|
89
89
|
And { result.score == 0 }
|
90
90
|
end
|
@@ -92,20 +92,20 @@ describe Text2048::Board do
|
|
92
92
|
describe '#to_a' do
|
93
93
|
When(:result) { board.to_a }
|
94
94
|
Then do
|
95
|
-
result == [[
|
96
|
-
[
|
97
|
-
[
|
98
|
-
[
|
95
|
+
result == [[nil, nil, nil, nil],
|
96
|
+
[nil, nil, nil, nil],
|
97
|
+
[nil, nil, nil, nil],
|
98
|
+
[nil, nil, nil, nil]]
|
99
99
|
end
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
103
|
context 'with four 2s in diagonal' do
|
104
104
|
Given(:initial_tiles) do
|
105
|
-
[[2,
|
106
|
-
[
|
107
|
-
[
|
108
|
-
[
|
105
|
+
[[2, nil, nil, nil],
|
106
|
+
[nil, 2, nil, nil],
|
107
|
+
[nil, nil, 2, nil],
|
108
|
+
[nil, nil, nil, 2]]
|
109
109
|
end
|
110
110
|
|
111
111
|
describe '#tiles' do
|
@@ -144,10 +144,10 @@ describe Text2048::Board do
|
|
144
144
|
When(:result) { board.right }
|
145
145
|
|
146
146
|
Then do
|
147
|
-
result.to_a == [[
|
148
|
-
[
|
149
|
-
[
|
150
|
-
[
|
147
|
+
result.to_a == [[nil, nil, nil, 2],
|
148
|
+
[nil, nil, nil, 2],
|
149
|
+
[nil, nil, nil, 2],
|
150
|
+
[nil, nil, nil, 2]]
|
151
151
|
end
|
152
152
|
And { result.merged_tiles.empty? }
|
153
153
|
And { result.score == 0 }
|
@@ -157,10 +157,10 @@ describe Text2048::Board do
|
|
157
157
|
When(:result) { board.left }
|
158
158
|
|
159
159
|
Then do
|
160
|
-
result.to_a == [[2,
|
161
|
-
[2,
|
162
|
-
[2,
|
163
|
-
[2,
|
160
|
+
result.to_a == [[2, nil, nil, nil],
|
161
|
+
[2, nil, nil, nil],
|
162
|
+
[2, nil, nil, nil],
|
163
|
+
[2, nil, nil, nil]]
|
164
164
|
end
|
165
165
|
And { result.merged_tiles.empty? }
|
166
166
|
And { result.score == 0 }
|
@@ -171,9 +171,9 @@ describe Text2048::Board do
|
|
171
171
|
|
172
172
|
Then do
|
173
173
|
result.to_a == [[2, 2, 2, 2],
|
174
|
-
[
|
175
|
-
[
|
176
|
-
[
|
174
|
+
[nil, nil, nil, nil],
|
175
|
+
[nil, nil, nil, nil],
|
176
|
+
[nil, nil, nil, nil]]
|
177
177
|
end
|
178
178
|
And { result.merged_tiles.empty? }
|
179
179
|
And { result.score == 0 }
|
@@ -183,9 +183,9 @@ describe Text2048::Board do
|
|
183
183
|
When(:result) { board.down }
|
184
184
|
|
185
185
|
Then do
|
186
|
-
result.to_a == [[
|
187
|
-
[
|
188
|
-
[
|
186
|
+
result.to_a == [[nil, nil, nil, nil],
|
187
|
+
[nil, nil, nil, nil],
|
188
|
+
[nil, nil, nil, nil],
|
189
189
|
[2, 2, 2, 2]]
|
190
190
|
end
|
191
191
|
And { result.merged_tiles.empty? }
|
@@ -195,20 +195,20 @@ describe Text2048::Board do
|
|
195
195
|
describe '#to_a' do
|
196
196
|
When(:result) { board.to_a }
|
197
197
|
Then do
|
198
|
-
result == [[2,
|
199
|
-
[
|
200
|
-
[
|
201
|
-
[
|
198
|
+
result == [[2, nil, nil, nil],
|
199
|
+
[nil, 2, nil, nil],
|
200
|
+
[nil, nil, 2, nil],
|
201
|
+
[nil, nil, nil, 2]]
|
202
202
|
end
|
203
203
|
end
|
204
204
|
end
|
205
205
|
|
206
206
|
context 'with six 2s that can be merged' do
|
207
207
|
Given(:initial_tiles) do
|
208
|
-
[[2,
|
209
|
-
[
|
210
|
-
[
|
211
|
-
[
|
208
|
+
[[2, nil, 2, nil],
|
209
|
+
[nil, 2, nil, nil],
|
210
|
+
[nil, 2, nil, 2],
|
211
|
+
[nil, nil, nil, 2]]
|
212
212
|
end
|
213
213
|
|
214
214
|
describe '#tiles' do
|
@@ -249,10 +249,10 @@ describe Text2048::Board do
|
|
249
249
|
When(:result) { board.right }
|
250
250
|
|
251
251
|
Then do
|
252
|
-
result.to_a == [[
|
253
|
-
[
|
254
|
-
[
|
255
|
-
[
|
252
|
+
result.to_a == [[nil, nil, nil, 4],
|
253
|
+
[nil, nil, nil, 2],
|
254
|
+
[nil, nil, nil, 4],
|
255
|
+
[nil, nil, nil, 2]]
|
256
256
|
end
|
257
257
|
And { result.to_a[0][3].merged? }
|
258
258
|
And { result.to_a[2][3].merged? }
|
@@ -264,10 +264,10 @@ describe Text2048::Board do
|
|
264
264
|
When(:result) { board.left }
|
265
265
|
|
266
266
|
Then do
|
267
|
-
result.to_a == [[4,
|
268
|
-
[2,
|
269
|
-
[4,
|
270
|
-
[2,
|
267
|
+
result.to_a == [[4, nil, nil, nil],
|
268
|
+
[2, nil, nil, nil],
|
269
|
+
[4, nil, nil, nil],
|
270
|
+
[2, nil, nil, nil]]
|
271
271
|
end
|
272
272
|
And { result.to_a[0][0].merged? }
|
273
273
|
And { result.to_a[2][0].merged? }
|
@@ -280,9 +280,9 @@ describe Text2048::Board do
|
|
280
280
|
|
281
281
|
Then do
|
282
282
|
result.to_a == [[2, 4, 2, 4],
|
283
|
-
[
|
284
|
-
[
|
285
|
-
[
|
283
|
+
[nil, nil, nil, nil],
|
284
|
+
[nil, nil, nil, nil],
|
285
|
+
[nil, nil, nil, nil]]
|
286
286
|
end
|
287
287
|
And { result.to_a[0][1].merged? }
|
288
288
|
And { result.to_a[0][3].merged? }
|
@@ -294,9 +294,9 @@ describe Text2048::Board do
|
|
294
294
|
When(:result) { board.down }
|
295
295
|
|
296
296
|
Then do
|
297
|
-
result.to_a == [[
|
298
|
-
[
|
299
|
-
[
|
297
|
+
result.to_a == [[nil, nil, nil, nil],
|
298
|
+
[nil, nil, nil, nil],
|
299
|
+
[nil, nil, nil, nil],
|
300
300
|
[2, 4, 2, 4]]
|
301
301
|
end
|
302
302
|
And { result.to_a[3][1].merged? }
|
@@ -308,20 +308,20 @@ describe Text2048::Board do
|
|
308
308
|
describe '#to_a' do
|
309
309
|
When(:result) { board.to_a }
|
310
310
|
Then do
|
311
|
-
result == [[2,
|
312
|
-
[
|
313
|
-
[
|
314
|
-
[
|
311
|
+
result == [[2, nil, 2, nil],
|
312
|
+
[nil, 2, nil, nil],
|
313
|
+
[nil, 2, nil, 2],
|
314
|
+
[nil, nil, nil, 2]]
|
315
315
|
end
|
316
316
|
end
|
317
317
|
end
|
318
318
|
|
319
319
|
context 'with one 2048 tile' do
|
320
320
|
Given(:initial_tiles) do
|
321
|
-
[[
|
322
|
-
[
|
323
|
-
[
|
324
|
-
[
|
321
|
+
[[nil, nil, nil, nil],
|
322
|
+
[nil, nil, nil, nil],
|
323
|
+
[nil, 2048, nil, nil],
|
324
|
+
[nil, nil, nil, nil]]
|
325
325
|
end
|
326
326
|
|
327
327
|
describe '#win?' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: text2048
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yasuhito Takamiya
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|