text2048 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9db309419e40329c97f3490cacf5b307023929e5
4
- data.tar.gz: 24055097be1b6246422694cb75b2b72685d37253
3
+ metadata.gz: 30b141b8b2c0dc4ac46adadb4c29b3bb9eb242a0
4
+ data.tar.gz: e975eab497eeb64b7d2cba4f5630f964b0291c15
5
5
  SHA512:
6
- metadata.gz: d8d9031ff6562432a491aab20592ad4f81148522417e1f5ef08a13686bcf13e7ba5301da9d403ad302601ca43cc874532dae8958f43a56658b3fa4fbc1587cf6
7
- data.tar.gz: 16d316b287ac2bd945e2e428026ad40990fac2d06fadfd9c742ef5cc7088d1ee9752fe9c23e713dc3337176d3e967551d65d91996cbfde0a6d440372fc2739f1
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 ('h'/'j'/'k'/'l') to move the tiles.
39
- - '+'/'-' to increase or decrease the size of the tiles displayed.
40
- - 'q' to quit.
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([[0, 0, 0, 0],
24
+ @board = Board.new([[nil, nil, nil, nil],
25
25
  [2, 0, 4, 8],
26
- [0, 0, 0, 0],
27
- [0, 0, 0, 0]])
26
+ [nil, nil, nil, nil],
27
+ [nil, nil, nil, nil]])
28
28
  @view.update(@board)
29
29
  @view.wait_any_key
30
30
  end
@@ -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) { Array.new(4, 0) }, score = 0)
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[sample_zero_tile] = Tile.new(rand < 0.9 ? 2 : 4, :generated)
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 zero_tiles
126
+ def empty_tiles
127
127
  @all_tiles.select { |_key, each| each.to_i == 0 }
128
128
  end
129
129
 
130
- def sample_zero_tile
131
- fail if zero_tiles.empty?
132
- zero_tiles.keys.shuffle.first
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)
@@ -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.to_i), @scale).show
125
+ Tile.new(tile, row, col, color(tile.value), @scale).show
126
126
  refresh
127
127
  end
128
128
  end
@@ -10,7 +10,8 @@ module Text2048
10
10
  include Curses
11
11
 
12
12
  COLORS = {
13
- 0 => COLOR_BLACK,
13
+ nil => COLOR_BLACK,
14
+ 0 => COLOR_WHITE,
14
15
  2 => COLOR_WHITE,
15
16
  4 => COLOR_GREEN,
16
17
  8 => COLOR_GREEN,
@@ -27,9 +27,9 @@ module Text2048
27
27
  (DEFAULT_HEIGHT * scale).to_i
28
28
  end
29
29
 
30
- def initialize(value, row, col, color, scale = 1)
30
+ def initialize(tile, row, col, color, scale = 1)
31
31
  klass = self.class
32
- @value = value.to_i
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 if @value == 0
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 != 0 && each == @height / 2
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]).reduce({}) do |memo, (col, row)|
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
@@ -28,7 +28,7 @@ module Text2048
28
28
 
29
29
  def fill_length(len)
30
30
  compact!
31
- unshift(Text2048::Tile.new(0)) until size == len
31
+ unshift(Text2048::Tile.new(nil)) until size == len
32
32
  self
33
33
  end
34
34
  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.to_i
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
@@ -3,5 +3,5 @@
3
3
  # This module smells of :reek:UncommunicativeModuleName
4
4
  module Text2048
5
5
  # gem version.
6
- VERSION = '0.9.0'.freeze
6
+ VERSION = '0.10.0'.freeze
7
7
  end
@@ -45,10 +45,10 @@ describe Text2048::Board do
45
45
  When(:result) { board.right }
46
46
 
47
47
  Then do
48
- result.to_a == [[0, 0, 0, 0],
49
- [0, 0, 0, 0],
50
- [0, 0, 0, 0],
51
- [0, 0, 0, 0]]
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 == [[0, 0, 0, 0],
61
- [0, 0, 0, 0],
62
- [0, 0, 0, 0],
63
- [0, 0, 0, 0]]
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 == [[0, 0, 0, 0],
73
- [0, 0, 0, 0],
74
- [0, 0, 0, 0],
75
- [0, 0, 0, 0]]
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 == [[0, 0, 0, 0],
85
- [0, 0, 0, 0],
86
- [0, 0, 0, 0],
87
- [0, 0, 0, 0]]
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 == [[0, 0, 0, 0],
96
- [0, 0, 0, 0],
97
- [0, 0, 0, 0],
98
- [0, 0, 0, 0]]
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, 0, 0, 0],
106
- [0, 2, 0, 0],
107
- [0, 0, 2, 0],
108
- [0, 0, 0, 2]]
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 == [[0, 0, 0, 2],
148
- [0, 0, 0, 2],
149
- [0, 0, 0, 2],
150
- [0, 0, 0, 2]]
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, 0, 0, 0],
161
- [2, 0, 0, 0],
162
- [2, 0, 0, 0],
163
- [2, 0, 0, 0]]
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
- [0, 0, 0, 0],
175
- [0, 0, 0, 0],
176
- [0, 0, 0, 0]]
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 == [[0, 0, 0, 0],
187
- [0, 0, 0, 0],
188
- [0, 0, 0, 0],
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, 0, 0, 0],
199
- [0, 2, 0, 0],
200
- [0, 0, 2, 0],
201
- [0, 0, 0, 2]]
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, 0, 2, 0],
209
- [0, 2, 0, 0],
210
- [0, 2, 0, 2],
211
- [0, 0, 0, 2]]
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 == [[0, 0, 0, 4],
253
- [0, 0, 0, 2],
254
- [0, 0, 0, 4],
255
- [0, 0, 0, 2]]
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, 0, 0, 0],
268
- [2, 0, 0, 0],
269
- [4, 0, 0, 0],
270
- [2, 0, 0, 0]]
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
- [0, 0, 0, 0],
284
- [0, 0, 0, 0],
285
- [0, 0, 0, 0]]
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 == [[0, 0, 0, 0],
298
- [0, 0, 0, 0],
299
- [0, 0, 0, 0],
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, 0, 2, 0],
312
- [0, 2, 0, 0],
313
- [0, 2, 0, 2],
314
- [0, 0, 0, 2]]
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
- [[0, 0, 0, 0],
322
- [0, 0, 0, 0],
323
- [0, 2048, 0, 0],
324
- [0, 0, 0, 0]]
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.9.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-20 00:00:00.000000000 Z
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler