Project 7 Solution

Project 7 Solution - / robots.cpp #include ...

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

View Full Document Right Arrow Icon
// robots.cpp #include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; /////////////////////////////////////////////////////////// //////////////// // Manifest constants /////////////////////////////////////////////////////////// //////////////// const int MAXROWS = 20; // max number of rows in the arena const int MAXCOLS = 40; // max number of columns in the arena const int MAXROBOTS = 200; // max number of robots allowed const int UP = 0; const int DOWN = 1; const int LEFT = 2; const int RIGHT = 3; /////////////////////////////////////////////////////////// //////////////// // Auxiliary function declarations /////////////////////////////////////////////////////////// //////////////// int decodeDirection(char dir); void clearScreen(); /////////////////////////////////////////////////////////// //////////////// // Type definitions /////////////////////////////////////////////////////////// //////////////// class Arena; // This is needed to let the compiler know that Arena is a
Background image of page 1

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

View Full DocumentRight Arrow Icon
// type name, since it's mentioned in the Robot declaration. class Robot { public: // Constructor Robot(Arena* ap, int r, int c); // Accessors int row() const; int col() const; // Mutators void move(); private: Arena* m_arena; int m_row; int m_col; }; class Player { public: // Constructor Player(Arena *ap, int r, int c); // Accessors int row() const; int col() const; int age() const; bool isDead() const; // Mutators string takeComputerChosenTurn(); void stand(); void move(int dir); bool shoot(int dir); void setDead(); private: Arena* m_arena;
Background image of page 2
int m_row; int m_col; int m_age; bool m_dead; int computeDanger(int r, int c) const; }; class Arena { public: // Constructor/destructor Arena(int nRows, int nCols); ~Arena(); // Accessors int rows() const; int cols() const; Player* player() const; int robotCount() const; int nRobotsAt(int r, int c) const; void display(string msg) const; // Mutators bool addRobot(int r, int c); bool addPlayer(int r, int c); bool destroyRobot(int r, int c); bool moveRobots(); private: int m_rows; int m_cols; Player* m_player; Robot* m_robots[MAXROBOTS]; int m_nRobots; }; class Game { public: // Constructor/destructor Game(int rows, int cols, int nRobots); ~Game();
Background image of page 3

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

View Full DocumentRight Arrow Icon
// Mutators void play(); private: Arena* m_arena; }; /////////////////////////////////////////////////////////// //////////////// // Robot implementation /////////////////////////////////////////////////////////// //////////////// Robot::Robot(Arena* ap, int r, int c) { if (ap == NULL) { cout << "***** A robot must be in some Arena!" << endl; exit(1); } if (r < 0 || r >= ap->rows() || c < 0 || c >= ap- >cols()) { cout << "***** Robot created with invalid coordinates (" << r << "," << c << ")!" << endl; exit(1); } m_arena = ap; m_row = r; m_col = c; } int Robot::row() const { return m_row; } int Robot::col() const { return m_col;
Background image of page 4
} void Robot::move() { // Attempt to move in a random direction; if we can't move, don't move switch (rand() % 4) { case UP: if (m_row > 0)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

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

Page1 / 19

Project 7 Solution - / robots.cpp #include ...

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

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