Skip to content

Instantly share code, notes, and snippets.

@aperezsantos
Created October 10, 2019 00:54
Show Gist options
  • Save aperezsantos/dae66c14075855ba77c115d98f681ff5 to your computer and use it in GitHub Desktop.
Save aperezsantos/dae66c14075855ba77c115d98f681ff5 to your computer and use it in GitHub Desktop.
These are my changes.

require 'minitest/autorun'

require 'minitest/pride'

require './lib/ship'

require './lib/cell'

class CellTest < Minitest::Test

def setup

@cell = Cell.new("B4")

@cell_2 = Cell.new("A1")

@cruiser = Ship.new("Cruiser", 3)

@submarine = Ship.new("Submarine", 2)

end

def test_it_exists

assert_instance_of Cell, @cell

assert_instance_of Cell, @cell_2

end

def test_coordinate_returns_cells_coordinate

assert_equal "B4", @cell.coordinate

assert_equal "A1", @cell_2.coordinate

end

def test_ship_returns_nil_initially

assert_nil @cell.ship

assert_nil @cell_2.ship

end

def test_empty_returns_true_initially

assert_equal true, @cell.empty?

assert_equal true, @cell_2.empty?

end

def test_it_has_ship_after_placing_ship

@cell.place_ship(@cruiser)

assert_equal @cruiser, @cell.ship

@cell_2.place_ship(@submarine)

assert_equal @submarine, @cell_2.ship

end

def test_it_is_not_empty_after_placing_ship

@cruiser = Ship.new("Cruiser", 3)

@cell.place_ship(@cruiser)

assert_equal false, @cell.empty?

@submarine = Ship.new("Submarine", 2)

@cell_2.place_ship(@submarine)

assert_equal false, @cell_2.empty?

end

def test_fired_upon_starts_false

assert_equal false, @cell.fired_upon?

assert_equal false, @cell_2.fired_upon?

end

def test_fire_upon_lowers_ship_health_by_one

@cell.place_ship(@submarine)

@cell.fire_upon

assert_equal 1, @cell.ship.health

assert_equal 2, @cell.ship.health

end

def test_cell_has_not_been_fired_upon

assert_equal false, @cell.fired_upon?

# @cell.fire_upon

# assert_equal true, @cell.fired_upon?

end

def test_fire_upon_reduces_health

@cell.fire_upon

assert_equal @ship.health, @ship.length - 1

end

end

class Cell

attr_reader :coordinate, :ship, :fired_upon

def initialize(coordinate_parameter, ship = nil)

@coordinate = coordinate_parameter

@ship = ship

@empty = true

end

def empty?

@empty

end

def place_ship(ship_object_parameter)

@ship = ship_object_parameter

end

def fired_upon?

false

end

def fire_upon

if @empty == true

"Missed!"

else

@ship.hit

end

end

end

#require 'minitest/autorun'

require 'minitest/pride'

require './lib/ship'

class ShipTest < Minitest::Test

def setup

@ship = Ship.new("Cruiser", 3)

@ship_2 = Ship.new("Submarine", 2)

end

def test_it_exists

assert_instance_of Ship, @ship

end

def test_it_initializes_with_name_and_length

assert_equal "Cruiser", @ship.name

assert_equal 3, @ship.length

assert_equal "Submarine", @ship_2.name

assert_equal 2, @ship_2.length

end

# @health

def test_health_equals_length

assert_equal @ship.length, @ship.health

assert_equal @ship_2.length, @ship_2.health

end

# .hit

def test_hit_decreases_health_by_one

assert_equal 3, @ship.health

@ship.hit

assert_equal 2, @ship.health

@ship.hit

assert_equal 1, @ship.health

@ship.hit

assert_equal 0, @ship.health

@ship.hit

assert_equal 0, @ship.health

assert_equal 2, @ship_2.health

@ship_2.hit

assert_equal 1, @ship_2.health

@ship_2.hit

assert_equal 0, @ship_2.health

end

# .sunk?

def test_it_is_sunk_or_not

assert_equal 3, @ship.health

@ship.hit

assert_equal false, @ship.sunk?

@ship.hit

assert_equal false, @ship.sunk?

@ship.hit

assert_equal true, @ship.sunk?

# second test on second ship

assert_equal 2, @ship_2.health

@ship_2.hit

assert_equal false, @ship_2.sunk?

@ship_2.hit

assert_equal true, @ship_2.sunk?

end

end

class Ship

attr_reader :name, :length, :health, :hit

def initialize(name_parameter, length_parameter)

@name = name_parameter

@length = length_parameter

@health = length_parameter

end

def hit

if @health > 0

@health -= 1

end

end

def sunk?

@health <= 0

end

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment