Cleaning up and writing tests
This commit is contained in:
+73
-16
@@ -48,9 +48,10 @@ require './universe_array'
|
||||
# @param universe_size [Integer] Controlls the size of the generated universe. This is a radius from the center point.
|
||||
class Universe
|
||||
# Setup logger for this class:
|
||||
sh 'mkdir -p logs'
|
||||
@@logger = Logger.new('logs/universe.log')
|
||||
|
||||
attr_reader :name
|
||||
|
||||
def initialize(name, universe_size: 4, create_chances: {planet: 1}, seed: nil)
|
||||
|
||||
@name = name
|
||||
@@ -69,12 +70,15 @@ class Universe
|
||||
@seed = seed
|
||||
@random = Random.new(@seed)
|
||||
end
|
||||
end # Initialize
|
||||
|
||||
|
||||
def create_universe()
|
||||
# Set center of universe
|
||||
@center = Position.new(@universe_size + 2, @universe_size)
|
||||
|
||||
@@logger.info "Generating Universe '#{@name}' with seed #{@seed}"
|
||||
puts "Center is #{@center}"
|
||||
@@logger.info "Center is #{@center}"
|
||||
@universe_map = UniverseArray.new()
|
||||
# Generate the universe. First generate the x values:
|
||||
(0 .. (@universe_size * 2)).each do |y_pos|
|
||||
@@ -90,16 +94,21 @@ class Universe
|
||||
next
|
||||
end
|
||||
|
||||
# Skip if it is the center
|
||||
if position == @center
|
||||
end
|
||||
|
||||
# Get what the planet should be:
|
||||
spot = nil
|
||||
if @random.rand < @create_chances[:planet]
|
||||
spot = Planet.new(@random)
|
||||
end
|
||||
@@logger.debug "Creating something at (#{position.x_pos} #{position.y_pos})"
|
||||
@universe_map[position.y_pos][position.x_pos] = spot
|
||||
@@logger.debug "Creating something at (#{position})"
|
||||
@universe_map[position] = spot
|
||||
end # Create y points
|
||||
end # Create x points
|
||||
end # Function initialize
|
||||
# Remove any empty nodes:
|
||||
end # Function create_universe
|
||||
|
||||
# Returns the distance between points. This is returned as a number which
|
||||
# indicates how far you have to go to get to a point. This is actually
|
||||
@@ -110,14 +119,10 @@ class Universe
|
||||
# @return [Integer] The distance between points in the universe.
|
||||
def distance(position_a, position_b)
|
||||
result = 0 # We can just add the abs diff of each direction.
|
||||
require 'byebug';debugger if position_a.nil?
|
||||
dist_x = (position_b.x_pos - position_a.x_pos).abs
|
||||
dist_y = (position_b.y_pos - position_a.y_pos).abs
|
||||
|
||||
# Sanity check, our coordinates should add to be even:
|
||||
if (dist_x + dist_y).odd?
|
||||
raise RangeError.new('When calculating the distance we calculated a non even number. This is not allowed!')
|
||||
end
|
||||
|
||||
res = (dist_x / 2) + dist_y
|
||||
if dist_x >= dist_y
|
||||
res = (dist_x + dist_y) / 2
|
||||
@@ -128,18 +133,31 @@ class Universe
|
||||
|
||||
# Prints the universe to the screen.
|
||||
def print_universe(simple: false)
|
||||
(0 .. (@universe_size * 2)).each do |y_pos|
|
||||
(0 .. (@universe_map.length)).each do |y_pos|
|
||||
# Print empty space before chart (for angles)
|
||||
nextline = ''
|
||||
if @universe_map[y_pos].nil?
|
||||
next
|
||||
end
|
||||
if @universe_map[y_pos] == []
|
||||
puts '_' * (4 * @universe_size * 2)
|
||||
next
|
||||
end
|
||||
nextline = '|'
|
||||
if y_pos < @universe_size
|
||||
nextline += ' ' * ((@universe_size - y_pos).abs - 1)
|
||||
else
|
||||
nextline += ' ' * ((@universe_size - y_pos).abs)
|
||||
end
|
||||
|
||||
if @universe_size.odd?
|
||||
nextline += ' '
|
||||
end
|
||||
|
||||
# Print more empty space for the odd places (provides offset)
|
||||
if y_pos.odd?
|
||||
print ' '
|
||||
print '| '
|
||||
else
|
||||
print '|'
|
||||
end
|
||||
|
||||
#Actually loop through everything
|
||||
@@ -156,9 +174,9 @@ class Universe
|
||||
end
|
||||
|
||||
# Print what we are:
|
||||
value = @universe_map[position.y_pos][position.x_pos]
|
||||
value = @universe_map[position]
|
||||
nextline += ' '
|
||||
if [x_pos, y_pos] == @center
|
||||
if position == @center
|
||||
print 'O'
|
||||
elsif value.class == Planet
|
||||
print 'P'
|
||||
@@ -184,6 +202,32 @@ class Universe
|
||||
end # For each row
|
||||
end # Function print_universe
|
||||
|
||||
# Returns all of the planets contained in the universe:
|
||||
#
|
||||
# @return [Array<Planet>]
|
||||
def planets
|
||||
result = []
|
||||
@universe_map.flatten.each do |item|
|
||||
result.push(item) if item.is_a? Planet
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
################################################################################
|
||||
################################################################################
|
||||
################################################################################
|
||||
################################################################################
|
||||
################################################################################
|
||||
################################################################################
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
##################################################
|
||||
################### UNIVERSE #####################
|
||||
##################################################
|
||||
@@ -195,8 +239,9 @@ class Universe
|
||||
#
|
||||
# @param player [Player] The player who is asking for the position
|
||||
# @param position [Position] The position that is being requested
|
||||
# @return [Planet, nil] Returns the thing at that point.
|
||||
def get_position(player, position)
|
||||
|
||||
return @universe_map[position]
|
||||
end
|
||||
|
||||
# Sets a position as explored for a player. This should be called as ships
|
||||
@@ -242,6 +287,12 @@ class Universe
|
||||
def remove_planet(planet, position)
|
||||
end
|
||||
|
||||
# Change the ownership of a planet
|
||||
#
|
||||
# @param planet [Planet] The planet to be modified
|
||||
# @param player [Player] The player who should now own the planet
|
||||
# @param position [Position] The position of the planet
|
||||
|
||||
##################################################
|
||||
##################### PLAYER #####################
|
||||
##################################################
|
||||
@@ -251,6 +302,12 @@ class Universe
|
||||
# @param player [Player] The player who should be added to the universe
|
||||
def add_player(player)
|
||||
@player_list.push(player)
|
||||
# Give the player a planet
|
||||
free_planets = []
|
||||
planets.each do |planet|
|
||||
free_planets.push(planet) if planet.owner.nil?
|
||||
end
|
||||
|
||||
return 'Added player to universe'
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user