Skip to content
10 changes: 10 additions & 0 deletions battleship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require './ship.rb'
require './board.rb'
require './player.rb'


# carrier = Ship.new(5)
# battleship = Ship.new(4)
# cruiser = Ship.new(3)
# submarine = Ship.new(3)
# destroyer = Ship.new(2)
110 changes: 110 additions & 0 deletions board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
require './ship.rb'

class Board
def initialize
@fleet = []
@placed = []
@shotstaken = []
@cells_with_ships = []

end

def has_ship_on?(x, y)
ship_on = false
@fleet.each do |ship|
if ship.covers?(x, y)
ship_on = true
end
end
return ship_on
end


def place_ship(ship, x, y, direction)
ship.place(x, y, direction)
@fleet.each do |s|
if s.overlaps_with?(ship)
return false
end
end
@fleet << ship
# a = ship.locations[0]
# b = ship.locations[1]
# c = ship.locations[2]
# @cells_with_ships << a << b << c
ship.locations.each do |location_on_board|
@cells_with_ships << location_on_board
end
end


def fire_at(x, y)
if @fleet.empty?
return false
end
if @shotstaken.include?([x,y])
return false #shot already taken
else
@fleet.each do |ship|
if ship.covers?(x,y)
@shotstaken << [x,y]
return true
else
@shotstaken << [x,y]
return false
end
end
end
end
# return false #representing a miss


def game_board_header
puts " 1 2 3 4 5 6 7 8 9 10"
puts " -----------------------------------------"
end

def game_board_footer
puts " -----------------------------------------"
end


def display
y_axis = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
output = ""
self.game_board_header
(1..10).each do |row|
output = "#{y_axis[row - 1]} |"
(1..10).each do |column|
if @shotstaken.include?([column, row]) && self.has_ship_on?(column, row)
output << " X |"
elsif self.has_ship_on?(column, row)
output << " O |"
else
output << " |"
end
end
puts output
end
self.game_board_footer
end

def sunk?
if @shotstaken.empty?
return false
end
if @shotstaken.sort == @cells_with_ships
return true
else
return false
end
end


end

# board = Board.new
#
#
# board.place_ship(Ship.new(3), 3, 3, true)
# puts "#{board.inspect}"
4 changes: 4 additions & 0 deletions computerplayer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ComputerPlayer
def intialize
end
end
4 changes: 4 additions & 0 deletions humanplayer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class HumanPlayer
def initialize
end
end
38 changes: 38 additions & 0 deletions player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def get_user_input
gets.chomp
end

require './board.rb'

class Player
def initialize
end

def name
return @name
end

def board
return Board.new
end
end

class HumanPlayer < Player
def initialize(player_name = "Dave")
@name = player_name
end

def name
return @name
end

end

class ComputerPlayer < Player
def initialize
@computer_name = "HAL 9000"
end
def name
return @computer_name
end
end
89 changes: 89 additions & 0 deletions ship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
class Ship
def initialize(shiplength)
@shiplength = shiplength
@locations = []
@has_been_placed = false
@overlaps_with = false
@hitpoints = @shiplength
end

def length
return @shiplength
#use @instance variable to indicate that you want to apply this to all the examples
end

# def place(x, y, direction)
# if @has_been_placed == false # ______ == _______ # has already been placed
# if direction
# @locations = [[x,y],[x+1,y],[x+2,y],[x+3,y]]
# else
# @locations = [[x,y],[x,y+1],[x,y+2],[x,y+3]]
# end
# @has_been_placed = true
# end
# end

def place(x,y,direction)
if @locations.empty?
@shiplength.times do
@locations << [x,y]
if direction
x = x+1
else
y = y+1
end
end
return true
else
return false
end
end


def covers?(x,y)
search_for = [x, y]
@locations.include?(search_for)
end

def locations
@locations
end

def overlaps_with?(othership)
# @locations.each do |check|
# @locations & othership.locations[x,y]
# end
if
(@locations & othership.locations).empty?
return false
else
return true
end
end

def fire_at(x,y)
if @locations.include?([x,y])
@hitpoints = (@hitpoints - 1)
# @hitpoints -=1
return true
else
return false
end
end

def sunk?
if @hitpoints == 0
return true
end
end




end

#ship = Ship.new(4)
#ship.place(2, 1, true)


#ship.place(2, 2, false)