text2048 0.5.0 → 0.6.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.
@@ -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
@@ -6,7 +6,7 @@ module Text2048
6
6
  module Array
7
7
  # 2048 related methods
8
8
  module Tile
9
- def rmerge
9
+ def right
10
10
  shrink
11
11
  score = (size - 1).downto(1).reduce(0) do |memo, each|
12
12
  memo + (self[each] == self[each - 1] ? merge_left(each) : 0)
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ require 'text2048/monkey_patch/hash/converter'
4
+
5
+ # Monkey-patched standard Hash class.
6
+ class Hash
7
+ include Text2048::MonkeyPatch::Hash::Converter
8
+ 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
@@ -3,5 +3,5 @@
3
3
  # This module smells of :reek:UncommunicativeModuleName
4
4
  module Text2048
5
5
  # gem version.
6
- VERSION = '0.5.0'.freeze
6
+ VERSION = '0.6.0'.freeze
7
7
  end
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 '#rmerge' do
14
- When(:score) { tiles.rmerge[1] }
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 '#rmerge' do
29
- When(:score) { tiles.rmerge[1] }
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 '#rmerge' do
44
- When(:score) { tiles.rmerge[1] }
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 '#rmerge' do
59
- When(:score) { tiles.rmerge[1] }
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 '#rmerge' do
74
- When(:score) { tiles.rmerge[1] }
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 '#rmerge' do
89
- When(:score) { tiles.rmerge[1] }
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 '#rmerge' do
104
- When(:score) { tiles.rmerge[1] }
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 '#rmerge' do
119
- When(:score) { tiles.rmerge[1] }
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 '#rmerge' do
134
- When(:score) { tiles.rmerge[1] }
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 '#rmerge' do
149
- When(:score) { tiles.rmerge[1] }
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 '#rmerge' do
164
- When(:score) { tiles.rmerge[1] }
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 '#rmerge' do
179
- When(:score) { tiles.rmerge[1] }
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 }
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
4
 
5
+ require 'codeclimate-test-reporter'
6
+ CodeClimate::TestReporter.start
7
+
5
8
  require 'coveralls'
6
9
  Coveralls.wear_merged!
7
10