Class: Board
- Inherits:
-
Object
- Object
- Board
- Defined in:
- lib/software_challenge_client/board.rb
Overview
A representation of a mississippi queen game board
Instance Attribute Summary collapse
-
#fields ⇒ Hash<Field>
readonly
coordinate-tuple (2-element-array) of the field.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #add_field(field) ⇒ Object
-
#field(x, y) ⇒ Field
Access fields of the board.
-
#get_all_in_direction(from_x, from_y, direction, distance = 1) ⇒ Array<Field>
A list of fields in given direction up to given distance from the field with given coordinates.
-
#get_in_direction(from_x, from_y, direction, distance = 1) ⇒ Field
The field in given direction with given distance from the field with given coordinates.
-
#get_neighbor(x, y, direction) ⇒ Integer
The coordinates of the neighbor of the field specified by given coordinates in specified direction.
-
#initialize ⇒ Board
constructor
Initializes the board.
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Board
Initializes the board
18 19 20 |
# File 'lib/software_challenge_client/board.rb', line 18 def initialize @fields = {} end |
Instance Attribute Details
Instance Method Details
#==(other) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/software_challenge_client/board.rb', line 26 def ==(other) fields.each_with_index do |row, x| row.each_with_index do |field, y| return false if field != other.field(x, y) end end true end |
#add_field(field) ⇒ Object
35 36 37 |
# File 'lib/software_challenge_client/board.rb', line 35 def add_field(field) fields[[field.x, field.y]] = field end |
#field(x, y) ⇒ Field
Access fields of the board.
98 99 100 |
# File 'lib/software_challenge_client/board.rb', line 98 def field(x, y) fields[[x,y]] end |
#get_all_in_direction(from_x, from_y, direction, distance = 1) ⇒ Array<Field>
85 86 87 88 89 90 91 |
# File 'lib/software_challenge_client/board.rb', line 85 def get_all_in_direction(from_x, from_y, direction, distance = 1) (1..distance).to_a.map do |i| get_in_direction( from_x, from_y, direction, i ) end end |
#get_in_direction(from_x, from_y, direction, distance = 1) ⇒ Field
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/software_challenge_client/board.rb', line 69 def get_in_direction(from_x, from_y, direction, distance = 1) x = from_x y = from_y distance.times do x, y = get_neighbor(x, y, direction) end if !field(x, y).nil? return field(x, y) else raise FieldUnavailableException.new(x, y) end end |
#get_neighbor(x, y, direction) ⇒ Integer
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/software_challenge_client/board.rb', line 42 def get_neighbor(x, y, direction) directions = { even_row: { Direction::RIGHT.key=> [+1, 0], Direction::UP_RIGHT.key=>[+1, -1], Direction::UP_LEFT.key=>[0, -1], Direction::LEFT.key=>[-1, 0], Direction::DOWN_LEFT.key=>[0, +1], Direction::DOWN_RIGHT.key=>[+1, +1] }, odd_row: { Direction::RIGHT.key=> [+1, 0], Direction::UP_RIGHT.key=> [ 0, -1], Direction::UP_LEFT.key=> [-1, -1], Direction::LEFT.key=> [-1, 0], Direction::DOWN_LEFT.key=> [-1, +1], Direction::DOWN_RIGHT.key=> [ 0, +1] } } parity = y.odd? ? :odd_row : :even_row dir = directions[parity][direction.key] return x + dir[0], y + dir[1] end |
#to_s ⇒ Object
22 23 24 |
# File 'lib/software_challenge_client/board.rb', line 22 def to_s fields.values.map { |f| f.type.key.to_s[0] }.join(' ') end |