Class: UssMonteCarlo::UssMonteCarlo

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUssMonteCarlo

:nodoc:



178
179
180
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 178

def initialize #:nodoc:
  reset
end

Instance Attribute Details

#disqualification_reasonObject (readonly)

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



176
177
178
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 176

def disqualification_reason
  @disqualification_reason
end

#enemy_targeted_sectorsObject (readonly)

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



176
177
178
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 176

def enemy_targeted_sectors
  @enemy_targeted_sectors
end

#opponentObject (readonly)

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



176
177
178
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 176

def opponent
  @opponent
end

#resultObject (readonly)

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



176
177
178
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 176

def result
  @result
end

#targets_remainingObject (readonly)

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



176
177
178
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 176

def targets_remaining
  @targets_remaining
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



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

def battleship_placement
  @ship_placements ||= place_ships
  @ship_placements[: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 accomidate 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 bothe ships are trying to occupy the square A1.

Invalid ship placements will result in disqualification of the player.



55
56
57
58
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 55

def carrier_placement
  @ship_placements ||= place_ships
  @ship_placements[:carrier]
end

#destroyer_placementObject

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

See carrier_placement for details on ship placement



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

def destroyer_placement
  @ship_placements ||= place_ships
  @ship_placements[:destroyer]
end

#enemy_targeting(coordinates) ⇒ Object

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

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



155
156
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 155

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


169
170
171
172
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 169

def game_over(result, disqualification_reason=nil)
  @result = result
  @disqualification_reason = disqualification_reason
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.



28
29
30
31
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 28

def new_game(opponent_name)
  reset
  @opponent = opponent_name
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.



113
114
115
116
117
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 113

def next_target
  target = @current_gunner.next_shot
  @shots_fired << target
  return target
end

#patrolship_placementObject

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

See carrier_placement for details on ship placement



91
92
93
94
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 91

def patrolship_placement
  @ship_placements ||= place_ships
  @ship_placements[:patrolship]
end

#submarine_placementObject

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

See carrier_placement for details on ship placement



82
83
84
85
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 82

def submarine_placement
  @ship_placements ||= place_ships
  @ship_placements[: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 paramters 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
  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.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/uss_monte_carlo/uss_monte_carlo.rb', line 130

def target_result(coordinates, was_hit, ship_sunk)
  @current_gunner.shot_result(coordinates, was_hit, ship_sunk)
  if ship_sunk
    @targets_remaining -= [ship_sunk]
    @unaccounted_hits += @current_gunner.unaccounted_hits
    
    # Protect against an infinite loop
    @unaccounted_hits.uniq!
    
    if @unaccounted_hits.empty?
      @current_gunner = RandomGunner.new(@targets_remaining, @shots_fired)
    else
      @current_gunner = HomingGunner.new(@targets_remaining, @unaccounted_hits.shift, @shots_fired)
    end
  elsif was_hit && !@current_gunner.kind_of?(HomingGunner)
    @current_gunner = HomingGunner.new(@targets_remaining, coordinates, @shots_fired)
  end
end