Class: Codenjoy::Client::Games::Tetris::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/codenjoy/games/tetris/board.rb

Constant Summary collapse

ELEMENTS =

This is glass content

{
  :I_BLUE => 'I',
  :J_CYAN => 'J',
  :L_ORANGE => 'L',
  :O_YELLOW => 'O',
  :S_GREEN => 'S',
  :T_PURPLE => 'T',
  :Z_RED => 'Z',
  :NONE => '.',
}
FIGURES =
[:I_BLUE, :J_CYAN, :L_ORANGE, :O_YELLOW, :S_GREEN, :T_PURPLE, :Z_RE]
.map{ |e| Codenjoy::Client::Games::Tetris::Board::ELEMENTS[e] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#boardObject

Returns the value of attribute board.



50
51
52
# File 'lib/codenjoy/games/tetris/board.rb', line 50

def board
  @board
end

#current_figure_pointObject

Returns the value of attribute current_figure_point.



54
55
56
# File 'lib/codenjoy/games/tetris/board.rb', line 54

def current_figure_point
  @current_figure_point
end

#current_figure_typeObject

Returns the value of attribute current_figure_type.



52
53
54
# File 'lib/codenjoy/games/tetris/board.rb', line 52

def current_figure_type
  @current_figure_type
end

#future_figuresObject

Returns the value of attribute future_figures.



53
54
55
# File 'lib/codenjoy/games/tetris/board.rb', line 53

def future_figures
  @future_figures
end

#sizeInteger

Returns board size

Returns:

  • (Integer)

    board size



87
88
89
# File 'lib/codenjoy/games/tetris/board.rb', line 87

def size
  @size
end

Instance Method Details

#compare(pt1, pt2) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/codenjoy/games/tetris/board.rb', line 56

def compare(pt1, pt2)
  if (pt1.x <=> pt2.x) != 0
    pt1.x <=> pt2.x
  else
    pt1.y <=> pt2.y
  end
end

#coords_to_pos(point) ⇒ Integer

Converts position in board string to coords

Parameters:

  • point (Point)

    position

Returns:

  • (Integer)

    position in board string



242
243
244
# File 'lib/codenjoy/games/tetris/board.rb', line 242

def coords_to_pos(point)
  (size - 1 - point.y) * size + point.x
end

#count_near(point, element) ⇒ Integer

Count how many objects of specified type around position

Parameters:

  • point (Point)

    position

  • element (String, Array)

    one or array of ELEMENTS[...]

Returns:

  • (Integer)

    number of objects around



138
139
140
141
# File 'lib/codenjoy/games/tetris/board.rb', line 138

def count_near(point, element)
  elements = get_near(point)
  elements.count { |it| it == element }
end

#get(element) ⇒ Array[Point]

List of given elements

Parameters:

  • element (String, Array)

    one or array of ELEMENTS[...]

Returns:

  • (Array[Point])

    list of barriers on the filed



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/codenjoy/games/tetris/board.rb', line 166

def get(element)
  res = []
  pos = 0
  board.chars.each do |ch|
    if element.is_a?(Array)
      res << pos_to_coords(pos) if element.include? ch
    elsif element.is_a?(String)
      res << pos_to_coords(pos) if element == ch
    else
      raise ArgumentError.new("Invalid argument type #{element.class}")
    end
    pos += 1
  end

  sort(res)
end

#get_at(point) ⇒ String

Get object at position

Parameters:

  • point (Point)

    position

Returns:

  • (String)

    char with object, compare with ELEMENTS[...]



95
96
97
# File 'lib/codenjoy/games/tetris/board.rb', line 95

def get_at(point)
  board[coords_to_pos(point)]
end

#get_figuresArray[Point]

List of busy spaces in the glass

Returns:

  • (Array[Point])

    list of barriers on the filed



186
187
188
# File 'lib/codenjoy/games/tetris/board.rb', line 186

def get_figures
  get(FIGURES)
end

#get_free_spaceArray[Point]

Return list of free spaces in the glass

Returns:

  • (Array[Point])

    array of walls positions



193
194
195
# File 'lib/codenjoy/games/tetris/board.rb', line 193

def get_free_space
  get(Codenjoy::Client::Games::Tetris::Board::ELEMENTS[:NONE])
end

#get_near(point) ⇒ Object

Check if element is near position

Parameters:

  • point (Point)

    position

  • element (String, Array)

    one or array of ELEMENTS[...]



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/codenjoy/games/tetris/board.rb', line 118

def get_near(point)
  res = []

  for dx in -1..1
    for dy in -1..1
      if dx == 0 && dy == 0
        next
      end
      res << get_at(Point.new(point.x + dx, point.y + dy))
    end
  end

  res.empty? ? nil : res
end

#is_at?(point, element) ⇒ Boolean

Is element type/s is at specified X,Y?

Parameters:

  • point (Point)

    position

  • element (String, Array)

    one or array of ELEMENTS[...]

Returns:

  • (Boolean)

    if element at position



104
105
106
107
108
109
110
111
112
# File 'lib/codenjoy/games/tetris/board.rb', line 104

def is_at?(point, element)
  if element.is_a?(Array)
    element.include?(get_at(point))
  elsif element.is_a?(String)
    get_at(point) == element
  else
    raise ArgumentError.new("Invalid argument type #{element.class}")
  end
end

#is_free?(point) ⇒ Boolean

Check if figures (elements of FIGURES array) at position

Parameters:

  • point (Point)

    position

Returns:

  • (Boolean)

    true if barrier at



157
158
159
160
# File 'lib/codenjoy/games/tetris/board.rb', line 157

def is_free?(point)
  element = board[coords_to_pos(point)]
  !FIGURES.include? element
end

#is_near?(point, element) ⇒ Integer

Count how many objects of specified type around position

Parameters:

  • point (Point)

    position

  • element (String, Array)

    one or array of ELEMENTS[...]

Returns:

  • (Integer)

    number of objects around



148
149
150
151
# File 'lib/codenjoy/games/tetris/board.rb', line 148

def is_near?(point, element)
  elements = get_near(point)
  elements.find { |it| it == element } != nil
end

#next_element_in_direction(point, direction, element) ⇒ Integer

How far specified element from position (strait direction) Return size if wall in specified direction

Parameters:

  • point (Point)

    position

  • direction (String)

    direction ‘UP’, ‘DOWN’, ‘LEFT’, ‘RIGHT’

  • element (String)

    on of ELEMENTS[...]

Returns:

  • (Integer)

    distance



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/codenjoy/games/tetris/board.rb', line 204

def next_element_in_direction(point, direction, element)
  dirs = {
      'UP'    => [0, -1],
      'DOWN'  => [0, +1],
      'LEFT'  => [-1, 0],
      'RIGHT' => [+1, 0],
  }

  (1..size).each do |distance|
    el = get_at(
        Point.new(
            (point.x + distance * dirs[direction].first),
            (point.y + distance * dirs[direction].last)
        )
    )

    return size if element == Codenjoy::Client::Games::Tetris::Board::ELEMENTS[:WALL]
    return distance if element == el
  end

  size
end

#pos_to_coords(pos) ⇒ Point

Converts position in board string to coords

Parameters:

  • pos (Integer)

    position in string

Returns:

  • (Point)

    point object



231
232
233
234
235
236
# File 'lib/codenjoy/games/tetris/board.rb', line 231

def pos_to_coords(pos)
  x = (pos % size)
  y = size - 1 - (pos / size).to_i

  Point.new x, y
end

#process(str) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/codenjoy/games/tetris/board.rb', line 68

def process(str)
  puts "-------------------------------------------------------------------------------------------"
  json = JSON.parse(str)
  @board = json["layers"][0]
  @size = Math.sqrt(@board.length).round
  @current_figure_type = json["currentFigureType"]
  @future_figures = json["futureFigures"]
  @current_figure_point = Point.new(json["currentFigurePoint"]["x"], json["currentFigurePoint"]["y"])
end

#sort(array) ⇒ Object



64
65
66
# File 'lib/codenjoy/games/tetris/board.rb', line 64

def sort(array)
  array.sort { |pt1, pt2| compare(pt1, pt2) }
end

#to_sObject



78
79
80
81
82
83
# File 'lib/codenjoy/games/tetris/board.rb', line 78

def to_s
  return ("currentFigure: \"" + @current_figure_type + "\" at: " + @current_figure_point.to_s + "\n" +
     "futureFigures: " + @future_figures.to_s + "\n" +
     "board:" + "\n" +
     @board.scan(/.{#{@size}}|.+/).join("\n"))
end