Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <sstream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- #define GRIDSIZE 4
- #define UP 0
- #define DOWN 2
- #define LEFT 3
- #define RIGHT 1
- #define MAX_SHOTS 3
- string tostring(int x) {
- std::string out_string;
- std::stringstream ss;
- ss << x;
- return ss.str();
- }
- class Position {
- int x, y;
- public:
- Position (int x, int y) {
- this->x = x;
- this->y = y;
- }
- Position() {}
- // Modify the following four so that the resulting position does not leave the grid
- void moveRight() {
- if (this->x<3) this->x++;
- }
- void moveLeft() {
- if(this->x>0) this->x--;
- }
- void moveUp() {
- if (this->y<3) this->y++;
- }
- void moveDown() {
- if (this->y>0) this->y--;
- }
- bool isAdjacent(Position p) {
- //implement the function
- 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))
- return true;
- else return false;
- }
- bool isSamePoint(Position p) {
- //implement the function
- if ((this->x-p.getX()==0 && this->y-p.getY()==0) || (this->x-p.getX()==0 && this->y-p.getY()==0))
- return true;
- else return false;
- }
- int getX() {
- return this->x;
- }
- int getY() {
- return this->y;
- }
- };
- class Wumpus {
- int x_wumpus,y_wumpus;
- bool killed;
- Position p;
- public:
- Wumpus(int x, int y)
- {
- p = Position(x, y);
- x_wumpus = x;
- y_wumpus = y;
- killed = false;
- }
- Wumpus() {
- killed = false;
- do
- {
- x_wumpus = rand()%4;
- y_wumpus = rand()%4;
- }
- while(x_wumpus==0 && y_wumpus==0);
- p = Position(x_wumpus,y_wumpus);
- }
- void kill() {
- killed = true;
- }
- Position getPosition() {
- return p;
- }
- int get_Wumpus_X()
- {
- return x_wumpus;
- }
- int get_Wumpus_Y()
- {
- return y_wumpus;
- }
- bool iskilled()
- {
- if (killed == true) return true;
- else return false;
- }
- };
- class Pit
- { int x_pit,y_pit;
- Position p;
- Wumpus w;
- bool killed;
- public:
- Pit(int x, int y)
- {
- p = Position(x, y);
- x_pit = x;
- y_pit = y;
- killed = false;
- }
- Pit() {
- do
- {
- x_pit = rand()%4;
- y_pit = rand()%4;
- }
- while( (x_pit==0 && y_pit==0) || (x_pit==w.get_Wumpus_X() && y_pit==w.get_Wumpus_Y()) );
- p = Position(x_pit,y_pit);
- killed = false;
- }
- Position getPosition() {
- return p;
- }
- int get_pit_X()
- {
- return x_pit;
- }
- int get_pit_Y()
- {
- return y_pit;
- }
- bool iskilled()
- {
- if(killed==true) return true;
- else return false;
- }
- };
- class Player {
- int x_player,y_player;
- int direction;
- int total_shots;
- bool killed;
- Position p;
- public:
- Player() {
- p = Position(0,0);
- killed = false;
- total_shots = MAX_SHOTS;
- direction = UP;
- }
- void turnLeft() {
- //...
- if(direction==UP) direction=LEFT;
- else if(direction==RIGHT) direction=UP;
- else if(direction==LEFT) direction=DOWN;
- else if(direction==DOWN) direction=RIGHT;
- }
- void turnRight() {
- //...
- if(direction==UP) direction=RIGHT;
- else if(direction==RIGHT) direction=DOWN;
- else if(direction==LEFT) direction=UP;
- else if(direction==DOWN) direction=LEFT;
- }
- void moveForward() {
- //...
- if(direction==UP) p.moveUp();
- if(direction==DOWN) p.moveDown();
- if(direction==LEFT) p.moveLeft();
- if(direction==RIGHT) p.moveRight();
- }
- bool isAdjacent(Position pos) {
- return p.isAdjacent(pos);
- }
- bool isSamePoint(Position pos) {
- return p.isSamePoint(pos);
- }
- void kill() {
- killed = true;
- }
- string getPositionInfo() {
- return "\nPlayer is now at " + tostring(p.getX()) + ", " + tostring(p.getY());
- }
- string getDirectionInfo() {
- string s;
- if (direction == UP) s = "up";
- if (direction == DOWN) s = "down";
- if (direction == LEFT) s = "left";
- if (direction == RIGHT) s = "right";
- return "Player is moving at direction: " + s;
- }
- int getDirection()
- {
- return direction;
- }
- int check_total_shots()
- {
- return total_shots;
- }
- int get_player_x()
- {
- return p.getX();
- }
- int get_player_y()
- {
- return p.getY();
- }
- void shot_decrease()
- {
- total_shots--;
- }
- };
- class Gold
- {
- int x_gold,y_gold;
- public:
- Gold()
- {
- goldsetter_x();
- goldsetter_y();
- x_gold = goldGetter_x();
- y_gold = goldGetter_y();
- }
- Gold(int x,int y)
- {
- x_gold = x;
- y_gold = y;
- }
- void goldsetter_x()
- {
- x_gold = rand()%4;
- }
- void goldsetter_y()
- {
- y_gold = rand()%4;
- }
- int goldGetter_x()
- {
- return x_gold;
- }
- int goldGetter_y()
- {
- return y_gold;
- }
- };
- class WumpusWorld {
- private:
- Player player;
- Wumpus wumpus;
- Pit pit;
- Gold gold;
- Position gold_position;
- Position position;
- Position shooter;
- bool ended;
- public:
- WumpusWorld() {
- //...
- ended = false;
- player = Player();
- wumpus = Wumpus();
- pit = Pit();
- do
- {
- gold.goldsetter_x();
- gold.goldsetter_y();
- }
- 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
- ///debug
- gold_position = Position(gold.goldGetter_x(),gold.goldGetter_y());
- }
- WumpusWorld(int wumpus_x, int wumpus_y) {
- //...
- ended = false;
- player = Player();
- wumpus = Wumpus(wumpus_x,wumpus_y);
- pit = Pit();
- do
- {
- gold.goldsetter_x();
- gold.goldsetter_y();
- }
- 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
- gold_position = Position(gold.goldGetter_x(),gold.goldGetter_y());
- }
- WumpusWorld(int wumpus_x, int wumpus_y, int gold_x, int gold_y) {
- //...
- ended = false;
- player = Player();
- wumpus = Wumpus(wumpus_x,wumpus_y);
- pit = Pit();
- gold_position = Position(gold_x,gold_y);
- }
- WumpusWorld(int wumpus_x, int wumpus_y, int gold_x, int gold_y, int pit_x, int pit_y) {
- //...
- ended = false;
- player = Player();
- wumpus = Wumpus(wumpus_x,wumpus_y);
- pit = Pit(pit_x,pit_y);
- gold_position = Position(gold_x,gold_y);
- }
- void moveForward() {
- player.moveForward();
- return showGameState();
- }
- void turnLeft() {
- player.turnLeft();
- return showGameState();
- }
- void turnRight() {
- player.turnRight();
- return showGameState();
- }
- void shoot() {
- ///SHOOOOTTTTT
- //...
- if (player.check_total_shots()>0)
- {
- player.shot_decrease();
- if (player.getDirection()==UP)
- {
- if(abs(wumpus.get_Wumpus_Y()-player.get_player_y())==1 && wumpus.get_Wumpus_X()==player.get_player_x())
- {
- cout<<"\n Auuuuccchhh! \"Wumpus is dead \" "<<endl;
- wumpus.kill();
- }
- }
- else if (player.getDirection()==DOWN)
- {
- if(abs(player.get_player_y()-wumpus.get_Wumpus_Y())==1 && wumpus.get_Wumpus_X()==player.get_player_x())
- {
- cout<<"\n Auuuuccchhh! \"Wumpus is dead \" "<<endl;
- wumpus.kill();
- }
- }
- else if (player.getDirection()==RIGHT)
- {
- if(abs(wumpus.get_Wumpus_X()-player.get_player_x())==1 && wumpus.get_Wumpus_Y()==player.get_player_y())
- {
- cout<<"\n Auuuuccchhh! \"Wumpus is dead \" "<<endl;
- wumpus.kill();
- }
- }
- else if (player.getDirection()==LEFT)
- {
- if(abs(player.get_player_x())-wumpus.get_Wumpus_X()==1 && wumpus.get_Wumpus_Y()==player.get_player_y())
- {
- cout<<"\n Auuuuccchhh! \"Wumpus is dead \" "<<endl;
- wumpus.kill();
- }
- }
- }
- cout<<"You have "<<player.check_total_shots()<<" shot(s) left"<<endl;
- return showGameState();
- }
- void showGameState() {
- cout << player.getPositionInfo() << endl;
- cout << player.getDirectionInfo() << endl;
- if (player.isAdjacent(wumpus.getPosition()) && wumpus.iskilled()==false) {
- cout << "stench!" << endl;
- }
- if (player.isAdjacent(pit.getPosition()) && pit.iskilled()==false) {
- cout << "Breeze!" << endl;
- }
- if (player.isSamePoint(wumpus.getPosition()) && wumpus.iskilled()==false ) {
- cout << "Player is killed!" << endl;
- player.kill();
- cout << "Game over!" << endl;
- ended = true;
- }
- if (player.isSamePoint(pit.getPosition())) {
- cout << "Player is killed!" << endl;
- player.kill();
- cout << "Game over!" << endl;
- ended = true;
- }
- if (player.isSamePoint(gold_position)){
- cout << "Got the gold!" << endl;
- cout << "Game ended, you won!" << endl;
- ended = true;
- }
- }
- bool isOver() {
- return ended;
- }
- };
- int main()
- { srand(time(0));
- int c, wumpus_x, wumpus_y, gold_x, gold_y, pit_x, pit_y;
- // take the six integers input from file
- // WumpusWorld w(wumpus_x, wumpus_y, gold_x, gold_y, pit_x, pit_y);
- WumpusWorld w;
- w.showGameState();
- while (!w.isOver()) {
- cout << "1: move forward" << endl;
- cout << "2: Turn left" << endl;
- cout << "3: Turn right" << endl;
- cout << "4: Shoot" << endl;
- cin >> c;
- if (c == 1) {
- w.moveForward();
- } else if (c == 2) {
- w.turnLeft();
- } else if (c == 3) {
- w.turnRight();
- } else if (c == 4) {
- w.shoot();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement