Advertisement
Ridwanul_Haque

Wumpus World (C++)

Nov 9th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <sstream>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. #define GRIDSIZE 4
  10. #define UP      0
  11. #define DOWN    2
  12. #define LEFT    3
  13. #define RIGHT   1
  14. #define MAX_SHOTS 3
  15.  
  16.  
  17. string tostring(int x) {
  18.     std::string out_string;
  19.     std::stringstream ss;
  20.     ss << x;
  21.     return ss.str();
  22. }
  23.  
  24.  
  25. class Position {
  26.  
  27.     int x, y;
  28.  
  29. public:
  30.  
  31.     Position (int x, int y) {
  32.         this->x = x;
  33.         this->y = y;
  34.     }
  35.  
  36.     Position() {}
  37.  
  38.     // Modify the following four so that the resulting position does not leave the grid
  39.     void moveRight() {
  40.         if (this->x<3) this->x++;
  41.     }
  42.  
  43.     void moveLeft() {
  44.        if(this->x>0) this->x--;
  45.     }
  46.  
  47.     void moveUp() {
  48.         if (this->y<3) this->y++;
  49.     }
  50.  
  51.     void moveDown() {
  52.         if (this->y>0) this->y--;
  53.     }
  54.  
  55.     bool isAdjacent(Position p) {
  56.         //implement the function
  57.         if ((abs(this->x-p.getX())==1 && abs(this->y-p.getY())==0) || (abs(this->x-p.getX())==0 && abs(this->y-p.getY())==1))
  58.             return true;
  59.         else return false;
  60.     }
  61.  
  62.     bool isSamePoint(Position p) {
  63.         //implement the function
  64.         if ((this->x-p.getX()==0 && this->y-p.getY()==0) || (this->x-p.getX()==0 && this->y-p.getY()==0))
  65.             return true;
  66.         else return false;
  67.     }
  68.  
  69.     int getX() {
  70.         return this->x;
  71.     }
  72.  
  73.     int getY() {
  74.         return this->y;
  75.     }
  76.  
  77. };
  78.  
  79.  
  80. class Wumpus {
  81.     int x_wumpus,y_wumpus;
  82.     bool killed;
  83.     Position p;
  84.  
  85. public:
  86.  
  87.     Wumpus(int x, int y)
  88.      {
  89.         p = Position(x, y);
  90.         x_wumpus = x;
  91.         y_wumpus = y;
  92.         killed = false;
  93.     }
  94.  
  95.     Wumpus() {
  96.  
  97.         killed = false;
  98.  
  99.        do
  100.        {
  101.            x_wumpus = rand()%4;
  102.            y_wumpus = rand()%4;
  103.         }
  104.         while(x_wumpus==0 && y_wumpus==0);
  105.         p = Position(x_wumpus,y_wumpus);
  106.  
  107.     }
  108.  
  109.     void kill() {
  110.         killed = true;
  111.     }
  112.  
  113.     Position getPosition() {
  114.         return p;
  115.     }
  116.  
  117.     int get_Wumpus_X()
  118.     {
  119.         return x_wumpus;
  120.     }
  121.     int get_Wumpus_Y()
  122.     {
  123.         return y_wumpus;
  124.     }
  125.     bool iskilled()
  126.     {
  127.         if (killed == true) return true;
  128.         else return false;
  129.     }
  130.  
  131. };
  132.  
  133. class Pit
  134. {   int x_pit,y_pit;
  135.     Position p;
  136.     Wumpus w;
  137.     bool killed;
  138. public:
  139.  
  140.      Pit(int x, int y)
  141.      {
  142.         p = Position(x, y);
  143.         x_pit = x;
  144.         y_pit = y;
  145.         killed = false;
  146.      }
  147.  
  148.     Pit() {
  149.  
  150.        do
  151.        {
  152.            x_pit = rand()%4;
  153.            y_pit = rand()%4;
  154.         }
  155.         while( (x_pit==0 && y_pit==0) || (x_pit==w.get_Wumpus_X() && y_pit==w.get_Wumpus_Y()) );
  156.  
  157.         p = Position(x_pit,y_pit);
  158.         killed = false;
  159.  
  160.     }
  161.  
  162.  
  163.  
  164.     Position getPosition() {
  165.         return p;
  166.     }
  167.  
  168.     int get_pit_X()
  169.     {
  170.         return x_pit;
  171.     }
  172.     int get_pit_Y()
  173.     {
  174.         return y_pit;
  175.     }
  176.     bool iskilled()
  177.     {
  178.         if(killed==true) return true;
  179.         else return false;
  180.     }
  181. };
  182.  
  183.  
  184. class Player {
  185.     int x_player,y_player;
  186.     int direction;
  187.     int total_shots;
  188.     bool killed;
  189.     Position p;
  190.  
  191. public:
  192.  
  193.     Player() {
  194.          p = Position(0,0);
  195.          killed = false;
  196.          total_shots = MAX_SHOTS;
  197.          direction = UP;
  198.     }
  199.  
  200.     void turnLeft() {
  201.         //...
  202.         if(direction==UP) direction=LEFT;
  203.         else if(direction==RIGHT) direction=UP;
  204.         else if(direction==LEFT) direction=DOWN;
  205.         else if(direction==DOWN) direction=RIGHT;
  206.     }
  207.  
  208.     void turnRight() {
  209.         //...
  210.         if(direction==UP) direction=RIGHT;
  211.         else if(direction==RIGHT) direction=DOWN;
  212.         else if(direction==LEFT) direction=UP;
  213.         else if(direction==DOWN) direction=LEFT;
  214.     }
  215.  
  216.     void moveForward() {
  217.         //...
  218.         if(direction==UP) p.moveUp();
  219.         if(direction==DOWN) p.moveDown();
  220.         if(direction==LEFT) p.moveLeft();
  221.         if(direction==RIGHT) p.moveRight();
  222.     }
  223.  
  224.     bool isAdjacent(Position pos) {
  225.         return p.isAdjacent(pos);
  226.     }
  227.  
  228.     bool isSamePoint(Position pos) {
  229.         return p.isSamePoint(pos);
  230.     }
  231.  
  232.     void kill() {
  233.         killed = true;
  234.     }
  235.  
  236.     string getPositionInfo() {
  237.         return "\nPlayer is now at " + tostring(p.getX()) + ", " + tostring(p.getY());
  238.     }
  239.  
  240.     string getDirectionInfo() {
  241.         string s;
  242.         if (direction == UP) s = "up";
  243.         if (direction == DOWN) s = "down";
  244.         if (direction == LEFT) s = "left";
  245.         if (direction == RIGHT) s = "right";
  246.         return "Player is moving at direction: " + s;
  247.     }
  248.  
  249.     int getDirection()
  250.     {
  251.         return direction;
  252.     }
  253.     int check_total_shots()
  254.     {
  255.         return total_shots;
  256.     }
  257.  
  258.     int get_player_x()
  259.     {
  260.         return p.getX();
  261.     }
  262.         int get_player_y()
  263.     {
  264.         return p.getY();
  265.     }
  266.  
  267.  
  268.     void shot_decrease()
  269.     {
  270.         total_shots--;
  271.     }
  272.  
  273.  
  274. };
  275.  
  276.  
  277.  
  278. class Gold
  279. {
  280.     int x_gold,y_gold;
  281. public:
  282.     Gold()
  283.     {
  284.         goldsetter_x();
  285.         goldsetter_y();
  286.         x_gold = goldGetter_x();
  287.         y_gold = goldGetter_y();
  288.     }
  289.  
  290.     Gold(int x,int y)
  291.     {
  292.  
  293.         x_gold = x;
  294.         y_gold = y;
  295.     }
  296.     void goldsetter_x()
  297.     {
  298.         x_gold = rand()%4;
  299.  
  300.     }
  301.  
  302.     void goldsetter_y()
  303.     {
  304.         y_gold = rand()%4;
  305.  
  306.     }
  307.  
  308.     int goldGetter_x()
  309.     {
  310.         return x_gold;
  311.     }
  312.     int goldGetter_y()
  313.     {
  314.         return y_gold;
  315.     }
  316.  
  317. };
  318.  
  319.  
  320.  
  321. class WumpusWorld {
  322.  
  323. private:
  324.  
  325.     Player player;
  326.     Wumpus wumpus;
  327.     Pit pit;
  328.     Gold gold;
  329.     Position gold_position;
  330.     Position position;
  331.     Position shooter;
  332.     bool ended;
  333.  
  334.  
  335. public:
  336.  
  337.   WumpusWorld() {
  338.         //...
  339.         ended = false;
  340.         player = Player();
  341.         wumpus = Wumpus();
  342.         pit = Pit();
  343.         do
  344.         {
  345.             gold.goldsetter_x();
  346.             gold.goldsetter_y();
  347.  
  348.         }
  349.         while ((gold.goldGetter_x()==pit.get_pit_X() && gold.goldGetter_y()==pit.get_pit_Y()) || (gold.goldGetter_x()==0 && gold.goldGetter_y()==0) || (gold.goldGetter_x()==wumpus.get_Wumpus_X() && gold.goldGetter_y()==wumpus.get_Wumpus_Y()));       ///debug
  350.  
  351.                                                                                    ///debug
  352.  
  353.         gold_position = Position(gold.goldGetter_x(),gold.goldGetter_y());
  354.  
  355.     }
  356.  
  357.     WumpusWorld(int wumpus_x, int wumpus_y) {
  358.         //...
  359.         ended = false;
  360.         player = Player();
  361.        wumpus = Wumpus(wumpus_x,wumpus_y);
  362.         pit = Pit();
  363.         do
  364.         {
  365.             gold.goldsetter_x();
  366.             gold.goldsetter_y();
  367.         }
  368.  while ((gold.goldGetter_x()==pit.get_pit_X() && gold.goldGetter_y()==pit.get_pit_Y()) || (gold.goldGetter_x()==0 && gold.goldGetter_y()==0) || (gold.goldGetter_x()==wumpus.get_Wumpus_X() && gold.goldGetter_y()==wumpus.get_Wumpus_Y()));   ///debug
  369.  
  370.  
  371.         gold_position = Position(gold.goldGetter_x(),gold.goldGetter_y());
  372.     }
  373.  
  374.     WumpusWorld(int wumpus_x, int wumpus_y, int gold_x, int gold_y) {
  375.         //...
  376.         ended = false;
  377.         player = Player();
  378.         wumpus = Wumpus(wumpus_x,wumpus_y);
  379.         pit = Pit();
  380.         gold_position = Position(gold_x,gold_y);
  381.  
  382.     }
  383.  
  384.     WumpusWorld(int wumpus_x, int wumpus_y, int gold_x, int gold_y, int pit_x, int pit_y) {
  385.         //...
  386.  
  387.         ended = false;
  388.         player = Player();
  389.         wumpus = Wumpus(wumpus_x,wumpus_y);
  390.         pit = Pit(pit_x,pit_y);
  391.         gold_position = Position(gold_x,gold_y);
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.     }
  400.  
  401.     void moveForward() {
  402.         player.moveForward();
  403.         return showGameState();
  404.     }
  405.  
  406.     void turnLeft() {
  407.         player.turnLeft();
  408.         return showGameState();
  409.     }
  410.  
  411.     void turnRight() {
  412.         player.turnRight();
  413.         return showGameState();
  414.     }
  415.  
  416.     void shoot() {
  417.                                     ///SHOOOOTTTTT
  418.         //...
  419.  
  420.         if (player.check_total_shots()>0)
  421.         {
  422.             player.shot_decrease();
  423.             if (player.getDirection()==UP)
  424.         {
  425.             if(abs(wumpus.get_Wumpus_Y()-player.get_player_y())==1 && wumpus.get_Wumpus_X()==player.get_player_x())
  426.             {
  427.                 cout<<"\n Auuuuccchhh!   \"Wumpus is dead \"  "<<endl;
  428.                 wumpus.kill();
  429.             }
  430.         }
  431.  
  432.        else if (player.getDirection()==DOWN)
  433.         {
  434.             if(abs(player.get_player_y()-wumpus.get_Wumpus_Y())==1 && wumpus.get_Wumpus_X()==player.get_player_x())
  435.             {
  436.                 cout<<"\n Auuuuccchhh!   \"Wumpus is dead \"  "<<endl;
  437.                 wumpus.kill();
  438.             }
  439.         }
  440.  
  441.        else if (player.getDirection()==RIGHT)
  442.         {
  443.             if(abs(wumpus.get_Wumpus_X()-player.get_player_x())==1 && wumpus.get_Wumpus_Y()==player.get_player_y())
  444.             {
  445.                 cout<<"\n Auuuuccchhh!   \"Wumpus is dead \"  "<<endl;
  446.                 wumpus.kill();
  447.             }
  448.         }
  449.         else if (player.getDirection()==LEFT)
  450.         {
  451.             if(abs(player.get_player_x())-wumpus.get_Wumpus_X()==1 && wumpus.get_Wumpus_Y()==player.get_player_y())
  452.             {
  453.                 cout<<"\n Auuuuccchhh!   \"Wumpus is dead \"  "<<endl;
  454.                 wumpus.kill();
  455.             }
  456.         }
  457.  
  458.     }
  459.     cout<<"You have "<<player.check_total_shots()<<" shot(s) left"<<endl;
  460.     return showGameState();
  461.  
  462.  
  463.     }
  464.  
  465.  
  466.     void showGameState() {
  467.         cout << player.getPositionInfo() << endl;
  468.         cout << player.getDirectionInfo() << endl;
  469.  
  470.         if (player.isAdjacent(wumpus.getPosition()) && wumpus.iskilled()==false) {
  471.             cout << "stench!" << endl;
  472.         }
  473.  
  474.         if (player.isAdjacent(pit.getPosition()) && pit.iskilled()==false) {
  475.             cout << "Breeze!" << endl;
  476.         }
  477.  
  478.         if (player.isSamePoint(wumpus.getPosition()) && wumpus.iskilled()==false ) {
  479.             cout << "Player is killed!" << endl;
  480.             player.kill();
  481.             cout << "Game over!" << endl;
  482.             ended = true;
  483.         }
  484.  
  485.         if (player.isSamePoint(pit.getPosition())) {
  486.             cout << "Player is killed!" << endl;
  487.             player.kill();
  488.             cout << "Game over!" << endl;
  489.             ended = true;
  490.         }
  491.  
  492.         if (player.isSamePoint(gold_position)){
  493.             cout << "Got the gold!" << endl;
  494.             cout << "Game ended, you won!" << endl;
  495.             ended = true;
  496.         }
  497.     }
  498.  
  499.     bool isOver() {
  500.         return ended;
  501.     }
  502.  
  503. };
  504.  
  505.  
  506. int main()
  507. {   srand(time(0));
  508.     int c, wumpus_x, wumpus_y, gold_x, gold_y, pit_x, pit_y;
  509.     // take the six integers input from file
  510.    // WumpusWorld w(wumpus_x, wumpus_y, gold_x, gold_y, pit_x, pit_y);
  511.     WumpusWorld w;
  512.    w.showGameState();
  513.     while (!w.isOver()) {
  514.         cout << "1: move forward" << endl;
  515.         cout << "2: Turn left" << endl;
  516.         cout << "3: Turn right" << endl;
  517.         cout << "4: Shoot" << endl;
  518.         cin >> c;
  519.         if (c == 1) {
  520.             w.moveForward();
  521.  
  522.         } else if (c == 2) {
  523.             w.turnLeft();
  524.  
  525.         } else if (c == 3) {
  526.             w.turnRight();
  527.  
  528.         } else if (c == 4) {
  529.             w.shoot();
  530.  
  531.         }
  532.     }
  533.  
  534.     return 0;
  535. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement