Created
October 5, 2011 16:03
-
-
Save jeronimo/1264822 to your computer and use it in GitHub Desktop.
Battleships - engine (and example how NOT to write code)
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
class Monkey | |
def initialize | |
@moves = [] | |
@queue = [] | |
@target = [] | |
@last_hit_move = [] | |
@l_last_hit_move = [] | |
@direction = 'h' #horizontal | |
@ships_remaining = [5,4,3,3,2] | |
make_queue | |
end | |
def name | |
"Monkey" | |
end | |
def make_queue | |
10.times do |i| | |
@queue << [i, i] | |
@queue << [i, (i-9).abs] | |
end | |
p1=4 | |
p2=5 | |
5.times do |i| | |
@queue << [i, p1] | |
@queue << [i+5, p1+5] | |
p1 -= 1 | |
@queue << [p2, i] | |
@queue << [i, p2] | |
p2 += 1 | |
end | |
@queue.uniq!.shuffle! | |
@queue_end = [] | |
[2,7].each do |x| | |
[0,4,5,9].each do |y| | |
@queue_end << [x, y] | |
@queue_end << [y, x] | |
end | |
end | |
@queue += @queue_end | |
@queue_end = [] | |
[1,3,6,8].each do |x| | |
@queue_end << [x, 4] | |
@queue_end << [4, x] | |
end | |
@queue_end | |
@queue += @queue_end | |
@queue.uniq! | |
end | |
def include_new_ship? all_ships, ship | |
ship.each do |s| | |
return true if all_ships.include? s | |
end | |
false | |
end | |
def new_game | |
# @ships = [] | |
# @all_cords = [] | |
# ships_coords = [] | |
# directions = [:across, :down] | |
# [5,4,3,3,2].each do |length| | |
# ship_coords = [] | |
# begin | |
# ship_coords = [] | |
# x, y = rand(10), rand(10) | |
# direction = rand(2) # 0 - :across, 1 - :down | |
# #direction = 1 | |
# if direction == 0 | |
# 0.upto(length-1).each do |px| | |
# ship_coords << [x+px,y] | |
# end | |
# ship_coords.reject! do |coord| | |
# coord[0] < 0 or coord[1] < 0 or coord[0] > 9 or coord[1] > 9 | |
# end | |
# if ship_coords.size < length | |
# (x-(length-ship_coords.size)).upto(x-1).each do |px| | |
# ship_coords << [px,y] | |
# end | |
# end | |
# else | |
# 0.upto(length-1).each do |py| | |
# ship_coords << [x,y+py] | |
# end | |
# ship_coords.reject! do |coord| | |
# coord[0] < 0 or coord[1] < 0 or coord[0] > 9 or coord[1] > 9 | |
# end | |
# if ship_coords.size < length | |
# (y-(length-ship_coords.size)).upto(y-1).each do |py| | |
# ship_coords << [x,py] | |
# end | |
# end | |
# end | |
# ship_coords.sort! | |
# end while include_new_ship? @all_cords, ship_coords | |
# @all_cords += ship_coords | |
# @all_cords << [ship_coords.first[0]+1, ship_coords.first[1]] | |
# @all_cords << [ship_coords.first[0]-1, ship_coords.first[1]] | |
# @all_cords << [ship_coords.first[0], ship_coords.first[1]+1] | |
# @all_cords << [ship_coords.first[0], ship_coords.first[1]-1] | |
# @all_cords << [ship_coords.last[0]+1, ship_coords.last[1]] | |
# @all_cords << [ship_coords.last[0]-1, ship_coords.last[1]] | |
# @all_cords << [ship_coords.last[0], ship_coords.last[1]+1] | |
# @all_cords << [ship_coords.last[0], ship_coords.last[1]-1] | |
# ships_coords << (ship_coords << directions[direction]) | |
# end | |
# result = [] | |
# ships_coords.each do |ship| | |
# result << [ship.first[0], ship.first[1], ship.size-1, ship.last] | |
# end | |
# result | |
#FAVOURITE | |
[ | |
[4, 4, 5, :down], | |
[0, 2, 4, :across], | |
[7, 4, 3, :across], | |
[6, 8, 3, :across], | |
[1, 5, 2, :down] | |
] | |
end | |
def take_turn(state, ships_remaining) | |
@state = state | |
if last_move_hit? | |
t = [] | |
t = target(*@last_hit_move) | |
t = target(*@l_last_hit_move) if t.size < 1 && @l_last_hit_move.size > 0 | |
@target += t | |
end | |
if @ships_remaining.size != ships_remaining.size | |
@target = [] | |
@last_hit_move = [] | |
@l_last_hit_move = [] | |
@direction = 'h' | |
end | |
@ships_remaining = ships_remaining | |
move = get_move | |
@moves << move | |
move | |
end | |
def last_move_hit? | |
return false if @moves.empty? | |
if state_for(*@moves.last) == :hit | |
@l_last_hit_move = @last_hit_move if @last_hit_move.size > 0 | |
@last_hit_move = @moves.last | |
return true | |
elsif @last_hit_move.size > 0 | |
@direction = 'v' if @target.size < 1 | |
@target += target(*@last_hit_move) | |
end | |
false | |
end | |
def state_for x, y | |
@state[y][x] | |
end | |
def get_move | |
if @target.size > 0 | |
begin | |
move = @target.shift | |
end while @moves.include?(move) && @target.size > 0 | |
elsif @queue.size > 0 | |
begin | |
move = @queue.shift | |
end while @moves.include?(move) && @queue.size > 0 | |
else | |
begin | |
move = [rand(10), rand(10)] | |
end while @moves.include?(move) | |
end | |
move = move.nil? ? get_move : move | |
end | |
def target x, y | |
result = [] | |
#horizontal | |
if @direction == 'h' | |
result << [x-1, y] | |
result << [x+1, y] | |
else | |
#vertical | |
result << [x, y-1] | |
result << [x, y+1] | |
end | |
r = result.delete_if do |coord| | |
coord[0] < 0 or coord[1] < 0 or coord[0] > 9 or coord[1] > 9 or @moves.include?(coord) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment