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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#boardObject

Returns the value of attribute board.



83
84
85
# File 'lib/codenjoy/games/tetris/board.rb', line 83

def board
  @board
end

#current_figure_pointObject

Returns the value of attribute current_figure_point.



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

def current_figure_point
  @current_figure_point
end

#current_figure_typeObject

Returns the value of attribute current_figure_type.



85
86
87
# File 'lib/codenjoy/games/tetris/board.rb', line 85

def current_figure_type
  @current_figure_type
end

#future_figuresObject

Returns the value of attribute future_figures.



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

def future_figures
  @future_figures
end

#sizeInteger

Returns board size

Returns:

  • (Integer)

    board size



108
109
110
# File 'lib/codenjoy/games/tetris/board.rb', line 108

def size
  @size
end

Instance Method Details

#coords_to_pos(point) ⇒ Integer

Converts position in board string to coords

Parameters:

  • point (Point)

    position

Returns:

  • (Integer)

    position in board string



263
264
265
# File 'lib/codenjoy/games/tetris/board.rb', line 263

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



159
160
161
162
# File 'lib/codenjoy/games/tetris/board.rb', line 159

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



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/codenjoy/games/tetris/board.rb', line 187

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[...]



116
117
118
# File 'lib/codenjoy/games/tetris/board.rb', line 116

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



207
208
209
# File 'lib/codenjoy/games/tetris/board.rb', line 207

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



214
215
216
# File 'lib/codenjoy/games/tetris/board.rb', line 214

def get_free_space
  get(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[...]



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/codenjoy/games/tetris/board.rb', line 139

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



125
126
127
128
129
130
131
132
133
# File 'lib/codenjoy/games/tetris/board.rb', line 125

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



178
179
180
181
# File 'lib/codenjoy/games/tetris/board.rb', line 178

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



169
170
171
172
# File 'lib/codenjoy/games/tetris/board.rb', line 169

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



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/codenjoy/games/tetris/board.rb', line 225

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 == 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



252
253
254
255
256
257
# File 'lib/codenjoy/games/tetris/board.rb', line 252

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

  Point.new x, y
end

#process(str) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/codenjoy/games/tetris/board.rb', line 89

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

#to_sObject



99
100
101
102
103
104
# File 'lib/codenjoy/games/tetris/board.rb', line 99

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