forked from threedaymonk/battleship
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request threedaymonk#6 from MrJaba/master
Add bots from Iprug.
- Loading branch information
Showing
8 changed files
with
1,027 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
class AndersPlayer | ||
def initialize | ||
@aim = Aim.new() | ||
end | ||
|
||
def name | ||
"Anders Player" | ||
end | ||
|
||
def new_game | ||
randy = rand(2) | ||
if(randy == 1) | ||
layout = [ | ||
[rand(5), rand(2), 5, :across], | ||
[rand(6), rand(2) + 2, 4, :across], | ||
[rand(7), rand(2) + 4, 3, :across], | ||
[rand(7), rand(2) + 6, 3, :across], | ||
[rand(8), rand(2) + 8, 2, :across] | ||
] | ||
else | ||
layout = [ | ||
[rand(2), rand(5), 5, :down], | ||
[rand(2)+2, rand(6), 4, :down], | ||
[rand(2)+4, rand(7), 3, :down], | ||
[rand(2)+6, rand(7), 3, :down], | ||
[rand(2)+8, rand(8), 2, :down] | ||
] | ||
end | ||
end | ||
|
||
def take_turn(state, ships_remaining) | ||
@aim.test(state) | ||
end | ||
end | ||
|
||
class Aim | ||
attr_accessor :x, :y | ||
|
||
def initialize | ||
@x = -1 | ||
@x_speed = 1 | ||
@y = 0 | ||
@y_speed = 0 | ||
@right_border = 9 | ||
@left_border = 0 | ||
@top_border = 0 | ||
@bottom_border = 9 | ||
|
||
end | ||
|
||
def test(state) | ||
if @x >= @right_border && @x_speed == 1 | ||
@x = @right_border | ||
@x_speed = 0 | ||
@y_speed = 1 | ||
@top_border += 1 | ||
end | ||
if @y >= @bottom_border && @y_speed == 1 | ||
@y = @bottom_border | ||
@x_speed = -1 | ||
@y_speed = 0 | ||
@right_border -= 1 | ||
end | ||
if @x <= @left_border && @x_speed == -1 | ||
@x = @left_border | ||
@x_speed = 0 | ||
@y_speed = -1 | ||
@bottom_border -= 1 | ||
end | ||
if @y <= @top_border && @y_speed == -1 | ||
@y = @top_border | ||
@x_speed = 1 | ||
@y_speed = 0 | ||
@left_border += 1 | ||
end | ||
|
||
@x += @x_speed | ||
@y += @y_speed | ||
[@x,@y] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
class DeathBonesDanPlayer | ||
|
||
def initialize | ||
@taken_positions = [] | ||
@positions = [] | ||
|
||
@shots = [] | ||
end | ||
|
||
def log(message) | ||
File.open('debug.log', 'a') { |f| f.write("#{Time.now} - #{message}\n") } | ||
end | ||
|
||
def init_taken | ||
(0..9).each do |x| | ||
@taken_positions[x] = [] | ||
(0..9).each do |y| | ||
@taken_positions[x][y] = 0 | ||
end | ||
end | ||
end | ||
|
||
def print_grid | ||
|
||
end | ||
|
||
def reserve_position (x,y,length,orientation) | ||
|
||
if orientation == :across | ||
(x..length).each do |p| | ||
@taken_positions[p][y] = 1 | ||
end | ||
else | ||
(y..length).each do |p| | ||
@taken_positions[x][p] = 1 | ||
end | ||
end | ||
|
||
end | ||
|
||
def has_space (x,y,length,orientation) | ||
|
||
if orientation == :across | ||
(x..length).each do |p| | ||
return false if @taken_positions[p][y] == 1 | ||
end | ||
else | ||
(y..length).each do |p| | ||
return false if @taken_positions[x][p] = 1 | ||
end | ||
end | ||
|
||
true | ||
|
||
end | ||
|
||
def generate_player_positions | ||
|
||
# initialise taken grid | ||
init_taken | ||
|
||
ship_lengths = [5,4,3,3,2] | ||
|
||
ship_lengths.each do |z| | ||
|
||
upperbound = (10 - z) | ||
|
||
# upperbound = (10 - z) | ||
|
||
x = rand(upperbound) | ||
y = rand(upperbound) | ||
|
||
orientation = :across | ||
|
||
placed = false | ||
|
||
while !placed | ||
|
||
orientation = orientation == :across ? :down : :across | ||
|
||
x = rand(upperbound) | ||
y = rand(upperbound) | ||
|
||
# check ship position | ||
if has_space(x,y,z,orientation) | ||
|
||
@positions << [x,y,z,orientation] | ||
reserve_position x,y,z,orientation | ||
|
||
placed = true | ||
|
||
end | ||
|
||
end | ||
|
||
end | ||
|
||
log @positions.inspect | ||
|
||
@positions | ||
|
||
end | ||
|
||
def generate_static_positions | ||
|
||
[ | ||
[0, 0, 5, :across], | ||
[0, 1, 4, :across], | ||
[0, 2, 3, :across], | ||
[0, 3, 3, :across], | ||
[0, 4, 2, :across] | ||
] | ||
|
||
end | ||
|
||
# battleship methods | ||
|
||
def name | ||
"Death Bones Dan" | ||
end | ||
|
||
def new_game | ||
generate_player_positions | ||
#generate_static_positions | ||
end | ||
|
||
def take_turn(state, ships_remaining) | ||
|
||
last_was_hit = false | ||
last_shot = [] | ||
|
||
# check on last shot | ||
if @shots.length > 0 | ||
last_shot = @shots.last | ||
log "last: #{last_shot.inspect}" | ||
last_was_hit = state[last_shot[1]][last_shot[0]] == :hit | ||
end | ||
|
||
if !last_was_hit | ||
|
||
shot_x = rand(10) | ||
shot_y = rand(10) | ||
|
||
until state[shot_y][shot_x] == :unknown | ||
shot_x = rand(10) | ||
shot_y = rand(10) | ||
end | ||
|
||
else | ||
|
||
shot_x = rand(10) | ||
shot_y = rand(10) | ||
|
||
until state[shot_y][shot_x] == :unknown | ||
shot_x = rand(10) | ||
shot_y = rand(10) | ||
end | ||
|
||
# last_x_u = last_shot[0] + 2 | ||
# last_y_u = last_shot[1] + 2 | ||
# | ||
# last_x_l = last_shot[0] - 2 | ||
# last_y_l = last_shot[1] - 2 | ||
# | ||
# shot_x = last_x_l + rand(last_x_u + 1 - last_x_l) | ||
# shot_y = last_x_l + rand(last_y_u + 1 - last_y_l) | ||
# | ||
# until state[shot_y][shot_x] == :unknown | ||
# shot_x = last_x_l + rand(last_x_u + 1 - last_x_l) | ||
# shot_y = last_x_l + rand(last_y_u + 1 - last_y_l) | ||
# end | ||
|
||
end | ||
|
||
log "Shooting at #{shot_x}, #{shot_y}" | ||
|
||
@shots << [shot_x, shot_y] | ||
|
||
[shot_x, shot_y] | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
class GrahamPlayer | ||
def initialize | ||
@x=0 | ||
@y=0 | ||
@advance = 3 | ||
@last_turn = [] | ||
@on_a_roll = false | ||
@direction_forward = true | ||
end | ||
|
||
def name | ||
"Graham Hadgraft" | ||
end | ||
|
||
def new_game | ||
[ | ||
[rand(5), 0, 5, :across], | ||
[rand(6), 4, 4, :across], | ||
[rand(7), 5, 3, :across], | ||
[rand(7), 6, 3, :across], | ||
[rand(8), 8, 2, :across] | ||
] | ||
end | ||
|
||
def take_turn(state, ships_remaining) | ||
if @last_turn[0] != nil && last_turn(state) == :hit | ||
@x += 1 | ||
@on_a_roll = true | ||
elsif @last_turn[0] != nil && @on_a_roll == true && last_turn(state) == :miss | ||
@x -= 2 | ||
@on_a_roll = false | ||
|
||
else | ||
@x += @advance | ||
end | ||
|
||
check_valid_coords(@x, @y) | ||
check_if_shot(@x,@y, state) | ||
|
||
@last_turn = [@x, @y] | ||
|
||
end | ||
|
||
private | ||
def last_turn(state) | ||
state[@last_turn[1]][@last_turn[0]] | ||
end | ||
|
||
def check_valid_coords(x,y) | ||
if @x > 9 | ||
@x -= 10 | ||
@y += 1 | ||
end | ||
|
||
if @y > 9 | ||
@y=0 | ||
end | ||
end | ||
|
||
def check_if_shot (x, y, state) | ||
while state[y][x] == :miss || state[y][x] == :hit | ||
if @direction_forward == true | ||
x += 1 | ||
else | ||
x -= 1 | ||
end | ||
|
||
if x > 9 | ||
x -= 10 | ||
y += 1 | ||
end | ||
|
||
if y > 9 | ||
y=0 | ||
end | ||
@x = x | ||
@y = y | ||
end | ||
end | ||
end |
Oops, something went wrong.