Skip to content

Commit

Permalink
Basic movements.
Browse files Browse the repository at this point in the history
  • Loading branch information
tooky committed Jun 13, 2012
1 parent 2a242fd commit 1b39af2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
16 changes: 12 additions & 4 deletions games/maze/lib/maze/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ def self.load(path)

def initialize(blueprint, player1_start, player2_start)
@blueprint = blueprint.split("\n").map { |row| row.scan(/./) }
@player1_position = player1_start
@player2_position = player2_start
@positions = { '1' => player1_start, '2' => player2_start }
end

def state
state = @blueprint.dup
state[@player1_position.first][@player1_position.last] = '1'
state[@player2_position.first][@player2_position.last] = '2'
state[@positions['1'].first][@positions['1'].last] = '1'
state[@positions['2'].first][@positions['2'].last] = '2'
state.map(&:join).join("\n")
end

def move(player, direction)
axis = ['E', 'W'].include?(direction) ? 1 : 0
movement = ['N', 'W'].include?(direction) ? -1 : 1

@positions[player][axis] += movement
return nil
end

end
49 changes: 42 additions & 7 deletions games/maze/spec/maze/map_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,54 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Map do
let(:blueprint) {
"*****\n" +
"*...*\n" +
"*...*\n" +
"**F**"
}
let(:map) { Map.new(blueprint, [2,1], [1,3]) }

it 'places players in their starting positions' do
blueprint = "*****\n" +
"*...*\n" +
"*...*\n" +
"**F**"
player1_start = [1,1]
player2_start = [1,3]
map.state.should == "*****\n" +
"*..2*\n" +
"*1..*\n" +
"**F**"
end

map = Map.new(blueprint, player1_start, player2_start)
it 'allows a player to move east to a valid space' do
map.move('1', 'E')

map.state.should == "*****\n" +
"*..2*\n" +
"*.1.*\n" +
"**F**"
end

it 'allows a player to move north to a valid space' do
map.move('1', 'N')

map.state.should == "*****\n" +
"*1.2*\n" +
"*...*\n" +
"**F**"
end

it 'allows a player to move south to a valid space' do
map.move('2', 'S')

map.state.should == "*****\n" +
"*...*\n" +
"*1.2*\n" +
"**F**"
end

it 'allows a player to move west to a valid space' do
map.move('2', 'W')

map.state.should == "*****\n" +
"*.2.*\n" +
"*1..*\n" +
"**F**"
end
end

0 comments on commit 1b39af2

Please sign in to comment.