mini_pacman.cpp

mini_pacman.cpp - #include #include ...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <cstring> #include <iostream> #include <cstdlib> using namespace std; /* * CS31 Mini PacMan demo. TA: Paul Wais (pwais@ucla.edu) */ /* We'll use a small fixed grid for this simple demo */ const int NUM_ROWS = 10; const int NUM_COLS = 10; /////////////////////////////////////////////////////////////////////////////// // PacMan /////////////////////////////////////////////////////////////////////////////// /* * A class encapsulating PacMan's location and functions for reading and * modifying his location. */ class PacMan { public: PacMan(); PacMan(int r, int c); // Move pacman in position `d` (e.g. 'u' to move PacMan up) void move(char dir); // Return true iff PacMan is at (r, c) bool isAt(int r, int c) const; // Accessors for getting PacMan's location int getRow() const; int getCol() const; private: int r; int c; }; // // Implementation of PacMan // PacMan::PacMan() :r(0), c(0) // Special "initialization list" syntax { } // for initializing member variables PacMan::PacMan(int r, int c) { this->r = r; this->c = c; } bool PacMan::isAt(int r, int c) const { return this->r == r && this->c == c;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
} int PacMan::getRow() const { return r; } int PacMan::getCol() const { return c; } void PacMan::move(char d) { // First, modify PacMan's location, ignoring boundary conditions switch(d) { case 'u': r -= 1; break; case 'd': r += 1; break; case 'r': c += 1; break; case 'l': c -= 1; break; default: // We usually don't expect these errors, but print a message anyway cout << "Error, can't move in direction: " << d << endl; } // Enforce boundary conditions; make sure PacMan stays in the grid if (r < 0) r += NUM_ROWS; else if (r >= NUM_ROWS) r -= NUM_ROWS; if (c < 0) c += NUM_COLS; else if (c >= NUM_COLS) c -= NUM_COLS; } /////////////////////////////////////////////////////////////////////////////// // Ghost /////////////////////////////////////////////////////////////////////////////// /* * A class encapsulating a Ghost's location, color, and functions for
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/06/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 8

mini_pacman.cpp - #include #include ...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online