Skip to content

Instantly share code, notes, and snippets.

@tehviking
Created May 10, 2012 02:36
Show Gist options
  • Save tehviking/2650640 to your computer and use it in GitHub Desktop.
Save tehviking/2650640 to your computer and use it in GitHub Desktop.
Pacman Code Kata for Austin.rb with @timtyrell and @ceez
class GameBoard
attr_accessor :game_grid
def initialize
@game_grid = [%w[. . .], %w[. . .], %w[. . .]]
@pacman = Pacman.new
@pacman_location = [1,1]
end
def place_pacman
game_grid[@pacman_location[0]][@pacman_location[1]] = Pacman::STATES[@pacman.current_direction]
end
def tick(command)
case command
when :up
@pacman.current_direction = :up
game_grid[@pacman_location[0]][@pacman_location[1]] = " "
@pacman_location[0] -= 1
when :down
@pacman.current_direction = :down
game_grid[@pacman_location[0]][@pacman_location[1]] = " "
@pacman_location[0] += 1
end
place_pacman
end
end
class Pacman
attr_accessor :current_direction
STATES = {up: "V", down: "^", left: ">", right: "<" }
def initialize
@current_direction = :up
end
end
describe "grid" do
before(:each) do
@grid = GameBoard.new
end
it "renders a field of dots" do
@grid.game_grid.should == [[".", ".", "."], [".", ".", "."], [".", ".", "."]]
end
describe "#place_pacman" do
it "places a pacman" do
@grid.place_pacman
@grid.game_grid.should == [[".", ".", "."], [".", "V", "."], [".", ".", "."]]
end
end
describe "#tick" do
context "gives command up" do
it "moves pacman north" do
@grid.place_pacman
@grid.tick(:up)
@grid.game_grid.should == [[".", "V", "."], [".", " ", "."], [".", ".", "."]]
end
end
context "gives command up" do
it "moves pacman north" do
@grid.place_pacman
@grid.tick(:down)
@grid.game_grid.should == [[".", ".", "."], [".", " ", "."], [".", "^", "."]]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment