-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
executable file
·385 lines (329 loc) · 11.7 KB
/
Copy pathmain.c
File metadata and controls
executable file
·385 lines (329 loc) · 11.7 KB
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
//Import other structs and functions
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
typedef struct card_struct
{
char Word[50];
bool used;
} CardType;
typedef struct Player_struct
{
char Name[50];
int numWins;
CardType redCards[7];
} PlayerType;
PlayerType *GetPlayerData(CardType *redCards, int redCardsNum, int *numPLayers, int *goal);
PlayerType GenPlayerHand(CardType *redcards, PlayerType player, int arraySize);
CardType *LoadCards(const char fileName[], int *arraySize);
CardType PickGreenCard(CardType *greenCards, int numGreenCards);
int PickRedCard(CardType greenCard, PlayerType *player);
int Czar(PlayerType *players, int roundCards[], CardType greenCard, int numPlayers);
void GameLoop(CardType *greenCards, CardType *redCards, PlayerType *players, int numPlayers, int numRedCards, int numGreenCards, int goal);
void ShiftCzar(PlayerType *players, int numPlayers);
void PromptContinue(void);
void PromptTurn(PlayerType player);
void PrintRedText(char message[]);
void PrintGreenText(char message[]);
void GetInt(char message[], int min, int max, int *userInput);
CardType *LoadCards(const char fileName[], int *arraySize)
{
//Opens the card file
FILE *inputFile = fopen(fileName, "r");
//initializes the pointer to null
CardType *cards = NULL;
//Initializes a variable to store the size of the array
int counter = 0;
//Scans the file until it reaches the end
while (!feof(inputFile))
{
counter++;
CardType temp;
//Scans the line into the var
fgets(temp.Word, 50, inputFile);
//removes the newline character that fgets adds
strtok(temp.Word, "\n");
temp.used = false;
//reallocates the card array size so the array can be any size
cards = realloc(cards, counter * sizeof(CardType));
//Adds the current word to the array
cards[counter - 1] = temp;
}
//Sets the array size
(*arraySize) = counter;
fclose(inputFile);
return cards;
}
PlayerType *GetPlayerData(CardType *redCards, int redCardsNum, int *numPlayers, int *goal)
{
//Creates a pointer to the memory that will be returned
PlayerType *players = NULL;
//Gets the number of players with in the valid range
GetInt("Please enter the number of players: ", 4, 8, numPlayers);
//Sets the number of green cards needed to win based on the number of players
switch (*numPlayers)
{
case 4:
(*goal) = 8;
break;
case 5:
(*goal) = 7;
break;
case 6:
(*goal) = 6;
break;
case 7:
(*goal) = 5;
break;
default:
(*goal) = 4;
break;
}
//Creaters and assign's memory for the number of players
players = realloc(players, (*numPlayers) * sizeof(PlayerType));
//Loops through each player and defines the players attributes
for (int i = 0; i < (*numPlayers); i++)
{
//Creates a temport player variable
PlayerType temp;
//Prompts the user for the their name
printf("Please enter playername: ");
scanf("%s", temp.Name);
//sets the initial handsize and number of wins to zero
temp.numWins = 0;
//loops through the players red cards and sets them to used
//This is so that GenPlayerHand will replace each card
for (int i = 0; i < 7; i++)
{
temp.redCards[i].used = true;
}
//Generates the players hand
temp = GenPlayerHand(redCards, temp, redCardsNum);
//Assigns the current position in the players list to the temporary player variable
players[i] = temp;
}
//return the pointer to the memory
return players;
}
PlayerType GenPlayerHand(CardType *redcards, PlayerType player, int arraySize)
{
//Loops through the players red cards
for (int i = 0; i < 7; i++)
{
//Checks if the cards have been used
if (player.redCards[i].used == true)
{
//Loops until a random red card has been found and isn't used
while (true)
{
int index = rand() % arraySize;
if (redcards[index].used == false)
{
//sets the new card
player.redCards[i] = redcards[index];
//sets the card used to true for the global list
redcards[index].used = true;
break;
}
}
}
}
//returns the edited player
return player;
}
CardType PickGreenCard(CardType *greenCards, int numGreenCards)
{
//Creates a temporary Card Variable
CardType currentGreenCard;
while (true)
{
//Returns a random index between 0 and the number of green cards
int index = rand() % (numGreenCards);
//Checks if the card is used
if (greenCards[index].used == false)
{
//sets the temporary variable to the first unused greencard found
currentGreenCard = greenCards[index];
//sets the global green card to used
greenCards[index].used = true;
break;
}
}
return currentGreenCard;
}
int PickRedCard(CardType greenCard, PlayerType *player)
{
//Creates a variable to store the users choice
int userSelection;
char message[500];
//Gives the user general information
printf("Its %s's turn\n", player->Name);
sprintf(message, "The green card is %s\n", greenCard.Word);
PrintGreenText(message);
printf("Your red Cards are: \n");
//Loops over and prints out the users redcards
for (int i = 0; i < 7; i++)
{
sprintf(message, "%d.%s\n", (i + 1), player->redCards[i].Word);
PrintRedText(message);
}
//Prompts the user for input
GetInt("Enter your selection: ",1,7,&userSelection);
//Sets the players selected redcard to used
player->redCards[userSelection - 1].used = true;
//clears the screen
system("clear");
return (userSelection - 1);
}
void ShiftCzar(PlayerType *players, int numPlayers)
//shifts the first player in the array to the last position
{
//creates a temp variable to store the first player in the array
PlayerType temp = players[0];
//loops over all players
for (int i = 0; i < numPlayers; i++)
{
if (i != numPlayers - 1)
{
//swaps two players in the array
PlayerType temp1 = players[i];
players[i] = players[i + 1];
players[i + 1] = temp1;
}
else
{
//sets the last player in the array to the temp player
players[numPlayers - 1] = temp;
}
}
}
int Czar(PlayerType *players, int roundCards[], CardType greenCard, int numPlayers)
{
//Creates a variable to store the Czars choice
int userSelection;
char message[500];
//Gives the czar general information
printf("You are the Czar\n");
sprintf(message, "The green card is %s", greenCard.Word);
PrintGreenText(message);
printf("\nYour red Cards are:\n");
//prints all the red cards that have been played
for (int i = 0; i < (numPlayers - 1); i++)
{
sprintf(message, "%d.%s\n", (i + 1), players[i].redCards[roundCards[i]].Word);
PrintRedText(message);
}
//Prompts the czar for their selection
GetInt("Enter your selection: ",1,(numPlayers-1),&userSelection);
players[userSelection - 1].numWins++;
return userSelection;
}
void PrintRedText(char message[])
{
printf("\033[1;31m");
printf("%s", message);
printf("\033[0m");
}
void PrintGreenText(char message[])
{
printf("\033[32;1m");
printf("%s", message);
printf("\033[0m");
}
//TODO:Add Verification if input is not an int
void GetInt(char message[], int min, int max, int *userInput)
{
printf("%s", message);
scanf("%d", userInput);
while ((*userInput) < min || (*userInput) > max )
{
printf("That is not in the range of %d - %d\n", min, max);
printf("%s", message);
scanf("%d", userInput);
}
}
void GameLoop(CardType *greenCards, CardType *redCards, PlayerType *players, int numPlayers, int numRedCards, int numGreenCards, int goal)
{
//Clears the Screen of any inital text
system("clear");
//Clears the initial input buffer
getchar();
//Initializes the Game
//Will note be exited till the game has been won
while (true)
{
//Loops over each players hand at the Beginning of the round and replaces any used cards
for (int i = 0; i < numPlayers; i++)
{
players[i] = GenPlayerHand(redCards, players[i], numRedCards);
}
//Moves the Czar to the end of the array
ShiftCzar(players, numPlayers);
//Picks the green card for the round
CardType greenCard = PickGreenCard(greenCards, numGreenCards);
//Creates an array for the index of the card in the user red cards
//The index of in this array matches the index of the player in the players array
int roundCards[numPlayers - 1];
//Loops over each player
for (int i = 0; i < (numPlayers - 1); i++)
{
//tells the user who's turn is next and prompts them to press enter to continue
printf("%s's turn is next\n", players[i].Name);
printf("Press any key to continue: ");
//Gets the users input
getchar();
//Clears the screen
system("clear");
//assigns the index of the redcard the player picked to the round cards
roundCards[i] = PickRedCard(greenCard, &players[i]);
getchar();
}
//Tells the user the who the czar is and prompts the user to press enter to continue
printf("The czar's turn is next.\n");
printf("%s is the czar\n", players[numPlayers - 1].Name);
printf("Press any key to continue: ");
getchar();
system("clear");
//Returns the index of the player that one
int winner = Czar(players, roundCards, greenCard, numPlayers);
getchar();
//Prints the winner and the card that one
printf("%s won the round with %s\n", players[winner - 1].Name, players[winner - 1].redCards[roundCards[winner - 1]].Word);
//Checks if the game has been won
if (players[winner - 1].numWins == goal)
{
printf("%s won the game\n", players[winner - 1].Name);
printf("Press any key to continue: ");
getchar();
break;
}
//Prompts the user to press enter to continue to the next line
printf("Press any key to continue: ");
getchar();
system("clear");
}
}
int main(void)
{
//Seeds the random function so the cards are never the same
srand(time(NULL));
int greenCardsNum;
int redCardsNum;
int numPlayers;
int goal;
//Retrive the Green and redcards
CardType *greenCards = LoadCards("greencards.txt", &greenCardsNum);
CardType *redCards = LoadCards("redcards.txt", &redCardsNum);
//Loads in the player
PlayerType *players = GetPlayerData(redCards, redCardsNum, &numPlayers, &goal);
//Starts the game
GameLoop(greenCards, redCards, players, numPlayers, greenCardsNum, greenCardsNum, goal);
//Release the memory
free(greenCards);
free(redCards);
free(players);
return 0;
}