Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <string>
- #include <stdlib.h>
- #include <random>
- #include <ctime>
- using namespace std;
- class Point {
- public:
- Point();
- int getx(Point A) {
- int returnMe=A.x;
- return returnMe;
- }
- int gety(Point A) {
- int returnMe=A.y;
- return returnMe;
- }
- void setx(int A);
- void sety(int A);
- private:
- int x;
- int y;
- };
- Point::Point() { //default constuctpr
- int x=0;
- int y=0;
- }
- void Point::setx(int A) {
- x=A;
- }
- void Point::sety(int A) {
- y=A;
- }
- class game {
- public:
- game();
- Point wumpus;
- Point player;
- Point hole;
- Point bats;
- Point arrow;
- int arrowCount=3;
- void setPosPlayer() { //randomly places player anywhere on board
- int tempx;
- int tempy;
- srand(time(0));
- tempx=rand()%5;
- tempy=rand()%5;
- player.setx(tempx);
- player.sety(tempy);
- }
- void setPosWumpus() { //randomly places wumpus in spot not occupied
- srand(time(0));
- int tempx=rand()%5;
- int tempy=rand()%5;
- int playerx=player.getx(player);
- int playery=player.gety(player);
- bool run=true;
- while (run==true) {
- if ((tempx==playerx) && (tempy==playery)) {
- tempx=rand()%5;
- tempy=rand()%5;
- }
- else {
- run=false;
- }
- }
- wumpus.setx(tempx);
- wumpus.sety(tempy);
- }
- void setPosHole() { //randomly places bottomless hole in spot not occupied
- srand(time(0));
- int holex=rand()%5;
- int holey=rand()%5;
- int playerx=player.getx(player);
- int playery=player.gety(player);
- int wumpusx=wumpus.getx(wumpus);
- int wumpusy=wumpus.gety(wumpus);
- bool run=true;
- while (run==true){
- if (((holex==wumpusx) && (holey==wumpusy)) || ((holex==playerx) && (holey==playery))) {
- holex=rand()%5;
- holey=rand()%5;
- }
- else {
- run=false;
- }
- }
- hole.setx(holex);
- hole.sety(holey);
- }
- void setPosBats() { //randomly places bats in spot not occupied
- srand(time(0));
- int batsx=rand()%5;
- int batsy=rand()%5;
- int playerx=player.getx(player);
- int playery=player.gety(player);
- int wumpusx=wumpus.getx(wumpus);
- int wumpusy=wumpus.gety(wumpus);
- int holex=hole.getx(hole);
- int holey=hole.gety(hole);
- bool run=true;
- while (run==true) {
- if (((batsx==playerx) && (batsy==playery)) || ((batsx==wumpusx) && (batsy==wumpusy)) || ((batsx==holex) && (batsy==holey))) {
- batsx=rand()%5;
- batsy=rand()%5;
- }
- else {
- run=false;
- }
- }
- bats.setx(batsx);
- bats.sety(batsy);
- }
- void setPosArrow() { //randomly places arrow at spot not occupied
- srand(time(0));
- int arrowx=rand()%5;
- int arrowy=rand()%5;
- int playerx=player.getx(player);
- int playery=player.gety(player);
- int wumpusx=wumpus.getx(wumpus);
- int wumpusy=wumpus.gety(wumpus);
- int holex=hole.getx(hole);
- int holey=hole.gety(hole);
- int batsx=bats.getx(bats);
- int batsy=bats.gety(bats);
- bool run=true;
- while (run==true) {
- if (((arrowx==playerx) && (arrowy==playery)) || ((arrowx==wumpusx) && (arrowy==wumpusy)) || ((arrowx==holex) && (arrowy==holey)) || ((arrowx==batsx) && (arrowy==batsy))) {
- arrowx=rand()%5;
- arrowy=rand()%5;
- }
- else {
- run=false;
- }
- }
- arrow.setx(arrowx);
- arrow.sety(arrowy);
- }
- void messageDisp() { //displays if anything is close to player
- int playerx = player.getx(player);
- int playery = player.gety(player);
- int wumpusx = wumpus.getx(wumpus);
- int wumpusy = wumpus.gety(wumpus);
- int holex = hole.getx(hole);
- int holey = hole.gety(hole);
- int batsx = bats.getx(bats);
- int batsy = bats.gety(bats);
- cout << "Current Position: (" << playerx << ", " << playery << ")" << endl;
- if ((abs(playerx-wumpusx)<=1) && (abs(playery-wumpusy)<=1)) {
- cout << "You smell a wumpus." << endl;
- }
- if ((abs(playerx-holex)<=1) && (abs(playery-holey)<=1)) {
- cout<<"You feel a breeze."<<endl;
- }
- if ((abs(playerx-batsx)<=1) && (abs(playery-batsy)<=1)) {
- cout << "You hear flapping." << endl;
- }
- dispBoard();
- cout << endl;
- }
- void dispBoard() {
- int x=player.getx(player);
- int y=player.gety(player);
- string s1 = " 0 1 2 3 4";
- cout << s1 << endl;
- for (int i=0;i<5;i++) {
- cout << i << " ";
- for(int j=0;j<5;j++) {
- if ((j==x) && (i==y)) {
- cout << "[X] ";
- }
- else {
- cout << "[ ] ";
- }
- }
- cout << endl;
- }
- }
- bool shootArrow() {
- char i;
- int playerx=player.getx(player);
- int playery=player.gety(player);
- int wumpusx=wumpus.getx(wumpus);
- int wumpusy=wumpus.gety(wumpus);
- bool hit;
- if (arrowCount==0) {
- cout << "You are out of arrows!" << endl;
- hit=false;
- }
- else {
- cout << "Use wasd keys to enter direction you wish to shoot" << endl;
- cin >> i;
- if (i=='w') {
- if ((playerx==wumpusx) && (playery==wumpusy+1)) {
- hit=true;
- }
- else {
- hit=false;
- arrowCount=arrowCount-1;
- }
- }
- else if (i=='a') {
- if ((playery==wumpusy) && (playerx==wumpusx+1)) {
- hit=true;
- }
- else {
- hit=false;
- arrowCount=arrowCount-1;
- }
- }
- else if (i=='s') {
- if ((playerx==wumpusx) && (playery==wumpusy-1)) {
- hit=true;
- }
- else {
- hit=false;
- arrowCount=arrowCount-1;
- }
- }
- else if (i=='d') {
- if ((playery==wumpusy) && (playerx==wumpusx-1)) {
- hit=true;
- }
- else {
- hit=false;
- arrowCount=arrowCount-1;
- }
- }
- else {
- cout << "Not valid direction" << endl;
- }
- return hit;
- }
- }
- void movePlayer() { //Moves player's position and doesn't allow player to move past boundaries of game
- char i;
- int x=player.getx(player);
- int y=player.gety(player);
- cout << "Use wasd keys to enter direction of movement." << endl;
- cin>>i;
- if (i=='w'){
- if (y==0) {
- cout << "You hit a wall! You cannot move that direction" << endl;
- }
- else {
- y=y-1;
- }
- }
- else if (i=='a') {
- if (x==0) {
- cout << "You hit a wall! You cannot move that direction" << endl;
- }
- else {
- x=x-1;
- }
- }
- else if (i=='s') {
- if (y==4) {
- cout << "You hit a wall! You cannot move that direction" << endl;
- }
- else {
- y=y+1;
- }
- }
- else if (i=='d') {
- if (x==4) {
- cout << "You hit a wall! You cannot move that direction" << endl;
- }
- else {
- x=x+1;
- }
- }
- else {
- cout << "Not a movement." << endl;
- }
- player.setx(x);
- player.sety(y);
- }
- bool gameLossWumpus() {
- int playerx = player.getx(player);
- int playery = player.gety(player);
- int wumpusx = wumpus.getx(wumpus);
- int wumpusy = wumpus.gety(wumpus);
- if ((playerx == wumpusx) && (playery == wumpusy)) {
- return true;
- }
- else {
- return false;
- }
- }
- bool gameLossHole() {
- int playerx = player.getx(player);
- int playery = player.gety(player);
- int holex = hole.getx(hole);
- int holey = hole.gety(hole);
- if ((playerx == holex) && (playery == holey)) {
- return true;
- }
- else {
- return false;
- }
- }
- bool playerEqualsBatsPos() {
- int playerx = player.getx(player);
- int playery = player.gety(player);
- int batsx = bats.getx(bats);
- int batsy = bats.gety(bats);
- if ((playerx==batsx) && (playery==batsy)) {
- return true;
- }
- else {
- return false;
- }
- }
- void batsMovePlayer() {
- int playerx = player.getx(player);
- int playery = player.gety(player);
- int wumpusx = wumpus.getx(wumpus);
- int wumpusy = wumpus.gety(wumpus);
- int holex = hole.getx(hole);
- int holey = hole.gety(hole);
- int batsx = bats.getx(bats);
- int batsy = bats.gety(bats);
- int arrowx = arrow.getx(arrow);
- int arrowy = arrow.gety(arrow);
- bool run=true;
- srand(time(0));
- while (run==true) {
- if (((playerx==wumpusx) && (playery==wumpusy)) || ((playerx==holex) && (playery==holey)) || ((playerx=batsx) && (playery==batsy)) || ((playerx==arrowx) && (playery==arrowy))) {
- playerx = rand()%5;
- playery = rand()%5;
- }
- else {
- run=false;
- }
- }
- player.setx(playerx);
- player.sety(playery);
- }
- bool playerEqualsArrowPos() {
- int playerx = player.getx(player);
- int playery = player.gety(player);
- int arrowx = arrow.getx(arrow);
- int arrowy = arrow.gety(arrow);
- if ((playerx==arrowx) && (playery==arrowy)) {
- return true;
- }
- else {
- return false;
- }
- }
- bool wumpusAttack() {
- int wumpusx = wumpus.getx(wumpus);
- int wumpusy = wumpus.gety(wumpus);
- int playerx = player.getx(player);
- int playery = player.gety(player);
- int differenceX = abs(wumpusx-playerx);
- int differenceY = abs(wumpusy-playery);
- bool dead;
- srand(time(0));
- int r;
- if (differenceX>=differenceY) {
- r=rand()%100;
- if (differenceX==1) {
- if (r<=59) {
- dead=true;
- }
- else {
- dead=false;
- }
- }
- else if (differenceX==2) {
- if (r<=24) {
- dead=true;
- }
- else {
- dead=false;
- }
- }
- else if (differenceX==3) {
- if (r<=9) {
- dead=true;
- }
- else {
- dead=false;
- }
- }
- else if (differenceX==4) {
- if (r<=4) {
- dead=true;
- }
- else {
- dead=false;
- }
- }
- }
- else if (differenceY>differenceX) {
- r=rand()%100;
- if (differenceY==1) {
- if (r<=59) {
- dead=true;
- }
- else {
- dead=false;
- }
- }
- else if (differenceY==2) {
- if (r<=24) {
- dead=true;
- }
- else {
- dead=false;
- }
- }
- else if (differenceY==3) {
- if (r<=9) {
- dead=true;
- }
- else {
- dead=false;
- }
- }
- else if (differenceY==4) {
- if (r<=4) {
- dead=true;
- }
- else {
- dead=false;
- }
- }
- }
- return dead;
- }
- void Maingame() {
- setPosPlayer();
- setPosWumpus();
- setPosHole();
- setPosBats();
- setPosArrow();
- char choice;
- bool deadFromWumpus;
- bool deadFromHole;
- bool movePlayerBats;
- bool pickUpArrow;
- bool alive=true;
- while (alive==true) {
- messageDisp();
- cout << "Would you like to move or shoot? (m/s)" << endl;
- cin >> choice;
- if (choice=='m') {
- movePlayer();
- deadFromWumpus=gameLossWumpus();
- deadFromHole=gameLossHole();
- movePlayerBats=playerEqualsBatsPos();
- pickUpArrow=playerEqualsArrowPos();
- if (deadFromWumpus==true) {
- cout << "The wumpus got you! You died!" << endl;
- cout << "Game over!" << endl;
- alive=false;
- }
- else if (deadFromHole==true) {
- cout << "You fell into the hole! You splat at the bottom and die!" << endl;
- cout << "Game over!" << endl;
- alive=false;
- }
- else if (movePlayerBats==true) {
- batsMovePlayer();
- cout << "You were picked up by bats and moved!" << endl;
- }
- else if (pickUpArrow==true) {
- cout << "You found an arrow!" << endl;
- arrowCount=arrowCount+1;
- }
- }
- else if (choice=='s') {
- bool hit=shootArrow();
- if (hit==true) {
- cout << "You killed the Wumpus!" << endl;
- cout << "You win!!" << endl;
- alive=false;
- }
- else {
- cout << "Dagnabit you missed the wumpus!" << endl;
- bool wumpusAttackYou=wumpusAttack();
- if (wumpusAttackYou==true) {
- cout << "You spooked the wumpus by shooting an arrow and he attacked you! You are now dead!" << endl;
- cout << "Game over!" << endl;
- alive=false;
- }
- }
- }
- else {
- cout << "Not valid entry" << endl;
- }
- }
- }
- private:
- };
- game::game() {}
- bool askPlayGame() { //function determines if player wants to play game
- bool check;
- string playCheck;
- char instructionCheck;
- cout << "Would you like to read the instructions? (y/n)" << endl;
- cin >> instructionCheck;
- if (instructionCheck=='y') {
- cout << "Hunt the wumpus is a game where you are a hunter, and the objective is to kill the wumpus." << endl;
- cout << "You move around a 5x5 area, looking for the wumpus. If you happen to walk into the wumpus," << endl;
- cout << "you die and the game ends. There are other dangers you should avoid. There is a deep hole" << endl;
- cout << "that if you fall in, you die. Also there is a group of bats that can carry you away to a" << endl;
- cout << "random location in the play space. When you are in a space adjacent to the wumpus, hole, or" << endl;
- cout << "bats, a message will appear warning you that they are nearby. You are equipped with 3" << endl;
- cout << "arrows. You can only kill the wumpus if he is directly beside you, not diagonally. If you shoot" << endl;
- cout << "and miss, there is a chance the wumpus will get scared and attack you, depending on how close you" << endl;
- cout << "are to him. If you run out of arrows, there is one extra hidden in the play space. In the" << endl;
- cout << "play space, your position is designated by an 'X'. The play space will look like this." << endl;
- cout << " 0 1 2 3 4" << endl;
- cout << " 0 [ ] [ ] [ ] [ ] [ ]" << endl;
- cout << " 1 [ ] [ ] [ ] [ ] [ ]" << endl;
- cout << " 2 [ ] [ ] [ ] [ ] [ ]" << endl;
- cout << " 3 [ ] [ ] [X] [ ] [ ]" << endl;
- cout << " 4 [ ] [ ] [ ] [ ] [ ]" << endl;
- cout << "In this example, your player would be in the position (2,3)." << endl;
- cout << "Have fun and thank you for playing our rendition of 'Hunt the Wumpus' by Cody Brown and Brandon Carroll." << endl;
- }
- cout << "Would you like to play Hunt the Wumpus? (y/n)" << endl;
- cin>>playCheck;
- if (playCheck.at(0)=='y') { //allows main game loop to run if 'y' is entered
- check=true;
- }
- else { //does not allow main game loop to run if anything else is entered
- check=false;
- }
- return check;
- }
- int main () {
- game HTW;
- bool play=askPlayGame();
- while (play==true) {
- HTW.Maingame();
- char playAgain;
- cout << "Would you like to play again? (y/n)" << endl;
- cin >> playAgain;
- if (playAgain=='y') {
- play=true;
- }
- else if (playAgain=='n') {
- play=false;
- }
- else {
- 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;
- play=true;
- }
- }
- cout << "Thank you for playing! Have a good day!" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement