-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_core.c
179 lines (148 loc) · 3.45 KB
/
game_core.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include "game_core.h"
#include "seg_display.h"
#include "client_interface.h"
#include "InputManager.h"
#include "textToSpeech.h"
#include "LED.h"
static int gameOver;
static char *gameOverMessage;
static GAMESPEC gameSpec;
static STATS playerStats;
//static char *playerName;
static struct timespec delay = {2l, 0l};
static LED *ledFlashes;
static int ledFlashCount;
static int blacklistContains(int input)
{
for (int i = 0; i < gameSpec.inputBlacklistSize; i++)
{
if (gameSpec.inputBlacklist[i] == input)
{
return 1;
}
}
return 0;
}
static int getNextInput()
{
int validInputs[NUMBER_OF_INPUTS];
int j = 0;
for (int i = 0; i < NUMBER_OF_INPUTS; i++)
{
if (!blacklistContains(i))
{
validInputs[j] = i;
j++;
}
}
int nextInput = validInputs[rand() % (NUMBER_OF_INPUTS - gameSpec.inputBlacklistSize)];
if (nextInput == BUTTON_SEQUENCE)
{
ledFlashCount = (rand() % 4) + 2;
ledFlashes = malloc(sizeof(LED) * ledFlashCount);
for (int i = 0; i < ledFlashCount; i++)
{
ledFlashes[i] = rand() % NUMBER_OF_LEDS;
}
}
return nextInput;
}
void *flashLEDS()
{
for (int i = 0; i < ledFlashCount; i++)
{
LED_flashLED(ledFlashes[i]);
}
return NULL;
}
void *startGame(GAMESPEC g)
{
gameOver = 0;
/* printf("Enter your name: ");
scanf("%s", playerName);
printf("Starting game!\n"); */
gameSpec = g;
srand(gameSpec.sequenceSeed);
InputManager_init(gameSpec.inputTime);
displayNumber(0);
playerStats.missCount = 0;
playerStats.wrongInputCount = 0;
playerStats.averageInputTime = gameSpec.inputTime;
playerStats.score = 0;
while (!gameOver)
{
int requestedInput = getNextInput();
printf("%s!\n", InputManager_getInputString(requestedInput));
TextToSpeech_speak(requestedInput);
int timeTaken;
int actualInput;
if (requestedInput == BUTTON_SEQUENCE)
{
pthread_t ledFlashingThreadID;
pthread_create(&ledFlashingThreadID, NULL, &flashLEDS, NULL);
//call InputManager or whatev to check button presses
if (InputManager_readButtonSequence(&timeTaken, ledFlashes, ledFlashCount))
{
actualInput = BUTTON_SEQUENCE;
}
else
{
actualInput = -1;
}
pthread_join(ledFlashingThreadID, NULL);
free(ledFlashes);
}
else
{
actualInput = InputManager_waitForInput(&timeTaken);
}
int totalIterations = playerStats.score + playerStats.wrongInputCount + playerStats.missCount + 1;
playerStats.averageInputTime = (playerStats.averageInputTime * (totalIterations - 1) / totalIterations) + (timeTaken / totalIterations);
if (actualInput != requestedInput)
{
if (actualInput == NO_INPUT)
{
playerStats.missCount++;
}
else
{
playerStats.wrongInputCount++;
}
int livesLeft = gameSpec.lives - (playerStats.wrongInputCount + playerStats.missCount);
if (livesLeft <= 0)
{
endGame("No lives left!");
}
else if (livesLeft == 1)
{
printf("Woops! 1 life left!\n");
}
else
{
printf("Woops! %d lives left!\n", livesLeft);
}
}
else
{
playerStats.score++;
displayNumber(playerStats.score);
}
nanosleep(&delay, (struct timespec *)NULL);
}
reportPlayerStats(playerStats);
printf("Game Over!\n%s\n", gameOverMessage);
free(gameOverMessage);
return NULL;
}
void endGame(char *message)
{
int messageSize = strlen(message) + 1;
gameOverMessage = malloc(messageSize * sizeof(char));
strcpy(gameOverMessage, message);
gameOver = 1;
}