-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupKeep.c
116 lines (89 loc) · 2.53 KB
/
upKeep.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
/**********************************************************************
Module: upKeep.c
Author: Junseok Lee
Purpose: Handles and manages the upkeep thread.
**********************************************************************/
#include "upKeep.h"
/* Board diagram for refresh */
char *BOARD[] = {
" Score: Lives:",
"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-centipiede!=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"",
"",
"",
"",
"",
"",
"",
"" };
/********************THREAD functions***************/
pthread_t createUpkeepT(pthread_mutex_t * mutex)
{
pthread_t upkeepThread;
//create and run upkeep thread
wrappedPthreadCreate(&(upkeepThread), NULL, runUpkeepT, (void*)mutex);
return upkeepThread;
}
void *runUpkeepT(void *data)
{
player* p = playerPtr;
pthread_mutex_t * mutex= (pthread_mutex_t *)(data);
char lives[PRINT_LIVES_SIZE]; //array containing lives
///print player's current lives to array
sprintf(lives,"%d",playerPtr->lives);
//Post initial player lives data
putString(lives,FIRST_ROW, (GAME_COLS/PRINT_LIVES_SIZE + PRINT_LIVES_SIZE) , PRINT_LIVES_SIZE-1);
//RUN THREAD WHILE GAME IS RUNNING & PLAYER IS ALIVE
while (p->running && p->lives >= 0 && gameRunning)
{
switch(p->state)
{
case DEAD:
//WHEN PLAYER DIES
killPlayer(p); //KILL (DECREMENT LIFE) PLAYER
printLives(mutex); //PRINT CURRENT LIFE
break;
case GAMEOVER:
break;
case GAME:
//Detect if all enemies dead
if(allEnemiesDead()){
wrappedMutexLock(&playerPtr->mutex);
playerPtr->state = WIN; //change player state
wrappedMutexUnlock(&playerPtr->mutex);
}
break;
default:
break;
}
}
return NULL;
}
/********************support functions***************/
void printLives(pthread_mutex_t * mutex){
char lives[2];
//redraw board
consoleInit(GAME_ROWS, GAME_COLS, BOARD);
//move lives data
wrappedMutexLock(&playerPtr->mutex);
sprintf(lives,"%d",playerPtr->lives);
wrappedMutexUnlock(&playerPtr->mutex);
//print lives data
putString(lives,0, GAME_COLS/2+2 ,1);
//destroy bullets when player dies
killBullets();
}