Browse files

killing time

  • Loading branch information...
1 parent f5e6d9d commit 53d041e5eec14304365dcb0d9806d2517ac2372a @CodeOfficer committed Mar 3, 2010
Showing with 74 additions and 48 deletions.
  1. +1 −0 config/environment.rb
  2. +7 −6 lib/base_player.rb
  3. +5 −3 lib/base_terrain.rb
  4. +7 −7 lib/base_unit.rb
  5. +14 −10 lib/game.rb
  6. +7 −5 lib/map.rb
  7. +8 −7 lib/matrix.rb
  8. +22 −10 spec/game_spec.rb
  9. +2 −0 spec/spec.opts
  10. +1 −0 spec/spec_helper.rb
View
1 config/environment.rb
@@ -1,3 +1,4 @@
+puts "loading environment.rb"
# load all the class files
Dir.glob(File.join(File.dirname(__FILE__), '../lib/*.rb')).each { |f| require f }
View
13 lib/base_player.rb
@@ -1,18 +1,19 @@
+puts "loading base_player.rb"
+
class BasePlayer
-
- attr_reader :game
+
attr_reader :name
-
+
def initialize(name)
@name = name
end
-
+
end
class ComputerPlayer < BasePlayer
-
+
end
class HumanPlayer < BasePlayer
-
+
end
View
8 lib/base_terrain.rb
@@ -1,11 +1,13 @@
+puts "loading base_terrain.rb"
+
class BaseTerrain
-
+
attr_reader :name
-
+
def initialize(name)
@name = name
end
-
+
end
class ForestTerrain < BaseTerrain
View
14 lib/base_unit.rb
@@ -1,19 +1,19 @@
+puts "loading base_unit.rb"
+
class BaseUnit
-
+
attr_reader :name
- attr_reader :player
-
- def initialize(player, name)
- @player = player
+
+ def initialize(name)
@name = name
end
end
class GeekUnit < BaseUnit
-
+
end
class NerdUnit < BaseUnit
-
+
end
View
24 lib/game.rb
@@ -1,23 +1,27 @@
+puts "loading game.rb"
+
class Game
-
+
attr_reader :running
attr_reader :maps
+ attr_reader :players
- def initialize(*args)
+ def initialize(players)
+ @players = Array.new(players)
@running = false
@maps = []
end
-
- def run
- @running = true
- while @running
- break
- end
+
+ def run(running = true)
+ @running = running
+ begin
+ @running = false
+ end until @running == false
end
-
+
def add_map(map, &block)
yield map if block_given? # yeild to make so stuff can be added?
@maps.push(map)
end
-
+
end
View
12 lib/map.rb
@@ -1,9 +1,11 @@
+puts "loading map.rb"
+
# maps contain layers of matrices
# units
# terrain
class Map
-
+
attr_reader :units
attr_reader :terrain
attr_reader :width, :height
@@ -14,22 +16,22 @@ def initialize(map_name, &block)
else
rows = File.open(File.dirname(__FILE__) + "/../maps/#{map_name}.txt", "r").read
end
-
+
rows = rows.split("\n")
rows.collect! { |row| row.gsub(/\s+/, '').split(//) }
@height = rows.size
@width = rows[0].size
@terrain = Matrix.new(@width, @height)
@units = Matrix.new(@width, @height)
rows.each_with_index do |row, y|
- row.each_with_index do |glyph, x|
+ row.each_with_index do |glyph, x|
@terrain[x, y] = Object.const_get( CONFIG[glyph] + "Terrain" ).new( CONFIG[glyph] )
end
end
end
-
+
def place(x, y, unit)
@units[x, y] = unit
end
-
+
end
View
15 lib/matrix.rb
@@ -1,5 +1,6 @@
+puts "loading matrix.rb"
-# matrix = Matrix.new(2, 2)
+# matrix = Matrix.new(2, 2)
# p matrix.all_positions #=> [[0, 0], [1, 0], [0, 1], [1, 1]]
class Matrix
@@ -11,30 +12,30 @@ def initialize(cols, rows)
@data[y] = Array.new(cols)
end
end
-
+
def [](x, y)
@data[y][x]
end
-
+
def []=(x, y, value)
@data[y][x] = value
end
-
+
# def all_positions
# (0...@rows).collect do |y|
# (0...@cols).collect do |x|
# [x,y]
# end
# end.inject([]) { |a, b| a.concat b }
# end
-
+
def to_s
- str = @data.inject('') do |result, row|
+ str = @data.inject('') do |result, row|
result + row.collect {|x| x.to_s }.join(' ') + "\n"
end
puts "\n#{str}"
end
-
+
# def rep
# @data.collect do |row|
# row.collect do |item|
View
32 spec/game_spec.rb
@@ -1,31 +1,43 @@
require File.expand_path(File.join('.', 'spec_helper'), File.dirname(__FILE__))
describe Game do
-
+
before(:each) do
- @game = Game.new
+ @game = Game.new(2)
+ end
+
+ it "should not run on its own" do
+ @game.running.should == false
+ end
+
+ it "should have room for 2 players" do
+ @game.players.should be_an_instance_of(Array)
+ @game.players.length.should == 2
+ @game.players[0].should == nil
+ @game.players[1].should == nil
end
- it "should be running" do
- @game.run
- @game.running.should == true
+ it "should run only once when told to" do
+ @game.running.should == false
+ @game.run(false)
+ @game.running.should == false
end
-
+
context "with maps" do
-
+
before(:each) do
@map = Map.new('001')
end
-
+
it "should have a map" do
@game.add_map(@map)
# @game.add_map(@map) do |map|
# map.place(0, 5, @player)
# end
@game.maps[0].should be_an_instance_of( Map )
- @game.maps.should include( @map )
+ @game.maps.should include( @map )
end
-
+
end
end
View
2 spec/spec.opts
@@ -0,0 +1,2 @@
+--colour
+--format nested
View
1 spec/spec_helper.rb
@@ -1,3 +1,4 @@
+puts "loading spec_helper.rb"
require "rubygems"
require "spec"
require "pp"

0 comments on commit 53d041e

Please sign in to comment.