daily pastebin goal
64%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // robots.cpp
  2.  
  3. // Portions you are to complete are marked with a TODO: comment.
  4. // We've provided some incorrect return statements (so indicated) just
  5. // to allow this skeleton program to compile and run, albeit incorrectly.
  6. // The first thing you probably want to do is implement the utterly trivial
  7. // functions (marked TRIVIAL).  Then get Arena::display going.  That gives
  8. // you more flexibility in the order you tackle the rest of the functionality.
  9. // As you finish implementing each TODO: item, remove its TODO: comment.
  10.  
  11. #include <iostream>
  12. #include <string>
  13. #include <algorithm>
  14. #include <cstdlib>
  15. #include <cctype>
  16. #include <cassert>
  17. using namespace std;
  18.  
  19. ///////////////////////////////////////////////////////////////////////////
  20. // Manifest constants
  21. ///////////////////////////////////////////////////////////////////////////
  22.  
  23. const int MAXROWS = 20;             // max number of rows in the arena
  24. const int MAXCOLS = 20;             // max number of columns in the arena
  25. const int MAXROBOTS = 100;          // max number of robots allowed
  26. const int MAXCHANNELS = 3;          // max number of channels
  27. const double WALL_DENSITY = 0.11;   // density of walls
  28.  
  29. const int NORTH = 0;
  30. const int EAST  = 1;
  31. const int SOUTH = 2;
  32. const int WEST  = 3;
  33. const int NUMDIRS = 4;
  34.  
  35. const int EMPTY = 0;
  36. const int WALL  = 1;
  37.  
  38. ///////////////////////////////////////////////////////////////////////////
  39. // Type definitions
  40. ///////////////////////////////////////////////////////////////////////////
  41.  
  42. class Arena;  // This is needed to let the compiler know that Arena is a
  43.               // type name, since it's mentioned in the Robot declaration.
  44.  
  45. class Robot
  46. {
  47.   public:
  48.         // Constructor
  49.     Robot(Arena* ap, int r, int c, int channel);
  50.  
  51.         // Accessors
  52.     int  row() const;
  53.     int  col() const;
  54.     int  channel() const;
  55.     bool isDead() const;
  56.  
  57.         // Mutators
  58.     void forceMove(int dir);
  59.     void move();
  60.  
  61.   private:
  62.     Arena* m_arena;
  63.     int    m_row;
  64.     int    m_col;
  65.     int    m_channel;
  66.     int    m_health;
  67. };
  68.  
  69. class Player
  70. {
  71.   public:
  72.         // Constructor
  73.     Player(Arena *ap, int r, int c);
  74.  
  75.         // Accessors
  76.     int  row() const;
  77.     int  col() const;
  78.     bool isDead() const;
  79.  
  80.         // Mutators
  81.     string stand();
  82.     string move(int dir);
  83.     void   setDead();
  84.  
  85.   private:
  86.     Arena* m_arena;
  87.     int    m_row;
  88.     int    m_col;
  89.     bool   m_dead;
  90. };
  91.  
  92. class Arena
  93. {
  94.   public:
  95.         // Constructor/destructor
  96.     Arena(int nRows, int nCols);
  97.     ~Arena();
  98.  
  99.         // Accessors
  100.     int     rows() const;
  101.     int     cols() const;
  102.     Player* player() const;
  103.     int     robotCount() const;
  104.     int     getCellStatus(int r, int c) const;
  105.     int     numberOfRobotsAt(int r, int c) const;
  106.     void    display(string msg) const;
  107.  
  108.         // Mutators
  109.     void   setCellStatus(int r, int c, int status);
  110.     bool   addRobot(int r, int c, int channel);
  111.     bool   addPlayer(int r, int c);
  112.     string moveRobots(int channel, int dir);
  113.  
  114.   private:
  115.     int     m_grid[MAXROWS][MAXCOLS];
  116.     int     m_rows;
  117.     int     m_cols;
  118.     Player* m_player;
  119.     Robot*  m_robots[MAXROBOTS];
  120.     int     m_nRobots;
  121.  
  122.         // Helper functions
  123.     void checkPos(int r, int c) const;
  124. };
  125.  
  126. class Game
  127. {
  128.   public:
  129.         // Constructor/destructor
  130.     Game(int rows, int cols, int nRobots);
  131.     ~Game();
  132.  
  133.         // Mutators
  134.     void play();
  135.  
  136.   private:
  137.     Arena* m_arena;
  138.  
  139.         // Helper functions
  140.     string takePlayerTurn();
  141.     string takeRobotsTurn();
  142. };
  143.  
  144. ///////////////////////////////////////////////////////////////////////////
  145. //  Auxiliary function declarations
  146. ///////////////////////////////////////////////////////////////////////////
  147.  
  148. int randInt(int lowest, int highest);
  149. bool charToDir(char ch, int& dir);
  150. bool attemptMove(const Arena& a, int dir, int& r, int& c);
  151. bool recommendMove(const Arena& a, int r, int c, int& bestDir);
  152. void clearScreen();
  153. int countSurroundRobot(const Arena& a, int r, int c);
  154. ///////////////////////////////////////////////////////////////////////////
  155. //  Robot implementation
  156. ///////////////////////////////////////////////////////////////////////////
  157.  
  158. Robot::Robot(Arena* ap, int r, int c, int channel)
  159. {
  160.     if (ap == NULL)
  161.     {
  162.         cout << "***** A robot must be created in some Arena!" << endl;
  163.         exit(1);
  164.     }
  165.     if (r < 1  ||  r > ap->rows()  ||  c < 1  ||  c > ap->cols())
  166.     {
  167.         cout << "***** Robot created with invalid coordinates (" << r << ","
  168.              << c << ")!" << endl;
  169.         exit(1);
  170.     }
  171.     if (channel < 1  ||  channel > MAXCHANNELS)
  172.     {
  173.         cout << "***** Robot created with invalid channel " << channel << endl;
  174.         exit(1);
  175.     }
  176.     m_arena = ap;
  177.     m_row = r;
  178.     m_col = c;
  179.     m_channel = channel;
  180.     m_health = 3;
  181. }
  182.  
  183. int Robot::row() const
  184. {
  185.     return m_row;
  186. }
  187.  
  188. int Robot::col() const
  189. {
  190.  
  191.     return m_col;
  192. }
  193.  
  194. int Robot::channel() const
  195. {
  196.     return m_channel;
  197. }
  198.  
  199. bool Robot::isDead() const
  200. {
  201.     return (m_health<=0);
  202. }
  203.  
  204. void Robot::forceMove(int dir)
  205. {
  206.     if (!isDead())
  207.     {
  208.         if(!attemptMove(*m_arena,dir,m_row,m_col))
  209.             m_health--;
  210.     }
  211. }
  212.  
  213. void Robot::move()
  214. {
  215.       // Attempt to move in a random direction; if we can't move, don't move
  216.     if (!isDead())
  217.         attemptMove(*m_arena, randInt(0, NUMDIRS-1), m_row, m_col);
  218. }
  219.  
  220. ///////////////////////////////////////////////////////////////////////////
  221. //  Player implementation
  222. ///////////////////////////////////////////////////////////////////////////
  223.  
  224. Player::Player(Arena* ap, int r, int c)
  225. {
  226.     if (ap == NULL)
  227.     {
  228.         cout << "***** The player must be created in some Arena!" << endl;
  229.         exit(1);
  230.     }
  231.     if (r < 1  ||  r > ap->rows()  ||  c < 1  ||  c > ap->cols())
  232.     {
  233.         cout << "**** Player created with invalid coordinates (" << r
  234.              << "," << c << ")!" << endl;
  235.         exit(1);
  236.     }
  237.     m_arena = ap;
  238.     m_row = r;
  239.     m_col = c;
  240.     m_dead = false;
  241. }
  242.  
  243. int Player::row() const
  244. {
  245.     return m_row;
  246. }
  247.  
  248. int Player::col() const
  249. {
  250.     return m_col;
  251. }
  252.  
  253. string Player::stand()
  254. {
  255.     return "Player stands.";
  256. }
  257.  
  258. string Player::move(int dir)
  259. {
  260.     if(!attemptMove(*m_arena,dir,m_row,m_col))
  261.         return "Player couldn't move; player stands.";
  262.     else if (m_arena->numberOfRobotsAt(m_row, m_col)!=0)
  263.         return "Player walked into a robot and died.";
  264.     else
  265.     {
  266.         switch (dir)
  267.         {
  268.         case 0:
  269.             return "Player moved north.";
  270.             break;
  271.         case 1:
  272.             return "Player moved east.";
  273.             break;
  274.         case 2:
  275.             return "Player moved south.";
  276.             break;
  277.         case 3:
  278.             return "Player moved west.";
  279.             break;
  280.         default:
  281.             return "";
  282.             break;
  283.         }
  284.     }
  285. }
  286.    
  287.  
  288. bool Player::isDead() const
  289. {
  290.     return m_dead;
  291. }
  292.  
  293. void Player::setDead()
  294. {
  295.     m_dead = true;
  296. }
  297.  
  298. ///////////////////////////////////////////////////////////////////////////
  299. //  Arena implementation
  300. ///////////////////////////////////////////////////////////////////////////
  301.  
  302. Arena::Arena(int nRows, int nCols)
  303. {
  304.     if (nRows <= 0  ||  nCols <= 0  ||  nRows > MAXROWS  ||  nCols > MAXCOLS)
  305.     {
  306.         cout << "***** Arena created with invalid size " << nRows << " by "
  307.              << nCols << "!" << endl;
  308.         exit(1);
  309.     }
  310.     m_rows = nRows;
  311.     m_cols = nCols;
  312.     m_player = NULL;
  313.     m_nRobots = 0;
  314.     for (int r = 1; r <= m_rows; r++)
  315.         for (int c = 1; c <= m_cols; c++)
  316.             setCellStatus(r, c, EMPTY);
  317. }
  318.  
  319. Arena::~Arena()
  320. {
  321.     // TODO:  release the player and all remaining dynamically allocated robots
  322.     for(int i =0; i<m_nRobots;i++)
  323.     {
  324.         delete m_robots[i];
  325.     }
  326.     delete m_player;
  327. }
  328.  
  329. int Arena::rows() const
  330. {
  331.     return m_rows;
  332. }
  333.  
  334. int Arena::cols() const
  335. {
  336.     return m_cols;
  337. }
  338.  
  339. Player* Arena::player() const
  340. {
  341.     return m_player;
  342. }
  343.  
  344. int Arena::robotCount() const
  345. {
  346.     return m_nRobots;
  347. }
  348.  
  349. int Arena::getCellStatus(int r, int c) const
  350. {
  351.     checkPos(r, c);
  352.     return m_grid[r-1][c-1];
  353. }
  354.  
  355. int Arena::numberOfRobotsAt(int r, int c) const
  356. {
  357.     int count=0;
  358.     for(int i = 0; i <m_nRobots; i ++)
  359.     {
  360.         if(m_robots[i]!=NULL&&!m_robots[i]->isDead()&&m_robots[i]->col()==c&&m_robots[i]->row()==c)
  361.         {
  362.             count++;
  363.         }
  364.  
  365.     }
  366.     return count;
  367. }
  368.  
  369. void Arena::display(string msg) const
  370. {
  371.     char displayGrid[MAXROWS][MAXCOLS];
  372.     int r, c;
  373.    
  374.       // Fill displayGrid with dots (empty) and stars (wall)
  375.     for (r = 1; r <= rows(); r++)
  376.         for (c = 1; c <= cols(); c++)
  377.             displayGrid[r-1][c-1] = (getCellStatus(r,c) == EMPTY ? '.' : '*');
  378.  
  379.       // Indicate robot positions by their channels.  If more than one robot
  380.       // occupies a cell, show just one (any one will do).
  381.  
  382.       // TODO:  For each robot, set the cell to the digit character
  383.       //        representing the channel number.
  384.     for(int i = 0; i<m_nRobots;i++)
  385.     {
  386.         if(m_robots[i]!=NULL)
  387.         displayGrid[m_robots[i]->row()-1][m_robots[i]->col()-1]='0'+m_robots[i]->channel();
  388.     }
  389.       // Indicate player's position
  390.     if (m_player != NULL)
  391.       displayGrid[m_player->row()-1][m_player->col()-1] = (m_player->isDead() ? 'X' : '@');
  392.  
  393.       // Draw the grid
  394.     clearScreen();
  395.     for (r = 1; r <= rows(); r++)
  396.     {
  397.         for (c = 1; c <= cols(); c++)
  398.             cout << displayGrid[r-1][c-1];
  399.         cout << endl;
  400.     }
  401.     cout << endl;
  402.  
  403.       // Write message, robot, and player info
  404.     if (msg != "")
  405.         cout << msg << endl;
  406.     cout << "There are " << robotCount() << " robots remaining." << endl;
  407.     if (m_player == NULL)
  408.         cout << "There is no player!" << endl;
  409.     else if (m_player->isDead())
  410.         cout << "The player is dead." << endl;
  411. }
  412.  
  413. void Arena::setCellStatus(int r, int c, int status)
  414. {
  415.     checkPos(r, c);
  416.     m_grid[r-1][c-1] = status;
  417. }
  418.  
  419. bool Arena::addRobot(int r, int c, int channel)
  420. {
  421.     if (m_nRobots == MAXROBOTS)
  422.          return false;
  423.     m_robots[m_nRobots] = new Robot(this, r, c, channel);
  424.     m_nRobots++;
  425.     return true;
  426. }
  427.  
  428. bool Arena::addPlayer(int r, int c)
  429. {
  430.     if (m_player != NULL)
  431.         return false;
  432.     m_player = new Player(this, r, c);
  433.     return true;
  434. }
  435.  
  436. string Arena::moveRobots(int channel, int dir)
  437. {
  438.       // Robots on the channel will respond with probability 1/2
  439.     bool willRespond = (randInt(0, 1) == 0);
  440.  
  441.       // Move all robots
  442.     int nRobotsOriginally = m_nRobots;
  443.     for(int i =0; i<m_nRobots;i++)
  444.     {
  445.         if(m_robots[i]->channel()!=channel)
  446.             m_robots[i]->move();
  447.         else if(willRespond)
  448.             m_robots[i]->forceMove(dir);
  449.         else
  450.             m_robots[i]->move();
  451.         if(m_robots[i]->isDead())
  452.             {
  453.                 nRobotsOriginally --;
  454.                 delete m_robots[i];
  455.                 m_robots[i]=NULL;
  456.             }
  457.         if(m_player->col()==m_robots[i]->col()&&m_player->row()==m_robots[i]->row())
  458.             m_player->setDead();
  459.     }
  460.       // TODO:  Move each robot.  Force robots listening on the channel
  461.       //        to move in the indicated direction if willRespond is true.
  462.       //        If willRespond is false, or if the robot listens on a
  463.       //        different channel, it just moves.  Mark the player as dead
  464.       //        if necessary.  Release any dead dynamically allocated robot.
  465.  
  466.     if (m_nRobots < nRobotsOriginally)
  467.         return "Some robots have been destroyed.";
  468.     else
  469.         return "No robots were destroyed.";
  470. }
  471.  
  472. void Arena::checkPos(int r, int c) const
  473. {
  474.     if (r < 1  ||  r > m_rows  ||  c < 1  ||  c > m_cols)
  475.     {
  476.         cout << "***** " << "Invalid arena position (" << r << ","
  477.              << c << ")" << endl;
  478.         exit(1);
  479.     }
  480. }
  481.  
  482. ///////////////////////////////////////////////////////////////////////////
  483. //  Game implementation
  484. ///////////////////////////////////////////////////////////////////////////
  485.  
  486. Game::Game(int rows, int cols, int nRobots)
  487. {
  488.     if (nRobots < 0  ||  nRobots > MAXROBOTS)
  489.     {
  490.         cout << "***** Game created with invalid number of robots:  "
  491.              << nRobots << endl;
  492.         exit(1);
  493.     }
  494.     int nEmpty = rows * cols - nRobots - 1;  // 1 for Player
  495.     if (nEmpty < 0)
  496.     {
  497.         cout << "***** Game created with a " << rows << " by "
  498.              << cols << " arena, which is too small too hold a player and "
  499.              << nRobots << " robots!" << endl;
  500.         exit(1);
  501.     }
  502.  
  503.         // Create arena
  504.     m_arena = new Arena(rows, cols);
  505.  
  506.         // Add some walls in WALL_DENSITY of the empty spots
  507.     assert(WALL_DENSITY >= 0  &&  WALL_DENSITY <= 1);
  508.     int nWalls = static_cast<int>(WALL_DENSITY * nEmpty);
  509.     while (nWalls > 0)
  510.     {
  511.         int r = randInt(1, rows);
  512.         int c = randInt(1, cols);
  513.         if (m_arena->getCellStatus(r, c) == WALL)
  514.             continue;
  515.         m_arena->setCellStatus(r, c, WALL);
  516.         nWalls--;
  517.     }
  518.  
  519.         // Add player
  520.     int rPlayer;
  521.     int cPlayer;
  522.     do
  523.     {
  524.         rPlayer = randInt(1, rows);
  525.         cPlayer = randInt(1, cols);
  526.     } while (m_arena->getCellStatus(rPlayer, cPlayer) != EMPTY);
  527.     m_arena->addPlayer(rPlayer, cPlayer);
  528.  
  529.       // Populate with robots
  530.     while (nRobots > 0)
  531.     {
  532.         int r = randInt(1, rows);
  533.         int c = randInt(1, cols);
  534.         if (m_arena->getCellStatus(r,c) != EMPTY  ||  (r == rPlayer && c == cPlayer))
  535.             continue;
  536.         m_arena->addRobot(r, c, randInt(1, MAXCHANNELS));
  537.         nRobots--;
  538.     }
  539. }
  540.  
  541. Game::~Game()
  542. {
  543.     delete m_arena;
  544. }
  545.  
  546. string Game::takePlayerTurn()
  547. {
  548.     for (;;)
  549.     {
  550.         cout << "Your move (n/e/s/w/x or nothing): ";
  551.         string playerMove;
  552.         getline(cin, playerMove);
  553.  
  554.         Player* player = m_arena->player();
  555.         int dir;
  556.  
  557.         if (playerMove.size() == 0)
  558.         {
  559.             if (recommendMove(*m_arena, player->row(), player->col(), dir))
  560.                 return player->move(dir);
  561.             else
  562.                 return player->stand();
  563.         }
  564.         else if (playerMove.size() == 1)
  565.         {
  566.             if (tolower(playerMove[0]) == 'x')
  567.                 return player->stand();
  568.             else if (charToDir(playerMove[0], dir))
  569.                 return player->move(dir);
  570.         }
  571.         cout << "Player move must be nothing, or 1 character n/e/s/w/x." << endl;
  572.     }
  573. }
  574.  
  575. string Game::takeRobotsTurn()
  576. {
  577.     for (;;)
  578.     {
  579.         cout << "Broadcast (e.g., 2n): ";
  580.         string broadcast;
  581.         getline(cin, broadcast);
  582.         if (broadcast.size() != 2)
  583.             cout << "Broadcast must be channel followed by direction." << endl;
  584.         else if (broadcast[0] < '1'  ||  broadcast[0] > '0'+MAXCHANNELS)
  585.             cout << "Channel must be a valid digit." << endl;
  586.         else
  587.         {
  588.             int dir;
  589.             if (charToDir(broadcast[1], dir))
  590.                 return m_arena->moveRobots(broadcast[0]-'0', dir);
  591.             else
  592.                 cout << "Direction must be n, e, s, or w." << endl;
  593.         }
  594.     }
  595. }
  596.  
  597. void Game::play()
  598. {
  599.     m_arena->display("");
  600.     while ( ! m_arena->player()->isDead()  &&  m_arena->robotCount() > 0)
  601.     {
  602.         string msg = takePlayerTurn();
  603.         m_arena->display(msg);
  604.         Player* player = m_arena->player();
  605.         if (player->isDead())
  606.             break;
  607.         msg = takeRobotsTurn();
  608.         m_arena->display(msg);
  609.     }
  610.     if (m_arena->player()->isDead())
  611.         cout << "You lose." << endl;
  612.     else
  613.         cout << "You win." << endl;
  614. }
  615.  
  616. ///////////////////////////////////////////////////////////////////////////
  617. //  Auxiliary function implementation
  618. ///////////////////////////////////////////////////////////////////////////
  619.  
  620.   // Return a uniformly distributed random int from lowest to highest, inclusive
  621. int randInt(int lowest, int highest)
  622. {
  623.     if (highest < lowest)
  624.         swap(highest, lowest);
  625.     return lowest + (rand() % (highest - lowest + 1));
  626. }
  627.  
  628. bool charToDir(char ch, int& dir)
  629. {
  630.     switch (tolower(ch))
  631.     {
  632.       default:  return false;
  633.       case 'n': dir = NORTH; break;
  634.       case 'e': dir = EAST;  break;
  635.       case 's': dir = SOUTH; break;
  636.       case 'w': dir = WEST;  break;
  637.     }
  638.     return true;
  639. }
  640.  
  641.   // Return false without changing anything if moving one step from (r,c)
  642.   // in the indicated direction would hit a wall or run off the edge of the
  643.   // arena.  Otherwise, update r and c to the position resulting from the
  644.   // move and return true.
  645. bool attemptMove(const Arena& a, int dir, int& r, int& c)
  646. {
  647.     switch(dir)
  648.         {
  649.         case NORTH:
  650.             r --;
  651.             break;
  652.         case EAST:
  653.             c ++;
  654.             break;
  655.         case SOUTH:
  656.             r ++;
  657.             break;
  658.         case WEST:
  659.             c --;
  660.             break;
  661.         default:
  662.             break;
  663.         }
  664.     if (r<1||r>a.rows()||c<1||c>a.cols()||a.getCellStatus(r, c)==1)
  665.     {
  666.         switch(dir)
  667.         {
  668.         case NORTH:
  669.             r ++;
  670.             break;
  671.         case EAST:
  672.             c --;
  673.             break;
  674.         case SOUTH:
  675.             r --;
  676.             break;
  677.         case WEST:
  678.             c ++;
  679.             break;
  680.         default:
  681.             break;
  682.         }
  683.         return false;
  684.     }
  685.     return true;
  686.       // TODO:  implement this function
  687.       // Delete the following line and replace it with the correct code.
  688.  
  689. }
  690.  
  691.   // Recommend a move for a player at (r,c):  A false return means the
  692.   // recommendation is that the player should stand; otherwise, bestDir is
  693.   // set to the recommended direction to move.
  694. bool recommendMove(const Arena& a, int r, int c, int& bestDir)
  695. {
  696.     int pos =0;
  697.     int score [5]={MAXROBOTS+1,MAXROBOTS+1,MAXROBOTS+1,MAXROBOTS+1,countSurroundRobot(a,r,c)};
  698.     if(r==1&&c==1)
  699.     {
  700.         score[1]=countSurroundRobot(a,r,c+1);
  701.         score[2]=countSurroundRobot(a,r+1,c);
  702.         if(a.numberOfRobotsAt(r,c+1)!=0)
  703.             score[1]=MAXROBOTS+1;
  704.         if(a.numberOfRobotsAt(r+1,c)!=0)
  705.             score[2]=MAXROBOTS+1;
  706.     }
  707.     else if(r==1)
  708.     {
  709.         score[1]=countSurroundRobot(a,r,c+1);
  710.         score[2]=countSurroundRobot(a,r+1,c);
  711.         score[3]=countSurroundRobot(a,r,c-1);
  712.         if(a.numberOfRobotsAt(r,c+1)!=0)
  713.             score[1]=MAXROBOTS+1;
  714.         if(a.numberOfRobotsAt(r+1,c)!=0)
  715.             score[2]=MAXROBOTS+1;
  716.         if(a.numberOfRobotsAt(r,c-1)!=0)
  717.             score[3]=MAXROBOTS+1;
  718.     }
  719.     else if(c==1)
  720.     {
  721.         score[0]=countSurroundRobot(a,r-1,c);
  722.         score[1]=countSurroundRobot(a,r,c+1);
  723.         score[2]=countSurroundRobot(a,r+1,c);
  724.         if(a.numberOfRobotsAt(r-1,c)!=0)
  725.             score[0]=MAXROBOTS+1;
  726.         if(a.numberOfRobotsAt(r,c+1)!=0)
  727.             score[1]=MAXROBOTS+1;
  728.         if(a.numberOfRobotsAt(r+1,c)!=0)
  729.             score[2]=MAXROBOTS+1;
  730.     }
  731.     else if(r==a.rows()&&c==a.cols())
  732.     {
  733.         score[0]=countSurroundRobot(a,r-1,c);
  734.         score[3]=countSurroundRobot(a,r,c-1);
  735.         if(a.numberOfRobotsAt(r-1,c)!=0)
  736.             score[0]=MAXROBOTS+1;
  737.         if(a.numberOfRobotsAt(r,c-1)!=0)
  738.             score[3]=MAXROBOTS+1;
  739.     }
  740.     else if(r==a.rows())
  741.     {
  742.         score[0]=countSurroundRobot(a,r-1,c);
  743.         score[1]=countSurroundRobot(a,r,c+1);
  744.         score[3]=countSurroundRobot(a,r,c-1);
  745.         if(a.numberOfRobotsAt(r-1,c)!=0)
  746.             score[0]=MAXROBOTS+1;
  747.         if(a.numberOfRobotsAt(r,c+1)!=0)
  748.             score[1]=MAXROBOTS+1;
  749.         if(a.numberOfRobotsAt(r,c-1)!=0)
  750.             score[3]=MAXROBOTS+1;
  751.     }
  752.     else if(c==a.cols())
  753.     {
  754.         score[0]=countSurroundRobot(a,r-1,c);
  755.         score[2]=countSurroundRobot(a,r+1,c);
  756.         score[3]=countSurroundRobot(a,r,c-1);
  757.         if(a.numberOfRobotsAt(r-1,c)!=0)
  758.             score[0]=MAXROBOTS+1;
  759.         if(a.numberOfRobotsAt(r+1,c)!=0)
  760.             score[2]=MAXROBOTS+1;
  761.         if(a.numberOfRobotsAt(r,c-1)!=0)
  762.             score[3]=MAXROBOTS+1;
  763.     }
  764.     else
  765.     {
  766.         score[0]=countSurroundRobot(a,r-1,c);
  767.         score[1]=countSurroundRobot(a,r,c+1);
  768.         score[2]=countSurroundRobot(a,r+1,c);
  769.         score[3]=countSurroundRobot(a,r,c-1);
  770.         if(a.numberOfRobotsAt(r-1,c)!=0)
  771.             score[0]=MAXROBOTS+1;
  772.         if(a.numberOfRobotsAt(r,c+1)!=0)
  773.             score[1]=MAXROBOTS+1;
  774.         if(a.numberOfRobotsAt(r+1,c)!=0)
  775.             score[2]=MAXROBOTS+1;
  776.         if(a.numberOfRobotsAt(r,c-1)!=0)
  777.             score[3]=MAXROBOTS+1;
  778.     }
  779.     for(int i =0; i < 5;i++)
  780.     {
  781.         if(score[i]<score[pos])
  782.             pos = i;
  783.     }
  784.     if(pos<4)
  785.     {
  786.         bestDir=pos;
  787.         return true;
  788.     }
  789.     else
  790.         return false;
  791. }
  792. int countSurroundRobot(const Arena& a, int r, int c)
  793. {
  794.     if(r==1&&c==1)
  795.         return (a.numberOfRobotsAt(r+1,c)+a.numberOfRobotsAt(r,c+1));
  796.     else if(r==1)
  797.         return (a.numberOfRobotsAt(r+1,c)+a.numberOfRobotsAt(r,c-1)+a.numberOfRobotsAt(r,c+1));
  798.     else if(c==1)
  799.         return (a.numberOfRobotsAt(r-1,c)+a.numberOfRobotsAt(r+1,c)+a.numberOfRobotsAt(r,c+1));
  800.     else if(r==a.rows()&&c==a.cols())
  801.         return (a.numberOfRobotsAt(r-1,c)+a.numberOfRobotsAt(r,c-1));
  802.     else if(r==a.rows())
  803.         return (a.numberOfRobotsAt(r-1,c)+a.numberOfRobotsAt(r,c-1)+a.numberOfRobotsAt(r,c+1));
  804.     else if(c==a.cols())
  805.         return (a.numberOfRobotsAt(r,c-1)+a.numberOfRobotsAt(r-1,c)+a.numberOfRobotsAt(r+1,c));
  806.     else
  807.         return (a.numberOfRobotsAt(r,c-1)+a.numberOfRobotsAt(r-1,c)+a.numberOfRobotsAt(r+1,c)+a.numberOfRobotsAt(r,c+1));
  808. }
  809. #ifdef _MSC_VER  //  Microsoft Visual C++
  810.  
  811. #include <windows.h>
  812.  
  813. void clearScreen()
  814. {
  815.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  816.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  817.     GetConsoleScreenBufferInfo(hConsole, &csbi);
  818.     DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  819.     COORD upperLeft = { 0, 0 };
  820.     DWORD dwCharsWritten;
  821.     FillConsoleOutputCharacter(hConsole, TCHAR(' '), dwConSize, upperLeft,
  822.                             &dwCharsWritten);
  823.     SetConsoleCursorPosition(hConsole, upperLeft);
  824. }
  825.  
  826. #else  // not Microsoft Visual C++, so assume UNIX interface
  827.  
  828. #include <cstring>
  829.  
  830. void clearScreen()
  831. {
  832.     static const char* term = getenv("TERM");
  833.     static const char* ESC_SEQ = "\x1B[";  // ANSI Terminal esc seq:  ESC [
  834.     if (term == NULL  ||  strcmp(term, "dumb") == 0)
  835.         cout << endl;
  836.      else
  837.         cout << ESC_SEQ << "2J" << ESC_SEQ << "H" << flush;
  838. }
  839.  
  840. #endif
  841.  
  842. ///////////////////////////////////////////////////////////////////////////
  843. // main()
  844. ///////////////////////////////////////////////////////////////////////////
  845.  
  846. #include <cstdlib>
  847. #include <ctime>
  848. using namespace std;
  849.  
  850. int main()
  851. {
  852.       // Initialize the random number generator
  853.     srand(static_cast<unsigned int>(time(0)));
  854.  
  855.       // Create a game
  856.       // Use this instead to create a mini-game:   Game g(3, 5, 2);
  857.     Game g(10, 12, 50);
  858.  
  859.       // Play the game
  860.     g.play();
  861. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top