Class: BTetrisKp::Board
- Inherits:
-
Object
- Object
- BTetrisKp::Board
- Defined in:
- lib/btetris_kp/core/board.rb
Overview
class representing one tetris board
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
-
#cur_piece ⇒ Object
Returns the value of attribute cur_piece.
Instance Method Summary collapse
-
#clear_row!(row_index) ⇒ Object
moves down all rows with smaller index then row_index, removing row at row_index content in the process.
-
#clear_rows! ⇒ Object
clears rows that are full, returns number of cleared rows.
-
#copy_row(r1, r2) ⇒ Object
copies content of one row into another r1 - source row, r2 - destination row.
-
#draw ⇒ Object
draws board and all blocks on it.
-
#draw_background ⇒ Object
draws board border and background.
-
#draw_block(x, y, val) ⇒ Object
draws block on board.
-
#fill_row_randomly(index) ⇒ Object
fills row randomly with blocks and spaces atleast one block has to be empty space.
-
#from_s!(state) ⇒ Object
loads board content from string !!! (expects board to contain only nrs from 0 to 9) !!!.
-
#game_over? ⇒ Boolean
checks if game is over.
-
#get_board_s ⇒ Object
returns string of board with current piece.
-
#initialize(window, posx, posy) ⇒ Board
constructor
A new instance of Board.
-
#insert_garbage!(cnt) ⇒ Object
inserts garbage in board depending on count of cleared rows.
-
#new_piece! ⇒ Object
generates new piece, current piece is set on board and replaced by new one.
-
#next_step! ⇒ Object
next step of game, returns count of cleared rows.
-
#piece_down! ⇒ Object
moves piece down faster.
-
#piece_drop! ⇒ Object
drops peace down as far as possible.
-
#piece_left! ⇒ Object
moves piece left.
-
#piece_right! ⇒ Object
moves piece right.
-
#piece_rotate! ⇒ Object
rotates piece, returns true when successful.
-
#piece_stuck? ⇒ Boolean
checks whether piece can move down.
-
#to_s ⇒ Object
converts board content to string.
Constructor Details
#initialize(window, posx, posy) ⇒ Board
Returns a new instance of Board.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/btetris_kp/core/board.rb', line 9 def initialize(window, posx, posy) @window = window @tile_size = @window.width / 30 # board corners position @posx = posx @posx2 = posx + Const::PNR_HOR * @tile_size @posy = posy @posy2 = posy + Const::PNR_VER * @tile_size # initializes board array and current piece @board = Array.new(Const::PNR_VER) { Array.new(Const::PNR_HOR, 0) } @cur_piece = Piece.new(@board, 0, Const::PNR_HOR / 3 + 1, rand(Const::TILES.size)) end |
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
7 8 9 |
# File 'lib/btetris_kp/core/board.rb', line 7 def board @board end |
#cur_piece ⇒ Object
Returns the value of attribute cur_piece.
7 8 9 |
# File 'lib/btetris_kp/core/board.rb', line 7 def cur_piece @cur_piece end |
Instance Method Details
#clear_row!(row_index) ⇒ Object
moves down all rows with smaller index then row_index, removing row at row_index content in the process
114 115 116 117 118 |
# File 'lib/btetris_kp/core/board.rb', line 114 def clear_row!(row_index) for index in row_index.downto(0) do copy_row(index - 1, index) end end |
#clear_rows! ⇒ Object
clears rows that are full, returns number of cleared rows
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/btetris_kp/core/board.rb', line 121 def clear_rows! cnt = 0 @board.each_with_index do |row, index| unless row.include?(0) clear_row!(index) cnt += 1 end end cnt end |
#copy_row(r1, r2) ⇒ Object
copies content of one row into another r1 - source row, r2 - destination row
85 86 87 88 89 90 91 |
# File 'lib/btetris_kp/core/board.rb', line 85 def copy_row(r1, r2) if r1 < 0 @board[r2].each_with_index { |val, index| @board[r2][index] = 0 } else @board[r1].each_with_index { |val, index| @board[r2][index] = val } end end |
#draw ⇒ Object
draws board and all blocks on it
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/btetris_kp/core/board.rb', line 187 def draw draw_background @cur_piece.set_on_board @board.each_with_index do |row, y| row.each_with_index do |val, x| draw_block(x, y, val) end end @cur_piece.unset_on_board end |
#draw_background ⇒ Object
draws board border and background
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/btetris_kp/core/board.rb', line 138 def draw_background @window.draw_quad(@posx, @posy, Const::BOARD_BACK_CLR, @posx2, @posy, Const::BOARD_BACK_CLR, @posx, @posy2, Const::BOARD_BACK_CLR, @posx2, @posy2, Const::BOARD_BACK_CLR) @window.draw_line(@posx - 1, @posy - 1, Const::BOARD_CLR, @posx2 + 1, @posy - 1, Const::BOARD_CLR) @window.draw_line(@posx2 + 1, @posy - 1, Const::BOARD_CLR, @posx2 + 1, @posy2 + 1, Const::BOARD_CLR) @window.draw_line(@posx - 1, @posy - 1, Const::BOARD_CLR, @posx - 1, @posy2 + 1, Const::BOARD_CLR) @window.draw_line(@posx - 1, @posy2 + 1, Const::BOARD_CLR, @posx2 + 1, @posy2 + 1, Const::BOARD_CLR) end |
#draw_block(x, y, val) ⇒ Object
draws block on board
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/btetris_kp/core/board.rb', line 154 def draw_block(x, y, val) unless val == 0 x1 = @posx + x * @tile_size x2 = @posx + (x + 1) * @tile_size y1 = @posy + y * @tile_size y2 = @posy + (y + 1) * @tile_size @window.draw_quad(x1, y1, Const::TILE_COLORS[val], x2, y1, Const::TILE_COLORS[val], x1, y2, Const::TILE_COLORS[val], x2, y2, Const::TILE_COLORS[val]) @window.draw_line(x2 - 1, y1 + 1, Const::TILE_SHADOW_CLR[val], x2 - 1, y2 - 1, Const::TILE_SHADOW_CLR[val]) @window.draw_line(x1 + 1 , y2 - 1, Const::TILE_SHADOW_CLR[val], x2 - 1, y2 - 1, Const::TILE_SHADOW_CLR[val]) @window.draw_line(x2, y1, Const::TILE_SHADOW_CLR[val], x2, y2, Const::TILE_SHADOW_CLR[val]) @window.draw_line(x1, y2, Const::TILE_SHADOW_CLR[val], x2, y2, Const::TILE_SHADOW_CLR[val]) @window.draw_line(x1, y1, Const::TILE_BRIGHT_CLR[val], x2, y1, Const::TILE_BRIGHT_CLR[val]) @window.draw_line(x1, y1, Const::TILE_BRIGHT_CLR[val], x1, y2, Const::TILE_BRIGHT_CLR[val]) @window.draw_line(x1 + 1, y1 + 1, Const::TILE_BRIGHT_CLR[val], x2 - 1, y1 + 1, Const::TILE_BRIGHT_CLR[val]) @window.draw_line(x1 + 1, y1 + 1, Const::TILE_BRIGHT_CLR[val], x1 + 1, y2 - 1, Const::TILE_BRIGHT_CLR[val]) end end |
#fill_row_randomly(index) ⇒ Object
fills row randomly with blocks and spaces atleast one block has to be empty space
95 96 97 98 99 100 |
# File 'lib/btetris_kp/core/board.rb', line 95 def fill_row_randomly(index) @board[index].each_with_index do |val, x| @board[index][x] = rand(Const::TILE_COLORS_NR + 1) end @board[index][rand(Const::PNR_HOR)] = 0 end |
#from_s!(state) ⇒ Object
loads board content from string !!! (expects board to contain only nrs from 0 to 9) !!!
200 201 202 |
# File 'lib/btetris_kp/core/board.rb', line 200 def from_s!(state) @board = state.split('').map { |i| i.to_i }.each_slice(Const::PNR_HOR).to_a end |
#game_over? ⇒ Boolean
checks if game is over
133 134 135 |
# File 'lib/btetris_kp/core/board.rb', line 133 def game_over? !@cur_piece.can_be_set? end |
#get_board_s ⇒ Object
returns string of board with current piece
205 206 207 208 209 210 |
# File 'lib/btetris_kp/core/board.rb', line 205 def get_board_s @cur_piece.set_on_board s = to_s @cur_piece.unset_on_board s end |
#insert_garbage!(cnt) ⇒ Object
inserts garbage in board depending on count of cleared rows
103 104 105 106 107 108 109 110 |
# File 'lib/btetris_kp/core/board.rb', line 103 def insert_garbage!(cnt) for index in cnt.upto(Const::PNR_VER - 1) copy_row(index, index - cnt) end for index in (Const::PNR_VER - cnt).upto(Const::PNR_VER - 1) fill_row_randomly(index) end end |
#new_piece! ⇒ Object
generates new piece, current piece is set on board and replaced by new one
26 27 28 29 |
# File 'lib/btetris_kp/core/board.rb', line 26 def new_piece! @cur_piece.set_on_board @cur_piece = Piece.new(@board, 0, Const::PNR_HOR / 3 + 1, rand(Const::TILES.size)) end |
#next_step! ⇒ Object
next step of game, returns count of cleared rows
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/btetris_kp/core/board.rb', line 72 def next_step! cnt = 0 @cur_piece.move_down! unless @cur_piece.can_be_set? @cur_piece.move_up! new_piece! cnt = clear_rows! end cnt end |
#piece_down! ⇒ Object
moves piece down faster
52 53 54 55 |
# File 'lib/btetris_kp/core/board.rb', line 52 def piece_down! @cur_piece.move_down! @cur_piece.move_up! unless @cur_piece.can_be_set? end |
#piece_drop! ⇒ Object
drops peace down as far as possible
66 67 68 69 |
# File 'lib/btetris_kp/core/board.rb', line 66 def piece_drop! @cur_piece.move_down! while @cur_piece.can_be_set? @cur_piece.move_up! end |
#piece_left! ⇒ Object
moves piece left
40 41 42 43 |
# File 'lib/btetris_kp/core/board.rb', line 40 def piece_left! @cur_piece.move_left! @cur_piece.move_right! unless @cur_piece.can_be_set? end |
#piece_right! ⇒ Object
moves piece right
46 47 48 49 |
# File 'lib/btetris_kp/core/board.rb', line 46 def piece_right! @cur_piece.move_right! @cur_piece.move_left! unless @cur_piece.can_be_set? end |
#piece_rotate! ⇒ Object
rotates piece, returns true when successful
58 59 60 61 62 63 |
# File 'lib/btetris_kp/core/board.rb', line 58 def piece_rotate! @cur_piece.rotate_right! return true if @cur_piece.can_be_set? @cur_piece.rotate_left! false end |
#piece_stuck? ⇒ Boolean
checks whether piece can move down
32 33 34 35 36 37 |
# File 'lib/btetris_kp/core/board.rb', line 32 def piece_stuck? @cur_piece.move_down! can_be_set = @cur_piece.can_be_set? @cur_piece.move_up! !can_be_set end |
#to_s ⇒ Object
converts board content to string
213 214 215 216 217 218 219 220 221 |
# File 'lib/btetris_kp/core/board.rb', line 213 def to_s s = '' @board.each do |row| row.each do |val| s << val.to_s end end s end |