-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
148 lines (138 loc) · 3.58 KB
/
game.cpp
File metadata and controls
148 lines (138 loc) · 3.58 KB
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
#include <bits/stdc++.h>
#include <conio.h>
#include <random>
#include <windows.h>
using namespace std;
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
// defining map size
#define mapX 20
#define mapY 10
void gameinit(char map[][mapX]) {
for(int y=0; y<mapY; y++)
for(int x=0; x<mapX; x++)
if(x==0 || y==0 || x==mapX-1 || y==mapY-1)
if(y==1 && x==0) map[y][x] = '>';
else if (y==mapY-2 && x==mapX-1) map[y][x] = '<';
else map[y][x] = '#';
else map[y][x] = ' ';
std::default_random_engine re;
re.seed(time(NULL));
std::uniform_int_distribution<int> ranX(2, mapX-3);
std::uniform_int_distribution<int> ranY(2, mapY-3);
int asteroid=mapX-5;
while(asteroid--)
map [ranY(re)]
[ranX(re)] = '0';
map[1][1] = '*';
}
void print(char map[][mapX], int level=0) {
system("cls");
cout <<"\
reach 10 levels to win!\n\
wasd/arrow-key to move\n\
dont touch the 0's and the wall!\n\n";
for(int y=0; y<mapY; y++) {
for(int x=0; x<mapX; x++) {
if(y==1 && x==0) cout << "in =";
else if(x==0) cout <<" ";
cout <<map[y][x];
if(y==mapY-2 && x==mapX-1) cout <<"= out";
}
cout<<endl;
}
if(level)
cout << "\n level "<<level<<endl;
}
void endScreen(string res, char map[][mapX]){
print(map);
Sleep(500);
char opt;
do{
system("cls");
cout << "YOU "<<res<<" !\n";
cout << "retry? (y/n): ";
cin >> opt;
if(opt == 'y') gameinit(map);
else if(opt == 'n') exit(0);
} while(opt!='y' && opt!='n');
}
tuple <int,int> findCurr(char map[][mapX]) {
for(int i=0; i<mapY; ++i)
for(int j=0; j<mapX; ++j)
if(map[i][j] == '*')
return make_tuple(j,i);
return make_tuple(1,1);
}
bool changePos(char *nmap, char *omap) {
char temp = *nmap;
*nmap = '*';
*omap = ' ';
if(temp == ' ') return 1;
return 0;
}
void moveUp(char map[][mapX]) {
int x,y;
tie(x,y) = findCurr(map);
if(!changePos(&map[y-1][x],&map[y][x]))
endScreen("LOSE",map);
// else map[mapY-2][x] = '*';
}
void moveDown(char map[][mapX]) {
int x,y;
tie(x,y) = findCurr(map);
if(!changePos(&map[y+1][x],&map[y][x]))
endScreen("LOSE",map);
// else map[1][x] = '*';
}
void moveLeft(char map[][mapX]) {
int x,y;
tie(x,y) = findCurr(map);
if(!changePos(&map[y][x-1],&map[y][x]))
endScreen("LOSE",map);
// else map[y][mapX-2] = '*';
}
void moveRight(char map[][mapX],int &l) {
int x,y;
tie(x,y) = findCurr(map);
if(map[y][x+1] == '<') {
if(*&l==10)
{
*&l=0;
endScreen("WIN",map);
}
else
{
*&l+=1;
gameinit(map);
}return;
}
if(!changePos(&map[y][x+1],&map[y][x]))
endScreen("LOSE",map);
// else map[y][1] = '*';
}
int getMovement(char m,char map[][mapX],int *l) {
switch(m) {
case 'w': moveUp(map);break;
case 'a': moveLeft(map);break;
case 's': moveDown(map);break;
case 'd': moveRight(map,*l);break;
case KEY_UP: moveUp(map);break;
case KEY_LEFT: moveLeft(map);break;
case KEY_DOWN: moveDown(map);break;
case KEY_RIGHT: moveRight(map,*l);break;
// default: return 0;
}
return 1;
}
int main(){
// y x
char map[mapY][mapX];
gameinit(map);
int level=1;
do{
print(map,level);
} while(getMovement(getch(),map,&level));
}