Search code examples
c++ooppacman

How to code a Map for PacMan in C++


Can anyone plese tell me How do i create a Map for PacMan in C++. Any Suggestions for getting m Started.?

Thanx in Advance.


Solution

  • That's quite a wide question and depends on additional requirements. Basically I would go for a two-dimensional array that will simply represent the cells. Each cell would contain either:

    1. A wall
    2. A one-way wall (to represent the gate through which the monsters go)
    3. An empty cell
    4. A cell with food
    5. A "teleportation cell" that will send the pacman to a different place (to represent the two cells on the left and right sides)

    The monsters and the pacman itself will not be represented in the map. Instead each should just have his position in the map.

    This doesn't require C++, it only requires C, but if you deliberately want a C++ solution you can replace the arrays with a vector, or in this case, a vector of vectors.

    How will the monsters move

    If you want to reach point A from point B in a map, you can use any of a variety of algorithms that will find you the shortest path within the map - like the A* algorithm. However, in this case, you need to take a few things into account:

    1. The pacman itself is always on the move, which changes the path from one step to another.
    2. You don't want all the monsters to have the same path, otherwise you won't really have several monsters, because all of them will move the same.
    3. You don't want the monsters to be extremely effective, because then the game will become too hard and will not be much fun.

    I would suggest using some random decisions when the monster reaches a "crossroad", along with a general bias towards the pacman itself. So if the monster is at a point where there are 3 directions, like East, West and North, and the pacman is to the north, I would make a random decision with 40% chance to the north, 30% to the east and 30% to the west.