text2048 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +55 -1
- data/bin/2048 +3 -1
- data/lib/text2048/app.rb +18 -34
- data/lib/text2048/board.rb +93 -50
- data/lib/text2048/curses_view.rb +23 -59
- data/lib/text2048/curses_view/colorize.rb +53 -0
- data/lib/text2048/curses_view/keyboard.rb +35 -0
- data/lib/text2048/curses_view/tile.rb +135 -0
- data/lib/text2048/curses_view/tile_effects.rb +35 -0
- data/lib/text2048/monkey_patch/array/tile.rb +1 -1
- data/lib/text2048/monkey_patch/hash.rb +8 -0
- data/lib/text2048/monkey_patch/hash/converter.rb +17 -0
- data/lib/text2048/version.rb +1 -1
- data/spec/array_spec.rb +24 -24
- data/spec/spec_helper.rb +3 -0
- data/spec/text2048/app_spec.rb +106 -0
- data/spec/text2048/board_spec.rb +332 -51
- metadata +11 -3
- data/lib/text2048/curses_tile.rb +0 -125
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'curses'
|
4
|
+
|
5
|
+
# This module smells of :reek:UncommunicativeModuleName
|
6
|
+
module Text2048
|
7
|
+
class CursesView
|
8
|
+
# Colorize characters.
|
9
|
+
module Colorize
|
10
|
+
include Curses
|
11
|
+
|
12
|
+
COLORS = {
|
13
|
+
0 => COLOR_BLACK,
|
14
|
+
2 => COLOR_WHITE,
|
15
|
+
4 => COLOR_GREEN,
|
16
|
+
8 => COLOR_GREEN,
|
17
|
+
16 => COLOR_CYAN,
|
18
|
+
32 => COLOR_CYAN,
|
19
|
+
64 => COLOR_BLUE,
|
20
|
+
128 => COLOR_BLUE,
|
21
|
+
256 => COLOR_YELLOW,
|
22
|
+
512 => COLOR_YELLOW,
|
23
|
+
1024 => COLOR_MAGENTA,
|
24
|
+
2048 => COLOR_RED
|
25
|
+
}
|
26
|
+
|
27
|
+
def color(number)
|
28
|
+
COLORS[number]
|
29
|
+
end
|
30
|
+
|
31
|
+
def colorize(color, &block)
|
32
|
+
maybe_init_colors
|
33
|
+
attron color_pair(color), &block
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def maybe_init_colors
|
39
|
+
return if @colors_initialized
|
40
|
+
init_colors
|
41
|
+
@colors_initialized = true
|
42
|
+
end
|
43
|
+
|
44
|
+
def init_colors
|
45
|
+
start_color
|
46
|
+
COLORS.each_pair do |_key, value|
|
47
|
+
init_pair value, COLOR_BLACK, value
|
48
|
+
init_pair value + 100, value, value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'curses'
|
4
|
+
|
5
|
+
# This module smells of :reek:UncommunicativeModuleName
|
6
|
+
module Text2048
|
7
|
+
class CursesView
|
8
|
+
# Handles user inputs in curses
|
9
|
+
class Keyboard
|
10
|
+
include Curses
|
11
|
+
|
12
|
+
KEYS = {
|
13
|
+
'h' => :left, 'l' => :right, 'k' => :up, 'j' => :down,
|
14
|
+
Key::LEFT => :left, Key::RIGHT => :right,
|
15
|
+
Key::UP => :up, Key::DOWN => :down,
|
16
|
+
'+' => :larger, '-' => :smaller,
|
17
|
+
'q' => :quit
|
18
|
+
}
|
19
|
+
|
20
|
+
def read
|
21
|
+
maybe_init
|
22
|
+
KEYS[getch]
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def maybe_init
|
28
|
+
return if @initialized
|
29
|
+
noecho
|
30
|
+
stdscr.keypad(true)
|
31
|
+
@initialized = true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'curses'
|
4
|
+
require 'text2048/curses_view/colorize'
|
5
|
+
|
6
|
+
# This module smells of :reek:UncommunicativeModuleName
|
7
|
+
module Text2048
|
8
|
+
class CursesView
|
9
|
+
# Shows tiles in curses.
|
10
|
+
class Tile
|
11
|
+
attr_reader :width
|
12
|
+
attr_reader :height
|
13
|
+
attr_reader :color
|
14
|
+
|
15
|
+
include Curses
|
16
|
+
include Colorize
|
17
|
+
|
18
|
+
DEFAULT_HEIGHT = 3
|
19
|
+
DEFAULT_WIDTH = 5
|
20
|
+
|
21
|
+
def self.width(scale)
|
22
|
+
@width = (DEFAULT_WIDTH * scale).to_i
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.height(scale)
|
26
|
+
(DEFAULT_HEIGHT * scale).to_i
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(value, row, col, color, scale = 1)
|
30
|
+
klass = self.class
|
31
|
+
@value = value.to_i
|
32
|
+
@height = klass.height(scale)
|
33
|
+
@width = klass.width(scale)
|
34
|
+
@row = (@height + 1) * row + 2
|
35
|
+
@col = (@width + 1) * col + 1
|
36
|
+
@color = color
|
37
|
+
end
|
38
|
+
|
39
|
+
def show
|
40
|
+
draw_box
|
41
|
+
fill_tile_color
|
42
|
+
draw_number
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
def pop
|
47
|
+
colorize(@color + 100) do
|
48
|
+
draw_box
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def draw_box
|
53
|
+
draw_square
|
54
|
+
[box_upper_left, box_upper_right,
|
55
|
+
box_lower_left, box_lower_right].each do |row, col|
|
56
|
+
setpos(row, col)
|
57
|
+
addstr('+')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def fill_tile_color
|
62
|
+
colorize(@color + 100) { fill }
|
63
|
+
end
|
64
|
+
|
65
|
+
def fill_black
|
66
|
+
colorize(COLOR_BLACK + 100) { fill }
|
67
|
+
refresh
|
68
|
+
end
|
69
|
+
|
70
|
+
def draw_number
|
71
|
+
return if @value == 0
|
72
|
+
setpos(@row + @height / 2, @col)
|
73
|
+
colorize(@color) do
|
74
|
+
addstr @value.to_s.center(@width)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def box_upper_left
|
81
|
+
[@row - 1, @col - 1]
|
82
|
+
end
|
83
|
+
|
84
|
+
def box_upper_right
|
85
|
+
[@row - 1, @col + @width]
|
86
|
+
end
|
87
|
+
|
88
|
+
def box_lower_left
|
89
|
+
[@row + @height, @col - 1]
|
90
|
+
end
|
91
|
+
|
92
|
+
def box_lower_right
|
93
|
+
[@row + @height, @col + @width]
|
94
|
+
end
|
95
|
+
|
96
|
+
def box_height
|
97
|
+
@height + 2
|
98
|
+
end
|
99
|
+
|
100
|
+
def box_width
|
101
|
+
@width + 2
|
102
|
+
end
|
103
|
+
|
104
|
+
def draw_square
|
105
|
+
draw_horizonal_line(*box_upper_left, box_width)
|
106
|
+
draw_vertical_line(*box_upper_left, box_height)
|
107
|
+
draw_vertical_line(*box_upper_right, box_height)
|
108
|
+
draw_horizonal_line(*box_lower_left, box_width)
|
109
|
+
end
|
110
|
+
|
111
|
+
def draw_horizonal_line(row, col, length)
|
112
|
+
setpos(row, col)
|
113
|
+
addstr('-' * length)
|
114
|
+
end
|
115
|
+
|
116
|
+
def draw_vertical_line(row, col, length)
|
117
|
+
(0..(length - 1)).each do |each|
|
118
|
+
setpos(row + each, col)
|
119
|
+
addstr('|')
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def fill
|
124
|
+
(0..(@height - 1)).each do |each|
|
125
|
+
setpos(@row + each, @col)
|
126
|
+
if @value != 0 && each == @height / 2
|
127
|
+
addstr @value.to_s.center(@width)
|
128
|
+
else
|
129
|
+
addstr('.' * @width)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'curses'
|
4
|
+
|
5
|
+
# This module smells of :reek:UncommunicativeModuleName
|
6
|
+
module Text2048
|
7
|
+
class CursesView
|
8
|
+
# Curses tile effects
|
9
|
+
module TileEffects
|
10
|
+
include Curses
|
11
|
+
|
12
|
+
def pop_tiles(list)
|
13
|
+
[:pop, :draw_box].each do |each|
|
14
|
+
list_do each, list
|
15
|
+
refresh
|
16
|
+
sleep 0.1
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def zoom_tiles(list)
|
21
|
+
[:fill_black, :draw_number, :show].each do |each|
|
22
|
+
list_do each, list
|
23
|
+
refresh
|
24
|
+
sleep 0.05
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def list_do(name, list)
|
31
|
+
list.each { |each| @tiles[each].__send__ name }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# This module smells of :reek:UncommunicativeModuleName
|
4
|
+
module Text2048
|
5
|
+
module MonkeyPatch
|
6
|
+
module Hash
|
7
|
+
# For 1.9 backword compatibility
|
8
|
+
module Converter
|
9
|
+
if RUBY_VERSION < '2.0'
|
10
|
+
def to_h
|
11
|
+
self
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/text2048/version.rb
CHANGED
data/spec/array_spec.rb
CHANGED
@@ -10,8 +10,8 @@ describe Array do
|
|
10
10
|
context '[0, 0, 0, 0]' do
|
11
11
|
Given(:tiles) { tiles_with([0, 0, 0, 0]) }
|
12
12
|
|
13
|
-
describe '#
|
14
|
-
When(:score) { tiles.
|
13
|
+
describe '#right' do
|
14
|
+
When(:score) { tiles.right[1] }
|
15
15
|
|
16
16
|
Then { tiles == [0, 0, 0, 0] }
|
17
17
|
And { score == 0 }
|
@@ -25,8 +25,8 @@ describe Array do
|
|
25
25
|
context '[2, 0, 0, 0]' do
|
26
26
|
Given(:tiles) { tiles_with([2, 0, 0, 0]) }
|
27
27
|
|
28
|
-
describe '#
|
29
|
-
When(:score) { tiles.
|
28
|
+
describe '#right' do
|
29
|
+
When(:score) { tiles.right[1] }
|
30
30
|
|
31
31
|
Then { tiles == [0, 0, 0, 2] }
|
32
32
|
And { score == 0 }
|
@@ -40,8 +40,8 @@ describe Array do
|
|
40
40
|
context '[0, 2, 0, 0]' do
|
41
41
|
Given(:tiles) { tiles_with([0, 2, 0, 0]) }
|
42
42
|
|
43
|
-
describe '#
|
44
|
-
When(:score) { tiles.
|
43
|
+
describe '#right' do
|
44
|
+
When(:score) { tiles.right[1] }
|
45
45
|
|
46
46
|
Then { tiles == [0, 0, 0, 2] }
|
47
47
|
And { score == 0 }
|
@@ -55,8 +55,8 @@ describe Array do
|
|
55
55
|
context '[0, 0, 2, 0]' do
|
56
56
|
Given(:tiles) { tiles_with([0, 0, 2, 0]) }
|
57
57
|
|
58
|
-
describe '#
|
59
|
-
When(:score) { tiles.
|
58
|
+
describe '#right' do
|
59
|
+
When(:score) { tiles.right[1] }
|
60
60
|
|
61
61
|
Then { tiles == [0, 0, 0, 2] }
|
62
62
|
And { score == 0 }
|
@@ -70,8 +70,8 @@ describe Array do
|
|
70
70
|
context '[0, 0, 0, 2]' do
|
71
71
|
Given(:tiles) { tiles_with([0, 0, 0, 2]) }
|
72
72
|
|
73
|
-
describe '#
|
74
|
-
When(:score) { tiles.
|
73
|
+
describe '#right' do
|
74
|
+
When(:score) { tiles.right[1] }
|
75
75
|
|
76
76
|
Then { tiles == [0, 0, 0, 2] }
|
77
77
|
And { score == 0 }
|
@@ -85,8 +85,8 @@ describe Array do
|
|
85
85
|
context '[0, 2, 0, 2]' do
|
86
86
|
Given(:tiles) { tiles_with([0, 2, 0, 2]) }
|
87
87
|
|
88
|
-
describe '#
|
89
|
-
When(:score) { tiles.
|
88
|
+
describe '#right' do
|
89
|
+
When(:score) { tiles.right[1] }
|
90
90
|
|
91
91
|
Then { tiles == [0, 0, 0, 4] }
|
92
92
|
And { score == 4 }
|
@@ -100,8 +100,8 @@ describe Array do
|
|
100
100
|
context '[2, 2, 0, 0]' do
|
101
101
|
Given(:tiles) { tiles_with([2, 2, 0, 0]) }
|
102
102
|
|
103
|
-
describe '#
|
104
|
-
When(:score) { tiles.
|
103
|
+
describe '#right' do
|
104
|
+
When(:score) { tiles.right[1] }
|
105
105
|
|
106
106
|
Then { tiles == [0, 0, 0, 4] }
|
107
107
|
And { score == 4 }
|
@@ -115,8 +115,8 @@ describe Array do
|
|
115
115
|
context '[2, 2, 2, 0]' do
|
116
116
|
Given(:tiles) { tiles_with([2, 2, 2, 0]) }
|
117
117
|
|
118
|
-
describe '#
|
119
|
-
When(:score) { tiles.
|
118
|
+
describe '#right' do
|
119
|
+
When(:score) { tiles.right[1] }
|
120
120
|
|
121
121
|
Then { tiles == [0, 0, 2, 4] }
|
122
122
|
And { score == 4 }
|
@@ -130,8 +130,8 @@ describe Array do
|
|
130
130
|
context '[2, 2, 2, 2]' do
|
131
131
|
Given(:tiles) { tiles_with([2, 2, 2, 2]) }
|
132
132
|
|
133
|
-
describe '#
|
134
|
-
When(:score) { tiles.
|
133
|
+
describe '#right' do
|
134
|
+
When(:score) { tiles.right[1] }
|
135
135
|
|
136
136
|
Then { tiles == [0, 0, 4, 4] }
|
137
137
|
And { score == 8 }
|
@@ -145,8 +145,8 @@ describe Array do
|
|
145
145
|
context '[4, 4, 2, 2]' do
|
146
146
|
Given(:tiles) { tiles_with([4, 4, 2, 2]) }
|
147
147
|
|
148
|
-
describe '#
|
149
|
-
When(:score) { tiles.
|
148
|
+
describe '#right' do
|
149
|
+
When(:score) { tiles.right[1] }
|
150
150
|
|
151
151
|
Then { tiles == [0, 0, 8, 4] }
|
152
152
|
And { score == 12 }
|
@@ -160,8 +160,8 @@ describe Array do
|
|
160
160
|
context '[0, 4, 0, 2]' do
|
161
161
|
Given(:tiles) { tiles_with([0, 4, 0, 2]) }
|
162
162
|
|
163
|
-
describe '#
|
164
|
-
When(:score) { tiles.
|
163
|
+
describe '#right' do
|
164
|
+
When(:score) { tiles.right[1] }
|
165
165
|
|
166
166
|
Then { tiles == [0, 0, 4, 2] }
|
167
167
|
And { score == 0 }
|
@@ -175,8 +175,8 @@ describe Array do
|
|
175
175
|
context 'with [16, 8, 4, 2]' do
|
176
176
|
Given(:tiles) { tiles_with([16, 8, 4, 2]) }
|
177
177
|
|
178
|
-
describe '#
|
179
|
-
When(:score) { tiles.
|
178
|
+
describe '#right' do
|
179
|
+
When(:score) { tiles.right[1] }
|
180
180
|
|
181
181
|
Then { tiles == [16, 8, 4, 2] }
|
182
182
|
And { score == 0 }
|