Class: Figure
- Inherits:
-
Object
- Object
- Figure
- Defined in:
- lib/figure.rb
Overview
The figure unit. It handles the figure basics, like drawing itself making all sorts of manipulations like movements and rotations, plus it keeps the track of it’s position and distance below
Copyright © 2011 Nikolay Nemshilov
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#distance ⇒ Object
Returns the value of attribute distance.
-
#matrix ⇒ Object
Returns the value of attribute matrix.
-
#name ⇒ Object
Returns the value of attribute name.
-
#pos_x ⇒ Object
Returns the value of attribute pos_x.
-
#pos_y ⇒ Object
Returns the value of attribute pos_y.
Instance Method Summary collapse
-
#draw ⇒ Object
Draws the blocks of the figure.
- #drop ⇒ Object
-
#initialize(window, name = nil) ⇒ Figure
constructor
Basic constructor.
- #move_down ⇒ Object
- #move_left ⇒ Object
- #move_right ⇒ Object
- #move_to(x, y) ⇒ Object
- #size_x ⇒ Object
- #size_y ⇒ Object
- #turn_left ⇒ Object
- #turn_right ⇒ Object
Constructor Details
#initialize(window, name = nil) ⇒ Figure
Basic constructor
NOTE! creates a random figure if there is no explicit config
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/figure.rb', line 16 def initialize(window, name=nil) name ||= FIGURES.keys.shuffle[0] config = FIGURES[name].split('|') @pos_x = 0 @pos_y = 0 @name = name @window = window @color = COLORS[config.pop.to_sym] @block = Block.new(window, @color) @matrix = config.map{ |row| row.split('').map{|c| c == 'x'}} # getting rid of empty colls and rows @matrix.reject!{ |row| row.none? } while @matrix.map{ |row| row.last }.none? @matrix.each{ |row| row.pop } end end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
9 10 11 |
# File 'lib/figure.rb', line 9 def color @color end |
#distance ⇒ Object
Returns the value of attribute distance.
9 10 11 |
# File 'lib/figure.rb', line 9 def distance @distance end |
#matrix ⇒ Object
Returns the value of attribute matrix.
9 10 11 |
# File 'lib/figure.rb', line 9 def matrix @matrix end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/figure.rb', line 9 def name @name end |
#pos_x ⇒ Object
Returns the value of attribute pos_x.
9 10 11 |
# File 'lib/figure.rb', line 9 def pos_x @pos_x end |
#pos_y ⇒ Object
Returns the value of attribute pos_y.
9 10 11 |
# File 'lib/figure.rb', line 9 def pos_y @pos_y end |
Instance Method Details
#draw ⇒ Object
Draws the blocks of the figure
41 42 43 44 45 46 47 |
# File 'lib/figure.rb', line 41 def draw @matrix.each_with_index do |row, y| row.each_with_index do |visible, x| @block.draw(@pos_x + x, @pos_y + y) if visible end end end |
#drop ⇒ Object
57 58 59 60 |
# File 'lib/figure.rb', line 57 def drop @window.glass.glue_in(self) @window.show_next_figure end |
#move_down ⇒ Object
74 75 76 |
# File 'lib/figure.rb', line 74 def move_down move_to(@pos_x, @pos_y + 1) end |
#move_left ⇒ Object
66 67 68 |
# File 'lib/figure.rb', line 66 def move_left move_to(@pos_x - 1, @pos_y) end |
#move_right ⇒ Object
70 71 72 |
# File 'lib/figure.rb', line 70 def move_right move_to(@pos_x + 1, @pos_y) end |
#move_to(x, y) ⇒ Object
62 63 64 |
# File 'lib/figure.rb', line 62 def move_to(x, y) try_set(@matrix, x, y) end |
#size_x ⇒ Object
49 50 51 |
# File 'lib/figure.rb', line 49 def size_x @matrix[0].size end |
#size_y ⇒ Object
53 54 55 |
# File 'lib/figure.rb', line 53 def size_y @matrix.size end |
#turn_left ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/figure.rb', line 78 def turn_left new_matrix = (0..size_x-1).map do |i| @matrix.map{ |row| row[size_x - 1 - i] } end try_set(new_matrix, @pos_x, @pos_y) end |
#turn_right ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/figure.rb', line 86 def turn_right new_matrix = (0..size_x-1).map do |i| @matrix.map{ |row| row[i] }.reverse end try_set(new_matrix, @pos_x, @pos_y) end |