console-tetris 0.1.2 → 0.1.3

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: b7a63eaef53112b4769b21342488b1f40eb0cda2
4
- data.tar.gz: 94edf57be236fce05ad15668093522a3ee4abf2d
3
+ metadata.gz: ec554203ec195d6a53199cf13febb46771d537cd
4
+ data.tar.gz: c10cc6150bb6d5ca6d9d66521b51f5b33600291f
5
5
  SHA512:
6
- metadata.gz: c4de5d34d902ecee323772f9437a3be3edfbf5b1fac57e6f906195b40933719afddd7dcc11ccfc6b68c52718a51bbe9a29f8945fbe8643e86057655c657572ef
7
- data.tar.gz: 467dc3544d0bd71331916fa53936931cba34f2ff67a3c82c096f3db91eb5d5eb86349e3d416f12a97ef798aad530aa65ebc9d3c874c05b55c521efd265bd61f1
6
+ metadata.gz: 3459c8086bc005d632fe6588078104b73d1e9e8401697d3546e47f23c17ae0847f2251c86c874f6787067eab32c25d0e6250b0b08f85d32d527bc58c2a868edb
7
+ data.tar.gz: 503cd47c13fa2f107f6b895605dff24c609824232dc612c950d2490c0008561b260d78d98e0de2f87db6499ad1381133e1ebbf0b953cd09403732de7637e7a00
data/README.md CHANGED
@@ -16,6 +16,12 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install console-tetris
18
18
 
19
+ ## Start Game
20
+
21
+ ```
22
+ $ console_tetris
23
+ ```
24
+
19
25
  ## Commands
20
26
 
21
27
  `.` : move right
@@ -22,5 +22,6 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.13"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "pry"
25
26
  spec.add_dependency 'io-console'
26
27
  end
@@ -9,10 +9,11 @@ class ConsoleTetris
9
9
  def self.start
10
10
  @background_board = BackgroundBoard.new
11
11
 
12
- type = "type_#{BLOCK_TYPES.sample}"
13
- @tetrimino = Tetrimino.new(block_type: type)
12
+ @tetrimino = Tetrimino.new(block_type: "type_#{BLOCK_TYPES.sample}")
13
+ next_tetrimino = Tetrimino.new(block_type: "type_#{BLOCK_TYPES.sample}")
14
14
 
15
- @background_board.print_block(@tetrimino.block)
15
+ @background_board.print_next_block(next_tetrimino)
16
+ @background_board.print_block(@tetrimino)
16
17
 
17
18
  Thread.new do
18
19
  catch :gameover do
@@ -20,26 +21,28 @@ class ConsoleTetris
20
21
  loop.with_index do |_, i|
21
22
  @tetrimino.down
22
23
 
23
- @background_board.print_block(@tetrimino.block) unless @background_board.overlap?(@tetrimino.block)
24
+ @background_board.print_block(@tetrimino) unless @background_board.overlap?(@tetrimino)
24
25
 
25
26
  sleep 0.1
26
27
 
27
- if @tetrimino.bottom_edge? || @background_board.overlap?(@tetrimino.block)
28
+ if @tetrimino.bottom_edge? || @background_board.overlap?(@tetrimino)
28
29
  throw :gameover if i == 0
29
30
 
30
- @tetrimino.back_vertically if @background_board.overlap?(@tetrimino.block)
31
+ @tetrimino.back_vertically if @background_board.overlap?(@tetrimino)
31
32
 
32
33
  break
33
34
  end
34
35
  end
35
36
 
36
- @background_board.stack!(@tetrimino.block)
37
+ @background_board.stack!(@tetrimino)
37
38
  @background_board.remove_filled_line!
38
39
 
39
- @background_board.print_block(@tetrimino.block)
40
+ @background_board.print_block(@tetrimino)
40
41
 
41
- type = "type_#{BLOCK_TYPES.sample}"
42
- @tetrimino = Tetrimino.new(block_type: type)
42
+ @tetrimino = next_tetrimino
43
+ next_tetrimino = Tetrimino.new(block_type: "type_#{BLOCK_TYPES.sample}")
44
+
45
+ @background_board.print_next_block(next_tetrimino)
43
46
  end
44
47
  end
45
48
 
@@ -54,17 +57,17 @@ class ConsoleTetris
54
57
  next if @tetrimino.right_edge?
