Class: Figure

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#colorObject

Returns the value of attribute color.



9
10
11
# File 'lib/figure.rb', line 9

def color
  @color
end

#distanceObject

Returns the value of attribute distance.



9
10
11
# File 'lib/figure.rb', line 9

def distance
  @distance
end

#matrixObject

Returns the value of attribute matrix.



9
10
11
# File 'lib/figure.rb', line 9

def matrix
  @matrix
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/figure.rb', line 9

def name
  @name
end

#pos_xObject

Returns the value of attribute pos_x.



9
10
11
# File 'lib/figure.rb', line 9

def pos_x
  @pos_x
end

#pos_yObject

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

#drawObject

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

#dropObject



57
58
59
60
# File 'lib/figure.rb', line 57

def drop
  @window.glass.glue_in(self)
  @window.show_next_figure
end

#move_downObject



74
75
76
# File 'lib/figure.rb', line 74

def move_down
  move_to(@pos_x, @pos_y + 1)
end

#move_leftObject



66
67
68
# File 'lib/figure.rb', line 66

def move_left
  move_to(@pos_x - 1, @pos_y)
end

#move_rightObject



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_xObject



49
50
51
# File 'lib/figure.rb', line 49

def size_x
  @matrix[0].size
end

#size_yObject



53
54
55
# File 'lib/figure.rb', line 53

def size_y
  @matrix.size
end

#turn_leftObject



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_rightObject



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