secret
Created

  • Download Gist
pc_player.rb
Ruby
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
$stdout.sync = true
 
class Battleships
 
def initialize
@@last = [1,1]
@@smallest = 2
@@shots = []
@@hits=[]
@@alph=[0,"A", "B", "C", "D","E","F","G","H","I","J"]
@@q=[]
end
def proceed
new=[]
shots=[]
g = @@smallest
new = [(@@last[0]+g),(@@last[1])]
if new[0] >10
new[1]+=1
new[0]=(new[0]%10 - (g+1)%2)
if new[1] >10
new= [1,1]
end
end
if new[0]<=0
new[0]+=g
end
@@last = new
return new
end
 
def target
i=@@hits.map{ |x|
x=x.to_s.scan(/(.)/)
x=[@@alph.index(x[0].to_s),x[1].to_s.to_i]
}
q=[]
i.each{|x|
q<<[x[0]+1,x[1]]<<[x[0],x[1]+1]<<[x[0]-1,x[1]]<<[x[0],x[1]-1]
}
q.delete_if{|x|
x[0]<=0 or x[1]<=0 or x[0]>=11 or x[1]>=11 or @@shots.include?(x)
}
@@q=q
end
def shoot(n)
target
turn=[]
for i in 1..n
turn<<@@q.shift
end
turn.compact!
n-=turn.length
for i in 1..n
turn<<proceed
end
turn.each{|x| @@shots<<x}
return shot_message(turn)
end
 
def shot_message(turn)
text="SHOTS"
turn.each{|x|
text<<" "+@@alph[x[0]].to_s+x[1].to_s
}
return text
end
def placement (ships)
@@ships = Array.new
@@message = "SHIPS"
ships.each_index { |x|
s = ships[x]
@@ships[x]=Array.new
occupancy = false
while occupancy == false
construct(x,s)
occupancy = check
end
}
print
return @@message
end
def check
check = true
buffer =
@@ships[-1].each { |y|
@@ships[0 .. -2].each { |w|
w[2 .. -1].each { |z|
if y == z
check = false
end
}
}
}
return check
end
 
def construct(x,s)
o = rand(2)
v = rand(9 - (s*o))
h = rand(9 + (s*(o-1)))
@@ships[x] = Array.new
if o == 1
@@ships[x][1] = "V"
else
@@ships[x][1] = "H"
end
@@ships[x][0] = s
for i in 2 .. s + 1
@@ships[x][i] = [h - (i*(o-1)),v + (i*o)]
end
end
def read(line)
player=line.scan(/SHOTS (\S*)/).to_s
if player=="pc_player"
@@hits=line.scan(/(..):hit/)
end
end
def print
alph = ["A", "B", "C", "D","E","F","G","H","I","J"]
@@ships.each { |x|
x[2][0] = alph[x[2][0]]
}
@@ships.each { |x|
@@message << " " + x[0].to_s + ":" + x[2][0].to_s + (x[2][1]+1).to_s + ":" + x[1]}
end
 
end
 
game = Battleships.new
ARGF.each_line do |line|
case line
when /\AACTION SHIPS\b/
ships = line.scan(/SHIPS (.*)/ ).to_s.split(',').map! { |x| x = x.to_i}
puts game.placement(ships)
when /\AACTION SHOTS (\d)/
puts game.shoot($1.to_i)
when /\AINFO SHOTS\b/
game.read(line)
when /\AACTION FINISH\b/
puts "FINISH"
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.