Pushing all changes

This commit is contained in:
2025-12-19 09:16:48 -07:00
parent b8f54b44a3
commit 4737531805
7 changed files with 105 additions and 13 deletions
+8
View File
@@ -29,4 +29,12 @@ class Planet
end
return false
end
# Incriment the planet. Used to add resources to the Player's global resource
# storage
def tick
if !@owner.nil?
@oener.add_resources({@resource => @prod_rate})
end
end
end
+12 -1
View File
@@ -3,12 +3,23 @@
# Class for the player. For now does not do much.
class Player
attr_reader :name
attr_reader :name, :resources
# initializes with just the player name.
#
# @param name [String] The name of the player
def initialize(name)
@name = name
@resources = {:ships => 10, :metal => 100}
end
def add_resources(resources)
resources.keys.each do |resource_key|
if @resources[resource_key].nil?
@resources[resource_key] = resources[resource_key]
else
@resources[resource_key] += resources[resource_key]
end
end
end
end
+10 -2
View File
@@ -8,8 +8,8 @@ RSpec.describe Planet do
let(:seed) { 12345 }
let(:random) { Random.new(seed) }
let(:planet) { Planet.new(random) }
let(:player_1) {Player.new('tester 01') }
let(:player_2) {Player.new('tester 02') }
let(:player_1) { Player.new('tester 01') }
let(:player_2) { Player.new('tester 02') }
describe '#initialize' do
it 'stores the random off' do
@@ -41,4 +41,12 @@ RSpec.describe Planet do
expect(planet.instance_variable_get(:@owner)).to eq player_2
end
end
describe '#tick' do
it 'adds resources to the Players storage' do
planet.change_owner(player_1)
planet.tick
expect(player_1.resources).to eq('')
end
end
end
+16
View File
@@ -0,0 +1,16 @@
# Nahtan Hinton 3 Oct 2025
require './player'
RSpec.describe Player do
let(:player_1) { Player.new('tester 01') }
describe '#initialize' do
it 'sets the player name' do
expect(player_1.instance_variable_get(:@name)).to eq 'tester 01'
end
it 'gives initial resources' do
expect(player_1.instance_variable_get(:@resources)).to eq ({:ships => 10, :metal => 100})
end
end
end
+13
View File
@@ -147,4 +147,17 @@ RSpec.describe Universe do
expect(universe.planets.length).to eq 18
end
end
describe '#tick' do
it 'runs a tick of the game' do
universe.create_universe
player_1 = Player.new('test player 01')
universe.add_player(player_1)
universe.add_ship(player_1, Position.new(3, 5))
ships = universe.get_player_ships(player_1)
universe.move_ship(player_1, ships[0], Position.new(2, 4))
# Now we tick the universe and the ship should move a little and the player should gain resources.
expect{universe.tick}.to_not raise_error()
end
end
end
+12
View File
@@ -221,7 +221,18 @@ class Universe
return result
end
# Runs a tick of the game. Passes it on to all of the sub objects
def tick
# Incriment planets
@planet_list.each do |planet|
planet.tick
end
# Incriment ships
@ship_list.each do |ship|
ship.tick
end
end
@@ -272,6 +283,7 @@ class Universe
def create_ship(player, position)
return false unless validate_player(player)
end
alias_method :add_ship, :create_ship
# Move a player's ship to a given position
#