-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrogger.c
103 lines (92 loc) · 2.19 KB
/
frogger.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
/* 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);
}