Class: ConsoleTetris::BackgroundBoard
- Inherits:
-
Object
- Object
- ConsoleTetris::BackgroundBoard
- Defined in:
- lib/console_tetris/background_board.rb
Constant Summary collapse
- BOARD_SIZE =
{x: 10, y: 20}
Instance Attribute Summary collapse
-
#point ⇒ Object
Returns the value of attribute point.
Instance Method Summary collapse
-
#initialize ⇒ BackgroundBoard
constructor
A new instance of BackgroundBoard.
- #overlap?(tetrimino) ⇒ Boolean
- #print_block(tetrimino) ⇒ Object
- #print_gameover ⇒ Object
- #print_next_block(tetrimino) ⇒ Object
- #print_point ⇒ Object
- #remove_filled_line! ⇒ Object
- #stack!(tetrimino) ⇒ Object
Constructor Details
#initialize ⇒ BackgroundBoard
Returns a new instance of BackgroundBoard.
9 10 11 12 |
# File 'lib/console_tetris/background_board.rb', line 9 def initialize @board = blank_board @point = 0 end |
Instance Attribute Details
#point ⇒ Object
Returns the value of attribute point.
5 6 7 |
# File 'lib/console_tetris/background_board.rb', line 5 def point @point end |
Instance Method Details
#overlap?(tetrimino) ⇒ Boolean
18 19 20 21 22 23 24 25 26 |
# File 'lib/console_tetris/background_board.rb', line 18 def overlap?(tetrimino) tetrimino.block.map.with_index.any? {|row, i| row.map.with_index.any? {|value, j| return false if @board[tetrimino.y_coordinate + i].nil? @board[tetrimino.y_coordinate + i][tetrimino.x_coordinate + j] > 0 && value > 0 } } end |
#print_block(tetrimino) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/console_tetris/background_board.rb', line 68 def print_block(tetrimino) dup_board = @board.map {|board| board.dup } tetrimino.block.each.with_index {|row, i| row.each.with_index {|value, j| next if dup_board[tetrimino.y_coordinate + i][tetrimino.x_coordinate + j].nil? dup_board[tetrimino.y_coordinate + i][tetrimino.x_coordinate + j] += value } } print_point print "\e[5;1H" print "\e[J" print "\e[G" print '__' * BOARD_SIZE[:x] print "\n" dup_board.each do |elements| print "\e[G" elements.each do |el| print case el when 0 ' ' when 1..7 "\e[3#{el}m[]" end end print "\e[0m" print "|\n" end print "\e[G" print '‾‾' * BOARD_SIZE[:x] end |
#print_gameover ⇒ Object
109 110 111 112 113 114 |
# File 'lib/console_tetris/background_board.rb', line 109 def print_gameover print "\e[2J" print "\e[1;1H" print "#{AsciiArt.number_to_aa(point).gsub(/^/, "\e[G")} Points!!!!\n" print AsciiArt::GAMEOVER.gsub(/^/, "\e[G") end |
#print_next_block(tetrimino) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/console_tetris/background_board.rb', line 49 def print_next_block(tetrimino) block = tetrimino.block.map {|row| row.map {|b| next ' ' if b == 0 "\e[3#{b}m[]" }.join('') + "\e[0m" } print "\e[5;1H" print "\e[1J" print "\e[1;1H" print "\e[G ---------- \n" print "\e[G| #{block[0].to_s.center(8, ' ')} |\n" print "\e[G| #{block[1].to_s.center(8, ' ')} |\n" print "\e[G ---------- \n" end |
#print_point ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/console_tetris/background_board.rb', line 37 def print_point print "\e[3;1H" print "\e[13G" print "\e[K" print "#{@point.to_s.rjust(6, ' ')} P\n" print "\e[13G" print "\e[K" print "-------- \n" end |
#remove_filled_line! ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/console_tetris/background_board.rb', line 28 def remove_filled_line! @board.reject! {|a| a.all? {|e| e != 0 } } remove_size = BOARD_SIZE[:y] - @board.count @point = @point + remove_size * 1000 remove_size.times { @board.unshift(blank_line) } end |
#stack!(tetrimino) ⇒ Object
14 15 16 |
# File 'lib/console_tetris/background_board.rb', line 14 def stack!(tetrimino) tetrimino.block.each.with_index {|row, i| row.each.with_index {|value, j| @board[tetrimino.y_coordinate + i][tetrimino.x_coordinate + j] += value } } end |