Class: Codenjoy::Client::Games::Tetris::Board
- Inherits:
-
Object
- Object
- Codenjoy::Client::Games::Tetris::Board
- Defined in:
- lib/codenjoy/games/tetris/board.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
-
#current_figure_point ⇒ Object
Returns the value of attribute current_figure_point.
-
#current_figure_type ⇒ Object
Returns the value of attribute current_figure_type.
-
#future_figures ⇒ Object
Returns the value of attribute future_figures.
-
#size ⇒ Integer
Returns board size.
Instance Method Summary collapse
-
#coords_to_pos(point) ⇒ Integer
Converts position in
boardstring to coords. -
#count_near(point, element) ⇒ Integer
Count how many objects of specified type around position.
-
#get(element) ⇒ Array[Point]
List of given elements.
-
#get_at(point) ⇒ String
Get object at position.
-
#get_figures ⇒ Array[Point]
List of busy spaces in the glass.
-
#get_free_space ⇒ Array[Point]
Return list of free spaces in the glass.
-
#get_near(point) ⇒ Object
Check if element is near position.
-
#is_at?(point, element) ⇒ Boolean
Is element type/s is at specified X,Y?.
-
#is_free?(point) ⇒ Boolean
Check if figures (elements of
FIGURESarray) at position. -
#is_near?(point, element) ⇒ Integer
Count how many objects of specified type around position.
-
#next_element_in_direction(point, direction, element) ⇒ Integer
How far specified element from position (strait direction) Return
sizeif wall in specified direction. -
#pos_to_coords(pos) ⇒ Point
Converts position in
boardstring to coords. - #process(str) ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
83 84 85 |
# File 'lib/codenjoy/games/tetris/board.rb', line 83 def board @board end |
#current_figure_point ⇒ Object
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_type ⇒ Object
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_figures ⇒ Object
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 |
#size ⇒ Integer
Returns 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
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
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
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
116 117 118 |
# File 'lib/codenjoy/games/tetris/board.rb', line 116 def get_at(point) board[coords_to_pos(point)] end |
#get_figures ⇒ Array[Point]
List of busy spaces in the glass
207 208 209 |
# File 'lib/codenjoy/games/tetris/board.rb', line 207 def get_figures get(FIGURES) end |
#get_free_space ⇒ Array[Point]
Return list of free spaces in the glass
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
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?
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
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
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
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
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_s ⇒ Object
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 |