working more

This commit is contained in:
2025-10-02 15:31:30 -06:00
parent 72bd5fb9c0
commit ed3cd08130
6 changed files with 67 additions and 47 deletions
+2
View File
@@ -1,3 +1,5 @@
**/doc **/doc
**/.byebug* **/.byebug*
**/.yard* **/.yard*
**/logs
**/*tmp*
+16
View File
@@ -1 +1,17 @@
# Nathan Hinton 2 Oct 2025 # Nathan Hinton 2 Oct 2025
# Does not work sadly...
require 'logger'
module Log
@log = Logger.new('game.log')
@log.level = Logger::INFO
def self.logger
@logger
end
def logger
Log.logger
end
end
+7 -2
View File
@@ -10,12 +10,13 @@ class Position
if args.sum.odd? if args.sum.odd?
raise ArgumentError.new('A position created in (x, y) must sum to an even number!') raise ArgumentError.new('A position created in (x, y) must sum to an even number!')
end end
x_pos = args[0] @x_pos = args[0]
y_pos = args[1] @y_pos = args[1]
elsif args.length == 3 elsif args.length == 3
# initialize with a, b, c position # initialize with a, b, c position
raise NotImplementedError.new('(a, b, c) positioning not implimented!') raise NotImplementedError.new('(a, b, c) positioning not implimented!')
else else
require 'byebug';debugger
raise ArgumentError.new('Creation of a position requires two or three integer args for a position.') raise ArgumentError.new('Creation of a position requires two or three integer args for a position.')
end end
end end
@@ -33,4 +34,8 @@ class Position
def two_d_cartesian def two_d_cartesian
return [x_pos, y_pos] return [x_pos, y_pos]
end end
def to_s
return "(#{@x_pos}, #{y_pos})"
end
end end
+22 -13
View File
@@ -1,7 +1,12 @@
# Nathan Hinton 1 Oct 2025 # Nathan Hinton 1 Oct 2025
require 'logger'
require './planet' require './planet'
require './position'
require './universe_array'
# Universe class. Holds a universe and provides the api for it. Universes are # Universe class. Holds a universe and provides the api for it. Universes are
# created on a 2d grid. I would like it to be hexagonal movement type so that # created on a 2d grid. I would like it to be hexagonal movement type so that
@@ -42,12 +47,15 @@ require './planet'
# @param name [String] The name of the game # @param name [String] The name of the game
# @param universe_size [Integer] Controlls the size of the generated universe. This is a radius from the center point. # @param universe_size [Integer] Controlls the size of the generated universe. This is a radius from the center point.
class Universe class Universe
def initialize(name, universe_size: 4, create_chances: {planet: 1}, seed: nil, verbose: true, debug: false) # Setup logger for this class:
sh 'mkdir -p logs'
@@logger = Logger.new('logs/universe.log')
def initialize(name, universe_size: 4, create_chances: {planet: 1}, seed: nil)
@name = name @name = name
@universe_size = universe_size @universe_size = universe_size
@create_chances = create_chances @create_chances = create_chances
@verbose = verbose
@debug = debug
# Setup internal lists: # Setup internal lists:
@player_list = [] @player_list = []
@@ -63,11 +71,11 @@ class Universe
end end
# Set center of universe # Set center of universe
@center = Position.new([@universe_size + 2, @universe_size ]) @center = Position.new(@universe_size + 2, @universe_size)
puts "Generating Universe '#{@name}' with seed #{@seed}" if @verbose @@logger.info "Generating Universe '#{@name}' with seed #{@seed}"
puts "Center is #{@center}" puts "Center is #{@center}"
@universe_map = [] @universe_map = UniverseArray.new()
# Generate the universe. First generate the x values: # Generate the universe. First generate the x values:
(0 .. (@universe_size * 2)).each do |y_pos| (0 .. (@universe_size * 2)).each do |y_pos|
@universe_map[y_pos] = [] @universe_map[y_pos] = []
@@ -75,7 +83,7 @@ class Universe
# Make the grid work right, x is actually skipping every other. # Make the grid work right, x is actually skipping every other.
x_pos *= 2 x_pos *= 2
x_pos += 1 if y_pos.odd? x_pos += 1 if y_pos.odd?
position = Position.new([x_pos, y_pos]) position = Position.new(x_pos, y_pos)
# Skip if the node is too far # Skip if the node is too far
if distance(@center, position) >= @universe_size if distance(@center, position) >= @universe_size
@@ -87,7 +95,7 @@ class Universe
if @random.rand < @create_chances[:planet] if @random.rand < @create_chances[:planet]
spot = Planet.new(@random) spot = Planet.new(@random)
end end
puts "Creating something at (#{position.x_pos} #{position.y_pos})" if @debug @@logger.debug "Creating something at (#{position.x_pos} #{position.y_pos})"
@universe_map[position.y_pos][position.x_pos] = spot @universe_map[position.y_pos][position.x_pos] = spot
end # Create y points end # Create y points
end # Create x points end # Create x points
@@ -114,7 +122,7 @@ class Universe
if dist_x >= dist_y if dist_x >= dist_y
res = (dist_x + dist_y) / 2 res = (dist_x + dist_y) / 2
end end
puts "Distance: #{point_a} is #{point_b} #{res} units away" if @debug @@logger.debug "Distance: #{position_a} and #{position_b} are #{res} units away"
return res return res
end # Function distance end # Function distance
@@ -140,18 +148,19 @@ class Universe
x_pos *= 2 x_pos *= 2
x_pos += 1 if y_pos.odd? x_pos += 1 if y_pos.odd?
position = Position.new(x_pos, y_pos)
# Print empty space if we are outside the universe (For items) # Print empty space if we are outside the universe (For items)
if distance(@center, [x_pos, y_pos]) >= @universe_size if distance(@center, position) >= @universe_size
print ' ' print ' '
next next
end end
# Print what we are: # Print what we are:
position = @universe_map[y_pos][x_pos] value = @universe_map[position.y_pos][position.x_pos]
nextline += ' ' nextline += ' '
if [x_pos, y_pos] == @center if [x_pos, y_pos] == @center
print 'O' print 'O'
elsif position.class == Planet elsif value.class == Planet
print 'P' print 'P'
else else
print ' ' print ' '
@@ -161,7 +170,7 @@ class Universe
if y_pos < @universe_size if y_pos < @universe_size
nextline += '/ \\' nextline += '/ \\'
end end
if distance(@center, [x_pos + 2, y_pos]) < @universe_size if distance(@center, Position.new(x_pos + 2, y_pos)) < @universe_size
print '---' print '---'
if y_pos >= @universe_size if y_pos >= @universe_size
nextline += '\\ /' nextline += '\\ /'
+20
View File
@@ -0,0 +1,20 @@
# Nathan Hinton 1 Oct 2025
# Create a custom hash class for the universe.
require './position'
# This will contain the universe hash inside it. It will allow for array type
# indexing as well as point type indexing
class UniverseArray < Array
def initialize
super
end
def [](index, *rest)
if index.is_a?(Position)
return self[position.y_pos][position.x_pos]
else
super
end
end
end
-32
View File
@@ -1,32 +0,0 @@
# Nathan Hinton 1 Oct 2025
# Create a custom hash class for the universe.
# This will contain the universe hash inside it. It will allow for array type
# indexing as well as point type indexing
class UniverseHash
def initialize()
@universe_hash = {}
end
# Setter for values in the hash
def =[](key, value)
if key.class == Array
@universe_hash[key[0]] = {} if @universe_hash[key[0]].nil?
@universe_hash[key[0]][key[1]] = {} if @universe_hash[key[0]][key[1]].nil?
@universe_hash[key[0]][key[1]][key[2]] = value
else
raise NotImplementedError.new('You can only set based on point values.')
end
end
def [](key)
if key.class == Array
return @universe_hash[key[0]][key[1]][key[2]]
elsif key.class == Integer
return @universe_hash[key]
else
raise NotImplementedError
end
end
end