Sunday, July 25, 2010

A whole new world...

Well, not really. I've just added a lot to the tests. Codedump:
class World
def initialize(x_size=20, y_size=20, building_control=10)
remake(x_size, y_size, building_control)
end

def remake(x_size=20, y_size=20, building_control=10)
@x_size = x_size
@y_size = y_size
@building_control = building_control

@map = createMap
placeBuildings
placeRobot
end

def createMap
map = Array.new(@x_size)
map.each_index do |col|
map[col] = Array.new(y_size)
map[col].each_index do |i|
map[col][i] = " "
end
end
map
end

def display
@map.each do |col|
col.each do |i|
printf(i)
end
puts ""
end
end

def placeBuildings
1.upto(@building_control) do
placeObject("B")
end
# fills the world with buildings
end

def placeRobot
placeObject("P")
# puts Robot in a place where buildings aren't
end

def placeObject(string)
placed = false
while placed == false
x = rand(@x_size)
y = rand(@y_size)
if @map[x][y] == " " then
@map[x][y] = string
placed = true
end
end
end

def line_of_sight
# returns position of next visible object
end

attr_reader :map, :x_size, :y_size, :building_control
end
require 'test/unit'
require 'src/world.rb'

class WorldTest < Test::Unit::TestCase

@@x_size_list = Array.new(10) {|i| i+10}
@@y_size_list = Array.new(10) {|i| i+10}
@@building_control_list = Array.new(3) {|i| i+50}

def setup
@space_count = 0
@building_count = 0
@player_count = 0
@other_count = 0

@w = World.new(@@x_size_list[0], @@y_size_list[0], @@building_control_list[0])
end

must "properly set the x_size parameter" do
assert_equal @w.x_size, @@x_size_list[0]
end

must "properly set the y_size parameter" do
assert_equal @w.y_size, @@y_size_list[0]
end

must "properly set the building control parameter" do
assert_equal @w.building_control, @@building_control_list[0]
end

must "generate a 2D map" do
assert_equal @w.map[0][0].class, String
end

must "generate a 2D map to at least the specified dimensions" do
assert_equal @w.map[@w.x_size-1][@w.y_size-1].class, String
end

must "generate a 2D map not exceeding the specified dimensions" do
assert_raises(NoMethodError) do
assert_equal @w.map[@w.x_size][@w.y_size].class, String
end
end

@@x_size_list.each do |x|
@@y_size_list.each do |y|
@@building_control_list.each do |b|
# puts "#{x} #{y} #{b}: layout"


must "place #{b} buildings on size #{x} * #{y}" do
# puts "#{x}, #{y}, #{b}: buildings"
@w.remake(x, y, b)
count_map_contents
assert_equal b, @w.building_control
assert_equal b, @building_count
end

must "place #{(x * y) - (b+1)} spaces on size #{x} * #{y}" do
# puts "#{x}, #{y}, #{b}: spaces"
@w.remake(x, y, b)
count_map_contents
assert_equal ((x * y) - (b+1)), @space_count
end

must "place 1 player on size #{x} * #{y}, building control #{b}" do
# puts "#{x}, #{y}, #{b}: 1 player"
@w.remake(x, y, b)
count_map_contents
assert_equal 1, @player_count
end

must "not put any ambiguous spaces on size #{x} * #{y}, building control #{b}" do
# puts "#{x}, #{y}, #{b}: no ambiguous"
@w.remake(x, y, b)
count_map_contents
assert_equal 0, @other_count
end
end
end
end

def count_map_contents
@w.map.each_index do |col|
@w.map[col].each do |i|
if i == "B" then
@building_count += 1
elsif i == " " then
@space_count += 1
elsif i == "P" then
@player_count += 1
else
@other_count += 1
end
end
end
end
end


Sooo yeah. 1340 tests, 1640 assertions, 0 failures and 0 errors. I'm not sure whether I should do the line of sight/check code next or the linking code next, to pass the messages around. I'm betting the line of sight will be a lot easier, but it may have to change when I glue tings together - so I'm wary of pushing into it. Glad to get world into a good, test-passing state before I head to Ottawa for a week. I think I'd like to put something into the Hunters story over in Fiction, after I'm back. And keep learning physics -- and of course, finish off this robot game. It's good to design something small, to wean myself onto the process.

No comments: