-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacman.c
1053 lines (847 loc) · 38.2 KB
/
pacman.c
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************
* Pacman For Console V1.3 *
* By: Mike Billars ([email protected]) *
* Date: 2014-04-26 *
* *
* Please see file COPYING for details on licensing *
* and redistribution of this program *
* *
* Adapted by Nir Lipovetzky for COMP20003 (2019) *
* *
****************************************************/
/***********
* INCLUDES *
***********/
#include <stdio.h>
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/timeb.h>
#include <assert.h>
#include "pacman.h"
#include "node.h"
#include "ai.h"
#include "utils.h"
/*************
* PROTOTYPES *
*************/
void IntroScreen(); //Show introduction screen and menu
void CheckCollision(); //See if Pacman and Ghosts collided
void CheckScreenSize(); //Make sure resolution is at least 32x29
void CreateWindows(int y, int x, int y0, int x0); //Make ncurses windows
void Delay(); //Slow down game for better control
void DrawWindow(); //Refresh display
void ExitProgram(const char *message); //Exit and display something
void GetInput(); //Get user input
void InitCurses(); //Start up ncurses
void LoadLevel(char *levelfile); //Load level into memory
void MainLoop(); //Main program function
void MoveGhosts(); //Update Ghosts' location
void MovePacman(); //Update Pacman's location
void PauseGame(); //Pause
void update_current_state(); //sync current game state
void send_action(move_t move); //Like getInput but for AI agent
/*********
* MACROS *
*********/
#define TERMINAL_TYPE (strcmp(getenv("TERM"), "xterm") == 0 ? "rxvt" : \
getenv("TERM"))
/*******************
* GLOBAL VARIABLES *
*******************/
//I know global variables are bad, but it's just sooo easy!
char LevelFile[] = LEVELS_FILE; //Locations of default levels
int FreeLife = 1000; //Starting points for free life
int Points = 0; //Initial points
int Lives = 3; //Number of lives you start with
int SpeedOfGame = 175; //How much of a delay is in the game
//For output.txt (the global variable is suggested by a tutor)
int currDepth;
int totalGenerated;
int totalExpanded;
clock_t timeStart, timeEnd;
//Windows used by ncurses
WINDOW * win;
WINDOW * status;
SCREEN *mainScreen = NULL;
//For colors
enum { Wall = 1, Normal = 2, Pellet = 3, PowerUp = 4, GhostWall = 5, Ghost1 = 6, Ghost2 = 7, Ghost3 = 8, Ghost4 = 9, BlueGhost = 10, Pacman = 11 };
int Loc[5][2] = { 0 }; //Location of Ghosts and Pacman (Pacman loc in idx 4)
int Dir[5][2] = { 0 }; //Direction of Ghosts and Pacman
int StartingPoints[5][2] = { 0 }; //Default location in case Pacman/Ghosts die
int Invincible = 0; //Check for invincibility
int Food = 0; //Number of pellets left in level
int Level[29][28] = { 0 }; //Main level array
int LevelNumber = 0; //What level number are we on?
int GhostsInARow = 0; //Keep track of how many points to give for eating ghosts
int tleft = 0; //How long left for invincibility
int budget=0; //Max budget expanded nodes Dijkstra
bool ai_run = false; //Run AI
bool ai_pause = false; // To check stats of every action
propagation_t propagation; //Propagation type
state_t current_state; //Contains a copy of the current game state
char ai_stats[500]; //Info printed out about AI algorithm
void print_usage(){
printf("To run the AI solver: \n");
printf("USAGE: ./pacman <[level_#/[level_file_name> <ai/ai_pause> <max/avg> <budget> slow\n");
printf("or, to play with the keyboard: \n");
printf("USAGE: ././pacman <[level_#/[level_file_name>\n");
}
/****************************************************************
* Function: main() *
* Parameters: argc, argv (passed from command line) *
* Returns: Success *
* Description: Initiate the program and call the subfunctions *
****************************************************************/
int main(int argc, char *argv[100]) {
/**
* Parsing command line options
*/
if( argc > 2 && argc < 5 ){
print_usage();
return 0;
}
int j = 0;
srand( (unsigned)time( NULL ) );
InitCurses(); //Must be called to start ncurses
CheckScreenSize(); //Make sure screen is big enough
CreateWindows(29, 28, 1, 1); //Create the main and status windows
if (argc > 2 ) {
ai_run = true;
if( strcmp(argv[2],"ai_pause")==0 ){
ai_pause = true;
}
initialize_ai();
if( strcmp(argv[3],"avg")==0 ){
propagation = avg;
}
else if( strcmp(argv[3],"max")==0 ){
propagation = max;
}
else{
print_usage();
return 0;
}
sscanf (argv[4],"%d",&budget);
}
timeStart = clock(); //time when game started
//If they specified a level to load
if((argc > 1) && (strlen(argv[1]) > 1)) {
argv[1][99] = '\0';
LoadLevel(argv[1]); //Load it and...
MainLoop(); //Start the game
}
//If they did not enter a level, display intro screen then use default levels
else {
IntroScreen(); //Show intro "movie"
j = 1; //Set initial level to 1
if(argc > 1) j = argv[1][0] - '0'; //They specified a level on which to start (1-9)
//Load 9 levels, 1 by 1, if you can beat all 9 levels in a row, you're awesome
for(LevelNumber = j; LevelNumber < 10; LevelNumber++) {
//Replace level string underscore with the actual level number (see pacman.h)
LevelFile[strlen(LevelFile) - 6] = '0';
LevelFile[strlen(LevelFile) - 5] = LevelNumber + '0';
LoadLevel(LevelFile); //Load level into memory
Invincible = 0; //Reset invincibility with each new level
MainLoop(); //Start the level
}
}
//Game has ended, deactivate and end program
ExitProgram(EXIT_MSG);
//This doesn't need to be here, but just to be proper
return(0);
}
/****************************************************************
* Function: CheckCollision() *
* Parameters: none *
* Returns: none *
* Description: Check and handle if Pacman collided with a ghost *
****************************************************************/
void CheckCollision() {
//Temporary variable
int a = 0;
//Check each ghost, one at a time for collision
for(a = 0; a < 4; a++) {
//Ghost X and Y location is equal to Pacman X and Y location (collision)
if((Loc[a][0] == Loc[4][0]) && (Loc[a][1] == Loc[4][1])) {
//Pacman is invincible, ghost dies
if(Invincible == 1) {
Points = Points + GhostsInARow * 20; //Increase points by an increasing value
//Display the points earned for eating the ghost
mvwprintw(win, Loc[4][0], Loc[4][1] - 1, "%d", (GhostsInARow * 20));
GhostsInARow *= 2;
wrefresh(win); //Update display
usleep(1000000); //and pause for a second
//Reset the ghost's position to the starting location
Loc[a][0] = StartingPoints[a][0]; Loc[a][1] = StartingPoints[a][1];
}
//Pacman is vulnerable, Pacman dies
else {
//Display an X in position
wattron(win, COLOR_PAIR(Pacman));
mvwprintw(win, Loc[4][0], Loc[4][1], "X");
wrefresh(win);
//Subtract one life and pause for 1 second
Lives--;
usleep(1000000);
//If no more lives, game over
if(Lives == -1) ExitProgram(END_MSG);
//If NOT game over...
//Reset Pacman's and ghosts' positions
for(a = 0; a < 5; a++) {
Loc[a][0] = StartingPoints[a][0];
Loc[a][1] = StartingPoints[a][1];
}
//Reset directions
Dir[0][0] = 1; Dir[0][1] = 0;
Dir[1][0] = -1; Dir[1][1] = 0;
Dir[2][0] = 0; Dir[2][1] = -1;
Dir[3][0] = 0; Dir[3][1] = 1;
Dir[4][0] = 0; Dir[4][1] = -1;
DrawWindow(); //Redraw window
usleep(1000000); //Pause for 1 second before continuing game
}
}
}
}
/****************************************************************
* Function: CheckScreenSize() *
* Parameters: none *
* Returns: none *
* Description: Make sure the virtual console is big enough *
****************************************************************/
void CheckScreenSize() {
//Make sure the window is big enough
int h, w; getmaxyx(stdscr, h, w);
if((h < 35) || (w < 29)) {
endwin();
delscreen(mainScreen);
fprintf(stderr, "\nSorry.\n");
fprintf(stderr, "To play Pacman for Console, your console window must be at least 35x29\n");
fprintf(stderr, "Please resize your window/resolution and re-run the game.\n\n");
exit(0);
}
}
/****************************************************************
* Function: CreateWindows() *
* Parameters: y, x, y0, x0 (coords and size of window) *
* Returns: none *
* Description: Create the main game windows *
****************************************************************/
void CreateWindows(int y, int x, int y0, int x0) {
//Create two new windows, for status bar and for main level
win = newwin(y, x, y0, x0);
status = newwin(16, 130, 29, 1);
}
/****************************************************************
* Function: Delay() *
* Parameters: none *
* Returns: none *
* Description: Pause the game and still get keyboard input *
****************************************************************/
void Delay() {
//Needed to get time
struct timeb t_start, t_current;
ftime(&t_start);
//Slow down the game a little bit
do {
if(ai_run == false)
GetInput(); //Still get the input from keyboard
ftime(&t_current); //Update time and check if enough time has overlapped
} while (abs(t_start.millitm - t_current.millitm) < SpeedOfGame);
}
/****************************************************************
* Function: DrawWindow() *
* Parameters: none *
* Returns: none *
* Description: Redraw each window to update the screen *
****************************************************************/
void DrawWindow() {
//Temporary variables
int a = 0; int b = 0;
//Variable used to display certain characters
chtype chr = ' ';
//Variable that displays normal/bold character
chtype attr = 0;
//Display level array
for(a = 0; a < 29; a++) for(b = 0; b < 28; b++) {
//Determine which character is in location and display it
switch(Level[a][b]) {
case 0: chr = ' '; attr = A_NORMAL; wattron(win, COLOR_PAIR(Normal)); break;
case 1: chr = ' '; attr = A_NORMAL; wattron(win, COLOR_PAIR(Wall)); break;
case 2: chr = '.'; attr = A_NORMAL; wattron(win, COLOR_PAIR(Pellet)); break;
case 3: chr = '*'; attr = A_BOLD; wattron(win, COLOR_PAIR(PowerUp)); break;
case 4: chr = ' '; attr = A_NORMAL; wattron(win, COLOR_PAIR(GhostWall)); break;
}
//Display character. "attr" is ORed with chr as documented for mvwaddch,
// this will make the character bold/normal
//assert(mvwaddch(win, a, b, chr | attr) != ERR);
mvwaddch(win, a, b, chr | attr);
}
//Display number of lives, score, and level
attr = A_NORMAL; //Reset attribute in case it's set to "BOLD"
//Switch window to status, and change color
wmove(status, 1, 1);
wattron(status, COLOR_PAIR(Pacman));
//Display number of lives
for(a = 0; a < Lives; a++)
wprintw(status, "C ");
wprintw(status, " ");
//Display level number and score
wattron(status, COLOR_PAIR(Normal));
mvwprintw(status, 2, 2, "Level: %d Score: %d ", LevelNumber, Points);
mvwprintw(status, 3, 2, "%s", ai_stats);
wrefresh(status); //Update status window
//Display ghosts if Pacman is not invincible
if(Invincible == 0) {
wattron(win, COLOR_PAIR(Ghost1)); mvwaddch(win, Loc[0][0], Loc[0][1], '&');
wattron(win, COLOR_PAIR(Ghost2)); mvwaddch(win, Loc[1][0], Loc[1][1], '&');
wattron(win, COLOR_PAIR(Ghost3)); mvwaddch(win, Loc[2][0], Loc[2][1], '&');
wattron(win, COLOR_PAIR(Ghost4)); mvwaddch(win, Loc[3][0], Loc[3][1], '&');
}
//OR display vulnerable ghosts
else {
wattron(win, COLOR_PAIR(BlueGhost));
mvwaddch(win, Loc[0][0], Loc[0][1], tleft + '0');
mvwaddch(win, Loc[1][0], Loc[1][1], tleft + '0');
mvwaddch(win, Loc[2][0], Loc[2][1], tleft + '0');
mvwaddch(win, Loc[3][0], Loc[3][1], tleft + '0');
}
//Display Pacman
wattron(win, COLOR_PAIR(Pacman)); mvwaddch(win, Loc[4][0], Loc[4][1], 'C' | A_NORMAL);
wrefresh(win); //Update main window
}
/****************************************************************
* Function: DrawWindowState(state_t state) *
* Parameters: none *
* Returns: none *
* Description: Redraw each window given a game state *
* to update the screen *
****************************************************************/
void DrawWindowState(state_t state) {
//Temporary variables
int a = 0; int b = 0;
chtype chr = ' '; //Variable used to display certain characters
chtype attr = 0; //Variable that displays normal/bold character
//Display level array
for(a = 0; a < 29; a++) for(b = 0; b < 28; b++) {
//Determine which character is in location and display it
switch(state.Level[a][b]) {
case 0: chr = ' '; attr = A_NORMAL; wattron(win, COLOR_PAIR(Normal)); break;
case 1: chr = ' '; attr = A_NORMAL; wattron(win, COLOR_PAIR(Wall)); break;
case 2: chr = '.'; attr = A_NORMAL; wattron(win, COLOR_PAIR(Pellet)); break;
case 3: chr = '*'; attr = A_BOLD; wattron(win, COLOR_PAIR(PowerUp)); break;
case 4: chr = ' '; attr = A_NORMAL; wattron(win, COLOR_PAIR(GhostWall)); break;
}
//Display character. "attr" is ORed with chr as documented for mvwaddch,
// this will make the character bold/normal
mvwaddch(win, a, b, chr | attr);
}
//Display number of lives, score, and level
attr = A_NORMAL; //Reset attribute in case it's set to "BOLD"
//Switch window to status, and change color
wmove(status, 1, 1);
wattron(status, COLOR_PAIR(Pacman));
//Display number of lives
for(a = 0; a < state.Lives; a++)
wprintw(status, "C ");
wprintw(status, " ");
//Display level number and score
wattron(status, COLOR_PAIR(Normal));
mvwprintw(status, 2, 2, "Level: %d Score: %d ", state.LevelNumber, state.Points);
wrefresh(status); //Update status window
//Display ghosts if Pacman is not invincible
if(state.Invincible == 0) {
wattron(win, COLOR_PAIR(Ghost1)); mvwaddch(win, state.Loc[0][0], state.Loc[0][1], '&');
wattron(win, COLOR_PAIR(Ghost2)); mvwaddch(win, state.Loc[1][0], state.Loc[1][1], '&');
wattron(win, COLOR_PAIR(Ghost3)); mvwaddch(win, state.Loc[2][0], state.Loc[2][1], '&');
wattron(win, COLOR_PAIR(Ghost4)); mvwaddch(win, state.Loc[3][0], state.Loc[3][1], '&');
}
//OR display vulnerable ghosts
else {
wattron(win, COLOR_PAIR(BlueGhost));
mvwaddch(win, state.Loc[0][0], state.Loc[0][1], tleft + '0');
mvwaddch(win, state.Loc[1][0], state.Loc[1][1], tleft + '0');
mvwaddch(win, state.Loc[2][0], state.Loc[2][1], tleft + '0');
mvwaddch(win, state.Loc[3][0], state.Loc[3][1], tleft + '0');
}
//Display Pacman
wattron(win, COLOR_PAIR(Pacman));
chr = 'C';
mvwaddch(win, state.Loc[4][0], state.Loc[4][1], chr | A_NORMAL);
wrefresh(win); //Update main window
}
/****************************************************************
* Function: ExitProgram() *
* Parameters: message: text to display before exiting *
* Returns: none *
* Description: Gracefully end program *
****************************************************************/
void ExitProgram(const char *message) {
endwin(); //Uninitialize ncurses and destroy windows
delscreen(mainScreen);
timeEnd = clock(); //time where game ends
double total_t = ((double)(timeEnd - timeStart)) / CLOCKS_PER_SEC;
//string to for textfile report
char prop_type[4];
if(propagation == max){
strcpy(prop_type , "Max");
} else {
strcpy(prop_type , "Min");;
}
//Generating a textfile report
FILE *outputFile;
outputFile = fopen("output.txt", "w");
fprintf(outputFile, "Propagation=%s\n", prop_type);
fprintf(outputFile, "Budget=%d\n", budget);
fprintf(outputFile, "MaxDepth = %d\n", currDepth);
fprintf(outputFile, "TotalGenerated = %d\n", totalGenerated);
fprintf(outputFile, "TotalExpanded = %d\n", totalExpanded);
fprintf(outputFile, "Time = %.2f seconds\n", total_t);
fprintf(outputFile, "Expanded/Second = %d\n", (int)(totalExpanded/total_t));
fprintf(outputFile, "Score = %d\n", Points);
printf("%s\n", message); //Display message
printf("Final Score: %d\n",Points);
exit(0); //End program, return 0
}
/****************************************************************
* Function: GetInput() *
* Parameters: none *
* Returns: none *
* Description: Get input from user and take appropriate action *
****************************************************************/
void GetInput() {
int ch; //Key pushed
static int chtmp; //Buffered key
int tmp;
ch = getch(); //Figure out which key is pressed
//If they are not pressing something, use previous input
if(ch == ERR) ch = chtmp;
chtmp = ch;
//Determine which button is pushed
switch (ch) {
case KEY_UP: case 'w': case 'W': //Move pacman up
if(Loc[4][0] <= 0) tmp = LEVEL_HEIGHT - 1;
else tmp = Loc[4][0] - 1;
if((Level[tmp][Loc[4][1]] != 1)
&& (Level[tmp][Loc[4][1]] != 4))
{ Dir[4][0] = -1; Dir[4][1] = 0; }
break;
case KEY_DOWN: case 's': case 'S': //Move pacman down
if(Loc[4][0] >= 28) tmp = 0;
else tmp = Loc[4][0] + 1;
if((Level[tmp][Loc[4][1]] != 1)
&& (Level[tmp][Loc[4][1]] != 4))
{ Dir[4][0] = 1; Dir[4][1] = 0; }
break;
case KEY_LEFT: case 'a': case 'A': //Move pacman left
if(Loc[4][1] <= 0) tmp = LEVEL_WIDTH - 1;
else tmp = Loc[4][1] - 1;
if((Level[Loc[4][0]][tmp] != 1)
&& (Level[Loc[4][0]][tmp] != 4))
{ Dir[4][0] = 0; Dir[4][1] = -1; }
break;
case KEY_RIGHT: case 'd': case 'D': //Move pacman right
if(Loc[4][1] >= 27) tmp = 0;
else tmp = Loc[4][1] + 1;
if((Level[Loc[4][0]][tmp] != 1)
&& (Level[Loc[4][0]][tmp] != 4))
{ Dir[4][0] = 0; Dir[4][1] = 1; }
break;
case 'p': case 'P': //Pause game
PauseGame();
chtmp = getch(); //Update buffered input
break;
case 'q': case 'Q': //End program
ExitProgram(QUIT_MSG);
break;
}
}
void send_action(move_t move) {
int ch; //Key pushed
static int chtmp; //Buffered key
int tmp;
ch = getch(); //Figure out which key is pressed
//If they are not pressing something, use previous input
if(ch == ERR) ch = chtmp;
chtmp = ch;
//Determine which button is pushed
switch (move) {
case up: //Move pacman up
if(Loc[4][0] <= 0) tmp = LEVEL_HEIGHT - 1;
else tmp = Loc[4][0] - 1;
if((Level[tmp][Loc[4][1]] != 1)
&& (Level[tmp][Loc[4][1]] != 4))
{ Dir[4][0] = -1; Dir[4][1] = 0; }
break;
case down: //Move pacman down
if(Loc[4][0] >= 28) tmp = 0;
else tmp = Loc[4][0] + 1;
if((Level[tmp][Loc[4][1]] != 1)
&& (Level[tmp][Loc[4][1]] != 4))
{ Dir[4][0] = 1; Dir[4][1] = 0; }
break;
case left: //Move pacman left
if(Loc[4][1] <= 0) tmp = LEVEL_WIDTH - 1;
else tmp = Loc[4][1] - 1;
if((Level[Loc[4][0]][tmp] != 1)
&& (Level[Loc[4][0]][tmp] != 4))
{ Dir[4][0] = 0; Dir[4][1] = -1; }
break;
case right: //Move pacman right
if(Loc[4][1] >= 27) tmp = 0;
else tmp = Loc[4][1] + 1;
if((Level[Loc[4][0]][tmp] != 1)
&& (Level[Loc[4][0]][tmp] != 4))
{ Dir[4][0] = 0; Dir[4][1] = 1; }
break;
}
}
/****************************************************************
* Function: InitCurses() *
* Parameters: none *
* Returns: none *
* Description: Initialize ncurses and set defined colors *
****************************************************************/
void InitCurses() {
//initscr(); //Needed for ncurses windows
mainScreen = newterm(TERMINAL_TYPE, stdout, stdin);
set_term(mainScreen);
start_color(); //Activate colors in console
curs_set(0); // Don't remember
keypad(stdscr, TRUE); //Activate arrow keys
nodelay(stdscr, TRUE); //Allow getch to work without pausing
nonl(); // Don't remember
cbreak(); // Don't remember
noecho(); //Don't display input key
//Make custom text colors
init_pair(Normal, COLOR_WHITE, COLOR_BLACK);
init_pair(Wall, COLOR_WHITE, COLOR_WHITE);
init_pair(Pellet, COLOR_WHITE, COLOR_BLACK);
init_pair(PowerUp, COLOR_BLUE, COLOR_BLACK);
init_pair(GhostWall, COLOR_WHITE, COLOR_CYAN);
init_pair(Ghost1, COLOR_RED, COLOR_BLACK);
init_pair(Ghost2, COLOR_CYAN, COLOR_BLACK);
init_pair(Ghost3, COLOR_MAGENTA, COLOR_BLACK);
init_pair(Ghost4, COLOR_YELLOW, COLOR_BLACK);
init_pair(BlueGhost, COLOR_BLUE, COLOR_RED);
init_pair(Pacman, COLOR_YELLOW, COLOR_BLACK);
}
/****************************************************************
* Function: IntroScreen() *
* Parameters: none *
* Returns: none *
* Description: Display the introduction animation *
****************************************************************/
void IntroScreen() {
int a = 0;
int b = 23;
a=getch(); a=getch(); a=getch();//Clear the buffer
mvwprintw(win, 20, 8, "Press any key...");
//Scroll Pacman to middle of screen
for(a = 0; a < 13; a++) {
if(getch()!=ERR) return;
wattron(win, COLOR_PAIR(Pacman));
mvwprintw(win, 8, a, " C");
wrefresh(win);
usleep(100000);
}
//Show "Pacman"
wattron(win, COLOR_PAIR(Pacman));
mvwprintw(win, 8, 12, "PACMAN");
wrefresh(win);
usleep(1000000);
//Ghosts chase Pacman
for(a = 0; a < 23; a++) {
if(getch()!=ERR) return;
wattron(win, COLOR_PAIR(Pellet)); mvwprintw(win, 13, 23, "*");
wattron(win, COLOR_PAIR(Pacman)); mvwprintw(win, 13, a, " C");
wattron(win, COLOR_PAIR(Ghost1)); mvwprintw(win, 13, a-3, " &");
wattron(win, COLOR_PAIR(Ghost3)); mvwprintw(win, 13, a-5, " &");
wattron(win, COLOR_PAIR(Ghost2)); mvwprintw(win, 13, a-7, " &");
wattron(win, COLOR_PAIR(Ghost4)); mvwprintw(win, 13, a-9, " &");
wrefresh(win);
usleep(100000);
}
usleep(150000);
//Pacman eats powerup and chases Ghosts
for(a = 25; a > 2; a--) {
if(getch()!=ERR) return;
wattron(win, COLOR_PAIR(Pellet)); mvwprintw(win, 13, 23, " ");
//Make ghosts half as fast so Pacman can catch them
if(a%2) b--;
wattron(win, COLOR_PAIR(BlueGhost)); mvwprintw(win, 13, b-9, "& & & &");
//Erase the old positions of ghosts
wattron(win, COLOR_PAIR(Pacman)); mvwprintw(win, 13, b-9+1, " ");
wattron(win, COLOR_PAIR(Pacman)); mvwprintw(win, 13, b-9+3, " ");
wattron(win, COLOR_PAIR(Pacman)); mvwprintw(win, 13, b-9+5, " ");
wattron(win, COLOR_PAIR(Pacman)); mvwprintw(win, 13, b-9+7, " ");
wattron(win, COLOR_PAIR(Pacman)); mvwprintw(win, 13, a-3, "C ");
wattron(win, COLOR_PAIR(Pellet)); mvwprintw(win, 13, 23, " ");
wrefresh(win);
usleep(100000);
}
}
/****************************************************************
* Function: LoadLevel() *
* Parameters: levelfile: name of file to load *
* Returns: none *
* Description: Open level file and load it into memory *
****************************************************************/
void LoadLevel(char levelfile[100]) {
int a = 0; int b = 0;
size_t l;
char error[sizeof(LEVEL_ERR)+255] = LEVEL_ERR; //Let's assume an error
FILE *fin; //New file variable
Food = 0; //Used to count how many food pellets must be eaten
//Reset defaults
Dir[0][0] = 1; Dir[0][1] = 0;
Dir[1][0] = -1; Dir[1][1] = 0;
Dir[2][0] = 0; Dir[2][1] = -1;
Dir[3][0] = 0; Dir[3][1] = 1;
Dir[4][0] = 0; Dir[4][1] = -1;
//Open file
fin = fopen(levelfile, "r");
//Make sure it didn't fail
if(!(fin)) {
l = sizeof(error)-strlen(error)-1;
strncat(error, levelfile, l);
if(strlen(levelfile) > l) {
error[sizeof(error)-2] = '.';
error[sizeof(error)-3] = '.';
error[sizeof(error)-4] = '.';
}
ExitProgram(error);
}
//Open file and load the level into the array
for(a = 0; a < 29; a++) {
for(b = 0; b < 28; b++) {
fscanf(fin, "%d", &Level[a][b]); //Get character from file
if(Level[a][b] == 2) { Food++; } //If it's a pellet, increase the pellet count
//Store ghosts' and Pacman's locations
if(Level[a][b] == 5) { Loc[0][0] = a; Loc[0][1] = b; Level[a][b] = 0;}
if(Level[a][b] == 6) { Loc[1][0] = a; Loc[1][1] = b; Level[a][b] = 0;}
if(Level[a][b] == 7) { Loc[2][0] = a; Loc[2][1] = b; Level[a][b] = 0;}
if(Level[a][b] == 8) { Loc[3][0] = a; Loc[3][1] = b; Level[a][b] = 0;}
if(Level[a][b] == 9) { Loc[4][0] = a; Loc[4][1] = b; Level[a][b] = 0;}
}
}
//All done, now get the difficulty level (AKA level number)
fscanf(fin, "%d", &LevelNumber);
//Save initial character positions in case Pacman or Ghosts die
for(a = 0; a < 5; a++) {
StartingPoints[a][0] = Loc[a][0]; StartingPoints[a][1] = Loc[a][1];
}
}
void update_current_state(){
//Location of Ghosts and Pacman
memcpy( current_state.Loc, Loc, 5*2*sizeof(int) );
//Direction of Ghosts and Pacman
memcpy( current_state.Dir, Dir, 5*2*sizeof(int) );
//Default location in case Pacman/Ghosts die
memcpy( current_state.StartingPoints, StartingPoints, 5*2*sizeof(int) );
//Check for invincibility
current_state.Invincible = Invincible;
//Number of pellets left in level
current_state.Food = Food;
//Main level array
memcpy( current_state.Level, Level, 29*28*sizeof(int) );
//What level number are we on?
current_state.LevelNumber=LevelNumber;
//Keep track of how many points to give for eating ghosts
current_state.GhostsInARow = GhostsInARow;
//How long left for invincibility
current_state.tleft = tleft;
//Initial points
current_state.Points = Points;
//Remiaining Lives
current_state.Lives = Lives;
}
/****************************************************************
* Function: MainLoop() *
* Parameters: none *
* Returns: none *
* Description: Control the main execution of the game *
****************************************************************/
void MainLoop() {
DrawWindow(); //Draw the screen
wrefresh(win); wrefresh(status); //Refresh it just to make sure
usleep(1000000); //Pause for a second so they know they're about to play
/* Move Pacman. Move ghosts. Check for extra life awarded
from points. Pause for a brief moment. Repeat until all pellets are eaten */
do {
MovePacman(); DrawWindow(); CheckCollision();
MoveGhosts(); DrawWindow(); CheckCollision();
if(Points > FreeLife) { Lives++; FreeLife *= 2;}
/**
* AI execution mode
*/
if(ai_run){
update_current_state();
/**
* ****** HERE IS WHERE YOUR SOLVER IS CALLED
*/
move_t selected_move = get_next_move( current_state, budget, propagation, ai_stats, &currDepth, &totalGenerated, &totalExpanded );
/**
* Execute the selected action
*/
send_action(selected_move);
if(ai_pause){
DrawWindow();
PauseGame();
}
}
Delay();
} while (Food > 0);
DrawWindow(); //Redraw window and...
usleep(1000000); //Pause, level complete
}
/****************************************************************
* Function: MoveGhosts() *
* Parameters: none *
* Returns: none *
* Description: Move all ghosts and check for wall collisions *
****************************************************************/
void MoveGhosts() {
//Set up some temporary variables
int a = 0; int b = 0; int c = 0;
int checksides[] = { 0, 0, 0, 0, 0, 0 };
static int SlowerGhosts = 0;
int tmp;
/* Move ghosts slower when they are vulnerable. This will allow
the ghosts to move only x times for every y times Pacman moves */
if(Invincible == 1) {
SlowerGhosts++;
if(SlowerGhosts > HOW_SLOW)
SlowerGhosts = 0;
}
//If ghosts are not vulnerable OR the ghosts can move now
if((Invincible == 0) || SlowerGhosts < HOW_SLOW)
//Loop through each ghost, one at a time
for(a = 0; a < 4; a++) {
//Switch sides? (Transport to other side of screen)
if((Loc[a][0] == 0) && (Dir[a][0] == -1)) Loc[a][0] = 28;
else if((Loc[a][0] == 28) && (Dir[a][0] == 1)) Loc[a][0] = 0;
else if((Loc[a][1] == 0) && (Dir[a][1] == -1)) Loc[a][1] = 27;
else if((Loc[a][1] == 27) && (Dir[a][1] == 1)) Loc[a][1] = 0;
else {
//Determine which directions we can go
for(b = 0; b < 4; b++) checksides[b] = 0;
if(Loc[a][0] == 28) tmp = 0;
else tmp = Loc[a][0] + 1;
if(Level[tmp][Loc[a][1]] != 1) checksides[0] = 1;
if(Loc[a][0] == 0) tmp = 28;
else tmp = Loc[a][0] - 1;
if(Level[tmp][Loc[a][1]] != 1) checksides[1] = 1;
if(Loc[a][1] == 27) tmp = 0;
else tmp = Loc[a][1] + 1;
if(Level[Loc[a][0]][tmp] != 1) checksides[2] = 1;
if(Loc[a][1] == 0) tmp = 27;
else tmp = Loc[a][1] - 1;
if(Level[Loc[a][0]][tmp] != 1) checksides[3] = 1;
//Don't do 180 unless we have to
c = 0; for(b = 0; b < 4; b++) if(checksides[b] == 1) c++;
if(c > 1) {
if(Dir[a][0] == 1) checksides[1] = 0;
else if(Dir[a][0] == -1) checksides[0] = 0;
else if(Dir[a][1] == 1) checksides[3] = 0;
else if(Dir[a][1] == -1) checksides[2] = 0;
}
c = 0;
do {
//Decide direction, based somewhat-randomly
b = (int)(rand() / (1625000000 / 4));
/* Tend to mostly chase Pacman if he is vulnerable
or run away when he is invincible */
if(checksides[b] == 1) {
if(b == 0) { Dir[a][0] = 1; Dir[a][1] = 0; }
else if(b == 1) { Dir[a][0] = -1; Dir[a][1] = 0; }
else if(b == 2) { Dir[a][0] = 0; Dir[a][1] = 1; }
else if(b == 3) { Dir[a][0] = 0; Dir[a][1] = -1; }
}
else {
if(Invincible == 0) {
//Chase Pacman
if((Loc[4][0] > Loc[a][0]) && (checksides[0] == 1)) { Dir[a][0] = 1; Dir[a][1] = 0; c = 1; }
else if((Loc[4][0] < Loc[a][0]) && (checksides[1] == 1)) { Dir[a][0] = -1; Dir[a][1] = 0; c = 1; }
else if((Loc[4][1] > Loc[a][1]) && (checksides[2] == 1)) { Dir[a][0] = 0; Dir[a][1] = 1; c = 1; }
else if((Loc[4][1] < Loc[a][1]) && (checksides[3] == 1)) { Dir[a][0] = 0; Dir[a][1] = -1; c = 1; }
}
else {
//Run away from Pacman
if((Loc[4][0] > Loc[a][0]) && (checksides[1] == 1)) { Dir[a][0] = -1; Dir[a][1] = 0; c = 1; }
else if((Loc[4][0] < Loc[a][0]) && (checksides[0] == 1)) { Dir[a][0] = 1; Dir[a][1] = 0; c = 1; }
else if((Loc[4][1] > Loc[a][1]) && (checksides[3] == 1)) { Dir[a][0] = 0; Dir[a][1] = -1; c = 1; }
else if((Loc[4][1] < Loc[a][1]) && (checksides[2] == 1)) { Dir[a][0] = 0; Dir[a][1] = 1; c = 1; }
}
}
} while ((checksides[b] == 0) && (c == 0));
//Move Ghost
Loc[a][0] += Dir[a][0];
Loc[a][1] += Dir[a][1];
}
}
}
/****************************************************************
* Function: MovePacman() *
* Parameters: none *
* Returns: none *
* Description: Move Pacman and check for wall collisions *
****************************************************************/
void MovePacman() {
static int itime = 0;
//Switch sides? (Transport to other side of screen)
if((Loc[4][0] == 0) && (Dir[4][0] == -1)) Loc[4][0] = 28;
else if((Loc[4][0] == 28) && (Dir[4][0] == 1)) Loc[4][0] = 0;
else if((Loc[4][1] == 0) && (Dir[4][1] == -1)) Loc[4][1] = 27;
else if((Loc[4][1] == 27) && (Dir[4][1] == 1)) Loc[4][1] = 0;
//Or
else {
//Move Pacman
Loc[4][0] += Dir[4][0];
Loc[4][1] += Dir[4][1];