Class: Interferoman::Interferoman

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

Overview

Battleship Player

Battleship is board game between two players. See en.wikipedia.org/wiki/Battleship for more information and game rules.

A player represents the conputer AI to play a game of Battleship. It should know how to place ships and target the opponents ships.

This version of Battleship is played on a 10 x 10 grid where rows are labled by the letters A - J and columns are labled by the numbers 1 - 10. At the start of the game, each player will be asked for ship placements. Once the ships are placed, play proceeeds by each player targeting one square on their opponents map. A player may only target one square, reguardless of whether it resulted in a hit or not, before changing turns with her opponent.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInterferoman

:nodoc:



186
187
188
# File 'lib/interferoman/interferoman.rb', line 186

def initialize #:nodoc:
  reset
end

Instance Attribute Details

#disqualification_reasonObject (readonly)

Non API methods #####################################



181
182
183
# File 'lib/interferoman/interferoman.rb', line 181

def disqualification_reason
  @disqualification_reason
end

#enemy_targeted_sectorsObject (readonly)

Non API methods #####################################



181
182
183
# File 'lib/interferoman/interferoman.rb', line 181

def enemy_targeted_sectors
  @enemy_targeted_sectors
end

#opponentObject (readonly)

Non API methods #####################################



181
182
183
# File 'lib/interferoman/interferoman.rb', line 181

def opponent
  @opponent
end

#resultObject (readonly)

Non API methods #####################################



181
182
183
# File 'lib/interferoman/interferoman.rb', line 181

def result
  @result
end

#stateObject

Returns the value of attribute state.



184
185
186
# File 'lib/interferoman/interferoman.rb', line 184

def state
  @state
end

#targetsObject (readonly)

Non API methods #####################################



181
182
183
# File 'lib/interferoman/interferoman.rb', line 181

def targets
  @targets
end

#unknown_hitsObject (readonly)

Non API methods #####################################



181
182
183
# File 'lib/interferoman/interferoman.rb', line 181

def unknown_hits
  @unknown_hits
end

Instance Method Details

#battleship_placementObject

Returns the placement of the battleship. A battleship consumes 4 squares.

See carrier_placement for details on ship placement



73
74
75
# File 'lib/interferoman/interferoman.rb', line 73

def battleship_placement
  return random_placement(:battleship)
end

#carrier_placementObject

Returns the placement of the carrier. A carrier consumes 5 squares.

The return value is a string that describes the placements of the ship. The placement string must be in the following format:

"#{ROW}#{COL} #{ORIENTATION}"

eg

A1 horizontal # the ship will occupy A1, A2, A3, A4, and A5
A1 vertical # the ship will occupy A1, B1, C1, D1, and E1
F5 horizontal # the ship will occupy F5, F6, F7, F8, and F9
F5 vertical # the ship will occupy F5, G5, H5, I5, and J5

The ship must not fall off the edge of the map. For example, a carrier placement of ‘A8 horizontal’ would not leave enough space in the A row to accomodate the carrier since it requires 5 squares.

Ships may not overlap with other ships. For example a carrier placement of ‘A1 horizontal’ and a submarine placement of ‘A1 vertical’ would be invalid because both ships are trying to occupy the square A1.

Invalid ship placements will result in disqualification of the player.



65
66
67
# File 'lib/interferoman/interferoman.rb', line 65

def carrier_placement
  return random_placement(:carrier)
end

#destroyer_placementObject

Returns the placement of the destroyer. A destroyer consumes 3 squares.

See carrier_placement for details on ship placement



81
82
83
# File 'lib/interferoman/interferoman.rb', line 81

def destroyer_placement
  return random_placement(:destroyer)
end

#enemy_targeting(coordinates) ⇒ Object

enemy_targeting is called by the system to inform a player of their opponent’s move. When the opponent targets a square, this method is called with the coordinates.

Players may use this information to understand an opponent’s targeting strategy and place ships differently in subsequent games.



160
161
# File 'lib/interferoman/interferoman.rb', line 160

def enemy_targeting(coordinates)
end

#game_over(result, disqualification_reason = nil) ⇒ Object

Called by the system at the end of a game to inform the player of the results.

result  : 1 of 3 possible values (:victory, :defeate, :disqualified)
disqualification_reason : nil unless the game ended as the
              result of a disqualification.  In the event of a
              disqualification, this paramter will hold a string
              description of the reason for disqualification.  Both
              players will be informed of the reason.
:victory # indicates the player won the game
:defeat # indicates the player lost the game
:disqualified # indicates the player was disqualified


176
177
# File 'lib/interferoman/interferoman.rb', line 176

def game_over(result, disqualification_reason=nil)
end

#new_game(opponent_name) ⇒ Object

This method is called at the beginning of each game. A player may only be instantiated once and used to play many games. So new_game should reset any internal state acquired in previous games so that it is prepared for a new game.

The name of the opponent player is passed in. This allows for the possibility to learn opponent strategy and play the game differently based on the opponent.



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

def new_game(opponent_name)
  reset
end

#next_targetObject

Returns the coordinates of the players next target. This method will be called once per turn. The player should return target coordinates as a string in the form of:

"#{ROW}#{COL}"

eg

A1 # the square in Row A and Column 1
F5 # the square in Row F and Column 5

Since the map contains only 10 rows and 10 columns, the ROW should be A, B, C, D, E, F, G H, I, or J. And the COL should be 1, 2, 3, 4, 5, 6, 7, 8, 9, or 10

Returning coordinates outside the range or in an invalid format will result in the players disqualification.

It is illegal to target a sector more than once. Doing so will also result in disqualification.



122
123
124
# File 'lib/interferoman/interferoman.rb', line 122

def next_target
  target_for_current_shot
end

#patrolship_placementObject

Returns the placement of the patrolship. A patrolship consumes 2 squares.

See carrier_placement for details on ship placement



97
98
99
# File 'lib/interferoman/interferoman.rb', line 97

def patrolship_placement
  return random_placement(:patrolship)
end

#submarine_placementObject

Returns the placement of the submarine. A submarine consumes 3 squares.

See carrier_placement for details on ship placement



89
90
91
# File 'lib/interferoman/interferoman.rb', line 89

def submarine_placement
  return random_placement(:submarine)
end

#target_result(coordinates, was_hit, ship_sunk) ⇒ Object

target_result will be called by the system after a call to next_target. The parameters supplied inform the player of the results of the target.

coordinates : string. The coordinates targeted.  It will
              be the same value returned by the previous call
              to next_target
was_hit     : boolean.  true if the target was occupied by
              a ship.  false otherwise.
ship_sunk   : symbol.  nil if the target did not result in
              the sinking of a ship.  If the target did result in
              the sinking of a ship, the ship type is supplied
              (:carrier, :battleship, :destroyer, :submarine,
              :patrolship).

An intelligent player will use the information to better play the game. For example, if the result indicates a hit, a player my choose to target neighboring squares to hit and sink the remainder of the ship.



146
147
148
149
150
# File 'lib/interferoman/interferoman.rb', line 146

def target_result(coordinates, was_hit, ship_sunk)
  row, column = *from_coord(coordinates)

  target_result_internal(row, column, was_hit, ship_sunk)
end