Skip to content

Instantly share code, notes, and snippets.

@labe
Created April 23, 2013 03:00
Show Gist options
  • Save labe/5440495 to your computer and use it in GitHub Desktop.
Save labe/5440495 to your computer and use it in GitHub Desktop.
Battleship!!! v1.0
#7 ships are only placed horizontally
#7 ships are placed at random by the computer on both boards
#future features:
# -ability to manually place ships
# -ability to place (manually and automatically) ships vertically
# -ability to manually choose coordinates for salvos
# -ability to dictate players (human vs human, human vs computer, computer vs computer)
class Battleship
def initialize(name)
@name = name
@attacks = 0
end
def define_board
@layout =[["/","/","/","/","/","/","/","/","/","/"],["/","/","/","/","/","/","/","/","/","/"],["/","/","/","/","/","/","/","/","/","/"],["/","/","/","/","/","/","/","/","/","/"],["/","/","/","/","/","/","/","/","/","/"],["/","/","/","/","/","/","/","/","/","/"],["/","/","/","/","/","/","/","/","/","/"],["/","/","/","/","/","/","/","/","/","/"],["/","/","/","/","/","/","/","/","/","/"],["/","/","/","/","/","/","/","/","/","/"]]
end
def show_board
@layout.each do |x|
print x
puts
end
end
def choose_coords
@x_coord = rand(10)
@y_coord = rand(10)
end
def place_submarine
choose_coords
if @layout[@x_coord][@y_coord] == "/"
@layout[@x_coord][@y_coord] = "s"
else
place_submarine
end
end
def place_destroyer
choose_coords
if @y_coord != 9 && @layout[@x_coord][@y_coord] == "/" && @layout[@x_coord][@y_coord+1] == "/"
@layout[@x_coord][@y_coord] = "d"
@layout[@x_coord][@y_coord+1] = "d"
else
place_destroyer
end
end
def place_cruiser
choose_coords
if @y_coord != 8 && @layout[@x_coord][@y_coord] == "/" && @layout[@x_coord][@y_coord+1] == "/" && @layout[@x_coord][@y_coord+2] == "/"
@layout[@x_coord][@y_coord] = "c"
@layout[@x_coord][@y_coord+1] = "c"
@layout[@x_coord][@y_coord+2] = "c"
else
place_cruiser
end
end
def place_battleship
choose_coords
if @y_coord != 7 && @layout[@x_coord][@y_coord] == "/" && @layout[@x_coord][@y_coord+1] == "/" && @layout[@x_coord][@y_coord+2] == "/" && @layout[@x_coord][@y_coord+3] == "/"
@layout[@x_coord][@y_coord] = "b"
@layout[@x_coord][@y_coord+1] = "b"
@layout[@x_coord][@y_coord+2] = "b"
@layout[@x_coord][@y_coord+3] = "b"
else
place_battleship
end
end
def place_aircraft
choose_coords
if @y_coord != 6 && @layout[@x_coord][@y_coord] == "/" && @layout[@x_coord][@y_coord+1] == "/" && @layout[@x_coord][@y_coord+2] == "/" && @layout[@x_coord][@y_coord+3] == "/" && @layout[@x_coord][@y_coord+4] == "/"
@layout[@x_coord][@y_coord] = "a"
@layout[@x_coord][@y_coord+1] = "a"
@layout[@x_coord][@y_coord+2] = "a"
@layout[@x_coord][@y_coord+3] = "a"
@layout[@x_coord][@y_coord+4] = "a"
else
place_aircraft
end
end
def place_all_ships
place_aircraft
place_battleship
place_cruiser
place_destroyer
place_destroyer
place_submarine
place_submarine
end
def count_ships
@ships = []
for x in @layout
for y in x
if y != "/" && @ships.include?(y) != true && y != "x"
@ships << y
end
end
end
return @ships.count
end
def under_attack
puts "#{@name} is under attack!"
@ships.count.times {
choose_coords
if @layout[@x_coord][@y_coord] == "/"
puts "Miss!"
else
@layout[@x_coord][@y_coord] = "x"
puts "Battleship hit!"
end
@attacks += 1
}
count_ships
puts "There are #{@ships.count} ships on #{@name}'s board."
puts @name + "\'s board:"
show_board
end
def show_attack_count
@attacks
end
end
Home = Battleship.new("HOME")
Enemy = Battleship.new("ENEMY")
def play_game
Home.define_board
Home.place_all_ships
Home.count_ships
Enemy.define_board
Enemy.place_all_ships
Enemy.count_ships
while Home.count_ships > 0
Enemy.under_attack
if Enemy.count_ships == 0
puts "HOME wins after #{Home.show_attack_count} hits!"
break
else
Home.under_attack
puts "ENEMY wins after #{Enemy.show_attack_count} hits!" if Home.count_ships == 0
end
end
end
play_game
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment