{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Project 7 Solution

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

Info icon This preview shows pages 1–7. 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
Image of page 1

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

View Full Document Right 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;
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();
Image of page 3

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

View Full Document Right 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;
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) m_row--; break; case DOWN: if (m_row < m_arena->rows()-1) m_row++; break; case LEFT: if (m_col > 0) m_col--; break; case RIGHT: if (m_col < m_arena->cols()-1) m_col++; break; } } /////////////////////////////////////////////////////////// //////////////// // Player implementations /////////////////////////////////////////////////////////// //////////////// Player::Player(Arena* ap, int r, int c) { if (ap == NULL) { cout << "***** The player must be in some Arena!" << endl; exit(1); } if (r < 0 || r >= ap->rows() || c < 0 || c >= ap- >cols()) { cout << "**** Player created with invalid coordinates (" << r << "," << c << ")!" << endl; exit(1); } m_arena = ap; m_row = r;
Image of page 5

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

View Full Document Right Arrow Icon
m_col = c; m_age = 0;
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern