class SeaBattle

Constants

VERSION

Public Class Methods

new(first_board, second_board, last_attack_move = nil) click to toggle source
# File lib/sea_battle.rb, line 7
def initialize(first_board, second_board, last_attack_move = nil)
  @last_attack_move = last_attack_move.split(";") unless last_attack_move.nil?
  @first_board = first_board
  @second_board = second_board
end

Public Instance Methods

active_user() click to toggle source
# File lib/sea_battle.rb, line 13
def active_user
  return :first_player if @last_attack_move.nil?
  player, row, column = @last_attack_move
  if player.to_sym == :first_player
    if @second_board.is_in_ship?(row.to_i, column.to_i)
      return :first_player
    else
      return :second_player
    end
  else
    if @first_board.is_in_ship?(row.to_i, column.to_i)
      return :second_player
    else
      return :first_player
    end
  end
end
is_activated?() click to toggle source
# File lib/sea_battle.rb, line 31
def is_activated?
  not first_status.eql?(:initialized) and not second_status.eql?(:initialized)
end
is_sunken_ship?(row, column, player) click to toggle source
# File lib/sea_battle.rb, line 35
def is_sunken_ship?(row, column, player)
  if player == :first_player
    @second_board.is_sunken_ship?(row, column)
  else
    @first_board.is_sunken_ship?(row, column)
  end
end
last_attack_move() click to toggle source
# File lib/sea_battle.rb, line 43
def last_attack_move
  return if @last_attack_move.nil?
  @last_attack_move.join(";")
end
move(player, type, row, column) click to toggle source
# File lib/sea_battle.rb, line 48
def move(player, type, row, column)
  return false unless winner_is.nil?

  return false unless [:first_player, :second_player].include?(player)
  return false unless [:choose, :attack, :mark].include?(type)

  case type
  when :attack
    return false unless active_user == player
    return false unless first_status.eql?(:activated) and second_status.eql?(:activated)
    if player == :first_player
      @second_board.attack(row, column)
    else
      @first_board.attack(row, column)
    end
    @last_attack_move = [player, row, column]
  else
  end
  true
end
winner_is() click to toggle source
# File lib/sea_battle.rb, line 69
def winner_is
  return :second_player if first_status.eql? :finished
  return :first_player if second_status.eql? :finished
  nil
end

Private Instance Methods

first_status() click to toggle source
# File lib/sea_battle.rb, line 77
def first_status
  @first_board.status
end
second_status() click to toggle source
# File lib/sea_battle.rb, line 81
def second_status
  @second_board.status
end