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
+16
View File
@@ -1 +1,17 @@
# 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?
raise ArgumentError.new('A position created in (x, y) must sum to an even number!')
end
x_pos = args[0]
y_pos = args[1]
@x_pos = args[0]
@y_pos = args[1]
elsif args.length == 3
# initialize with a, b, c position
raise NotImplementedError.new('(a, b, c) positioning not implimented!')
else
require 'byebug';debugger
raise ArgumentError.new('Creation of a position requires two or three integer args for a position.')
end
end
@@ -33,4 +34,8 @@ class Position
def two_d_cartesian
return [x_pos, y_pos]
end
def to_s
return "(#{@x_pos}, #{y_pos})"
end
end
+22 -13
View File
@@ -1,7 +1,12 @@
# Nathan Hinton 1 Oct 2025
require 'logger'
require './planet'
require './position'
require './universe_array'
# 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
@@ -42,12 +47,15 @@ require './planet'
# @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.
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
@universe_size = universe_size
@create_chances = create_chances
@verbose = verbose
@debug = debug
# Setup internal lists:
@player_list = []
@@ -63,11 +71,11 @@ class Universe
end
# 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}"
@universe_map = []
@universe_map = UniverseArray.new()
# Generate the universe. First generate the x values:
(0 .. (@universe_size * 2)).each do |y_pos|
@universe_map[y_pos] = []
@@ -75,7 +83,7 @@ class Universe
# Make the grid work right, x is actually skipping every other.
x_pos *= 2
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
if distance(@center, position) >= @universe_size
@@ -87,7 +95,7 @@ class Universe
if @random.rand < @create_chances[:planet]
spot = Planet.new(@random)
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
end # Create y points
end # Create x points
@@ -114,7 +122,7 @@ class Universe
if dist_x >= dist_y
res = (dist_x + dist_y) / 2
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
end # Function distance
@@ -140,18 +148,19 @@ class Universe
x_pos *= 2
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)
if distance(@center, [x_pos, y_pos]) >= @universe_size
if distance(@center, position) >= @universe_size
print ' '
next
end
# Print what we are:
position = @universe_map[y_pos][x_pos]
value = @universe_map[position.y_pos][position.x_pos]
nextline += ' '
if [x_pos, y_pos] == @center
print 'O'
elsif position.class == Planet
elsif value.class == Planet
print 'P'
else
print ' '
@@ -161,7 +170,7 @@ class Universe
if y_pos < @universe_size
nextline += '/ \\'
end
if distance(@center, [x_pos + 2, y_pos]) < @universe_size
if distance(@center, Position.new(x_pos + 2, y_pos)) < @universe_size
print '---'
if y_pos >= @universe_size
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