Skip to content

Commit

Permalink
add counter for number of filled rows
Browse files Browse the repository at this point in the history
  • Loading branch information
lau committed Apr 5, 2013
1 parent dd5889c commit 9a3febc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/letris/board.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def is_game_over?
@game_over_state
end

def filled_row_count
count = 0
@rows.each { |row| count = count + 1 if row.compact.size == width }
count
end

def current_piece=(piece)
@current_piece = piece
init_piece_pos
Expand Down Expand Up @@ -51,15 +57,16 @@ def move_piece_down
if @current_piece_pos[1] == 0 || would_current_piece_collide_if_moved_down?
place_current_piece_on_board
get_next_piece
else
@current_piece_pos[1] = @current_piece_pos[1]-1
end
@current_piece_pos[1] = @current_piece_pos[1]-1
end

def place_current_piece_on_board
# 4 times because 4 is the maximum width and height of a piece. TODO: don't use a hardcoded number like this
4.times do |y|
4.times do |x|
@rows[cur_piece_pos_x+x][cur_piece_pos_y+y] = "X" if @current_piece.has_tile?(x,y)
@rows[cur_piece_pos_y+y][cur_piece_pos_x+x] = "X" if @current_piece.has_tile?(x,y) && cur_piece_pos_y+y <= 19
end
end
check_for_game_over
Expand All @@ -84,8 +91,8 @@ def rotate_piece
end

def tile_for_xy(x, y)
return 'X' if current_piece_has_tile_at?(x, y) #x==@current_piece_pos[0] && y == @current_piece_pos[1]
return 'B' if @rows[x][y]
return 'X' if current_piece_has_tile_at?(x, y)
return 'B' if @rows[y][x]
" "
end

Expand Down
1 change: 1 addition & 0 deletions run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
def draw_board(board)
system("clear")
buf = ''
#buf = buf + "rows: #{board.filled_row_count}\n"
y_pos = 19
board.height.times do |y|
buf = buf + ''
Expand Down
32 changes: 32 additions & 0 deletions spec/unit/board_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@
Letris::Board.new.height.must_equal 20
end

describe "When there is a filled row" do
before do
@board = Letris::Board.new
@board.add_new_piece_by_name(:o)
4.times { @board.move_piece_left }
20.times { @board.move_piece_down }

@board.add_new_piece_by_name(:o)
2.times { @board.move_piece_left }
20.times { @board.move_piece_down }

@board.add_new_piece_by_name(:o)
20.times { @board.move_piece_down }

@board.add_new_piece_by_name(:o)
2.times { @board.move_piece_right }
20.times { @board.move_piece_down }

@board.add_new_piece_by_name(:l)
4.times { @board.move_piece_right }
20.times { @board.move_piece_down }
end

it "the board should return the number of filled rows" do
@board.filled_row_count.must_equal 1
end
end

describe "When a piece is added to the board and the top of the piece is above the top" do
before do
@board = Letris::Board.new
Expand Down Expand Up @@ -77,6 +105,10 @@
@board.tile_for_xy_empty?(5,2).must_equal false
end

it "the board should return the number of filled rows" do
@board.filled_row_count.must_equal 0
end

it "it's not yet game over" do
@board.is_game_over?.must_equal false
end
Expand Down

0 comments on commit 9a3febc

Please sign in to comment.