Class: Alpha::Alpha

Inherits:
Object
  • Object
show all
Defined in:
lib/alpha/alpha.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

#initializeAlpha

:nodoc:



173
174
175
# File 'lib/alpha/alpha.rb', line 173

def initialize #:nodoc:
  reset
end

Instance Attribute Details

#battle_queueObject (readonly)

Returns the value of attribute battle_queue.



171
172
173
# File 'lib/alpha/alpha.rb', line 171

def battle_queue
  @battle_queue
end

#disqualification_reasonObject (readonly)

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



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

def disqualification_reason
  @disqualification_reason
end

#enemy_targeted_sectorsObject (readonly)

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



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

def enemy_targeted_sectors
  @enemy_targeted_sectors
end

#opponentObject (readonly)

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



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

def opponent
  @opponent
end

#opponents_gridObject (readonly)

Returns the value of attribute opponents_grid.



171
172
173
# File 'lib/alpha/alpha.rb', line 171

def opponents_grid
  @opponents_grid
end

#red_zoneObject (readonly)

Returns the value of attribute red_zone.



171
172
173
# File 'lib/alpha/alpha.rb', line 171

def red_zone
  @red_zone
end

#resultObject (readonly)

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



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

def result
  @result
end

#targetsObject (readonly)

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



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

def targets
  @targets
end

Instance Method Details

#battleship_placementObject

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



79
80
81
# File 'lib/alpha/alpha.rb', line 79

def battleship_placement
  return place(4)
end

#carrier_placementObject



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

def carrier_placement
  return place(5)
end

#destroyer_placementObject

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



84
85
86
# File 'lib/alpha/alpha.rb', line 84

def destroyer_placement
  return place(3)
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.



151
152
# File 'lib/alpha/alpha.rb', line 151

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


165
166
# File 'lib/alpha/alpha.rb', line 165

def game_over(result, disqualification_reason=nil)
end

#get_placement(block_number) ⇒ Object

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
59
60
# File 'lib/alpha/alpha.rb', line 55

def get_placement(block_number)
  placement = "#{@randomizer.rand_column}#{@randomizer.rand_row} #{@randomizer.rand_direction}"
  square, direction = parse_placement(placement)
  return get_placement(block_number) unless @my_grid.can_place_block?(square, direction, block_number)
  return placement
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
# File 'lib/alpha/alpha.rb', line 28

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 illegal to target a sector more than once. Doing so will also result in disqualification.



115
116
117
118
119
# File 'lib/alpha/alpha.rb', line 115

def next_target
  target = target_for_current_shot
  @shots_taken += 1
  return target
end

#parse_placement(placement) ⇒ Object



62
63
64
65
# File 'lib/alpha/alpha.rb', line 62

def parse_placement(placement)
  parts = placement.split(" ")
  return  parts[0], parts[1]
end

#patrolship_placementObject

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



94
95
96
# File 'lib/alpha/alpha.rb', line 94

def patrolship_placement
  return place(2)
end

#place(occupied_squares) ⇒ Object



67
68
69
70
71
72
# File 'lib/alpha/alpha.rb', line 67

def place(occupied_squares)
  placement = get_placement(occupied_squares)
  square, direction = parse_placement(placement)
  @my_grid.place_block(square, direction, occupied_squares) 
  return placement
end

#submarine_placementObject

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



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

def submarine_placement
  return place(3)
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.



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/alpha/alpha.rb', line 132

def target_result(coordinates, was_hit, ship_sunk)
  result = nil
  if was_hit
    result = :hit
    @red_zone = true unless ship_sunk
  else
    result = :miss
  end
  @red_zone = false if ship_sunk
  
  @opponents_grid.place(coordinates, result)
end