Permalink
Browse files

more fixes and specs addition

  • Loading branch information...
hindenbug committed Mar 8, 2012
1 parent 4ecc629 commit 45bb661cfb10ac8f09657df3c371129c799b7597
@@ -1,26 +1,29 @@
require_relative "ship"
-module BattleShip
- class BattleField
- attr_accessor :rows, :cols, :warzone
+class BattleField
+ attr_accessor :rows, :cols, :warzone, :ships
- def initialize(rows, cols)
- @rows = rows
- @cols = cols
- @warzone = prepare_field
- end
+ def initialize(rows, cols)
+ @rows = rows
+ @cols = cols
+ @warzone = prepare_field
+ @ships = []
+ end
- def prepare_field
- Array.new(@rows) { Array.new(@cols) { "x" } }
- end
+ def prepare_field
+ Array.new(@rows) { Array.new(@cols) { "-" } }
+ end
- def deploy_ship(ship, x, y, direction = "horizontal")
+ def deploy_ship(ship, x, y, direction = :h)
+ begin
if (x>=0 && x < rows - 1) && (y >= 0 && y < cols-1)
- Ship.new(self, ship, x, y, direction)
+ @ships << Ship.new(self, ship, x, y, direction)
+ else
+ raise "Given co-ordinates are out of bound"
end
+ rescue Exception => e
+ puts e.message
end
-
- private
end
end
@@ -0,0 +1,3 @@
+module Battleship
+ SHIPS = [:aircraft_carrier, :battleship, :destroyer, :submarine, :cruiser]
+end
View
@@ -1,21 +1,32 @@
require_relative "battle_field"
require_relative "player"
+require_relative 'battleship'
class Game
+ include Battleship
attr_accessor :rows, :cols, :player_one, :player_two
def initialize(rows=10, cols=10)
@rows = rows
@cols = cols
end
- def start
- [ :player_one, :player_two].each do |player|
+ def setup
+ [ :player_one, :player_two].map! do |player|
puts "Enter #{player}'s name:"
player = setup_player
- puts "#{player.name}'s Board:"
+ puts "#{player.name}'s BattleField:"
+ puts player.display_field
+ puts player.position_ships
puts player.display_field
- puts
+ end
+ puts "Ready to Rumble !!"
+ end
+
+ def start
+ setup
+ unless winner?
+ take_turns
end
end
@@ -24,15 +35,41 @@ def start
def setup_player
name = gets.strip
field = setup_field
- BattleShip::Player.new(name, field)
+ Player.new(name, field)
end
def setup_field
- BattleShip::BattleField.new(rows, cols)
+ BattleField.new(rows, cols)
end
-end
+ def take_turns
+ @player_one.field.ships.times do
+ puts "#{@player_one} attack:"
+ @player_one.attack(@player_two, get_attack_coords)
+ end
+ @player_two.field.ships.times do
+ puts "#{@player_two} attack:"
+ @player_two.attack(@player_one, get_attack_coords)
+ end
+ end
+
+ def get_attack_coords
+ ### TODO ENFORCE TYPE CHECK ####
+ puts "Enter X co-ordinate to attack :"
+ x = gets.chomp.to_i
+ puts "Enter Y co-ordinate to attack :"
+ y = gets.chomp.to_i
+ [x, y]
+ end
-@game = Game.new
-@game.start
-puts @game.player_one
+ def winner?
+ if @player_one.alive? && @player_two.dead?
+ puts "#{@player_one.name} YOU WIN!!"
+ elsif @player_one.dead? && @player_one.alive?
+ puts "#{@player_two.name} YOU WIN!"
+ else
+ return false
+ end
+ end
+
+end
View
@@ -1,15 +1,56 @@
-module BattleShip
- class Player
+require_relative "battleship"
+require_relative "battle_field"
- attr_accessor :name, :warzone
+class Player
+ include Battleship
+ attr_accessor :name, :battle_field
- def initialize(name, field)
- @name = name
- @warzone = field.warzone
+ def initialize(name, field)
+ @name = name
+ @battle_field = field
+ end
+
+ def display_field
+ @battle_field.warzone.map {|row| row.join(" ")}.join("\n")
+ end
+
+ def position_ships
+ SHIPS.each do |ship|
+ puts "Enter co-ordinates for the #{ship.to_s.upcase}:"
+ x, y = get_coordinates
+ puts "Enter Ship's orientation 'horizontal' or 'vertical' :"
+ direction = gets.chomp.to_sym
+ battle_field.deploy_ship(ship, x, y, direction)
end
+ puts "All Ships deployed to the Warzone !!"
+ end
- def display_field
- warzone.map {|row| row.join(" ")}.join("\n")
+ def attack(player, x, y)
+ player.battle_field.ships.each do |ship|
+ if ship.hit?(x, y)
+ ship.hit!(x, y)
+ puts "HIT!!!"
+ else
+ puts "MISS!!"
+ end
end
end
+
+ def alive?
+ !battle_field.ships.empty?
+ end
+
+ def dead?
+ battle_field.ships.empty?
+ end
+
+ private
+
+ def get_coordinates
+ puts "Enter X co-ordinate"
+ x = gets.chomp.to_i
+ puts "Enter Y co-ordinate"
+ y = gets.chomp.to_i
+ return x, y
+ end
end
View
@@ -1,56 +1,72 @@
-module BattleShip
- class Ship
- attr_accessor :ship_type, :x, :y, :warzone, :direction, :state
+ class Ship
+ attr_accessor :ship_type, :x, :y, :warzone, :direction, :hits
+ SHIP_SIZE = {:aircraft_carrier => 5, :battleship => 4, :cruiser => 3, :submarine => 3, :destroyer => 2}
- def initialize(field, ship, x, y, direction)
- @ship_type = ship
- @x = x
- @y = y
- @warzone = field.warzone
- @direction = direction
- place_ships
- end
+ def initialize(field, ship, x, y, direction)
+ @ship_type = ship
+ @x = x
+ @y = y
+ @warzone = field.warzone
+ @direction = direction
+ @hits = 0
+ place_ships
+ end
+
+ def hit?(x, y)
+ warzone[x][y] != "-"
+ end
- private
+ def hit!(x, y)
+ warzone[x][y] = "*"
+ @hits += 1
+ end
- def place_ships
- case ship_type.downcase
- when "aircraft_carrier"
- deploy(5, "A")
- when "battleship"
- deploy(4, "W")
- when "cruiser"
- deploy(3, "S")
- when "submarine"
- deploy(3, "C")
- when "destroyer"
- deploy(2, "D")
- end
+ def sunk?
+ hits == SHIP_SIZE[ship_type]
+ end
+
+ private
+
+ def place_ships
+ case ship_type
+ when :aircraft_carrier
+ deploy(5, "A")
+ when :battleship
+ deploy(4, "W")
+ when :cruiser
+ deploy(3, "S")
+ when :submarine
+ deploy(3, "C")
+ when :destroyer
+ deploy(2, "D")
+ else
+ puts "Invalid Ship Type"
end
+ end
- def deploy(length, ship_name)
- if direction == "vertical"
- end_point = x + (length - 1)
- x.upto(end_point) do |i|
- if warzone[i][y].empty?
- warzone[i][y] = ship_name
- else
- raise "ERROR"
- false
- end
+ def deploy(length, ship_name)
+ ### TODO refactor looks ugly ####
+ if direction == :v
+ end_point = x + (length - 1)
+ x.upto(end_point) do |i|
+ if warzone[i][y] == "-"
+ warzone[i][y] = ship_name
+ else
+ raise "ERROR"
+ false
end
- elsif direction == "horizontal"
- end_point = y + (length - 1)
- y.upto(end_point) do |i|
- if warzone[x][i].empty?
- warzone[x][i] = ship_name
- else
- raise "ERROR"
- false
- end
+ end
+ elsif direction == :h
+ end_point = y + (length - 1)
+ y.upto(end_point) do |i|
+ if warzone[x][i] == '-'
+ warzone[x][i] = ship_name
+ else
+ raise "ERROR"
+ false
end
end
- warzone
end
+ warzone
end
end
@@ -1,7 +1,7 @@
require "rspec"
require_relative "../game"
-describe Game do
+describe Game do
let(:game) { Game.new(3, 3) }
@@ -13,11 +13,16 @@
game.should respond_to(:player_two)
end
+ describe "#setup" do
+ #before(:each) { game.setup }
+
+ #it "should setup players and boards" do
+ # game
+ #end
+ end
+
describe "#start" do
- before(:each) { game.start }
- it "should setup players and boards" do
- # game.player
- end
+
end
end
Oops, something went wrong.

0 comments on commit 45bb661

Please sign in to comment.