# Patrick Coleman # Comp Sys Res - 6th period - 2007-08 # Sugarscape - environment require 'location.rb' require 'agent.rb' class Environment #width of environment, height of environment, matrix of locations, #growback rate, array of agents, empty locations attr_accessor :width, :height, :env, :growback, :agents, :emptylocs #Initialize matrix of locations using file def initialize (file, growback=1) @env = [] k = 0 File.open(file).each do |line| arr = [] j = 0 #Read sugar quantity information from file and create location objects (line.chomp.split("")).each {|e| arr << Location.new(j,k,e.to_i,growback); j += 1} @env[k] = arr k += 1 end #Initialize variables @width = @env.length @height = @env[0].length @growback = growback #Initializes hash of empty locations @emptylocs = {} for i in 0...50 do for j in 0...50 do @emptylocs[[i,j]] = 0 end end #Adds the first agent, storing location and a copy of the matrix of locations @agents = [Agent.new(rand(50),rand(50),@env,@emptylocs)] @emptylocs.delete([@agents[0].posY,@agents[0].posX]) end #Adds an agent to a random location in the environment def addAgent key = @emptylocs.keys arr = key[rand(key.length)] x = arr[1]; y = arr[0] =begin #Cycles until a valid location is found x = rand(50); y = rand(50) while @env[y][x].hasAgent != -1 x = rand(50); y = rand(50) end =end #Adds the agent @agents << Agent.new(x,y) end end