55
58
 
56
59
  @tetrimino.move_right
57
- @tetrimino.back_horizontally if @background_board.overlap?(@tetrimino.block)
60
+ @tetrimino.back_horizontally if @background_board.overlap?(@tetrimino)
58
61
  when 'z'
59
62
  next if @tetrimino.left_edge?
60
63
 
61
64
  @tetrimino.move_left
62
- @tetrimino.back_horizontally if @background_board.overlap?(@tetrimino.block)
65
+ @tetrimino.back_horizontally if @background_board.overlap?(@tetrimino)
63
66
  when "\r"
64
67
  @tetrimino.rotate
65
68
 
66
69
  loop do
67
- if @background_board.overlap?(@tetrimino.block)
70
+ if @background_board.overlap?(@tetrimino)
68
71
  @tetrimino.up
69
72
  else
70
73
  break
@@ -72,7 +75,7 @@ class ConsoleTetris
72
75
  end
73
76
  end
74
77
 
75
- @background_board.print_block(@tetrimino.block)
78
+ @background_board.print_block(@tetrimino)
76
79
  end
77
80
  end
78
81
  end
@@ -121,7 +121,12 @@ class ConsoleTetris
121
121
 
122
122
  splitted_number_aas = number_aas.map {|n| n.split("\n") }
123
123
 
124
- 6.times.map {|i| splitted_number_aas.map {|aa| aa[i].to_s.ljust(8) }.join('') }.join("\n")
124
+ 6.times.map {|i|
125
+ splitted_number_aas.map {|aa|
126
+ max_length = aa.map(&:size).max
127
+ aa[i].to_s.ljust(max_length)
128
+ }.join('')
129
+ }.join("\n")
125
130
  end
126
131
  end
127
132
  end
@@ -6,27 +6,23 @@ class ConsoleTetris
6
6
 
7
7
  BOARD_SIZE = {x: 10, y: 20}
8
8
 
9
- class << self
10
- def blank_board
11
- BOARD_SIZE[:y].times.map { blank_line }
12
- end
13
-
14
- def blank_line
15
- Array.new(BOARD_SIZE[:x]).fill(0)
16
- end
17
- end
18
-
19
9
  def initialize
20
- @board = self.class.blank_board
10
+ @board = blank_board
21
11
  @point = 0
22
12
  end
23
13
 
24
- def stack!(block)
25
- @board = @board.map.with_index {|a, i| a.map.with_index {|e, j| e | block[i][j] } }
14
+ def stack!(tetrimino)
15
+ tetrimino.block.each.with_index {|row, i| row.each.with_index {|value, j| @board[tetrimino.y_coordinate + i][tetrimino.x_coordinate + j] |= value } }
26
16
  end
27
17
 
28
- def overlap?(block)
29
- @board.map.with_index {|a, i| a.map.with_index {|e, j| e & block[i][j] } }.any? {|a| a.any? {|e| e > 0 } }
18
+ def overlap?(tetrimino)
19
+ tetrimino.block.map.with_index.any? {|row, i|
20
+ row.map.with_index.any? {|value, j|
21
+ return false if @board[tetrimino.y_coordinate + i].nil?
22
+
23
+ @board[tetrimino.y_coordinate + i][tetrimino.x_coordinate + j] & value > 0
24
+ }
25
+ }
30
26
  end
31
27
 
32
28
  def remove_filled_line!
@@ -35,22 +31,49 @@ class ConsoleTetris
35
31
  remove_size = BOARD_SIZE[:y] - @board.count
36
32
 
37
33
  @point = @point + remove_size * 1000
38
- remove_size.times { @board.unshift(self.class.blank_line) }
34
+ remove_size.times { @board.unshift(blank_line) }
39
35
  end
40
36
 
41
- def print_block(block)
42
- print "\e[2J"
37
+ def print_point
38
+ print "\e[3;1H"
39
+
40
+ print "\e[13G"
41
+ print "\e[K"
42
+ print "#{@point.to_s.rjust(6, ' ')} P\n"
43
+
44
+ print "\e[13G"
45
+ print "\e[K"
46
+ print "-------- \n"
47
+ end
48
+
49
+ def print_next_block(tetrimino)
50
+ block = tetrimino.block.map {|row| row.map {|b| b == 1 ? '[]' : ' ' }.join('') }
51
+
52
+ print "\e[5;1H"
53
+ print "\e[1J"
43
54
  print "\e[1;1H"
