Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 9136935c77
Fetching contributors…

Cannot retrieve contributors at this time

file 60 lines (48 sloc) 1.214 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
$:.unshift File.expand_path("../../lib", __FILE__)
$:.unshift File.expand_path("../../players/lib", __FILE__)
$:.unshift File.expand_path("../../data", __FILE__)

require "battleship/board"
require "battleship/util"
require "sample_boards"
require "parallel"

SIZE = 10
FLEET = [5, 4, 3, 3, 2]

module Stats
  def sum(&blk)
    map(&blk).inject { |sum, element| sum + element }
  end

  def mean
    (sum.to_f / length.to_f)
  end

  def median
    sort[length / 2]
  end

  def variance
    m = mean
    sum { |i| ( i - m )**2 } / length
  end

  def std_dev
    Math.sqrt(variance)
  end
end

path = ARGV[0]
$:.unshift File.join(File.dirname(path), "lib")
load path

player_class = Battleship::Util.find_player_classes.first

results = Parallel.map(Battleship::SAMPLE_BOARDS) { |positions|
  player = player_class.new
  player.new_game
  board = Battleship::Board.new(SIZE, FLEET, positions)
  shots = 0
  until board.sunk?
    board.try(player.take_turn(board.report, board.ships_remaining).dup)
    shots += 1
  end
  shots
}

results.extend(Stats)

puts "mean: %.1f" % results.mean
puts "median: %d" % results.median
puts "min: %d" % results.min
puts "max: %d" % results.max
puts "sd: %.1f" % results.std_dev
Something went wrong with that request. Please try again.