-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinal.cpp
More file actions
619 lines (599 loc) · 15.1 KB
/
final.cpp
File metadata and controls
619 lines (599 loc) · 15.1 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#include<stack>
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<cstdlib>
#include<bits/stdc++.h>
#include<string>
#include<ctime>
#include<algorithm>
using namespace std;
int score=0;
int counter = 1;
char **maze;
static int lives= 3;
int player_count=0;
vector<class player> leader;
class Graph
{
public:
int nodes;
vector<int>*adj;
int sp;
int lp;
Graph(int n)
{
sp=0;
this->nodes=n;
adj=new vector<int>[n*n];
}
void redef_graph(int n1)
{
sp=0;
nodes=n1;
adj=new vector<int>[nodes*nodes];
}
void init()
{
adj=new vector<int>[nodes*nodes];
}
void checkEdge()
{
for(int i=0;i<nodes;i++)
{
for(int j=0;j<nodes;j++)
{
edge(i,j);
}
}
}
void add_edge(int org,int dest)
{
adj[org].push_back(dest);
}
void edge(int i,int j)
{
if(maze[i][j]!='X')
{
if(i>0 && maze[i-1][j]!='X')
{
add_edge((i*nodes)+j,(i-1)*nodes+j);
}
if(i<nodes-1 && maze[i+1][j]!='X')
{
add_edge((i*nodes)+j,(i+1)*nodes+j);
}
if(j>0 && maze[i][j-1]!='X')
{
add_edge((i*nodes)+j,(i*nodes)+(j-1));
}
if(j<nodes-1 && maze[i][j+1]!='X')
{
add_edge((i*nodes)+j,(i*nodes)+(j+1));
}
}
}
int minEdgeBFS(int u, int v)
{
vector<bool> visited(nodes*nodes, false);
vector<int> distance(nodes*nodes, 0);
queue <int> Q;
distance[u] = 0;
Q.push(u);
visited[u] = true;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int i=0; i<adj[x].size(); i++)
{
if (visited[adj[x][i]])
continue;
distance[adj[x][i]] = distance[x] + 1;
Q.push(adj[x][i]);
visited[adj[x][i]] = 1;
}
}
return distance[v];
}
};
class mazegame
{
public:
string player;
stack<pair<int,int> > S;
int n , steps;
void randomObstruction(double percent)
{
int c=(percent*n*n);
while(c>0)
{
int i=rand()%n;
int j=rand()%n;
if(maze[i][j]!='X' && maze[i][j]!='#' && (i!=0 || j!=0))
{
maze[i][j]='X';
c--;
}
}
}
mazegame(int n , string name)
{
this->n=n;
player = name;
steps=0;
maze = new char * [n];
for (int i = 0; i < n; ++i )
maze[i] = new char [n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
maze[i][j]=' ';
}
maze[0][0]='@';
maze[n-1][n-1]='#';
}
void redefine(int n1)
{
n=n1;
steps=0;
maze = new char * [n];
for (int i = 0; i < n; ++i )
maze[i] = new char [n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
maze[i][j]=' ';
}
maze[0][0]='@';
maze[n-1][n-1]='#';
}
void drawMaze()
{
system("clear");
cout<<"\n\n\t\t\t\t\t\t\t LEVEL "<<counter<<"\n\n";
int i,j=0;
cout<<"\n\n\n";
for(i=0;i<n;i++)
{
cout<<"\t "<<(i+1)<<" ";
}
cout<<"\n";
for(i=0;i<n;i++)
{
cout<<"\t ___ ";
}
cout<<"\n";
while(j<n)
{
for(i=0;i<n;i++)
{
if(maze[i][j]!=' ')
printf("\t| _%c_ |",maze[i][j]);
else
cout<<"\t| ___ |";
}
cout<<"\n";
j++;
}
cout<<"\n\n\n\n\n";
}
int move(int x,int y)
{
pair<int,int> K;
char ch;
int termin=0;
do
{
cout<<"\nUP....'W'";
cout<<"\nLEFT....'A'";
cout<<"\nDOWN....'S'";
cout<<"\nRIGHT....'D'";
cout<<"\nUNDO....'U'";
cout<<"\nQUIT....'Q'";
cout<<"\n\t\t\t\t Your Lives:- "<<lives;
cout<<"\nEnter Your Move:- ";
//system("stty raw");
ch=getchar();
//system("stty cooked");
switch(ch)
{
case 'W':
{
if(y>0 && maze[x][y-1]!='X'&& maze[x][y-1]!='#')
{
maze[x][y]='_';
if(!S.empty())
{ K=S.top();}
S.push(make_pair(x,y));
y-=1;
if((K.first==x)&&(K.second==y))
{ lives--;}
maze[x][y] = '@';
steps++;
}
else if(maze[x][y-1]=='#')
{
cout<<"\n\t\t\tHURRAY!! Level Cleared !!";
y-=1;
break;
}
else
cout<<"\nCan't Move up";
if(lives==0)
{
cout<<"\n"<<player<<" Your Steps are:- "<<steps;
exit(0);
}
break;
}
case 'A':
{
if(x>0 && maze[x-1][y]!='X'&& maze[x-1][y]!='#')
{
maze[x][y]='_';
if(!S.empty())
{ K=S.top();}
S.push(make_pair(x,y));
x-=1;
if((K.first==x)&&(K.second==y))
{ lives--;}
maze[x][y] = '@';
steps++;
}
else if(maze[x-1][y]=='#')
{
cout<<"\n\t\t\tHURRAY!! Level Cleared !!";
x-=1;
break;
}
else
cout<<"\nCan't Move left";
if(lives==0)
{
cout<<"\n"<<player<<" Your Steps are:- "<<steps;
exit(0);
}
break;
}
case 'S':
{
if(y>=0 && y+1<n && maze[x][y+1]!='X' && maze[x][y+1]!='#')
{
maze[x][y]='_';
if(!S.empty())
{ K=S.top();}
S.push(make_pair(x,y));
y+=1;
if((K.first==x)&&(K.second==y))
{ lives--;}
maze[x][y] = '@';
steps++;
}
else if(maze[x][y+1]=='#')
{
cout<<"\n\t\t\tHURRAY!! Level Cleared !!";
y+=1;
break;
}
else
cout<<"\nCan't Move Down";
if(lives==0)
{
cout<<"\n"<<player<<" Your Steps are:- "<<steps;
exit(0);
}
break;
}
case 'D':
{
if(x>=0 && x+1<n && maze[x+1][y]!='X' && maze[x+1][y]!='#')
{
maze[x][y]='_';
if(!S.empty())
{ K=S.top();}
S.push(make_pair(x,y));
x+=1;
if((K.first==x)&&(K.second==y))
{ lives--;}
maze[x][y] = '@';
steps++;
}
else if(maze[x+1][y]=='#')
{
cout<<"\n\t\t\tHURRAY!! Level Cleared !!";
x+=1;
break;
}
else
cout<<"\nCan't Move Right";
if(lives==0)
{
cout<<"\n"<<player<<" Your Steps are:- "<<steps;
exit(0);
}
break;
}
case 'U':
{
int i,j;
if(S.empty()==true)
{ cout<<"\nUNDO NOT POSSIBLE";
break;
}
else
{
pair<int,int> P=S.top();
i=P.first;
j=P.second;
S.pop();
maze[x][y] = '_';
maze[i][j] = '@';
x=i;
y=j;
steps--;
}
if(lives==0)
{
cout<<"\n"<<player<<" Your Steps are:- "<<steps;
int count_player=0;
}
else
lives--;
break;
}
case 'Q':
{
cout<<"\n"<<player<<" Your Steps are:- "<<steps;
termin=1;
int count_player=0;
}
}
//system("clear");
if(termin==0)
{
drawMaze();
}
else
{
break;
}
}while(maze[x][y]!='#' && termin==0);
return(termin);
}
void scoreBoard(int shortest , double percent)
{
int longest = (n*n)-(percent*n*n);
int avg = (longest+shortest)/2;
int count=0;
if((steps+1)>avg)
{
score += 40;
}
else if((steps+1) == avg)
{
score+=50;
}
else if((steps+1)<avg)
{
if((steps+1)==shortest)
score+=100;
else
{
count = (steps+1)-shortest;
if(count==1)
score+=95;
else if(count==2)
score+=90;
else if(count==3)
score+=85;
else if(count==4)
score+=80;
else if(count==5)
score+=75;
else if(count==6)
score+=70;
else if(count==7)
score+=65;
else if(count==8)
score+=60;
else if(count==9)
score+=55;
else
score+=50;
}
}
}
};
class Levels
{
public:
double level(int size)
{
if(size==8)
return(.15);
if(size==9)
return(.2);
if(size==10)
return(.3);
if(size==11)
return(.35);
if(size==12)
return(.3);
}
};
class player
{
public:
string pl_name;
int pl_score;
};
bool compare(player i1,player i2)
{
return(i1.pl_score>i2.pl_score);
}
void add_player(string name,int sco,int count_player)
{
player p1;
p1.pl_name=name;
p1.pl_score=sco;
leader.push_back(p1);
count_player++;
}
void update_player(string name,int sco,int count_player)
{
for(int i=0;i<count_player;i++)
{
if(leader[i].pl_name==name)
{
leader[i].pl_name=sco;
count_player++;
}
}
}
int find_pl(string name,int count_player)
{
int temp=0;
for(int i=0;i<count_player;i++)
{
if(leader[count_player].pl_name==name)
{
temp=1;
}
}
return temp;
}
void create_leader()
{
sort(leader.begin(),leader.end(),compare);
ofstream m4("n1.txt");
for(int i=0;i<player_count;i++)
{
m4<<leader[i].pl_name<<" "<<leader[i].pl_score<<"\n";
}
m4.close();
}
void get_player_count()
{
ifstream m1("n1.txt");
if(!m1.fail())
{
string n;
while(!m1.eof())
{
getline(m1,n);
player_count++;
}
}
m1.close();
}
int find_player(string n1,int score_of_pl,int count_player)
{
string nam;
int score_p;
ifstream m2("n1.txt");
if(!m2.fail())
{
while(!m2.eof())
{
m2>>nam>>score_p;
if(nam!="")
{
add_player(nam,score_p,count_player);
}
}
if(find_pl(n1,count_player)!=1)
{
add_player(n1,score_of_pl,count_player);
}
else if(find_pl(n1,count_player))
{
update_player(n1,score_of_pl,count_player);
}
m2.close();
}
}
void show_leader()
{
int count_temp=1;
cout<<"RANK"<<" "<<"NAME"<<" "<<"SCORE"<<" "<<endl;
for(int i=0;i<player_count;i++)
{
cout<<count_temp<<" "<<leader[i].pl_name<<" "<<leader[i].pl_score<<endl;
count_temp++;
}
}
int main()
{
srand(time(NULL));
string name;
int n=8;
double c;
cout<<"********************************************** WELCOME TO CODE CAB!!!!**************************************************";
cout<<"\n\t Enter your name CABBY! :- "<<endl;
cin>>name;
cout<<endl;
getchar();
system("clear");
cout<<"HEY "<<name<<" "<<"YOU CAB IS @ IN THE GAME AND PASSENGER IS #"<<endl<<endl;
cout<<"LET ME ELABORATE YOU THE RULES OF THE GAME :-"<<endl<<endl;
cout<<"1) YOU HAVE 3 LIVES AND THEY REDUCE WHEN YOU TRY TO COPY YOUR PREVIOUS MOVE !!! \n2) THE GAME HAS 5 STAGES ... EACH STAGE HAS DIFFERENT POINTS...AND I BET YOU'LL ENJOY TILL THE LAST"<<endl;;
cout<<"\n3)AVOID TAKING ROUTES FULL OF OBSTACLES AND GET TO THE PASSENGER ON TIME !!!!! \n ENJOY DRIVING CABBY!!!!!!"<<endl;;
cout<<"\n PRESS ENTER TO PLAY"<<endl;
getchar();
int sp=0;
int player_score=0;
mazegame a(n,name);
Levels l;
Graph g(n) ;
while(counter<=5)
{
int sp=0;
char ch;
c = l.level(n);
while(sp==0)
{
a.redefine(n);
g.redef_graph(n);
a.randomObstruction(c);
g.init();
g.checkEdge();
sp=g.minEdgeBFS(0,(n*n)-1);
if(sp!=0)
{
a.drawMaze();
}
}
char lo;
int player_score;
int temp=a.move(0,0);
if(!temp)
{
a.scoreBoard(sp,c);
cout<<"\nYour total Steps were:"<<(a.steps+1);
cout<<"\nYour Score:"<<score;
cout<<"\nMin steps were: "<<sp<<endl;
cout<<"PRESS ENTER TO CONTINUE!!!!"<<endl;
cin>>lo;
}
else if(temp)
{
cout<<"\nYour Score: "<<0;
cout<<"\nMin steps were: "<<sp<<endl;
counter=6;
getchar();
cout<<"PRESS ENTER TO CONTINUE!!!!"<<endl;
cin>>lo;
break;
}
counter++;
n++;
}
int count_player=0;
get_player_count();
//player players[player_count];
find_player(a.player,score,count_player);
create_leader();
show_leader();
return 0;
}