44
55
 
45
- print "\e[G _____________ \n"
46
- print "\e[G| #{@point.to_s.rjust(11, ' ')} |\n"
47
- print "\e[G ‾‾‾‾‾‾‾‾‾‾‾‾‾ \n"
56
+ print "\e[G ---------- \n"
57
+ print "\e[G| #{block[0].to_s.center(8, ' ')} |\n"
58
+ print "\e[G| #{block[1].to_s.center(8, ' ')} |\n"
59
+ print "\e[G ---------- \n"
60
+ end
61
+
62
+ def print_block(tetrimino)
63
+ dup_board = @board.map {|board| board.dup }
64
+
65
+ tetrimino.block.each.with_index {|row, i| row.each.with_index {|value, j| dup_board[tetrimino.y_coordinate + i][tetrimino.x_coordinate + j] |= value } }
66
+
67
+ print_point
68
+
69
+ print "\e[5;1H"
70
+ print "\e[J"
48
71
 
49
72
  print "\e[G"
50
73
  print '__' * BOARD_SIZE[:x]
51
74
  print "\n"
52
75
 
53
- @board.map.with_index {|a, i| a.map.with_index {|e, j| e | block[i][j] } }.each do |elements|
76
+ dup_board.each do |elements|
54
77
  print "\e[G"
55
78
 
56
79
  elements.each do |el|
