Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 84 additions & 74 deletions Functions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Functions.h
*
* Created on: 2018�~3��22��
* Created on: 2018�~3��22��
* Author: Mystic
*/

Expand All @@ -12,151 +12,161 @@
#include <cstdlib>
using namespace std;

void CheckWin(){}

void CheckCrash(bool& crash, int count, int Me_Row, int Me_Col, int Mon_Row[], int Mon_Col[]){
bool CheckCrash(bool crash, int count, int MePosition[], int Mon_Row[], int Mon_Col[]){
for (int i=0; i<=count; i++){
if ((Me_Row==Mon_Row[i])&&(Me_Col==Mon_Col[i])){
if ((MePosition[0]==Mon_Row[i])&&(MePosition[1]==Mon_Col[i])){
crash=true;
break;
}
}
cout<<"crash: "<<crash<<endl;
return crash;
}
// create a new monster (dont put new monster on Mel)
// create a new monster
void NewMonster(int Mon_Row[], int Mon_Col[], int count, int Me_Row, int Me_Col){
//cout<<"generating new monster..."<<endl;
int row=rand()%15;
int col=rand()%15;
//cout<<"row is "<<row<<", col is "<<col<<endl;
while (((row==14)&&(col==0))||((row==0)&&(col==14))||((row==Me_Row)&&(col==Me_Col))){
//cout<<"Generating random..."<<endl;
row=rand()%15;
col=rand()%15;
//cout<<"row is "<<row<<", col is "<<col<<endl;
}
cout<<"New monster done: row is "<<row<<", col is "<<col<<endl;
Mon_Row[count]=row;
Mon_Col[count]=col;
cout<<" new monster row:"<<Mon_Row[count]+1<<" col:"<<Mon_Col[count]+1<<endl;

}
// move all the monsters randomly

void MoveMonster(int Mon_Row[], int Mon_Col[], int count){
for (int i=0; i<count; i++){
for (int i=0; i<=count; i++){
int addrow=rand()%3-1;
int addcol=rand()%3-1;
while ((addrow==0)&&(addcol==0)){
//cout<<"addrow is "<<addrow<<", addcol is "<<addcol<<endl;
while (((addrow+addcol!=1)&&(addrow+addcol!=-1))||((Mon_Row[i]+addrow==0)&&(Mon_Col[i]+addcol==14))){
//cout<<"generating random..."<<endl;
addrow=rand()%3-1;
addcol=rand()%3-1;
//cout<<"addrow is "<<addrow<<", addcol is "<<addcol<<endl;
}
//cout<<"addrow is "<<addrow<<", addcol is "<<addcol<<endl;
Mon_Row[i]=((Mon_Row[i]+addrow==-1)||(Mon_Row[i]+addrow==15))? Mon_Row[i]:Mon_Row[i]+addrow;
Mon_Col[i]=((Mon_Col[i]+addcol==-1)||(Mon_Col[i]+addcol==15))? Mon_Col[i]:Mon_Col[i]+addcol;
cout<<"Mon_Row"<<i<<"="<<Mon_Row[i]<<"Mon_Col"<<i<<"="<<Mon_Col[i]<<endl;
// cout<<"Move Monster done: The "<<i<<"th monster has the row of "<<Mon_Row[i]<<" and the col of "<<Mon_Col[i]<<endl;
}
}

// move all the monsters toward the direction of MEL
void MoveMonster2(int Mon_Row[], int Mon_Col[], int count, int Mel_Row, int Mel_Col){
void MoveMonster2(int Mon_Row[], int Mon_Col[], int count, int MePosition[]){
for (int i=0; i<count; i++){
int addrow=0, addcol=0;
addrow= ((Mel_Row-Mon_Row[i])>0)? 1:-1;
addcol= ((Mel_Col-Mon_Col[i])>0)? 1:-1;
addrow= ((MePosition[0]-Mon_Row[i])>0)? 1:-1;
addcol= ((MePosition[1]-Mon_Col[i])>0)? 1:-1;
Mon_Row[i]=((Mon_Row[i]+addrow==-1)||(Mon_Row[i]+addrow==15))? Mon_Row[i]:Mon_Row[i]+addrow;
Mon_Col[i]=((Mon_Col[i]+addcol==-1)||(Mon_Col[i]+addcol==15))? Mon_Col[i]:Mon_Col[i]+addcol;
//cout<<"Mon_Row"<<i<<"="<<Mon_Row[i]<<"Mon_Col"<<i<<"="<<Mon_Col[i]<<endl;
}
}

// move all the monsters backward the direction of MEL
void Bomb(int Mon_Row[], int Mon_Col[], int count, int& Me_Row, int& Me_Col){
for (int i=0; i<count; i++){
void Bomb(int Mon_Row[], int Mon_Col[], int count, int MePosition[]){
for (int i=0; i<=count; i++){
int addrow=0, addcol=0;
int dis_row=Mon_Row[i]-Me_Row, dis_col=Mon_Col[i]-Me_Col;
if(dis_row<0)
addrow=-1;
else if(dis_row==0)
int dis_row=Mon_Row[i]-MePosition[0], dis_col=Mon_Col[i]-MePosition[1];
if (dis_row<0){
addrow=-1;
}
else if (dis_row==0){
addrow=0;
else addrow=1;
if(dis_col<0)
}
else {
addrow=1;
}

if (dis_col<0) {
addcol=-1;
else if(dis_col==0)
}
else if (dis_col==0){
addcol=0;
else addcol=1;

// cout<<addrow<<" "<<addcol<<" ";
}
else {
addcol=1;
}
//cout<<addrow<<" "<<addcol<<" \n";
Mon_Row[i]=((Mon_Row[i]+addrow==-1)||(Mon_Row[i]+addrow==15))? Mon_Row[i]:Mon_Row[i]+addrow;
Mon_Col[i]=((Mon_Col[i]+addcol==-1)||(Mon_Col[i]+addcol==15))? Mon_Col[i]:Mon_Col[i]+addcol;
//cout<<"Mon_Row"<<i<<"="<<Mon_Row[i]<<"Mon_Col"<<i<<"="<<Mon_Col[i]<<endl;
// cout<<"Mon_Row"<<i<<"="<<Mon_Row[i]<<"Mon_Col"<<i<<"="<<Mon_Col[i]<<endl;
}

}

// move the player
char Move(int Mon_Row[], int Mon_Col[], int count, int& Me_Row, int& Me_Col, int& count_b){
cout<<"Enter the direction or use the TNT"<<endl;
void Move(int MePosition[], int count, int Mon_Row[], int Mon_Col[], int& count_b){
cout<<"Now Enter the Direction, or Use the Bomb."<<endl;
char direction;
cin>>direction;
while ((direction!='a')&&(direction!='s')&&(direction!='d')&&(direction!='w')&&(direction!='b')){
cout<<"Give a right direction: ";
cout<<"not in scope."<<endl;
cin>>direction;
}
switch (direction){
case 'a':
Me_Col=(Me_Col==0)? Me_Col:Me_Col-1;
break;
case 's':
Me_Row=(Me_Row==14)? Me_Row:Me_Row+1;
break;
case 'd':
Me_Col=(Me_Col==14)? Me_Col:Me_Col+1;
break;
case 'w':
Me_Row=(Me_Row==0)? Me_Row:Me_Row-1;
break;
case 'b':
if(count_b<4)
Bomb(Mon_Row,Mon_Col,count, Me_Row, Me_Col);
else
cout<<cout<<"No TNT Left!"<<endl;
count_b+=1;
break;

if (direction=='a'){
MePosition[1]=(MePosition[1]==0)? MePosition[1]:MePosition[1]-1;
}
else if (direction=='s'){
MePosition[0]=(MePosition[0]==14)? MePosition[0]:MePosition[0]+1;
}
else if (direction=='d'){
MePosition[1]=(MePosition[1]==14)? MePosition[1]:MePosition[1]+1;
}
else if (direction=='w'){
MePosition[0]=(MePosition[0]==0)? MePosition[0]:MePosition[0]-1;;
}
else {
if(count_b<4){
Bomb(Mon_Row,Mon_Col,count, MePosition);
}
else {
cout<<"No TNT Left!"<<endl;
}
count_b+=1;
}
return direction;
}

// count how many monsters in a particular box
int CountNum(int Mon_Row[], int Mon_Col[], int count, int row, int col){
int num=0;
for (int i=0; i<count; i++){
for (int i=0; i<=count; i++){
if ((Mon_Row[i]==row)&&(Mon_Col[i]==col)){
num=num+1;
//cout<<"Mon_Row"<<i<<"="<<Mon_Row[i]<<"Mon_Col"<<i<<"="<<Mon_Col[i]<<endl;
}
}
return num;

}

//construct MAP
void Construct(int Map[][15],int Mon_Row[], int Mon_Col[], int count, int& Me_Row, int& Me_Col){
void Construct(int Map[][15],int Mon_Row[], int Mon_Col[], int count, int MePosition[], bool get_d){
int mon_num=0;
for (int i=0; i<15; i++){
for (int j=0; j<15; j++){
mon_num=CountNum(Mon_Row, Mon_Col, count,i,j);
Map[i][j]=mon_num;
}
}
Map[Me_Row][Me_Col]=9;
}
Map[MePosition[0]][MePosition[1]]=9;
if (get_d==false){
Map[0][14]=8;
}
}

// show the display
void Display(int Map[][15],int Mon_Row[], int Mon_Col[], int count, int& Me_Row, int& Me_Col){

//show MAP
void Display(int Map[][15], int Mon_Row[], int Mon_Col[], int count, int MePosition[]){
// Construct the map
for (int i=0; i<15; i++){
for (int j=0; j<15; j++){
if(Map[i][j]==0)
cout<<'|'<<" ";
else
cout<<'|'<<Map[i][j];
}
cout<<'|'<<endl;
for (int j=0; j<15; j++){
if(Map[i][j]==0)
cout<<'|'<<" ";
else
cout<<'|'<<Map[i][j];
}

cout<<'|'<<endl;
}
}


Expand Down
117 changes: 84 additions & 33 deletions LAB6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,98 @@
#include <cstdlib>
using namespace std;

void CheckCrash(bool& crash, int count, int Me_Row, int Me_Col, int Mon_Row[], int Mon_Col[]);
bool CheckCrash(bool crash, int count, int MePosition[], int Mon_Row[], int Mon_Col[]);
void NewMonster(int Mon_Row[], int Mon_Col[], int count, int Me_Row, int Me_Col);
void MoveMonster(int Mon_Row[], int Mon_Col[], int count);
//void MoveMonster2(int Mon_Row[], int Mon_Col[], int count, int Mel_Row, int Mel_Col);
char Move(int Mon_Row[], int Mon_Col[], int count, int& Me_Row, int& Me_Col, int& count_b);
void Construct(int Map[][15],int Mon_Row[], int Mon_Col[], int count, int& Me_Row, int& Me_Col);
void Display(int Map[][15],int Mon_Row[], int Mon_Col[], int count, int& Me_Row, int& Me_Col);
void Move(int MePosition[], int count, int Mon_Row[], int Mon_Col[], int& count_b);
//int CountNum(int Mon_Row[], int Mon_Col[], int count, int row, int col);
void Display(int Map[][15], int Mon_Row[], int Mon_Col[], int count, int MePosition[]);
void Construct(int Map[][15],int Mon_Row[], int Mon_Col[], int count, int MePosition[], bool get_d);


int main() {
srand(time(NULL));
// Evil Monsters[225];
int Mon_Row[225], Mon_Col[225], Me_Row=14, Me_Col=0;
int count = 0;
int count_b=0; // number of bombs
char dir;
bool crash = false;
int Map[15][15];

Construct(Map,Mon_Row,Mon_Col,count,Me_Row, Me_Col);
Display(Map,Mon_Row, Mon_Col,count,Me_Row,Me_Col);

while (crash == false){
//MoveMonster2(Mon_Row, Mon_Col, count, Me_Row, Me_Col);

dir=Move(Mon_Row, Mon_Col, count, Me_Row, Me_Col, count_b); // cin direction or bomb
// in normal situation
if (dir!='b'){
cout<<"count: "<<count;
NewMonster(Mon_Row, Mon_Col, count, Me_Row, Me_Col);
count=count+1; cout<<"count: "<<count;
MoveMonster(Mon_Row, Mon_Col,count);
// Initialize variables
int Mon_Row[225], Mon_Col[225], map[15][15],MePosition[2]={14, 0};
int count = 0, count_b=0;
bool crash = false, get_d = false;
// Initialize arrays
for (int m=0; m<225;m++){
Mon_Row[m]=111;
Mon_Col[m]=111;
}

cout<<"Welcome!!! Welcome the Purple Minion Maze!"<<endl;
cout<<"Your fellow minions have been injected a kind of virus and thus became purple and evil."<<endl;
cout<<"Your mission is to get the virus sample denoted by \"8\" back to Glu and Dr. Nefario,"<<endl;
cout<<"so that they can make the Antidote and save them."<<endl;
cout<<"In this game, you are given the symbol \"9\"."<<endl;
cout<<"However, in this maze, evil minions are everywhere and you need to watch out!!!"<<endl;
cout<<"You and the evil minions can move only in 4 directions by themselves by one box."<<endl;
cout<<"Each step you take, there will be one more evil minion appears and other minions will move randomly by one box!"<<endl;
cout<<"Glu is worried that they may hurt you, so he has given you three bombs."<<endl;
cout<<"Once you have use the bomb, each evil minion will move away from you by one box."<<endl;

while (crash==false) {



if (count!=0){
MoveMonster(Mon_Row, Mon_Col, count-1);
//cout<<"Move Monster Done"<<endl;
}
// check crash
CheckCrash(crash, count, Me_Row, Me_Col, Mon_Row, Mon_Col);
if(crash == true){
cout<<"Bomb!!!"<<endl;

NewMonster(Mon_Row, Mon_Col, count, MePosition[0], MePosition[1]);
//cout<<"NewMonster Done"<<endl;
crash=CheckCrash(crash, count, MePosition, Mon_Row, Mon_Col);
Construct(map, Mon_Row, Mon_Col, count, MePosition, get_d);
Display(map, Mon_Row, Mon_Col, count, MePosition);
//cout<<"crash is "<<crash<<endl;

if (crash==true){
cout<<"Gosh! You are hurt by the evil minion!"<<endl;
cout<<"It seems that Glu has to send someone else to get the virus!"<<endl;
break;
}
Construct(Map,Mon_Row,Mon_Col,count,Me_Row, Me_Col);
Display(Map,Mon_Row, Mon_Col,count,Me_Row,Me_Col); //make a display
else {
Move(MePosition, count, Mon_Row, Mon_Col, count_b);
if ((MePosition[0]==0)&&(MePosition[1]==14)){
cout<<"Wooow, you got the virus! Your fellow minions have a chance to recover!"<<endl;
get_d=true;
}
Construct(map, Mon_Row, Mon_Col, count, MePosition, get_d);
Display(map, Mon_Row, Mon_Col, count, MePosition);
crash=CheckCrash(crash, count, MePosition, Mon_Row, Mon_Col);
//cout<<"crash is "<<crash<<endl;
if (crash==false){
if ((MePosition[0]==14)&&(MePosition[1]==0)&&(get_d==true)){
cout<<"You are a Hero!\nIt is such a surprise that you can bring the virus back!"<<endl;
break;
}
}
else {
cout<<"Gosh! You are hurt by the evil minion!"<<endl;
cout<<"It seems that Glu has to send someone else to get the virus!"<<endl;
break;
}
}

count=count+1;
//cout<<"count is "<<count<<endl;
/*
cout<<"Mon_Row: ";
for (int a=0; a<count; a++){
cout<<Mon_Row[a]<<" ";
}
cout<<endl;
cout<<"Mon_Col: ";
for (int b=0; b<count; b++){
cout<<Mon_Col[b]<<" ";
}
cout<<endl;
*/

}
return 0;
}

}