Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with implement both header and source files. how would i split this code up so i have a .h file and a cpp.

Need help with implement both header and source files. how would i split this code up so i have a .h file and a cpp. what would i put in the .h file?

#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; void createDungeon(char player,int numberOfTrap,char treasure){ for(int i=0;iboard[i][j]='.'; } } // player placed on board int a=rand()%MAX_SIZE; int b=rand()%MAX_SIZE; board[a][b]=player; xP=a;yP=b; // trap 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; } } } // treasure while(true){ a=rand()%MAX_SIZE; b=rand()%MAX_SIZE; if(board[a][b]=='.'){ board[a][b]=treasure; xT=a;yT=b; break; } } } // display board void showBoard(char theBoard[MAX_SIZE][MAX_SIZE]){ for(int i=0;icout<<theBoard[j][i]<<" "; } cout<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(xm=='D'){ y++; if(ym=='U'){ y--; if(y>=0){ ret=true; } } // board update possible by new move if(ret){ if(board[x][y]=='.' || board[x][y]==t){ ret==true; } else{ ret=false; } } return ret; } // 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 (Up)"; cout<<" D (Down)"; cout<<" 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=='R'){ x++; } if(m=='U'){ y--; } if(m=='D'){ y++; } if(m=='L'){ x--; } // place player board[x][y]=player; // update new move xP=x;yP=y; } // main function int main(){ srand(time(0)); createDungeon('G',8,'X'); while(true){ showBoard(board); char m=getMove(xP,yP,'X'); updateDungeon(xP,yP,'G',m); cout<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'); } else{ break; } } } cout<<"Return for more"; return(0); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

More Books

Students also viewed these Databases questions