Class: Rubytris::Current

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytris/current.rb

Constant Summary collapse

BLOCKS =
[
      [ [0, 1, 0, 0],
        [1, 1, 1, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0], ],

[ [0, 1, 1, 0],
  [1, 1, 0, 0],
  [0, 0, 0, 0],
  [0, 0, 0, 0], ],

[ [1, 1, 0, 0],
  [0, 1, 1, 0],
  [0, 0, 0, 0],
  [0, 0, 0, 0], ],

[ [1, 1, 1, 1],
  [0, 0, 0, 0],
  [0, 0, 0, 0],
  [0, 0, 0, 0], ],

[ [1, 1, 0, 0],
  [1, 1, 0, 0],
  [0, 0, 0, 0],
  [0, 0, 0, 0], ],

[ [1, 0, 0, 0],
  [1, 1, 1, 0],
  [0, 0, 0, 0],
  [0, 0, 0, 0], ],

[ [0, 0, 1, 0],
  [1, 1, 1, 0],
  [0, 0, 0, 0],
  [0, 0, 0, 0], ],
]

Instance Method Summary collapse

Constructor Details

#initialize(x, y, current = new_block) ⇒ Current

Returns a new instance of Current.



40
41
42
43
44
# File 'lib/rubytris/current.rb', line 40

def initialize(x, y, current = new_block)
      @x = x
      @y = y
      @current = current
end

Instance Method Details

#fallObject

move fall



71
72
73
# File 'lib/rubytris/current.rb', line 71

def fall()
      Current.new(@x, @y + 1, @current)
end

#leftObject

move left



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

def left()
      Current.new(@x - 1, @y, @current)
end

#move_positionObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubytris/current.rb', line 46

def move_position()
      count = 0
      block_position = Array.new(4)
      @current.each_with_index do |block, y|
            block.each_with_index do |b, x|
                  if b == 1
                        block_position[count] = [ @x + x, @y + y ]
                        count += 1
                  end
            end
      end
      block_position
end

#new_blockObject

create new block



90
91
92
93
# File 'lib/rubytris/current.rb', line 90

def new_block()
      rnd = rand(BLOCKS.length)
      BLOCKS[rnd]
end

#rightObject

move right



61
62
63
# File 'lib/rubytris/current.rb', line 61

def right()
      Current.new(@x + 1, @y, @current)
end

#rotationObject

move rotation



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rubytris/current.rb', line 76

def rotation()
      tmp = Array.new(4){ Array.new(4, 0)}
      @current.each_with_index do |line, y|
            line.each_with_index do |l, x|
                  tmp[x][y] = l
            end
      end
      tmp.each_with_index do |line, i|
            tmp[i] = line.reverse
      end
      Current.new(@x, @y, tmp)
end