@@ -75,5 +98,15 @@ class ConsoleTetris
75
98
  print "#{AsciiArt.number_to_aa(point).gsub(/^/, "\e[G")} Points!!!!\n"
76
99
  print AsciiArt::GAMEOVER.gsub(/^/, "\e[G")
77
100
  end
101
+
102
+ private
103
+
104
+ def blank_board
105
+ BOARD_SIZE[:y].times.map { blank_line }
106
+ end
107
+
108
+ def blank_line
109
+ Array.new(BOARD_SIZE[:x]).fill(0)
110
+ end
78
111
  end
79
112
  end
@@ -0,0 +1,145 @@
1
+ class ConsoleTetris
2
+ module Block
3
+ module_function
4
+
5
+ def type_j(degree)
6
+ case degree
7
+ when 0
8
+ [
9
+ [1, 1, 1, 1],
10
+ [0, 0, 0, 1]
11
+ ]
12
+ when 90
13
+ [
14
+ [0, 1],
15
+ [0, 1],
16
+ [0, 1],
17
+ [1, 1]
18
+ ]
19
+ when 180
20
+ [
21
+ [1, 0, 0, 0],
22
+ [1, 1, 1, 1]
23
+ ]
24
+ when 270
25
+ [
26
+ [1, 1],
27
+ [1, 0],
28
+ [1, 0],
29
+ [1, 0]
30
+ ]
31
+ end
32
+ end
33
+
34
+ def type_l(degree)
35
+ case degree
36
+ when 0
37
+ [
38
+ [1, 1, 1, 1],
39
+ [1, 0, 0, 0]
40
+ ]
41
+ when 90
42
+ [
43
+ [1, 1],
44
+ [0, 1],
45
+ [0, 1],
46
+ [0, 1]
47
+ ]
48
+ when 180
49
+ [
50
+ [0, 0, 0, 1],
51
+ [1, 1, 1, 1]
52
+ ]
53
+ when 270
54
+ [
55
+ [1, 0],
56
+ [1, 0],
57
+ [1, 0],
58
+ [1, 1]
59
+ ]
60
+ end
61
+ end
62
+
63
+ def type_i(degree)
64
+ case degree
65
+ when 0, 180
66
+ [
67
+ [1, 1, 1, 1]
68
+ ]
69
+ when 90, 270
70
+ [
71
+ [1],
72
+ [1],
73
+ [1],
74
+ [1]
75
+ ]
76
+ end
77
+ end
78
+
79
+ def type_o(degree)
80
+ [
81
+ [1, 1],
82
+ [1, 1]
83
+ ]
84
+ end
85
+
86
+ def type_s(degree)
87
+ case degree
88
+ when 0, 180
89
+ [
90
+ [0, 1, 1],
91
+ [1, 1, 0]
92
+ ]
93
+ when 90, 270
94
+ [
95
+ [1, 0],
96
+ [1, 1],
97
+ [0, 1]
98
+ ]
99
+ end
100
+ end
101
+
102
+ def type_z(degree)
103
+ case degree
104
+ when 0, 180
105
+ [
106
+ [1, 1, 0],
107
+ [0, 1, 1]
108
+ ]
109
+ when 90, 270
110
+ [
111
+ [0, 1],
112
+ [1, 1],
113
+ [1, 0]
114
+ ]
115
+ end
116
+ end
117
+
118
+ def type_t(degree)
119
+ case degree
120
+ when 0
121
+ [
122
+ [1, 1, 1],
123
+ [0, 1, 0]
124
+ ]
125
+ when 90
126
+ [
127
+ [0, 1],
128
+ [1, 1],
129
+ [0, 1]
130
+ ]
131
+ when 180
132
+ [
133
+ [0, 1, 0],
134
+ [1, 1, 1]
135
+ ]
136
+ when 270
137
+ [
138
+ [1, 0],
139
+ [1, 1],
140
+ [1, 0]
141
+ ]
142
+ end
143
+ end
144
+ end
145
+ end
@@ -1,7 +1,9 @@
1
- require_relative './background_board'
1
+ require_relative './block'
2
2
 
3
3
  class ConsoleTetris
4
4
  class Tetrimino
5
+ attr_accessor :x_coordinate, :y_coordinate
6
+
5
7
  def initialize(x = 0, y = 0, degree = 0, block_type:)
6
8
  @x_coordinate = x
7
9
  @y_coordinate = y
@@ -49,170 +51,27 @@ class ConsoleTetris
49
51
  @degree += 90
50
52
  @degree = 0 if @degree == 360
51
53
 
52
- @y_coordinate = @y_coordinate - before_block.max.count(1).times.count {|i| BackgroundBoard.blank_board[@y_coordinate + i].nil? }
53
- @x_coordinate = @x_coordinate - before_block.map {|a| a.any? {|e| e == 1} ? 1 : 0 }.count(1).times.count {|i| BackgroundBoard.blank_board[@y_coordinate][@x_coordinate + i].nil? }
54
+ @y_coordinate = 20 - block.size if @y_coordinate + block.size > 20
55
+ @x_coordinate = 10 - block.first.size if @x_coordinate + block.first.size > 10
54
56
 
55
57
  @y_coordinate = 0 if @y_coordinate.negative?
56
58
  @x_coordinate = 0 if @x_coordinate.negative?
57
59
  end
58
60
 
59
61
  def block
60
- send(@block_type)
62
+ Block.send(@block_type, @degree)
61
63
  end
62
64
 
63
65
  def left_edge?
64
- block.any? {|a| a.first == 1 }
66
+ @x_coordinate == 0
65
67
  end
66
68
 
67
69
  def right_edge?
68
- block.any? {|a| a.last == 1 }
70
+ @x_coordinate + block.first.size > 9
69
71
  end
70
72
 
71
73
  def bottom_edge?
72
- block.last.any? {|e| e == 1 }
73
- end
74
-
75
- private
76
-
77
- def type_j
78
- board = BackgroundBoard.blank_board
79
-
80
- case @degree
81
- when 0
82
- 4.times {|i| board[@y_coordinate][@x_coordinate + i] = 1 }
83
-
84
- board[@y_coordinate + 1][@x_coordinate + 3] = 1
85
- when 90
86
- 4.times {|i| board[@y_coordinate + i][@x_coordinate + 1] = 1 }
87
-
88
- board[@y_coordinate + 3][@x_coordinate] = 1
89
- when 180
90
- 4.times {|i| board[@y_coordinate][@x_coordinate + i] = 1 }
91
-
92
- board[@y_coordinate - 1][@x_coordinate] = 1
93
- when 270
94
- 4.times {|i| board[@y_coordinate + i][@x_coordinate] = 1 }
95
-
96
- board[@y_coordinate][@x_coordinate + 1] = 1
97
- end
98
-
99
- board
100
- end
101
-
102
- def type_l
103
- board = BackgroundBoard.blank_board
104
-
105
- case @degree
106
- when 0
107
- 4.times {|i| board[@y_coordinate][@x_coordinate + i] = 1 }
108
-
109
- board[@y_coordinate + 1][@x_coordinate] = 1
110
- when 90
111
- 4.times {|i| board[@y_coordinate + i][@x_coordinate + 1] = 1 }
112
-
113
- board[@y_coordinate][@x_coordinate] = 1
114
- when 180
115
- 4.times {|i| board[@y_coordinate + 1][@x_coordinate + i] = 1 }
116
-
117
- board[@y_coordinate][@x_coordinate + 3] = 1
118
- when 270
119
- 4.times {|i| board[@y_coordinate + i][@x_coordinate] = 1 }
120
-
121
- board[@y_coordinate + 3][@x_coordinate + 1] = 1
122
- end
123
-
124
- board
125
- end
126
-
127
- def type_i
128
- board = BackgroundBoard.blank_board
129
-
130
- case @degree
131
- when 0
132
- 4.times {|i| board[@y_coordinate][@x_coordinate + i] = 1 }
133
- when 90
134
- 4.times {|i| board[@y_coordinate + i][@x_coordinate + 2] = 1 }
135
- when 180
136
- 4.times {|i| board[@y_coordinate + 2][@x_coordinate + i] = 1 }
137
- when 270
138
- 4.times {|i| board[@y_coordinate + i][@x_coordinate] = 1 }
139
- end
140
-
141
- board
142
- end
143
-
144
- def type_o
145
- board = BackgroundBoard.blank_board
146
-
147
- 2.times {|i|
148
- board[@y_coordinate][@x_coordinate + i] = 1
149
- board[@y_coordinate + 1][@x_coordinate + i] = 1
150
- }
151
-
152
- board
153
- end
154
-
155
- def type_s
156
- board = BackgroundBoard.blank_board
157
-
158
- case @degree
159
- when 0, 180
160
- 2.times {|i|
161
- board[@y_coordinate][@x_coordinate + i + 1] = 1
162
- board[@y_coordinate + 1][@x_coordinate + i] = 1
163
- }
164
- when 90, 270
165
- 2.times {|i|
166
- board[@y_coordinate + i][@x_coordinate] = 1
167
- board[@y_coordinate + i + 1][@x_coordinate + 1] = 1
168
- }
169
- end
170
-
171
- board
172
- end
173
-
174
- def type_z
175
- board = BackgroundBoard.blank_board
176
-
177
- case @degree
178
- when 0, 180
179
- 2.times {|i|
180
- board[@y_coordinate][@x_coordinate + i] = 1
181
- board[@y_coordinate + 1][@x_coordinate + i + 1] = 1
182
- }
183
- when 90, 270
184
- 2.times {|i|
185
- board[@y_coordinate + i + 1][@x_coordinate] = 1
186
- board[@y_coordinate + i][@x_coordinate + 1] = 1
187
- }
188
- end
189
-
190
- board
191
- end
192
-
193
- def type_t
194
- board = BackgroundBoard.blank_board
195
-
196
- case @degree
197
- when 0
198
- 3.times {|i| board[@y_coordinate + 1][@x_coordinate + i] = 1 }
199
-
200
- board[@y_coordinate + 2][@x_coordinate + 1] = 1
201
- when 90
202
- 3.times {|i| board[@y_coordinate + i][@x_coordinate + 1] = 1 }
203
-
204
- board[@y_coordinate + 1][@x_coordinate] = 1
205
- when 180
206
- 3.times {|i| board[@y_coordinate + 1][@x_coordinate + i] = 1 }
207
-
208
- board[@y_coordinate][@x_coordinate + 1] = 1
209
- when 270
210
- 3.times {|i| board[@y_coordinate + i][@x_coordinate + 1] = 1 }
211
-
212
- board[@y_coordinate + 1][@x_coordinate + 2] = 1
213
- end
214
-
215
- board
74
+ @y_coordinate + block.size > 19
216
75
  end
217
76
  end
218
77
  end
@@ -1,3 +1,3 @@
1
1
  class ConsoleTetris
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console-tetris
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - pekepek
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: io-console
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -69,6 +83,7 @@ files:
69
83
  - lib/console_tetris.rb
70
84
  - lib/console_tetris/ascii_art.rb
71
85
  - lib/console_tetris/background_board.rb
86
+ - lib/console_tetris/block.rb
72
87
  - lib/console_tetris/tetrimino.rb
73
88
  - lib/console_tetris/version.rb
74
89
  homepage: https://github.com/pekepek/console-tetris