-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.h
163 lines (147 loc) · 4.48 KB
/
Functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Functions.h
*
* Created on: 2018�~3��22��
* Author: Mystic
*/
#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_
#include <iostream>
#include <ctime>
#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[]){
for (int i=0; i<=count; i++){
if ((Me_Row==Mon_Row[i])&&(Me_Col==Mon_Col[i])){
crash=true;
}
}
cout<<"crash: "<<crash<<endl;
}
// create a new monster (dont put new monster on Mel)
void NewMonster(int Mon_Row[], int Mon_Col[], int count, int Me_Row, int Me_Col){
int row=rand()%15;
int col=rand()%15;
while (((row==14)&&(col==0))||((row==0)&&(col==14))||((row==Me_Row)&&(col==Me_Col))){
row=rand()%15;
col=rand()%15;
}
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++){
int addrow=rand()%3-1;
int addcol=rand()%3-1;
while ((addrow==0)&&(addcol==0)){
addrow=rand()%3-1;
addcol=rand()%3-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 toward the direction of MEL
void MoveMonster2(int Mon_Row[], int Mon_Col[], int count, int Mel_Row, int Mel_Col){
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;
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++){
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)
addrow=0;
else addrow=1;
if(dis_col<0)
addcol=-1;
else if(dis_col==0)
addcol=0;
else addcol=1;
// cout<<addrow<<" "<<addcol<<" ";
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 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;
char direction;
cin>>direction;
while ((direction!='a')&&(direction!='s')&&(direction!='d')&&(direction!='w')&&(direction!='b')){
cout<<"Give a right direction: ";
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;
}
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++){
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){
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;
}
// show the display
void Display(int Map[][15],int Mon_Row[], int Mon_Col[], int count, int& Me_Row, int& Me_Col){
//show 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;
}
}
#endif /* FUNCTIONS_H_ */