Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <string>
  5. #include <stdlib.h>
  6. #include <random>
  7. #include <ctime>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. class Point {
  13.     public:
  14.         Point();
  15.        
  16.         int getx(Point A) {
  17.             int returnMe=A.x;
  18.             return returnMe;
  19.         }
  20.        
  21.         int gety(Point A) {
  22.             int returnMe=A.y;
  23.             return returnMe;
  24.         }
  25.        
  26.         void setx(int A);
  27.        
  28.         void sety(int A);
  29.        
  30.        
  31.     private:
  32.         int x;
  33.         int y;
  34. };
  35.  
  36. Point::Point() {            //default constuctpr
  37.     int x=0;
  38.     int y=0;
  39. }
  40.  
  41. void Point::setx(int A) {
  42.     x=A;
  43. }
  44.  
  45. void Point::sety(int A) {
  46.     y=A;
  47. }
  48.  
  49. class game {
  50.     public:
  51.         game();
  52.         Point wumpus;                                                        
  53.         Point player;
  54.         Point hole;
  55.         Point bats;
  56.         Point arrow;
  57.         int arrowCount=3;
  58.        
  59.         void setPosPlayer() {           //randomly places player anywhere on board
  60.             int tempx;
  61.             int tempy;
  62.             srand(time(0));
  63.             tempx=rand()%5;
  64.             tempy=rand()%5;
  65.             player.setx(tempx);
  66.             player.sety(tempy);
  67.         }
  68.        
  69.         void setPosWumpus() {           //randomly places wumpus in spot not occupied
  70.             srand(time(0));
  71.             int tempx=rand()%5;
  72.             int tempy=rand()%5;
  73.             int playerx=player.getx(player);
  74.             int playery=player.gety(player);
  75.             bool run=true;
  76.            
  77.             while (run==true) {
  78.                 if ((tempx==playerx) && (tempy==playery)) {
  79.                     tempx=rand()%5;
  80.                     tempy=rand()%5;
  81.                 }
  82.                 else {
  83.                     run=false;
  84.                 }
  85.             }
  86.             wumpus.setx(tempx);
  87.             wumpus.sety(tempy);
  88.         }
  89.        
  90.         void setPosHole() {                //randomly places bottomless hole in spot not occupied
  91.             srand(time(0));
  92.             int holex=rand()%5;
  93.             int holey=rand()%5;
  94.             int playerx=player.getx(player);
  95.             int playery=player.gety(player);
  96.             int wumpusx=wumpus.getx(wumpus);
  97.             int wumpusy=wumpus.gety(wumpus);
  98.             bool run=true;
  99.            
  100.             while (run==true){
  101.                 if (((holex==wumpusx) && (holey==wumpusy)) || ((holex==playerx) && (holey==playery))) {
  102.                     holex=rand()%5;
  103.                     holey=rand()%5;
  104.                 }
  105.                 else {
  106.                     run=false;
  107.                 }
  108.             }
  109.             hole.setx(holex);
  110.             hole.sety(holey);
  111.         }
  112.        
  113.         void setPosBats() {             //randomly places bats in spot not occupied
  114.             srand(time(0));
  115.             int batsx=rand()%5;
  116.             int batsy=rand()%5;
  117.             int playerx=player.getx(player);
  118.             int playery=player.gety(player);
  119.             int wumpusx=wumpus.getx(wumpus);
  120.             int wumpusy=wumpus.gety(wumpus);
  121.             int holex=hole.getx(hole);
  122.             int holey=hole.gety(hole);
  123.             bool run=true;
  124.            
  125.             while (run==true) {
  126.                 if (((batsx==playerx) && (batsy==playery)) || ((batsx==wumpusx) && (batsy==wumpusy)) || ((batsx==holex) && (batsy==holey))) {
  127.                     batsx=rand()%5;
  128.                     batsy=rand()%5;
  129.                 }
  130.                 else {
  131.                     run=false;
  132.                 }
  133.             }
  134.             bats.setx(batsx);
  135.             bats.sety(batsy);
  136.         }
  137.        
  138.         void setPosArrow() {               //randomly places arrow at spot not occupied
  139.             srand(time(0));
  140.             int arrowx=rand()%5;
  141.             int arrowy=rand()%5;
  142.             int playerx=player.getx(player);
  143.             int playery=player.gety(player);
  144.             int wumpusx=wumpus.getx(wumpus);
  145.             int wumpusy=wumpus.gety(wumpus);
  146.             int holex=hole.getx(hole);
  147.             int holey=hole.gety(hole);
  148.             int batsx=bats.getx(bats);
  149.             int batsy=bats.gety(bats);
  150.             bool run=true;
  151.            
  152.             while (run==true) {
  153.                 if (((arrowx==playerx) && (arrowy==playery)) || ((arrowx==wumpusx) && (arrowy==wumpusy)) || ((arrowx==holex) && (arrowy==holey)) || ((arrowx==batsx) && (arrowy==batsy))) {
  154.                     arrowx=rand()%5;
  155.                     arrowy=rand()%5;
  156.                 }
  157.                 else {
  158.                     run=false;
  159.                 }
  160.             }
  161.             arrow.setx(arrowx);
  162.             arrow.sety(arrowy);
  163.         }
  164.        
  165.         void messageDisp() {        //displays if anything is close to player
  166.             int playerx = player.getx(player);
  167.             int playery = player.gety(player);
  168.             int wumpusx = wumpus.getx(wumpus);
  169.             int wumpusy = wumpus.gety(wumpus);
  170.             int holex = hole.getx(hole);
  171.             int holey = hole.gety(hole);
  172.             int batsx = bats.getx(bats);
  173.             int batsy = bats.gety(bats);
  174.             cout << "Current Position: (" << playerx << ", " << playery << ")" << endl;
  175.             if ((abs(playerx-wumpusx)<=1) && (abs(playery-wumpusy)<=1)) {
  176.                 cout << "You smell a wumpus." << endl;
  177.             }
  178.             if ((abs(playerx-holex)<=1) && (abs(playery-holey)<=1)) {
  179.                 cout<<"You feel a breeze."<<endl;
  180.             }
  181.            
  182.             if ((abs(playerx-batsx)<=1) && (abs(playery-batsy)<=1)) {
  183.                 cout << "You hear flapping." << endl;
  184.             }
  185.             dispBoard();
  186.             cout << endl;
  187.         }
  188.        
  189.         void dispBoard() {
  190.             int x=player.getx(player);
  191.             int y=player.gety(player);
  192.             string s1 = "    0   1   2   3   4";
  193.             cout << s1 << endl;
  194.             for (int i=0;i<5;i++) {
  195.                 cout << i << "  ";
  196.                 for(int j=0;j<5;j++) {
  197.                     if ((j==x) && (i==y)) {
  198.                         cout << "[X] ";
  199.                     }
  200.                     else {
  201.                         cout << "[ ] ";
  202.                     }
  203.                 }
  204.                 cout << endl;
  205.             }
  206.         }
  207.        
  208.         bool shootArrow() {
  209.             char i;
  210.             int playerx=player.getx(player);
  211.             int playery=player.gety(player);
  212.             int wumpusx=wumpus.getx(wumpus);
  213.             int wumpusy=wumpus.gety(wumpus);
  214.             bool hit;
  215.             if (arrowCount==0) {
  216.                 cout << "You are out of arrows!" << endl;
  217.                 hit=false;
  218.             }
  219.             else {
  220.                 cout << "Use wasd keys to enter direction you wish to shoot" << endl;
  221.                 cin >> i;
  222.                 if (i=='w') {
  223.                     if ((playerx==wumpusx) && (playery==wumpusy+1)) {
  224.                         hit=true;
  225.                     }
  226.                     else {
  227.                         hit=false;
  228.                         arrowCount=arrowCount-1;
  229.                     }
  230.                 }
  231.                 else if (i=='a') {
  232.                     if ((playery==wumpusy) && (playerx==wumpusx+1)) {
  233.                         hit=true;
  234.                     }
  235.                     else {
  236.                         hit=false;
  237.                         arrowCount=arrowCount-1;
  238.                     }
  239.                 }
  240.                 else if (i=='s') {
  241.                     if ((playerx==wumpusx) && (playery==wumpusy-1)) {
  242.                         hit=true;
  243.                     }
  244.                     else {
  245.                         hit=false;
  246.                         arrowCount=arrowCount-1;
  247.                     }
  248.                 }
  249.                 else if (i=='d') {
  250.                     if ((playery==wumpusy) && (playerx==wumpusx-1)) {
  251.                         hit=true;
  252.                     }
  253.                     else {
  254.                         hit=false;
  255.                         arrowCount=arrowCount-1;
  256.                     }
  257.                 }
  258.                 else {
  259.                     cout << "Not valid direction" << endl;
  260.                 }
  261.                 return hit;
  262.             }
  263.         }
  264.        
  265.         void movePlayer() {     //Moves player's position and doesn't allow player to move past boundaries of game
  266.             char i;
  267.             int x=player.getx(player);
  268.             int y=player.gety(player);
  269.             cout << "Use wasd keys to enter direction of movement." << endl;
  270.             cin>>i;
  271.             if (i=='w'){
  272.                 if (y==0) {
  273.                     cout << "You hit a wall! You cannot move that direction" << endl;
  274.                 }
  275.                 else {
  276.                     y=y-1;
  277.                 }
  278.             }
  279.             else if (i=='a') {
  280.                 if (x==0) {
  281.                     cout << "You hit a wall! You cannot move that direction" << endl;
  282.                 }
  283.                 else {
  284.                     x=x-1;
  285.                 }
  286.             }
  287.             else if (i=='s') {
  288.                 if (y==4) {
  289.                     cout << "You hit a wall! You cannot move that direction" << endl;
  290.                 }
  291.                 else {
  292.                     y=y+1;
  293.                 }
  294.             }
  295.             else if (i=='d') {
  296.                 if (x==4) {
  297.                     cout << "You hit a wall! You cannot move that direction" << endl;
  298.                 }
  299.                 else {
  300.                     x=x+1;
  301.                 }
  302.             }
  303.             else {
  304.                 cout << "Not a movement." << endl;
  305.             }
  306.             player.setx(x);
  307.             player.sety(y);
  308.         }
  309.        
  310.         bool gameLossWumpus() {        
  311.             int playerx = player.getx(player);
  312.             int playery = player.gety(player);
  313.             int wumpusx = wumpus.getx(wumpus);
  314.             int wumpusy = wumpus.gety(wumpus);
  315.             if ((playerx == wumpusx) && (playery == wumpusy)) {
  316.                 return true;
  317.             }
  318.             else {
  319.                 return false;
  320.             }
  321.         }
  322.        
  323.         bool gameLossHole() {
  324.             int playerx = player.getx(player);
  325.             int playery = player.gety(player);
  326.             int holex = hole.getx(hole);
  327.             int holey = hole.gety(hole);
  328.             if ((playerx == holex) && (playery == holey)) {
  329.                 return true;
  330.             }
  331.             else {
  332.                 return false;
  333.             }
  334.         }
  335.        
  336.         bool playerEqualsBatsPos() {
  337.             int playerx = player.getx(player);
  338.             int playery = player.gety(player);
  339.             int batsx = bats.getx(bats);
  340.             int batsy = bats.gety(bats);
  341.             if ((playerx==batsx) && (playery==batsy)) {
  342.                 return true;
  343.             }
  344.             else {
  345.                 return false;
  346.             }
  347.         }
  348.        
  349.         void batsMovePlayer() {
  350.             int playerx = player.getx(player);
  351.             int playery = player.gety(player);
  352.             int wumpusx = wumpus.getx(wumpus);
  353.             int wumpusy = wumpus.gety(wumpus);
  354.             int holex = hole.getx(hole);
  355.             int holey = hole.gety(hole);
  356.             int batsx = bats.getx(bats);
  357.             int batsy = bats.gety(bats);
  358.             int arrowx = arrow.getx(arrow);
  359.             int arrowy = arrow.gety(arrow);
  360.             bool run=true;
  361.             srand(time(0));
  362.            
  363.             while (run==true) {
  364.                 if (((playerx==wumpusx) && (playery==wumpusy)) || ((playerx==holex) && (playery==holey)) || ((playerx=batsx) && (playery==batsy)) || ((playerx==arrowx) && (playery==arrowy))) {
  365.                     playerx = rand()%5;
  366.                     playery = rand()%5;
  367.                 }
  368.                 else {
  369.                     run=false;
  370.                 }
  371.             }
  372.             player.setx(playerx);
  373.             player.sety(playery);
  374.         }
  375.        
  376.         bool playerEqualsArrowPos() {
  377.             int playerx = player.getx(player);
  378.             int playery = player.gety(player);
  379.             int arrowx = arrow.getx(arrow);
  380.             int arrowy = arrow.gety(arrow);
  381.             if ((playerx==arrowx) && (playery==arrowy)) {
  382.                 return true;
  383.             }
  384.             else {
  385.                 return false;
  386.             }
  387.         }
  388.        
  389.         bool wumpusAttack() {
  390.             int wumpusx = wumpus.getx(wumpus);
  391.             int wumpusy = wumpus.gety(wumpus);
  392.             int playerx = player.getx(player);
  393.             int playery = player.gety(player);
  394.             int differenceX = abs(wumpusx-playerx);
  395.             int differenceY = abs(wumpusy-playery);
  396.             bool dead;
  397.             srand(time(0));
  398.             int r;
  399.            
  400.             if (differenceX>=differenceY) {
  401.                 r=rand()%100;
  402.                 if (differenceX==1) {
  403.                     if (r<=59) {
  404.                         dead=true;
  405.                     }
  406.                     else {
  407.                         dead=false;
  408.                     }
  409.                 }
  410.                 else if (differenceX==2) {
  411.                     if (r<=24) {
  412.                         dead=true;
  413.                     }
  414.                     else {
  415.                         dead=false;
  416.                     }
  417.                 }
  418.                 else if (differenceX==3) {
  419.                     if (r<=9) {
  420.                         dead=true;
  421.                     }
  422.                     else {
  423.                         dead=false;
  424.                     }
  425.                 }
  426.                 else if (differenceX==4) {
  427.                     if (r<=4) {
  428.                         dead=true;
  429.                     }
  430.                     else {
  431.                         dead=false;
  432.                     }
  433.                 }
  434.             }
  435.             else if (differenceY>differenceX) {
  436.                 r=rand()%100;
  437.                 if (differenceY==1) {
  438.                     if (r<=59) {
  439.                         dead=true;
  440.                     }
  441.                     else {
  442.                         dead=false;
  443.                     }
  444.                 }
  445.                 else if (differenceY==2) {
  446.                     if (r<=24) {
  447.                         dead=true;
  448.                     }
  449.                     else {
  450.                         dead=false;
  451.                     }
  452.                 }
  453.                 else if (differenceY==3) {
  454.                     if (r<=9) {
  455.                         dead=true;
  456.                     }
  457.                     else {
  458.                         dead=false;
  459.                     }
  460.                 }
  461.                 else if (differenceY==4) {
  462.                     if (r<=4) {
  463.                         dead=true;
  464.                     }
  465.                     else {
  466.                         dead=false;
  467.                     }
  468.                 }
  469.             }
  470.             return dead;
  471.         }
  472.        
  473.         void Maingame() {
  474.             setPosPlayer();
  475.             setPosWumpus();
  476.             setPosHole();
  477.             setPosBats();
  478.             setPosArrow();
  479.             char choice;
  480.             bool deadFromWumpus;
  481.             bool deadFromHole;
  482.             bool movePlayerBats;
  483.             bool pickUpArrow;
  484.             bool alive=true;
  485.            
  486.             while (alive==true) {
  487.                 messageDisp();
  488.                 cout << "Would you like to move or shoot? (m/s)" << endl;
  489.                 cin >> choice;
  490.                
  491.                 if (choice=='m') {
  492.                     movePlayer();
  493.                     deadFromWumpus=gameLossWumpus();
  494.                     deadFromHole=gameLossHole();
  495.                     movePlayerBats=playerEqualsBatsPos();
  496.                     pickUpArrow=playerEqualsArrowPos();
  497.                    
  498.                     if (deadFromWumpus==true) {
  499.                         cout << "The wumpus got you! You died!" << endl;
  500.                         cout << "Game over!" << endl;
  501.                         alive=false;
  502.                     }
  503.                     else if (deadFromHole==true) {
  504.                         cout << "You fell into the hole! You splat at the bottom and die!" << endl;
  505.                         cout << "Game over!" << endl;
  506.                         alive=false;
  507.                     }
  508.                     else if (movePlayerBats==true) {
  509.                         batsMovePlayer();
  510.                         cout << "You were picked up by bats and moved!" << endl;
  511.                     }
  512.                     else if (pickUpArrow==true) {
  513.                         cout << "You found an arrow!" << endl;
  514.                         arrowCount=arrowCount+1;
  515.                     }
  516.                 }
  517.                 else if (choice=='s') {
  518.                     bool hit=shootArrow();
  519.                     if (hit==true) {
  520.                         cout << "You killed the Wumpus!" << endl;
  521.                         cout << "You win!!" << endl;
  522.                         alive=false;
  523.                     }
  524.                     else {
  525.                         cout << "Dagnabit you missed the wumpus!" << endl;
  526.                         bool wumpusAttackYou=wumpusAttack();
  527.                         if (wumpusAttackYou==true) {
  528.                             cout << "You spooked the wumpus by shooting an arrow and he attacked you! You are now dead!" << endl;
  529.                             cout << "Game over!" << endl;
  530.                             alive=false;
  531.                         }
  532.                     }
  533.                 }
  534.                 else {
  535.                     cout << "Not valid entry" << endl;
  536.                 }
  537.             }
  538.         }
  539.        
  540.     private:
  541. };
  542.  
  543. game::game() {}
  544.  
  545. bool askPlayGame() {               //function determines if player wants to play game
  546.     bool check;
  547.     string playCheck;
  548.     char instructionCheck;
  549.     cout << "Would you like to read the instructions? (y/n)" << endl;
  550.     cin >> instructionCheck;
  551.     if (instructionCheck=='y') {
  552.         cout << "Hunt the wumpus is a game where you are a hunter, and the objective is to kill the wumpus." << endl;
  553.         cout << "You move around a 5x5 area, looking for the wumpus. If you happen to walk into the wumpus," << endl;
  554.         cout << "you die and the game ends. There are other dangers you should avoid. There is a deep hole" << endl;
  555.         cout << "that if you fall in, you die. Also there is a group of bats that can carry you away to a" << endl;
  556.         cout << "random location in the play space. When you are in a space adjacent to the wumpus, hole, or" << endl;
  557.         cout << "bats, a message will appear warning you that they are nearby. You are equipped with 3" << endl;
  558.         cout << "arrows. You can only kill the wumpus if he is directly beside you, not diagonally. If you shoot" << endl;
  559.         cout << "and miss, there is a chance the wumpus will get scared and attack you, depending on how close you" << endl;
  560.         cout << "are to him. If you run out of arrows, there is one extra hidden in the play space. In the" << endl;
  561.         cout << "play space, your position is designated by an 'X'.  The play space will look like this." << endl;
  562.         cout << "                    0   1   2   3   4" << endl;
  563.         cout << "                 0 [ ] [ ] [ ] [ ] [ ]" << endl;
  564.         cout << "                 1 [ ] [ ] [ ] [ ] [ ]" << endl;
  565.         cout << "                 2 [ ] [ ] [ ] [ ] [ ]" << endl;
  566.         cout << "                 3 [ ] [ ] [X] [ ] [ ]" << endl;
  567.         cout << "                 4 [ ] [ ] [ ] [ ] [ ]" << endl;
  568.         cout << "In this example, your player would be in the position (2,3)." << endl;
  569.         cout << "Have fun and thank you for playing our rendition of 'Hunt the Wumpus' by Cody Brown and Brandon Carroll." << endl;
  570.     }
  571.     cout << "Would you like to play Hunt the Wumpus? (y/n)" << endl;
  572.     cin>>playCheck;
  573.     if (playCheck.at(0)=='y') {            //allows main game loop to run if 'y' is entered
  574.         check=true;
  575.     }
  576.     else {            //does not allow main game loop to run if anything else is entered
  577.         check=false;
  578.     }
  579.     return check;
  580. }
  581.  
  582.  
  583. int main () {
  584.     game HTW;
  585.     bool play=askPlayGame();
  586.    
  587.     while (play==true) {
  588.         HTW.Maingame();
  589.         char playAgain;
  590.         cout << "Would you like to play again? (y/n)" << endl;
  591.         cin >> playAgain;
  592.         if (playAgain=='y') {
  593.             play=true;
  594.         }
  595.         else if (playAgain=='n') {
  596.             play=false;
  597.         }
  598.         else {
  599.             cout << "This is not a valid entry, so for your lack of ability to pay attention to the choices, you have to play our wonderful game again!" << endl;
  600.             play=true;
  601.         }
  602.     }
  603.    
  604.    
  605.     cout << "Thank you for playing! Have a good day!" << endl;
  606.    
  607.    
  608.     return 0;
  609. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement