Guest User

Untitled

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