Skip to content

Instantly share code, notes, and snippets.

@raorao
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raorao/9595132 to your computer and use it in GitHub Desktop.
Save raorao/9595132 to your computer and use it in GitHub Desktop.

PacMan!

Given a map, your program should find the shortest path between PacMan and Food.

The map is defined as a two-dimensional array, with the following key:

  • 'o' = open space
  • 'w' = wall
  • 'F' = food
  • 'P' = PacMan

PacMan is allowed to only move up, down, right, or left, and cannot move through walls or past the boundary of the map. It should run like this:

salar = Pacman.new

salar.map = [ ['o','o','o','w','o','o','o','o'],
              ['o','o','o','w','o','o','o','o'],
              ['o','w','o','w','o','w','o','o'],
              ['o','w','o','o','o','w','w','o'],
              ['o','w','o','F','o','w','o','o'],
              ['o','w','o','o','o','w','P','o'],
              ['o','o','o','w','w','w','o','o'],
              ['o','o','o','o','o','o','o','o'] ]

salar.get_food
#=> returns 10

Okay, so this isn't actually PacMan, I'm sorry. But it is an example of depth-first search, which is probably more valuable to you.

Once you have the basic example working, attempt to get it working with Energizers. A new character, 'E', is available on the map. If PacMan eats it, he moves at twice his normal rate.

So given the following map:

map = [ ['o','o','o','o','o','o','o','o'],
        ['o','w','o','o','o','o','w','o'],
        ['o','w','F','o','o','w','E','o'],
        ['o','w','w','w','w','o','o','o'],
        ['o','o','o','o','w','o','o','o'],
        ['o','o','o','o','w','o','o','o'],
        ['o','o','o','o','w','w','o','o'],
        ['o','o','o','o','P','o','o','o'],
        ['o','o','o','o','o','o','o','o'] ]

get_food should return 12.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment