Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE HELP ME FIX MY CODE, C++ Program I missed few things below: you have global variables for board, moves you are not using constants

PLEASE HELP ME FIX MY CODE, C++ Program

I missed few things below:

you have global variables for board, moves

you are not using constants for items in the dungeon and moves

rdirection could use a switch or an array of moves and be simpler -- just a suggestion

you are using a while true with break in setting traps, please recode differently

you are using single character names for variables, please redo with better names

you have == instead of = in checkMove

========

#include #include #include

#define MAX_SIZE 8

using namespace std;

char board[MAX_SIZE][MAX_SIZE]; int xP=-1,yP=-1; int xT=-1,yT=-1;

int xM[MAX_SIZE],yM[MAX_SIZE];

void createDungeon(char player,int numberOfTrap,char treasure,int monstarNo){ // initialise for(int i=0;i for(int j=0;j board[i][j]='.'; } } // random player placed on board int a=rand()%MAX_SIZE; int b=rand()%MAX_SIZE; board[a][b]=player; xP=a;yP=b; // random trap placed on board for(int i=1;i<=numberOfTrap;i++){ while(true){ a=rand()%MAX_SIZE; b=rand()%MAX_SIZE; if(board[a][b]=='.'){ board[a][b]='T'; break; } } } // random treasure placed on board while(true){ a=rand()%MAX_SIZE; b=rand()%MAX_SIZE; if(board[a][b]=='.'){ board[a][b]=treasure; xT=a;yT=b; break; } } // random moster placed on board for(int i=1;i<=monstarNo;i++){ while(true){ a=rand()%MAX_SIZE; b=rand()%MAX_SIZE; if(board[a][b]=='.'){ board[a][b]='M'; xM[i-1]=a; yM[i-1]=b; break; } } } }

// display board

void showBoard(char theBoard[MAX_SIZE][MAX_SIZE]){ for(int i=0;i for(int j=0;j cout< } cout< } }

// check user move bool checkMove(int x,int y,char m,char t){ bool ret=false; // for left if(m=='L'){ x--; if(x>=0){ ret=true; } } // for right if(m=='R'){ x++; if(x ret=true; } } // for down if(m=='D'){ y++; if(y ret=true; } } // for up if(m=='U'){ y--; if(y>=0){ ret=true; } } // board update possible by new move if(ret==true){ if(board[x][y]=='.' || board[x][y]==t){ ret==true; } else{ ret=false; } } return ret; }

// random direction char rDirection(){ char ret=' '; int r=rand()%4; if(r==0){ret='L';} if(r==1){ret='R';} if(r==2){ret='U';} if(r==4){ret='D';} return(ret); }

// monster move bool updateMosterMove(int _n,char p){ for(int i=0;i<_n;i++){ char ch=rDirection(); bool b=checkMove(xM[i],yM[i],ch,p); if(b==true){ board[xM[i]][yM[i]]='.'; if(ch=='L'){xM[i]--;} if(ch=='R'){xM[i]++;} if(ch=='U'){yM[i]--;} if(ch=='D'){yM[i]++;} board[xM[i]][yM[i]]='M'; } } }

// user move char getMove(int x,int y,char t){ char ret=' '; bool b=false; char m=' '; while(true){ cout<<" L (Left)"; cout<<" R (Right)"; cout<<" U (Upper)"; cout<<" D (Down)"; cout<<" Enter Your Move : "; cin>>m; // for valid move if(m=='L' || m=='R' || m=='U' || m=='D'){ b=checkMove(x,y,m,t); if(b==false){ cout<<" Wrong Move , Please enter correct move."; } else{ ret=m; break; } } // invalid user move else{ cout<<" Invalid move "; } } return ret; }

// user move update in board void updateDungeon(int x,int y,char player,char m){ board[x][y]='.'; if(m=='L'){ x--; } if(m=='R'){ x++; } if(m=='D'){ y++; } if(m=='U'){ y--; } // place player board[x][y]=player; // update new move xP=x;yP=y; }

// check monster eat player bool check(int _n){ bool ret=false; for(int i=0;i<_n;i++){ if(xM[i]==xP && yM[i]==yP){ ret=true; break; } } return ret; }

// main function int main(){ srand(time(0)); createDungeon('G',8,'X',4); while(true){ showBoard(board); char m=getMove(xP,yP,'X'); updateDungeon(xP,yP,'G',m); cout< updateMosterMove(4,'G'); bool b=check(4); if(b==true){ cout<<" You lost game . "; showBoard(board); cout<<" Want to play again [y/n]"; char again; cin>>again; if(again=='Y' || again=='y'){ createDungeon('G',8,'X',4); } else{ break; } } else{ if(xP==xT && yP==yT){ cout<<" ******^^^^^^****** "; showBoard(board); cout<<" You got Treasure."; cout<<" Want to play again [y/n]"; char again; cin>>again; if(again=='Y' || again=='y'){ createDungeon('G',8,'X',4); } else{ break; } } } } cout<<" GAME OVER"; return(0); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions