-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing.txt
1404 lines (1169 loc) · 31.1 KB
/
listing.txt
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
/**********************************************************************
Module: console.c
Author: Jim Young
Date: 2017 May 3
Purpose: see console.h
Changes:
2017 May 3 [ Jim Young ]
NOTES: none
**********************************************************************/
#include "console.h"
#include <curses.h>
#include <string.h>
#include <time.h> /*for nano sleep */
#include <stdbool.h>
static int CON_WIDTH, CON_HEIGHT;
static int consoleLock = false;
static int MAX_STR_LEN = 256; /* for strlen checking */
/* Local functions */
static bool checkConsoleSize(int reqHeight, int reqWidth)
{
if ( (reqWidth > COLS) || (reqHeight > LINES) )
{
fprintf(stderr, "\n\n\rSorry, your window is only %ix%i. \n\r%ix%i is required. Sorry.\n\r", COLS, LINES, reqWidth, reqHeight);
return (false);
}
return(true);
}
bool consoleInit(int height, int width, char *image[]) /* assumes image height/width is same as height param */
{
bool status;
initscr();
crmode();
noecho();
clear();
CON_HEIGHT = height; CON_WIDTH = width;
status = checkConsoleSize(CON_HEIGHT, CON_WIDTH);
if (status)
{
consoleDrawImage(0, 0, image, CON_HEIGHT);
consoleRefresh();
}
return(status);
}
void consoleDrawImage(int row, int col, char *image[], int height)
{
int i, length;
int newLeft, newRight, newOffset, newLength;
if (consoleLock) return;
newLeft = col < 0 ? 0 : col;
newOffset = col < 0 ? -col : 0;
for (i = 0; i < height; i++)
{
if (row+i < 0 || row+i >= CON_HEIGHT)
continue;
length = strnlen(image[i], MAX_STR_LEN);
newRight = col+length >= CON_WIDTH ? CON_WIDTH-1 : col+length;
newLength = newRight - newLeft + 1;
if (newOffset >= length || newLength <= 0)
continue;
if (mvaddnstr(row+i, newLeft, image[i]+newOffset, newLength) == ERR)
fprintf(stderr, "ERROR drawing to screen"); /* smarter handling is needed */
}
}
void consoleClearImage(int row, int col, int height, int width)
{
int i, j;
if (consoleLock) return;
if (col+width > CON_WIDTH)
width = CON_WIDTH-col;
if (col < 0)
{
width += col; /* -= -col */
col = 0;
}
if (width < 1 || col >= CON_WIDTH) /* nothing to clear */
return;
for (i = 0; i < height; i++)
{
if (row+i < 0 || row+i >= CON_HEIGHT)
continue;
move(row+i, col);
for (j = 0; j < width; j++)
addch(' ');
}
}
void consoleRefresh(void)
{
if (!consoleLock)
{
move(LINES-1, COLS-1);
refresh();
}
}
void consoleFinish(void)
{
endwin();
}
void putBanner(const char *str)
{
if (consoleLock) return;
int len;
len = strnlen(str,MAX_STR_LEN);
move (CON_HEIGHT/2, (CON_WIDTH-len)/2);
addnstr(str, len);
consoleRefresh();
}
void putString(char *str, int row, int col, int maxlen)
{
if (consoleLock) return;
move(row, col);
addnstr(str, maxlen);
}
/* setup to work in USECS, reduces risk of overflow */
/* 10000 usec = 10 ms, or 100fps */
#define TIMESLICE_USEC 10000
#define TIME_USECS_SIZE 1000000
#define USEC_TO_NSEC 1000
struct timespec getTimeout(int ticks)
{
struct timespec rqtp;
/* work in usecs at first */
rqtp.tv_nsec = TIMESLICE_USEC * ticks;
/* handle usec overflow */
rqtp.tv_sec = rqtp.tv_nsec / TIME_USECS_SIZE;
rqtp.tv_nsec %= TIME_USECS_SIZE;
rqtp.tv_nsec *= USEC_TO_NSEC; /*convert to nsecs */
return rqtp;
}
void sleepTicks(int ticks)
{
if (ticks <= 0)
return;
struct timespec rqtp = getTimeout(ticks);
nanosleep(&rqtp, NULL);
}
#define FINAL_PAUSE 2
void finalKeypress()
{
flushinp();
sleepTicks(FINAL_PAUSE);
move(LINES-1, COLS-1);
getch(); /* wait for user to press a character, blocking. */
}
void disableConsole(int disabled)
{
consoleLock = disabled;
}
/* COMP 3430
* PROF: JIM YOUNG
* REBECCA TIESSEN
*
* This file takes care of the main game logic, and setting up initializer
* methods. It also keeps track of player lives
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "console.h"
#include "frogger.h"
#include "threadwrappers.h"
#include "player.h"
#include "gameglobals.h"
#include "log.h"
#include "llist.h"
//-------------------------------------------------------------------//
int main(int argc, char**argv) {
startGame();
printf("done!\n");
}
void startGame(){
//initialize all mutexes and condLock
initLocks();
if(drawScreen()){
createThread(&tids[getThreadCount()], updateLives, NULL);
createThread(&tids[getThreadCount()], refreshScreen, NULL);
initializePlayer();
initializeLogs();
lockMutex(&mainLock);
while(!isGameOver()){
pthread_cond_wait(&condLock, &mainLock);
}
unlockMutex(&mainLock);
}
finalKeypress();
int i;
for(i = 0; i < getThreadCount(); i++){
joinThread(tids[i]);
}
destroyLocks();
free(getFrog());
deleteList();
consoleFinish();
}
void *refreshScreen(){
while(!isGameOver()){
lockMutex(&screenLock);
consoleRefresh();
unlockMutex(&screenLock);
sleepTicks(1);
}
pthread_exit(NULL);
}
void *updateLives(){
Frog *frog = getFrog();
int lifeCount = MAX_LIVES;
char strLives[2];
while(frog == NULL){
frog = getFrog(); //deals with initialization of thread before frog is created
}
while(!isGameOver()){
if(frog->dead){
lifeCount--;
sprintf(strLives, "%d", lifeCount);
lockMutex(&screenLock);
putString(strLives, 0, 42, 1);
unlockMutex(&screenLock);
lockMutex(&playerLock);
frog->dead = false;
unlockMutex(&playerLock);
}
sleepTicks(1);
if(lifeCount <= 0){
endGame("GAME OVER");
}
}
pthread_exit(NULL);
}
void endGame(char *endMsg){
lockMutex(&screenLock);
putBanner(endMsg);
disableConsole(1);
unlockMutex(&screenLock);
lockMutex(&mainLock);
setGameOver();
pthread_cond_signal(&condLock);
unlockMutex(&mainLock);
}
/* COMP 3430
* PROF: JIM YOUNG
* REBECCA TIESSEN
*
* This file includes methods used by all files in order to check for game over, thread count, and setting up locks.
*
*/
#include <pthread.h>
#include <stdbool.h>
#include "gameglobals.h"
#include "console.h"
static bool gameOver = false;
static int threadCount = 0;
char *GAME_BOARD[] = {
" Lives: 4",
"/------\\ /------\\ /------\\ /------\\ /------\\",
"| | | | | | | | | |",
"+ +----------+ +----------+ +----------+ +----------+ +",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"",
"",
"",
"" };
bool drawScreen(){
return consoleInit(GAME_ROWS, GAME_COLS, GAME_BOARD);
}
void initLocks(){
// Mutex locks
pthread_mutex_init(&screenLock, NULL);
pthread_mutex_init(&playerLock, NULL);
pthread_mutex_init(&mainLock, NULL);
pthread_mutex_init(&threadCountLock, NULL);
pthread_mutex_init(&listLock, NULL);
// Conditional variable lock
pthread_cond_init(&condLock, NULL);
}
void destroyLocks(){
pthread_mutex_destroy(&screenLock);
pthread_mutex_destroy(&playerLock);
pthread_mutex_destroy(&mainLock);
pthread_mutex_destroy(&threadCountLock);
pthread_mutex_destroy(&listLock);
pthread_cond_destroy(&condLock);
}
void setGameOver(){
gameOver = true;
}
bool isGameOver(){
return gameOver;
}
int getThreadCount(){
return threadCount;
}
void incrementThreadCount(i){
threadCount++;
}
void decrementThreadCount(){
threadCount--;
}
void sleepLoop(int numTicks){
int i;
for(i = 0; i < numTicks && !isGameOver(); i++){
sleepTicks(i);
}
}
/* COMP 3430
* PROF: JIM YOUNG
* REBECCA TIESSEN
*
* This file holds the linked list that the logs are stored in when created. There are standard insert and search/delete methods,
* as well as traversals that use a static Node to keep track of the current place in the list.
*
*/
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include "log.h"
#include "llist.h"
static Node *top = NULL;
static Node *traverseNode = NULL;
static Node *toDelete = NULL;
static int logCount = 0;
bool insert(Log *newLog){
bool success = false;
Node *newNode = NULL;
if(newLog != NULL){
newNode = (Node *)malloc(sizeof(Node));
if(newNode != NULL){
newNode->log = newLog;
newNode->next = top;
top = newNode;
logCount++;
success = true;
}
else{
free(newNode);
newNode = NULL;
}
}
return success;
}
bool searchAndRemove(Log *log){
bool deleted = false;
Node *curr = top;
Node *prev = NULL;
if(curr != NULL){
while(curr != NULL && curr->log != log){
prev = curr;
curr = curr->next;
}
if(curr != NULL){
if(prev != NULL){
prev->next = curr->next;
}
else{
top = curr->next;
}
free(curr->log);
curr->log = NULL;
free(curr);
curr = NULL;
deleted = true;
logCount--;
}
}
return deleted;
}
Log *firstItem(){
Log *first = NULL;
if(top != NULL){
traverseNode = top->next;
first = top->log;
}
return first;
}
Log *nextAvailableLog(){ //for frog search method
Log *next = NULL;
if(traverseNode != NULL){
next = traverseNode->log;
traverseNode = traverseNode->next;
}
return next;
}
bool listIsEmpty(){
return top == NULL;
}
Log *cleanupNextLog(){ //for cleanupLogs method
Log *potentialDelete = NULL;
if(toDelete == NULL){
toDelete = top;
}
if(toDelete != NULL){
potentialDelete = toDelete->log;
toDelete = toDelete->next;
}
return potentialDelete;
}
void deleteList(){
Node *temp = NULL;
while(top != NULL){
temp = top;
top = top->next;
free(temp->log);
free(temp);
}
}
/* COMP 3430
* PROF: JIM YOUNG
* REBECCA TIESSEN
*
* This file takes care of animating, moving, and spawning logs. The frog is allowed to jump on the logs to get to the home bank.
*
*/
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "log.h"
#include "console.h"
#include "threadwrappers.h"
#include "gameglobals.h"
#include "llist.h"
#include "player.h"
#define LOG_ANIM_TILES 2
#define LOG_HEIGHT 4
#define NUM_ROWS 4
Frog *frog;
static int log_width;
static char* LOG_GRAPHIC[LOG_ANIM_TILES][LOG_HEIGHT+1] = {
{"/======================\\",
"| |",
"| |",
"\\======================/"},
{"/======================\\",
"- +",
"+ -",
"\\======================/"}
};
//---PROTOTYPES------------------------------------------------//
static void drawLog();
static void setLogWidth();
//---METHODS---------------------------------------------------//
void initializeLogs(){
frog = getFrog();
setLogWidth();
enum row rows[] = {one, two, three, four};
int i;
for(i = 0; i < NUM_ROWS; i++){
void *row = malloc(sizeof(int));
*((int*)row) = rows[i];
createThread(&tids[getThreadCount()], runLogs, (void *)row);
}
createThread(&tids[getThreadCount()], cleanUpLogs, NULL);
}
void *runLogs(void *startRow){
int *row = (int *)startRow;
while(!isGameOver()){
Log *newLog = (Log *)malloc(sizeof(Log));
logStartup(newLog, row);
lockMutex(&listLock);
insert(newLog);
unlockMutex(&listLock);
createLogThread(&(newLog->threadID), logController, (void *)newLog);
sleepTicks((rand()%200)+150); //random log generation speed
}
pthread_exit(NULL);
}
void *logController(void *currLog){
Log *log = (Log *)currLog;
while(!isGameOver() && !log->dead){
if(log->hasFrog){
moveFrogAndLog(log);
moveFrogAndLog(log);
}
else{
moveLog(log);
moveLog(log);
animateLog(log);
}
sleepTicks(log->speed);
}
pthread_exit(NULL);
}
void moveLog(Log *log){
if(log->direction == left){
log->prevCol = log->currCol;
log->currCol--;
}
else{
log->prevCol = log->currCol;
log->currCol++;
}
drawLog(log);
checkIsDead(log);
}
void animateLog(Log *log){
if(log->animateState == first)
log->animateState = second;
else
log->animateState = first;
drawLog(log);
}
static void drawLog(Log *log){
char** tile = LOG_GRAPHIC[log->animateState];
lockMutex(&screenLock);
consoleClearImage(log->startRow, log->prevCol, LOG_HEIGHT, log_width);
consoleDrawImage(log->startRow, log->currCol, tile, LOG_HEIGHT);
unlockMutex(&screenLock);
}
void moveFrogAndLog(Log *log){
moveLog(log);
animateLog(log);
if(log->direction == left)
moveFrog(LEFT_KEY);
else
moveFrog(RIGHT_KEY);
}
void *cleanUpLogs(){
Log *curr = NULL;
Log *logToDelete = NULL;
while(!isGameOver()){
lockMutex(&listLock);
curr = cleanupNextLog();
unlockMutex(&listLock);
while(curr != NULL && !isGameOver()){
if(curr->dead){
joinLogThread(curr->threadID);
logToDelete = curr;
}
if(logToDelete != NULL){
lockMutex(&listLock);
searchAndRemove(logToDelete);
unlockMutex(&listLock);
logToDelete = NULL;
}
lockMutex(&listLock);
curr = cleanupNextLog();
unlockMutex(&listLock);
sleepTicks(1);
}
unlockMutex(&listLock);
sleepLoop(100);
}
pthread_exit(NULL);
}
void logStartup(Log *log, int *startRow){
log->startRow = (enum row)*startRow;
setLogSpeed(log, startRow);
setDirection(log);
log->width = log_width;
log->height = LOG_HEIGHT;
log->animateState = first;
if(log->direction == left){
log->prevCol = log->currCol = RIGHT_EDGE;
}
else{
log->prevCol = log->currCol = LEFT_EDGE-log->width;
}
log->dead = false;
log->hasFrog = false;
}
void setDirection(Log *log){
if(log->startRow%8 == 0) //alternate every other row
log->direction = left;
else
log->direction = right;
}
void setLogSpeed(Log *log, int *startRow){
enum row currRow = (enum row)*startRow;
if(currRow == one)
log->speed = 5;
else if(currRow == two)
log->speed = 7;
else if(currRow == three)
log->speed = 10;
else
log->speed = 12;
}
void checkIsDead(Log *log){
if(log->currCol > RIGHT_EDGE || log->currCol < LEFT_EDGE-log->width){
log->dead = true;
}
}
void setLogWidth(){
char **tile = LOG_GRAPHIC[0];
log_width = strlen(tile[0]);
}
/* COMP 3430
* PROF: JIM YOUNG
* REBECCA TIESSEN
*
* Player.c includes all the logic required for the frog to move around, animate, and die if it hits the water.
* It also keeps track of the number of safe pods you've made it to.
*
*/
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/select.h> //for keyboard functinonality
#include <stdlib.h>
#include "player.h"
#include "threadwrappers.h"
#include "console.h"
#include "gameglobals.h"
#include "log.h"
#include "llist.h"
#include "frogger.h"
#define PLAYER_ANIM_TILES 2
#define PLAYER_HEIGHT 2
#define F_START_ROW 21
#define F_START_COL 40
#define VERTICAL_JUMP 4
#define SIDE_JUMP 1
#define HOME_JUMP 3
static char* PLAYER_GRAPHIC[PLAYER_ANIM_TILES][PLAYER_HEIGHT+1] = {
{"@@",
"<>"},
{"--",
"<>"}
};
static const int SAFE_POD_1 = 0;
static const int SAFE_POD_2 = 18;
static const int SAFE_POD_3 = 36;
static const int SAFE_POD_4 = 54;
static const int SAFE_POD_5 = 72;
Frog *frog; //global frog
//---PROTOTYPES---------------------------------------------------------
static void updatePrevious();
static void drawFrog();
//---METHODS------------------------------------------------------------//
void initializePlayer(){
frog = (Frog *)malloc(sizeof(Frog));
createFrog();
createThread(&tids[getThreadCount()], animateFrog, NULL);
createThread(&tids[getThreadCount()], initMovement, NULL);
}
void *animateFrog(){
while(!isGameOver()){
lockMutex(&playerLock);
if(frog->animateState == first)
frog->animateState = second;
else
frog->animateState = first;
unlockMutex(&playerLock);
drawFrog();
sleepTicks(frog->blinkSpeed);
}
pthread_exit(NULL);
}
void *initMovement(){
fd_set set;
while(!isGameOver()){
FD_ZERO(&set);
FD_SET(STDIN_FILENO, &set);
struct timespec timeout = getTimeout(1); /* duration of one tick */
int ret = pselect(FD_SETSIZE, &set, NULL, NULL, &timeout, NULL);
if(ret == -1){
exit(1);
}
else if(ret == 0){
continue; //pselect timed out, no chars were entered
}
else{
char c = getchar();
if(c == QUIT){
endGame("quitters never prosper");
}
else{
moveFrog(c);
}
}
sleepTicks(1);
}
pthread_exit(NULL);
}
void moveFrog(char c){
if(c == LEFT_KEY && frog->currPos[1] > LEFT_EDGE){
lockMutex(&playerLock);
updatePrevious();
frog->currPos[1] -= SIDE_JUMP;
unlockMutex(&playerLock);
}else if(c == RIGHT_KEY && frog->currPos[1] < RIGHT_EDGE-frog->width){ //screen size - width of frog
lockMutex(&playerLock);
updatePrevious();
frog->currPos[1] += SIDE_JUMP;
unlockMutex(&playerLock);
}else if(c == UP_KEY && homeFree()){ //safe!!
lockMutex(&playerLock);
updatePrevious();
frog->currPos[0] -= HOME_JUMP;
unlockMutex(&playerLock);
sleepTicks(10);
drawFrog();
sleepTicks(10);
moveHome();
checkWin();
}else if(c == DOWN_KEY && frog->currPos[0] < F_START_ROW-frog->height){
lockMutex(&playerLock);
updatePrevious();
frog->currPos[0] += VERTICAL_JUMP;
unlockMutex(&playerLock);
}else if(c == UP_KEY && frog->currPos[0] > 8){
lockMutex(&playerLock);
updatePrevious();
frog->currPos[0] -= VERTICAL_JUMP;
unlockMutex(&playerLock);
}
drawFrog();
isFrogOnAnyLog();
checkDead();
}
void moveHome(){
char **tile = PLAYER_GRAPHIC[frog->animateState];
setHomePosition();
lockMutex(&screenLock);
consoleDrawImage(frog->currPos[0], frog->currPos[1], tile, frog->height);
unlockMutex(&screenLock);
drawFrog();
}
bool homeFree(){
int podLocations[] = {SAFE_POD_1, SAFE_POD_2, SAFE_POD_3, SAFE_POD_4, SAFE_POD_5};
bool home = false;
int i;
int podCount = 0;
for(i = 0; i < NUM_PODS && !home; i++){
if(frog->currPos[0] == 5 && frog->currPos[1] > podLocations[i] && frog->currPos[1] < podLocations[i] + POD_WIDTH - frog->width &&
!frog->podFull[podCount]){
home = true;
frog->podFull[i] = true;
}
}
return home;
}
void isFrogOnAnyLog(){
Log *currLog = NULL;
bool onLog = false;
lockMutex(&listLock);
lockMutex(&playerLock);
while(!isGameOver() && currLog == NULL){
currLog = firstItem();
}
while(!isGameOver() && currLog != NULL){
if((frog->currPos[0] > currLog->startRow) && ((frog->currPos[0]+frog->height) < currLog->startRow+currLog->height) &&
((frog->currPos[1]+frog->width) < (currLog->currCol+currLog->width)) && (frog->currPos[1] > currLog->currCol)){
currLog->hasFrog = true;
frog->onLog = true;
onLog = true;
}
else{
currLog->hasFrog = false;
}
currLog = nextAvailableLog();
}
unlockMutex(&playerLock);
unlockMutex(&listLock);
lockMutex(&playerLock);
if(!onLog){
frog->onLog = false;
}
unlockMutex(&playerLock);
}
static void drawFrog(){
char **tile = PLAYER_GRAPHIC[frog->animateState];
lockMutex(&screenLock);
lockMutex(&playerLock);
consoleClearImage(frog->prevPos[0], frog->prevPos[1], frog->height, frog->width);
consoleDrawImage(frog->currPos[0], frog->currPos[1], tile, frog->height);
unlockMutex(&playerLock);
unlockMutex(&screenLock);
}
void setHomePosition(){
frog->prevPos[0] = frog->currPos[0] = F_START_ROW;
frog->prevPos[1] = frog->currPos[1] = F_START_COL;
}
void checkWin(){
int winCount = 0;
int i;
for(i = 0; i < NUM_PODS; i++){
if(frog->podFull[i])
winCount++;
}
if(winCount == NUM_PODS){ //all spaces are filled
endGame("you're a champ");
}
}
void checkDead(){
if(!inSafeZone()){
lockMutex(&playerLock);
frog->dead = true;
unlockMutex(&playerLock);
}
if(frog->dead){
sleepTicks(10);
moveHome();
lockMutex(&playerLock);
frog->dead = false;
unlockMutex(&playerLock);
}
}
bool inSafeZone(){
bool safe = false;
if(frog->onLog || frog->currPos[0] > START_BANK || frog->currPos[0] < SAFE_BANK){
safe = true;
}
return safe;
}
void createFrog(){
char **tile = PLAYER_GRAPHIC[0];
setHomePosition();
lockMutex(&playerLock);
frog->blinkSpeed = 30;
frog->animateState = first;
frog->height = PLAYER_HEIGHT;
frog->width = strlen(tile[0]);
frog->dead = false;
frog->onLog = false;
int i;
for(i = 0; i < NUM_PODS; i++){
frog->podFull[i] = false;
}
unlockMutex(&playerLock);
}
Frog *getFrog(){
return frog;
}
static void updatePrevious(){
frog->prevPos[0] = frog->currPos[0];
frog->prevPos[1] = frog->currPos[1];
}
/* COMP 3430
* PROF: JIM YOUNG
* REBECCA TIESSEN
*
* This file is a wrapper for the pthread methods in order to check for errors. An error will print and the program will exit if
* a method errors.
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "threadwrappers.h"
#include "gameglobals.h"
void createThread(pthread_t *thread, void *(*func)(void *), void *param){
int ret;
ret = pthread_create(thread, NULL, func, param);
if(ret){
printError();
}
lockMutex(&threadCountLock);