class Rubytris::Field
Constants
- BLOCK_CHAR
- FINISH_COUNT
- HEIGHT
- NONE_CHAR
- WALL_CHAR
- WIDTH
Attributes
point[R]
Public Class Methods
new()
click to toggle source
# File lib/rubytris/field.rb, line 18 def initialize() @field = init_field @point = 0 end
Public Instance Methods
are_block?(next_pos)
click to toggle source
# File lib/rubytris/field.rb, line 45 def are_block?(next_pos) result = false next_pos.each do |pos| if is_block?(pos[0], pos[1]) result = true end end result end
clear()
click to toggle source
# File lib/rubytris/field.rb, line 23 def clear() @field.each_with_index do |line, y| line.each_with_index do |l, x| @field[y][x] = 0 if l == FieldStatus::ACTIVE end end end
fix(now_pos)
click to toggle source
# File lib/rubytris/field.rb, line 55 def fix(now_pos) now_pos.each do |pos| @field[pos[1]][pos[0]] = FieldStatus::FIX end end
game_finish?()
click to toggle source
# File lib/rubytris/field.rb, line 41 def game_finish?() @point >= 40 end
line_clear()
click to toggle source
# File lib/rubytris/field.rb, line 31 def line_clear() @field.each_with_index do |line, y| if line == [FieldStatus::WALL, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::FIX, FieldStatus::WALL] @point += 1 @field.delete_at(y) @field.insert(0, [FieldStatus::WALL, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::NONE, FieldStatus::WALL]) end end end
pre_fix(now_pos)
click to toggle source
# File lib/rubytris/field.rb, line 61 def pre_fix(now_pos) now_pos.each do |pos| @field[pos[1]][pos[0]] = FieldStatus::ACTIVE end end
write()
click to toggle source
# File lib/rubytris/field.rb, line 67 def write() text = "\n\e[25D" @field.each do |line| line.each do |l| if l == FieldStatus::NONE text += NONE_CHAR elsif l == FieldStatus::ACTIVE || l == FieldStatus::FIX text += BLOCK_CHAR else text += WALL_CHAR end end text += "\n\e[25D" end puts text end
Private Instance Methods
init_field()
click to toggle source
# File lib/rubytris/field.rb, line 89 def init_field f = [] HEIGHT.times do |i| line = [] WIDTH.times do |j| line[j] = (j == 0 || j == WIDTH - 1 || i == HEIGHT - 1) ? FieldStatus::WALL : FieldStatus::NONE end f[i] = line end return f end
is_block?(x, y)
click to toggle source
# File lib/rubytris/field.rb, line 85 def is_block?(x, y) @field[y][x] == FieldStatus::WALL || @field[y][x] == FieldStatus::FIX end