Permalink
Browse files

Add players from Magrails dojo afternoon.

  • Loading branch information...
1 parent b238c26 commit f6d3b2f2fb50a8b49d40f198dc3b796c107acf61 @threedaymonk committed Oct 15, 2011
@@ -0,0 +1,105 @@
+# encoding: utf-8
+class HungryWolfPlayer
+ BOAT_SIZES = [5, 4, 3, 3, 2]
+
+ def initialize
+ @shots = []
+ end
+
+ def name
+ 'Hungry ♥ Wolf'
+ end
+
+ # [ x, y, length, orientation ]
+ # 5, 4, 3, 3, 2
+ def new_game
+ @shots = []
+ boats = []
+
+ BOAT_SIZES.each do |size|
+ boats << place_boat(size, boats)
+ end
+ boats
+ end
+
+ def take_turn board, ships_remaining
+ last_shot = @shots.last
+
+ possibles = []
+
+ if last_shot && hit?(board, *last_shot)
+ possibles = surrounding(*last_shot).reject do |shot|
+ @shots.include?(shot)
+ end
+ end
+
+ loop do
+ shot = possibles.first || [rand(10), rand(10)]
+ unless @shots.include?(shot)
+ @shots << shot
+ return shot
+ end
+ end
+ end
+
+ def hit?(board, x, y)
+ begin
+ board[y][x] == :hit
+ rescue
+ false
+ end
+ end
+
+ def surrounding(x, y)
+ possibles = [-1, 0, 1].map { |delta_x|
+ [-1, 0, 1].map { |delta_y|
+ [x + delta_x, y + delta_y]
+ }
+ }.flatten(1) - [x, y]
+ possibles.find_all { |x,y| x > 0 && y > 0 }
+ end
+
+ ###
+ # size [5, 4, 3, 3, 2]
+ # [[x, y, length, orientation]]
+ def place_boat(size, boats)
+ loop do
+ guess = suggest_position(size)
+ if on_map?(guess) && boats.none? { |b| overlapping?(b, guess) }
+ return guess
+ end
+ end
+ end
+
+ def suggest_position(size)
+ guess_x = rand(10)
+ guess_y = rand(10)
+ guess_orination = [:down, :across].shuffle.first
+ [guess_x, guess_y, size, guess_orination]
+ end
+
+ # left: [x, y, length, orientation]
+ # right: [x, y, length, orientation]
+ def overlapping?(left, right)
+ (tuples(left) & tuples(right)).any?
+ end
+
+ def tuples boat
+ x, y, length, orientation = *boat
+ if orientation == :across
+ length.times.map { |i| [x + i, y] }
+ else
+ length.times.map { |i| [x, y + i] }
+ end
+ end
+
+ # boat: [x, y, length, orientation]
+ def on_map?(boat)
+ x, y, length, orientation = *boat
+ if orientation == :down
+ y + length <= 10
+ else
+ x + length <= 10
+ end
+ end
+end
@@ -0,0 +1,131 @@
+class JLozinskiPlayer
+ def name
+ "Jonathan Lozinski"
+ end
+
+ def new_game
+ @my_board = init_board
+ @my_ships = []
+ @last_shot = nil
+ @priority_targets = []
+ init_ships_to_place
+ place_my_ships
+ @my_ships
+ end
+
+ def take_turn(state, ships_remaining)
+ if @last_shot && state[@last_shot[0], @last_shot[1]] == :hit
+ @priority_targets += find_possible_around(@last_shot, state)
+ @priority_targets.uniq!
+ else
+ poss = find_possibles(state)
+ end
+
+ unless @priority_targets.empty?
+ @last_shot = @priority_targets.sample(1).first || poss.sample(1).first
+ else
+ @last_shot = poss.sample(1).first
+ end
+ @last_shot
+ end
+
+ private
+
+ @ships_to_place = []
+
+ @my_board = []
+
+ def init_ships_to_place
+ @ships_to_place = [5,4,3,3,2].reverse!
+ end
+
+ def find_possibles(state)
+ poss = []
+ (0..9).each do |x|
+ (0..9).each do |y|
+ poss << [x,y] if state[y][x] == :unknown
+ end
+ end
+ poss
+ end
+
+ def find_possible_around(pos, state)
+ lx = pos[0]-1
+ ux = pos[0]+1
+ ly = pos[1]-1
+ uy = pos[1]+1
+ lx = [0,lx].min
+ ux = [9,ux].max
+ ly = [0,ly].min
+ uy = [9,uy].max
+ poss = []
+ (lx..ux).each do |x|
+ (ly..uy).each do |y|
+ poss << [x,y] if state[y][x] == :unknown
+ end
+ end
+ poss
+ end
+
+ def place_my_ships
+ ship = @ships_to_place.pop
+ r = Random.new
+ until ship.nil?
+ placed = false
+ until placed do
+ x = r.rand(10)
+ y = r.rand(10)
+ orient = [:across, :down].sample(1).first
+ placed = can_place?(ship, x, y, orient)
+ place_ship(ship, x, y, orient) if placed
+ end
+ ship = @ships_to_place.pop
+ end
+ end
+
+ def init_board
+ Array.new(10) do
+ Array.new(10, ' ')
+ end
+ end
+
+ def can_place?(ship, x, y, orientation)
+ cx = x
+ cy = y
+ case orientation
+ when :across
+ ship.times do
+ return false if @my_board[cx][cy] != " "
+ cx += 1
+ return false if cx >= 10
+ end
+ return true
+ when :down
+ ship.times do
+ return false if @my_board[cx][cy] != " "
+ cy += 1
+ return false if cy >= 10
+ end
+ return true
+ end
+ end
+
+ def place_ship(ship, x, y, orientation)
+ @my_ships << [x,y,ship,orientation]
+ cx = x
+ cy = y
+ case orientation
+ when :across
+ ship.times do
+ @my_board[cx][cy] = "S"
+ cx += 1
+ end
+ when :down
+ ship.times do
+ @my_board[cx][cy] = "S"
+ cy += 1
+ end
+ end
+
+ end
+end
@@ -0,0 +1,14 @@
+require "rubygems"
+require "test/unit"
+
+require './jlozinski.rb'
+
+class TestJLozinskiPlayer < Test::Unit::TestCase
+ def setup
+ @player = JLozinskiPlayer.new
+ end
+
+ def test_board
+ p @player.new_game
+ end
+end
@@ -0,0 +1,102 @@
+class Player
+ attr_accessor :my_board
+ attr_accessor :my_ships
+ attr_accessor :opp_board
+ attr_accessor :opp_ships
+
+ def initalize
+ @my_board = [[:unknown] * 10] * 10
+ @my_ships = [5, 4, 3, 3, 2]
+ @prob_grid = [[0] * 10] * 10
+ end
+
+ def name
+ "@p7r the Pirate arrrrr!"
+ end
+
+ def new_game
+ positions = [
+ [
+ [5, 5, 5, :across],
+ [5, 7, 4, :across],
+ [1, 1, 3, :down],
+ [3, 1, 3, :down],
+ [5, 1, 2, :down]
+ ],
+ [
+ [5, 1, 5, :across],
+ [5, 3, 4, :across],
+ [0, 6, 3, :down],
+ [2, 6, 3, :down],
+ [4, 6, 2, :down]
+ ],
+ [
+ [5, 1, 5, :across],
+ [5, 3, 4, :across],
+ [1, 5, 3, :across],
+ [1, 6, 3, :across],
+ [1, 8, 2, :across]
+ ],
+ [
+ [1, 1, 5, :across],
+ [1, 3, 4, :across],
+ [5, 5, 3, :across],
+ [5, 6, 3, :across],
+ [5, 8, 2, :across]
+ ],
+ [
+ [1, 1, 5, :down],
+ [3, 1, 4, :down],
+ [4, 6, 3, :down],
+ [6, 6, 3, :down],
+ [8, 6, 2, :down]
+ ],
+ [
+ [6, 1, 5, :down],
+ [8, 1, 4, :down],
+ [0, 6, 3, :down],
+ [2, 6, 3, :down],
+ [4, 6, 2, :down]
+ ]
+ ]
+ return positions[rand(positions.size)]
+ end
+
+ def take_turn(state, ships)
+ @opp_board = state
+ @opp_ships = ships
+ @test_x, @test_y = nil, nil
+ unless empty_board?
+ for x in 0..9
+ for y in 0..9
+ @test_x, @test_y = x, y if check_if_hit(x, y)
+ end
+ end
+ end
+ while check_if_hit(@test_x, @test_y) || check_if_miss(@test_x, @test_y) || @test_x.nil? || @test_y.nil?
+ x, y = rand(10), rand(10)
+ check_if_hit(x, y) ? (@test_x, @test_y = rand(3)-1, rand(3)-1) : (@test_x, @test_y = rand(10), rand(10))
+ end
+ return [@test_x, @test_y]
+ end
+
+ def check_if_hit(x, y)
+ return true if x.nil? || y.nil? || x < 0 || y < 0
+ @opp_board[y][x] == :hit
+ end
+
+ def check_if_miss(x, y)
+ return true if x.nil? || y.nil? || x < 0 || y < 0
+ @opp_board[y][x] == :miss
+ end
+
+ def empty_board?
+ for x in 0..9
+ for y in 0..9
+ return false if check_if_hit(x, y) || check_if_miss(x, y)
+ end
+ end
+ return true
+ end
+
+end
Oops, something went wrong.

0 comments on commit f6d3b2f

Please sign in to comment.