Class: Interferoman::BattleshipBoard

Inherits:
Object
  • Object
show all
Defined in:
lib/interferoman/battleship_board.rb

Constant Summary collapse

@@sizes =
{:carrier => 5,
:battleship => 4,
:destroyer => 3,
:submarine => 3,
:patrolship => 2}

Instance Method Summary collapse

Constructor Details

#initializeBattleshipBoard

Returns a new instance of BattleshipBoard.



9
10
11
# File 'lib/interferoman/battleship_board.rb', line 9

def initialize
  @board = []
end

Instance Method Details

#[](row, column) ⇒ Object



35
36
37
# File 'lib/interferoman/battleship_board.rb', line 35

def [](row, column)
  @board[index_for(row, column)]
end

#[]=(row, column, value) ⇒ Object



31
32
33
# File 'lib/interferoman/battleship_board.rb', line 31

def []=(row, column, value)
  @board[index_for(row, column)] = value
end

#collides?(ship, row, column, orientation) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/interferoman/battleship_board.rb', line 19

def collides?(ship, row, column, orientation)
  for_ship(ship, row, column, orientation) do |r, c|
    return true unless self[r, c].nil?
  end

  false
end

#first_blank(row, column) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/interferoman/battleship_board.rb', line 66

def first_blank(row, column)
  [:east, :south, :west, :north].each do |direction|
    if is_blank?(row, column, direction)
      return get_position(row, column, direction) << direction
    end
  end

  nil
end

#get_position(row, column, movement) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/interferoman/battleship_board.rb', line 51

def get_position(row, column, movement)
  case movement
  when :north
    return [row-1, column] if row > 0
  when :south
    return [row+1, column] if row < 9
  when :east
    return [row, column+1] if column < 9
  when :west
    return [row, column-1] if column > 0
  end

  raise ArgumentError.new("Invalid movement: #{row}, #{column}, #{movement}")
end

#has_room_for_ship(row, column, size) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/interferoman/battleship_board.rb', line 76

def has_room_for_ship(row, column, size)
  return false unless self[row, column].nil?

  blanks_to_east = number_of_blanks_in_direction(row, column, :east)
  blanks_to_west = number_of_blanks_in_direction(row, column, :west)
  return true if blanks_to_east + blanks_to_west + 1 >= size

  blanks_to_north = number_of_blanks_in_direction(row, column, :north)
  blanks_to_south = number_of_blanks_in_direction(row, column, :south)
  return true if blanks_to_north + blanks_to_south + 1 >= size

  false
end

#is_blank?(row, column, direction = nil) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/interferoman/battleship_board.rb', line 39

def is_blank?(row, column, direction=nil)
  if direction == nil
    self[row, column] == nil
  else
    begin
      self[*get_position(row, column, direction)] == nil
    rescue
      false
    end
  end
end

#number_of_blanks_in_direction(row, column, direction) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/interferoman/battleship_board.rb', line 90

def number_of_blanks_in_direction(row, column, direction)
  begin
    next_position = get_position(row, column, direction)
    if self[*next_position].nil?
      return 1 + number_of_blanks_in_direction(next_position[0],
                                               next_position[1], direction)
    else
      return 0
    end
  rescue
    return 0
  end
end

#place(ship, row, column, orientation) ⇒ Object



13
14
15
16
17
# File 'lib/interferoman/battleship_board.rb', line 13

def place(ship, row, column, orientation)
  for_ship(ship, row, column, orientation) do |r, c|
    self[r, c] = ship
  end
end

#size_of(ship) ⇒ Object



27
28
29
# File 'lib/interferoman/battleship_board.rb', line 27

def size_of(ship)
  @@sizes[ship]
end