{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Project 7 Solution

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

This preview shows pages 1–7. Sign up to view the full content.

// 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

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

View Full Document
// 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;
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();

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

View Full Document
// 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;
} 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;

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

View Full Document
m_col = c; m_age = 0;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern