text2048 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -12
- data/features/step_definitions/2048_steps.rb +2 -2
- data/lib/text2048/app.rb +7 -2
- data/lib/text2048/board.rb +22 -41
- data/lib/text2048/curses_tile.rb +25 -16
- data/lib/text2048/curses_view.rb +39 -46
- data/lib/text2048/monkey_patch/array.rb +2 -0
- data/lib/text2048/monkey_patch/array/board.rb +19 -0
- data/lib/text2048/monkey_patch/array/tile.rb +5 -6
- data/lib/text2048/text_view.rb +3 -3
- data/lib/text2048/tiles.rb +1 -1
- data/lib/text2048/version.rb +1 -1
- data/spec/text2048/board_spec.rb +35 -24
- data/spec/text2048/tiles_spec.rb +26 -26
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a002ee28cb82470ec94b93d63f285f907d2eaaa2
|
4
|
+
data.tar.gz: 1c290e6b0968fbcc2978d701f640f8f0c750a383
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a5da8d1544c359adfd7212a9cd9cac4ec81cd7f4f056ddbf5b6ee5ac2e81cc3963727f0bb0ba4a1b0a69b72893fd9902b0ebec7c8b3c8b018e92f806a0e7a92
|
7
|
+
data.tar.gz: 51f12d2914d8ebb9cff9a10500a9de1494a9d48936cf777d1bba2261c763b546c12063eecdedb2b57d5da0af45b41e87dffc46fd36141a8070e8d1393414a09e
|
data/README.md
CHANGED
@@ -8,11 +8,21 @@ text2048
|
|
8
8
|
|
9
9
|
Text mode 2048 game.
|
10
10
|
|
11
|
+
[![Screenshot](https://raw.github.com/yasuhito/text2048/develop/screen_shot.png)][screenshot]
|
12
|
+
|
11
13
|
[gem]: https://rubygems.org/gems/text2048
|
12
14
|
[travis]: http://travis-ci.org/yasuhito/text2048
|
13
15
|
[codeclimate]: https://codeclimate.com/github/yasuhito/text2048
|
14
16
|
[coveralls]: https://coveralls.io/r/yasuhito/text2048?branch=develop
|
15
17
|
[gemnasium]: https://gemnasium.com/yasuhito/text2048
|
18
|
+
[screenshot]: https://raw.github.com/yasuhito/text2048/develop/screen_shot.png
|
19
|
+
|
20
|
+
Installation
|
21
|
+
============
|
22
|
+
|
23
|
+
```
|
24
|
+
$ gem install text2048
|
25
|
+
```
|
16
26
|
|
17
27
|
How to Play
|
18
28
|
===========
|
@@ -24,17 +34,8 @@ $ 2048
|
|
24
34
|
- Use your arrow keys to move the tiles.
|
25
35
|
- +/- for increase or decrease the size of the tiles displayed.
|
26
36
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
Installation
|
31
|
-
============
|
32
|
-
|
33
|
-
```
|
34
|
-
$ gem install text2048
|
35
|
-
```
|
36
|
-
|
37
|
-
Link
|
38
|
-
====
|
37
|
+
Links
|
38
|
+
=====
|
39
39
|
|
40
40
|
* [The official version of 2048](http://gabrielecirulli.github.io/2048/) by Gabriele Cirulli
|
41
|
+
* [bfontaine/term2048](https://github.com/bfontaine/term2048) a terminal-based version in Python
|
@@ -3,8 +3,8 @@
|
|
3
3
|
require 'text2048'
|
4
4
|
|
5
5
|
Given(/^a board:$/) do |string|
|
6
|
-
layout = string.split("\n").reduce([]) do |
|
7
|
-
|
6
|
+
layout = string.split("\n").reduce([]) do |memo, row|
|
7
|
+
memo << row.split(' ').map { |each| each == '_' ? nil : each.to_i }
|
8
8
|
end
|
9
9
|
@view = Text2048::TextView.new(output)
|
10
10
|
@board = Text2048::Board.new(layout)
|
data/lib/text2048/app.rb
CHANGED
@@ -20,10 +20,15 @@ module Text2048
|
|
20
20
|
def initialize
|
21
21
|
@view = CursesView.new
|
22
22
|
@board = Board.new
|
23
|
-
|
23
|
+
init_screen
|
24
|
+
curs_set(0)
|
25
|
+
noecho
|
26
|
+
stdscr.keypad(true)
|
27
|
+
at_exit { close_screen }
|
24
28
|
end
|
25
29
|
|
26
30
|
def start
|
31
|
+
@view.update(@board)
|
27
32
|
loop do
|
28
33
|
@view.win if @board.win?
|
29
34
|
@view.game_over if @board.lose?
|
@@ -46,7 +51,7 @@ module Text2048
|
|
46
51
|
|
47
52
|
def move_and_generate(command)
|
48
53
|
last = move(command)
|
49
|
-
generate if @board != last
|
54
|
+
generate if @board.to_a != last.to_a
|
50
55
|
end
|
51
56
|
|
52
57
|
def move(command)
|
data/lib/text2048/board.rb
CHANGED
@@ -13,9 +13,9 @@ module Text2048
|
|
13
13
|
def initialize(tiles = nil, score = 0)
|
14
14
|
@score = score
|
15
15
|
if tiles
|
16
|
-
@tiles = tiles.
|
16
|
+
@tiles = tiles.to_h
|
17
17
|
else
|
18
|
-
@tiles = Array.new(4) { Array.new(4)
|
18
|
+
@tiles = (Array.new(4) { Array.new(4) }).to_h
|
19
19
|
2.times { generate }
|
20
20
|
end
|
21
21
|
end
|
@@ -24,20 +24,22 @@ module Text2048
|
|
24
24
|
@tiles = board.tiles.dup
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
28
|
-
@tiles.
|
29
|
-
row
|
27
|
+
def to_a
|
28
|
+
@tiles.reduce(Array.new(4) { Array.new(4) }) do |array, (key, value)|
|
29
|
+
row, col = key
|
30
|
+
array[row][col] = value && value.to_i
|
31
|
+
array
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
35
|
def win?
|
34
|
-
|
35
|
-
each.to_i >= 2048
|
36
|
-
end.size > 0
|
36
|
+
@tiles.any? { |_key, value| value.to_i >= 2048 }
|
37
37
|
end
|
38
38
|
|
39
39
|
def lose?
|
40
|
-
right.left.up.down.
|
40
|
+
right.left.up.down.tiles.select do |_key, each|
|
41
|
+
each.to_i > 0
|
42
|
+
end.size == 4 * 4
|
41
43
|
end
|
42
44
|
|
43
45
|
def left
|
@@ -60,10 +62,6 @@ module Text2048
|
|
60
62
|
self.class.new tiles, @score + score
|
61
63
|
end
|
62
64
|
|
63
|
-
def ==(other)
|
64
|
-
layout == other.layout
|
65
|
-
end
|
66
|
-
|
67
65
|
def merged_tiles
|
68
66
|
find_tiles :merged
|
69
67
|
end
|
@@ -74,49 +72,32 @@ module Text2048
|
|
74
72
|
|
75
73
|
def generate
|
76
74
|
loop do
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
@tiles[line][col] = Tile.new(rand < 0.8 ? 2 : 4, :generated)
|
75
|
+
sample = @tiles.keys.sample
|
76
|
+
unless @tiles[sample]
|
77
|
+
@tiles[sample] = Tile.new(rand < 0.8 ? 2 : 4, :generated)
|
81
78
|
return
|
82
79
|
end
|
83
80
|
end
|
84
81
|
end
|
85
82
|
|
86
|
-
def numbers
|
87
|
-
tiles.reduce([]) do |result, row|
|
88
|
-
result + row.select { |each| each != 0 }
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
83
|
private
|
93
84
|
|
94
85
|
def move(direction)
|
95
|
-
|
96
|
-
|
86
|
+
to_a.reduce([[], 0]) do |memo, each|
|
87
|
+
tiles, score = memo
|
97
88
|
row, sc = Tiles.new(each).__send__ direction
|
98
|
-
score
|
99
|
-
row
|
89
|
+
[tiles << row, score + sc]
|
100
90
|
end
|
101
|
-
[tiles, score]
|
102
91
|
end
|
103
92
|
|
104
93
|
def find_tiles(status)
|
105
|
-
|
106
|
-
@tiles.each_with_index do |row, line|
|
107
|
-
row.each_with_index do |each, col|
|
108
|
-
list << [line, col] if each.status == status
|
109
|
-
end
|
110
|
-
end
|
111
|
-
list
|
94
|
+
@tiles.select { |_key, each| each && each.status == status }.keys
|
112
95
|
end
|
113
96
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
@tiles = @tiles.transpose
|
119
|
-
[@tiles, score]
|
97
|
+
def transpose(&block)
|
98
|
+
board = self.class.new(to_a.transpose, @score)
|
99
|
+
tiles, score = board.instance_eval(&block)
|
100
|
+
[tiles.transpose, score]
|
120
101
|
end
|
121
102
|
end
|
122
103
|
end
|
data/lib/text2048/curses_tile.rb
CHANGED
@@ -15,13 +15,22 @@ module Text2048
|
|
15
15
|
DEFAULT_HEIGHT = 3
|
16
16
|
DEFAULT_WIDTH = 5
|
17
17
|
|
18
|
-
def
|
18
|
+
def self.width(scale)
|
19
|
+
@width = (DEFAULT_WIDTH * scale).to_i
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.height(scale)
|
23
|
+
(DEFAULT_HEIGHT * scale).to_i
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(value, row, col, color, scale = 1)
|
27
|
+
klass = self.class
|
19
28
|
@value = value.to_i
|
20
|
-
@height = (
|
29
|
+
@height = klass.height(scale)
|
21
30
|
@box_height = @height + 2
|
22
|
-
@width = (
|
31
|
+
@width = klass.width(scale)
|
23
32
|
@box_width = @width + 2
|
24
|
-
@
|
33
|
+
@row = (@height + 1) * row + 2
|
25
34
|
@col = (@width + 1) * col + 1
|
26
35
|
@color = color
|
27
36
|
end
|
@@ -42,8 +51,8 @@ module Text2048
|
|
42
51
|
def draw_box
|
43
52
|
draw_square
|
44
53
|
[box_upper_left, box_upper_right,
|
45
|
-
box_lower_left, box_lower_right].each do |
|
46
|
-
setpos(
|
54
|
+
box_lower_left, box_lower_right].each do |row, col|
|
55
|
+
setpos(row, col)
|
47
56
|
addstr('+')
|
48
57
|
end
|
49
58
|
end
|
@@ -59,7 +68,7 @@ module Text2048
|
|
59
68
|
|
60
69
|
def draw_number
|
61
70
|
return if @value == 0
|
62
|
-
setpos(@
|
71
|
+
setpos(@row + @height / 2, @col)
|
63
72
|
attron(color_pair(@color)) do
|
64
73
|
addstr @value.to_s.center(@width)
|
65
74
|
end
|
@@ -68,19 +77,19 @@ module Text2048
|
|
68
77
|
private
|
69
78
|
|
70
79
|
def box_upper_left
|
71
|
-
[@
|
80
|
+
[@row - 1, @col - 1]
|
72
81
|
end
|
73
82
|
|
74
83
|
def box_upper_right
|
75
|
-
[@
|
84
|
+
[@row - 1, @col + @width]
|
76
85
|
end
|
77
86
|
|
78
87
|
def box_lower_left
|
79
|
-
[@
|
88
|
+
[@row + @height, @col - 1]
|
80
89
|
end
|
81
90
|
|
82
91
|
def box_lower_right
|
83
|
-
[@
|
92
|
+
[@row + @height, @col + @width]
|
84
93
|
end
|
85
94
|
|
86
95
|
def draw_square
|
@@ -90,21 +99,21 @@ module Text2048
|
|
90
99
|
draw_horizonal_line(*box_lower_left, @box_width)
|
91
100
|
end
|
92
101
|
|
93
|
-
def draw_horizonal_line(
|
94
|
-
setpos(
|
102
|
+
def draw_horizonal_line(row, col, length)
|
103
|
+
setpos(row, col)
|
95
104
|
addstr('-' * length)
|
96
105
|
end
|
97
106
|
|
98
|
-
def draw_vertical_line(
|
107
|
+
def draw_vertical_line(row, col, length)
|
99
108
|
(0..(length - 1)).each do |each|
|
100
|
-
setpos(
|
109
|
+
setpos(row + each, col)
|
101
110
|
addstr('|')
|
102
111
|
end
|
103
112
|
end
|
104
113
|
|
105
114
|
def fill
|
106
115
|
(0..(@height - 1)).each do |each|
|
107
|
-
setpos(@
|
116
|
+
setpos(@row + each, @col)
|
108
117
|
if @value != 0 && each == @height / 2
|
109
118
|
addstr @value.to_s.center(@width)
|
110
119
|
else
|
data/lib/text2048/curses_view.rb
CHANGED
@@ -10,16 +10,16 @@ module Text2048
|
|
10
10
|
# Curses tile effects
|
11
11
|
module TileEffects
|
12
12
|
def pop_tiles(list)
|
13
|
-
pop
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
[:pop, :draw_box].each do |each|
|
14
|
+
list_do each, list
|
15
|
+
refresh
|
16
|
+
sleep 0.1
|
17
|
+
end
|
18
18
|
end
|
19
19
|
|
20
20
|
def zoom_tiles(list)
|
21
21
|
[:fill_black, :draw_number, :show].each do |each|
|
22
|
-
|
22
|
+
list_do each, list
|
23
23
|
refresh
|
24
24
|
sleep 0.05
|
25
25
|
end
|
@@ -27,12 +27,8 @@ module Text2048
|
|
27
27
|
|
28
28
|
private
|
29
29
|
|
30
|
-
def
|
31
|
-
list.each { |
|
32
|
-
end
|
33
|
-
|
34
|
-
def draw_box(list)
|
35
|
-
list.each { |line, col| @tiles[line][col].draw_box }
|
30
|
+
def list_do(name, list)
|
31
|
+
list.each { |each| @tiles[each].__send__ name }
|
36
32
|
end
|
37
33
|
end
|
38
34
|
|
@@ -58,7 +54,7 @@ module Text2048
|
|
58
54
|
DEFAULT_HEIGHT = (CursesTile::DEFAULT_HEIGHT + 1) * 4 + 2
|
59
55
|
|
60
56
|
def initialize
|
61
|
-
@tiles =
|
57
|
+
@tiles = {}
|
62
58
|
@scale = 2
|
63
59
|
@scale_min = 1
|
64
60
|
@scale_step = 0.5
|
@@ -67,60 +63,65 @@ module Text2048
|
|
67
63
|
def update(board)
|
68
64
|
maybe_init_curses
|
69
65
|
draw_score(board.score)
|
70
|
-
draw_tiles(board.
|
66
|
+
draw_tiles(board.to_a)
|
71
67
|
refresh
|
72
|
-
# zoom_tiles(board.generated_tiles)
|
73
68
|
end
|
74
69
|
|
75
70
|
def height
|
76
|
-
(
|
71
|
+
(CursesTile.height(@scale) + 1) * 4 + 1
|
77
72
|
end
|
78
73
|
|
79
74
|
def width
|
80
|
-
(
|
75
|
+
(CursesTile.width(@scale) + 1) * 4 + 1
|
81
76
|
end
|
82
77
|
|
83
78
|
def larger(board)
|
84
|
-
|
85
|
-
|
86
|
-
@scale
|
87
|
-
|
88
|
-
update(board)
|
79
|
+
rwidth = (Curses.cols - 1) / DEFAULT_WIDTH
|
80
|
+
rheight = Curses.lines / DEFAULT_HEIGHT
|
81
|
+
return if @scale > [rwidth, rheight].min
|
82
|
+
change_scale(board, @scale_step)
|
89
83
|
end
|
90
84
|
|
91
85
|
def smaller(board)
|
92
86
|
return if @scale <= @scale_min
|
93
|
-
|
94
|
-
@scale -= @scale_step
|
95
|
-
clear
|
96
|
-
update(board)
|
87
|
+
change_scale(board, -1 * @scale_step)
|
97
88
|
end
|
98
89
|
|
99
90
|
def win
|
100
|
-
setpos(
|
91
|
+
setpos(rows_center, cols_center - 1)
|
101
92
|
attron(color_pair(COLOR_RED)) { addstr('WIN!') }
|
102
93
|
end
|
103
94
|
|
104
95
|
def game_over
|
105
|
-
setpos(
|
96
|
+
setpos(rows_center, cols_center - 4)
|
106
97
|
attron(color_pair(COLOR_RED)) { addstr('GAME OVER') }
|
107
98
|
end
|
108
99
|
|
109
100
|
private
|
110
101
|
|
102
|
+
def change_scale(board, scale_step)
|
103
|
+
maybe_init_curses
|
104
|
+
@scale += scale_step
|
105
|
+
clear
|
106
|
+
update(board)
|
107
|
+
end
|
108
|
+
|
109
|
+
def rows_center
|
110
|
+
height / 2
|
111
|
+
end
|
112
|
+
|
113
|
+
def cols_center
|
114
|
+
width / 2
|
115
|
+
end
|
116
|
+
|
111
117
|
def maybe_init_curses
|
112
118
|
@curses_initialized || init_curses
|
113
119
|
@curses_initialized = true
|
114
120
|
end
|
115
121
|
|
116
122
|
def init_curses
|
117
|
-
init_screen
|
118
|
-
curs_set(0)
|
119
123
|
start_color
|
120
|
-
stdscr.keypad(true)
|
121
|
-
noecho
|
122
124
|
init_color_pairs
|
123
|
-
at_exit { close_screen }
|
124
125
|
end
|
125
126
|
|
126
127
|
def init_color_pairs
|
@@ -130,25 +131,17 @@ module Text2048
|
|
130
131
|
end
|
131
132
|
end
|
132
133
|
|
133
|
-
def scale_max
|
134
|
-
ratio_width = (cols - 1) / DEFAULT_WIDTH
|
135
|
-
ratio_height = lines / DEFAULT_HEIGHT
|
136
|
-
ratio_width < ratio_height ? ratio_width : ratio_height
|
137
|
-
end
|
138
|
-
|
139
134
|
def draw_score(score)
|
140
135
|
setpos(0, 0)
|
141
136
|
addstr("Score: #{score}")
|
142
137
|
end
|
143
138
|
|
144
139
|
def draw_tiles(tiles)
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
@tiles[line][col] =
|
151
|
-
CursesTile.new(each, line, col, COLORS[each.to_i], @scale).show
|
140
|
+
[0, 1, 2, 3].product([0, 1, 2, 3]).each do |row, col|
|
141
|
+
tile = tiles[row][col]
|
142
|
+
@tiles[[row, col]] =
|
143
|
+
CursesTile.new(tile, row, col, COLORS[tile.to_i], @scale).show
|
144
|
+
refresh
|
152
145
|
end
|
153
146
|
end
|
154
147
|
end
|
@@ -1,8 +1,10 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
require 'text2048/monkey_patch/array/board'
|
3
4
|
require 'text2048/monkey_patch/array/tile'
|
4
5
|
|
5
6
|
# Monkey-patched standard Array class.
|
6
7
|
class Array
|
8
|
+
include Text2048::MonkeyPatch::Array::Board
|
7
9
|
include Text2048::MonkeyPatch::Array::Tile
|
8
10
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# This module smells of :reek:UncommunicativeModuleName
|
4
|
+
module Text2048
|
5
|
+
module MonkeyPatch
|
6
|
+
module Array
|
7
|
+
# 2048 related methods
|
8
|
+
module Board
|
9
|
+
def to_h
|
10
|
+
tiles = {}
|
11
|
+
[0, 1, 2, 3].product([0, 1, 2, 3]).each do |col, row|
|
12
|
+
tiles[[col, row]] = self[col][row]
|
13
|
+
end
|
14
|
+
tiles
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -10,9 +10,9 @@ module Text2048
|
|
10
10
|
score = 0
|
11
11
|
tiles = dup
|
12
12
|
(size - 1).downto(1) do |each|
|
13
|
-
if tiles[each - 1] == tiles[each]
|
13
|
+
if tiles[each - 1] && tiles[each - 1] == tiles[each]
|
14
14
|
tiles[each] = Text2048::Tile.new(tiles[each].to_i * 2, :merged)
|
15
|
-
tiles[each - 1] =
|
15
|
+
tiles[each - 1] = nil
|
16
16
|
score += tiles[each].to_i
|
17
17
|
end
|
18
18
|
end
|
@@ -20,10 +20,9 @@ module Text2048
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def rshrink
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
::Array.new(orig_size - tiles.size) { Text2048::Tile.new(0) } + tiles
|
23
|
+
orig_size = size
|
24
|
+
tiles = compact
|
25
|
+
::Array.new(orig_size - tiles.size) + tiles
|
27
26
|
end
|
28
27
|
end
|
29
28
|
end
|
data/lib/text2048/text_view.rb
CHANGED
@@ -11,7 +11,7 @@ module Text2048
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def to_s
|
14
|
-
@row.map { |each| each
|
14
|
+
@row.map { |each| each ? each.to_s : '_' }.join(' ')
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -19,8 +19,8 @@ module Text2048
|
|
19
19
|
@output = output
|
20
20
|
end
|
21
21
|
|
22
|
-
def update(
|
23
|
-
@output.puts
|
22
|
+
def update(board)
|
23
|
+
@output.puts board.to_a.map { |row| Row.new(row).to_s }.join("\n")
|
24
24
|
end
|
25
25
|
|
26
26
|
def pop_tiles(_list)
|
data/lib/text2048/tiles.rb
CHANGED
data/lib/text2048/version.rb
CHANGED
data/spec/text2048/board_spec.rb
CHANGED
@@ -5,60 +5,71 @@ require 'text2048'
|
|
5
5
|
describe Text2048::Board, '.new' do
|
6
6
|
context 'with all zeroes' do
|
7
7
|
Given(:board) do
|
8
|
-
Text2048::Board.new([[
|
9
|
-
[
|
10
|
-
[
|
11
|
-
[
|
8
|
+
Text2048::Board.new([[nil, nil, nil, nil],
|
9
|
+
[nil, nil, nil, nil],
|
10
|
+
[nil, nil, nil, nil],
|
11
|
+
[nil, nil, nil, nil]])
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#to_a' do
|
15
|
+
When(:result) { board.to_a }
|
16
|
+
|
17
|
+
Then do
|
18
|
+
result == [[nil, nil, nil, nil],
|
19
|
+
[nil, nil, nil, nil],
|
20
|
+
[nil, nil, nil, nil],
|
21
|
+
[nil, nil, nil, nil]]
|
22
|
+
end
|
12
23
|
end
|
13
24
|
|
14
25
|
describe '#right' do
|
15
26
|
When(:result) { board.right }
|
16
27
|
|
17
28
|
Then do
|
18
|
-
result.
|
19
|
-
|
20
|
-
|
21
|
-
|
29
|
+
result.to_a == [[nil, nil, nil, nil],
|
30
|
+
[nil, nil, nil, nil],
|
31
|
+
[nil, nil, nil, nil],
|
32
|
+
[nil, nil, nil, nil]]
|
22
33
|
end
|
23
34
|
end
|
24
35
|
end
|
25
36
|
|
26
37
|
context 'with four 2s' do
|
27
38
|
Given(:board) do
|
28
|
-
Text2048::Board.new([[2,
|
29
|
-
[
|
30
|
-
[
|
31
|
-
[
|
39
|
+
Text2048::Board.new([[2, nil, nil, nil],
|
40
|
+
[nil, 2, nil, nil],
|
41
|
+
[nil, nil, 2, nil],
|
42
|
+
[nil, nil, nil, 2]])
|
32
43
|
end
|
33
44
|
|
34
45
|
describe '#right' do
|
35
46
|
When(:result) { board.right }
|
36
47
|
|
37
48
|
Then do
|
38
|
-
result.
|
39
|
-
|
40
|
-
|
41
|
-
|
49
|
+
result.to_a == [[nil, nil, nil, 2],
|
50
|
+
[nil, nil, nil, 2],
|
51
|
+
[nil, nil, nil, 2],
|
52
|
+
[nil, nil, nil, 2]]
|
42
53
|
end
|
43
54
|
end
|
44
55
|
end
|
45
56
|
|
46
57
|
context 'with six 2s that can be merged' do
|
47
58
|
Given(:board) do
|
48
|
-
Text2048::Board.new([[2,
|
49
|
-
[
|
50
|
-
[
|
51
|
-
[
|
59
|
+
Text2048::Board.new([[2, nil, 2, nil],
|
60
|
+
[nil, 2, nil, nil],
|
61
|
+
[nil, 2, nil, 2],
|
62
|
+
[nil, nil, nil, 2]])
|
52
63
|
end
|
53
64
|
|
54
65
|
describe '#right' do
|
55
66
|
When(:result) { board.right }
|
56
67
|
|
57
68
|
Then do
|
58
|
-
result.
|
59
|
-
|
60
|
-
|
61
|
-
|
69
|
+
result.to_a == [[nil, nil, nil, 4],
|
70
|
+
[nil, nil, nil, 2],
|
71
|
+
[nil, nil, nil, 4],
|
72
|
+
[nil, nil, nil, 2]]
|
62
73
|
end
|
63
74
|
end
|
64
75
|
end
|
data/spec/text2048/tiles_spec.rb
CHANGED
@@ -3,73 +3,73 @@
|
|
3
3
|
require 'text2048'
|
4
4
|
|
5
5
|
describe Text2048::Tiles, '.new' do
|
6
|
-
context 'with [
|
7
|
-
Given(:tiles) { Text2048::Tiles.new([
|
6
|
+
context 'with [nil, nil, nil, nil]' do
|
7
|
+
Given(:tiles) { Text2048::Tiles.new([nil, nil, nil, nil]) }
|
8
8
|
|
9
9
|
describe '#right' do
|
10
10
|
When(:result) { tiles.right }
|
11
11
|
|
12
|
-
Then { result[0] == [
|
12
|
+
Then { result[0] == [nil, nil, nil, nil] }
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
context 'with [2,
|
17
|
-
Given(:tiles) { Text2048::Tiles.new([2,
|
16
|
+
context 'with [2, nil, nil, nil]' do
|
17
|
+
Given(:tiles) { Text2048::Tiles.new([2, nil, nil, nil]) }
|
18
18
|
|
19
19
|
describe '#right' do
|
20
20
|
When(:result) { tiles.right }
|
21
21
|
|
22
|
-
Then { result[0] == [
|
22
|
+
Then { result[0] == [nil, nil, nil, 2] }
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
context 'with [
|
27
|
-
Given(:tiles) { Text2048::Tiles.new([
|
26
|
+
context 'with [nil, 2, nil, nil]' do
|
27
|
+
Given(:tiles) { Text2048::Tiles.new([nil, 2, nil, nil]) }
|
28
28
|
|
29
29
|
describe '#right' do
|
30
30
|
When(:result) { tiles.right }
|
31
31
|
|
32
|
-
Then { result[0] == [
|
32
|
+
Then { result[0] == [nil, nil, nil, 2] }
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
context 'with [
|
37
|
-
Given(:tiles) { Text2048::Tiles.new([
|
36
|
+
context 'with [nil, nil, 2, nil]' do
|
37
|
+
Given(:tiles) { Text2048::Tiles.new([nil, nil, 2, nil]) }
|
38
38
|
|
39
39
|
describe '#right' do
|
40
40
|
When(:result) { tiles.right }
|
41
41
|
|
42
|
-
Then { result[0] == [
|
42
|
+
Then { result[0] == [nil, nil, nil, 2] }
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
context 'with [
|
47
|
-
Given(:tiles) { Text2048::Tiles.new([
|
46
|
+
context 'with [nil, nil, nil, 2]' do
|
47
|
+
Given(:tiles) { Text2048::Tiles.new([nil, nil, nil, 2]) }
|
48
48
|
|
49
49
|
describe '#right' do
|
50
50
|
When(:result) { tiles.right }
|
51
51
|
|
52
|
-
Then { result[0] == [
|
52
|
+
Then { result[0] == [nil, nil, nil, 2] }
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
context 'with [2, 2,
|
57
|
-
Given(:tiles) { Text2048::Tiles.new([2, 2,
|
56
|
+
context 'with [2, 2, nil, nil]' do
|
57
|
+
Given(:tiles) { Text2048::Tiles.new([2, 2, nil, nil]) }
|
58
58
|
|
59
59
|
describe '#right' do
|
60
60
|
When(:result) { tiles.right }
|
61
61
|
|
62
|
-
Then { result[0] == [
|
62
|
+
Then { result[0] == [nil, nil, nil, 4] }
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
-
context 'with [2, 2, 2,
|
67
|
-
Given(:tiles) { Text2048::Tiles.new([2, 2, 2,
|
66
|
+
context 'with [2, 2, 2, nil]' do
|
67
|
+
Given(:tiles) { Text2048::Tiles.new([2, 2, 2, nil]) }
|
68
68
|
|
69
69
|
describe '#right' do
|
70
70
|
When(:result) { tiles.right }
|
71
71
|
|
72
|
-
Then { result[0] == [
|
72
|
+
Then { result[0] == [nil, nil, 2, 4] }
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
@@ -79,7 +79,7 @@ describe Text2048::Tiles, '.new' do
|
|
79
79
|
describe '#right' do
|
80
80
|
When(:result) { tiles.right }
|
81
81
|
|
82
|
-
Then { result[0] == [
|
82
|
+
Then { result[0] == [nil, nil, 4, 4] }
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
@@ -89,17 +89,17 @@ describe Text2048::Tiles, '.new' do
|
|
89
89
|
describe '#right' do
|
90
90
|
When(:result) { tiles.right }
|
91
91
|
|
92
|
-
Then { result[0] == [
|
92
|
+
Then { result[0] == [nil, nil, 8, 4] }
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
96
|
-
context 'with [
|
97
|
-
Given(:tiles) { Text2048::Tiles.new([
|
96
|
+
context 'with [nil, 4, nil, 2]' do
|
97
|
+
Given(:tiles) { Text2048::Tiles.new([nil, 4, nil, 2]) }
|
98
98
|
|
99
99
|
describe '#right' do
|
100
100
|
When(:result) { tiles.right }
|
101
101
|
|
102
|
-
Then { result[0] == [
|
102
|
+
Then { result[0] == [nil, nil, 4, 2] }
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
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.4.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-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- lib/text2048/curses_tile.rb
|
54
54
|
- lib/text2048/curses_view.rb
|
55
55
|
- lib/text2048/monkey_patch/array.rb
|
56
|
+
- lib/text2048/monkey_patch/array/board.rb
|
56
57
|
- lib/text2048/monkey_patch/array/tile.rb
|
57
58
|
- lib/text2048/text_view.rb
|
58
59
|
- lib/text2048/tile.rb
|