/
saucer.c
229 lines (203 loc) · 5.12 KB
/
saucer.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
/*
* CMPUT 379, Assignment 3, Fall 2013
* Due Saturday, Nov 30 2013 (submit by 11:59pm)
* (threads & synchronization & locking)
*
* Submitted by: Jessica Surya
*
*/
#include "saucer.h"
int main(int ac, char *av[]) {
/* set up curses */
initscr(); // init curses
crmode(); // disable required \r after each key press
noecho(); // don't print out the character the key is bound to
clear();
int c; /* user input */
pthread_t maker;
int i = 0;
int column = COLS/2;
ammo = MAXROCKET+10;
escaped = 0;
pthread_create(&maker, NULL, setupUFO, NULL);
pthread_mutex_init(&mx, NULL);
pthread_mutex_init(&scr_mx, NULL);
pthread_mutex_init(&esc_mx, NULL);
pthread_mutex_init(&rckt_mx, NULL);
pthread_mutex_init(&ufo_mx, NULL);
struct propset launchSite = { "|", COLS, LINES-3, 0, 0};
displayInfo();
moveHorizontal(&launchSite, column);
while(1) {
displayInfo();
if (ammo < 0 || escaped >= 3) {
break;
}
c = getchar();
if ( c == 'Q' || c == 'q' ) { // Q quits the program
break;
}
if ( c == ' ' ) { // check if space button was pressed
int k = i % MAXROCKET;
buildRocket(k, column);
i++;
pthread_mutex_lock(&mx);
ammo--;
pthread_mutex_unlock(&mx);
/* refresh info */
displayInfo();
}
if ( c == ',') {
if (column > 0) {
column--;
moveHorizontal(&launchSite, column);
}
}
if ( c == '.') {
if (column+1 < COLS-1) {
column++;
moveHorizontal(&launchSite, column);
}
}
}
pthread_cancel(maker);
endwin();
return 0;
}
void displayInfo() {
mvprintw(LINES-1,0,"%s %s %d %s %d %s %02d",
"'Q' to quit, ',' or '.'",
"to move launch site, score: ", score,
"escaped: ", escaped, "rockets: ", ammo);
}
void buildRocket(int k, int column) {
pthread_t rckt[MAXROCKET];
strcpy(rcktInfo[k].str, "^");
rcktInfo[k].col = column+1,
rcktInfo[k].row = LINES-4;
rcktInfo[k].delay = 3;
rcktInfo[k].live = true;
pthread_create(&rckt[k], NULL, rocketThread, &rcktInfo[k]);
pthread_detach(rckt[k]);
}
void *setupUFO() {
char* string = "<--->";
pthread_t thrds[MAXSAUCER];
int i = 0;
int j;
/* assign rows and velocities to each saucer */
srand(getpid());
while(1) {
usleep(40*TUNIT);
if (rand()%9 >= 7) {
j = i % MAXSAUCER;
if (ufoInfo[j].live == false &&
pthread_mutex_lock(&ufo_mx) == 0) {
strcpy(ufoInfo[j].str, string); /* the saucer*/
ufoInfo[j].col = 0;
ufoInfo[j].row = (rand()%3); /* the row */
ufoInfo[j].delay = 5+(rand()%8); /* a speed */
ufoInfo[j].live = true;
logMSG(&ufoInfo[j], i, j);
pthread_create(&thrds[j], NULL,
animateSaucer, &ufoInfo[j]);
pthread_mutex_unlock(&ufo_mx);
}
}
i++;
}
}
void *animateSaucer(void *arg) {
struct propset *info = arg; /* point to info block */
int len = strlen(info->str)+2; /* +2 for padding */
int col = 0;
while(col+len < COLS && info->live == true) {
usleep(info->delay*TUNIT);
pthread_mutex_lock(&ufo_mx);
moveHorizontal(info, col);
info->col = col;
pthread_mutex_unlock(&ufo_mx);
col++;
}
pthread_mutex_lock(&esc_mx);
if(info->live == true && !strcmp(info->str, "<--->")) {
escaped++;
displayInfo();
}
pthread_mutex_unlock(&esc_mx);
info->live = false;
strcpy(info->str, " ");
moveHorizontal(info, col);
if (escaped >= 3) {
displayInfo();
endwin();
exit(0);
}
}
void *rocketThread(void *arg) {
struct propset *rckt = arg; /* point to info block */
int row = rckt->row;
while(row > 0) {
usleep(TUNIT*rckt->delay); // sleep for microseconds
pthread_mutex_lock(&ufo_mx); /* only one thread */
moveVertical(rckt);
if (detectHit(rckt)) {
strcpy(rckt->str, " ");
rckt->live = false;
}
pthread_mutex_unlock(&ufo_mx); /* done with curses */
row--;
}
pthread_mutex_lock(&rckt_mx);
rckt->live = false;
pthread_mutex_unlock(&rckt_mx);
strcpy(rckt->str, " ");
moveVertical(rckt);
}
void moveHorizontal(struct propset * info, int col) {
move(info->row, col);
addch(' ');
addstr(info->str);
addch(' ');
move(LINES-1,COLS-1); /* park extreme bottom right of the screen */
/* to hide accidental key presses and the cursor itself */
/* from users */
refresh(); /* and show it */
}
void moveVertical(struct propset * info) {
move(info->row-1, info->col);
addstr(info->str);
move(info->row, info->col);
addch(' ');
pthread_mutex_lock(&rckt_mx);
info->row--;
pthread_mutex_unlock(&rckt_mx);
move(LINES-1,COLS-1);
refresh(); /* and show it */
}
bool detectHit(struct propset * rckt) {
int i;
for (i = 0; i < MAXSAUCER; i++) {
if (ufoInfo[i].live == true && rckt->live == true &&
ufoInfo[i].row == rckt->row &&
(rckt->col >= ufoInfo[i].col &&
rckt->col <=
ufoInfo[i].col+strlen(ufoInfo[i].str))) {
pthread_mutex_lock(&scr_mx);
strcpy(ufoInfo[i].str, " ");
ufoInfo[i].live = false;
score++;
ammo+= 3;
displayInfo();
pthread_mutex_unlock(&scr_mx);
return true;
}
}
return false;
}
void logMSG(struct propset * info, int i, int j) {
FILE * logFile = fopen("/cshome/jsurya/Dropbox/school/CMPUT_379/asn3j/saucer_log.txt", "a");
fprintf(logFile,"index = %d\t %s\tlive = %d\tpos = %d\n", i, info->str, info->live,
j);
fclose(logFile);
}