-
Notifications
You must be signed in to change notification settings - Fork 0
/
game-of-chance.c
364 lines (319 loc) · 10.7 KB
/
game-of-chance.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
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
/*
The following is a game of chance wherein there are multiple options for the game a user would like to play.
There is also the opportunity to reset a users number of tokens. Each user on a unix like system will have
a different 'save' if you will. The user can create a screen name and bet against the program.
This game compiles most of the information that I currently have when it comes to C programming, the game
makes use of reading and writing files, as well as data segmentation, structs, and more.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include "couple.h"
#define DATAFILE "/var/chance.data" // File that users' game data is stored.
struct user{
int uid;
int credits;
int highScore;
char name[100];
int (*currentGame)();
};
//Function prototypes for the game.
int getPlayerData();
void registerNewPlayer();
void updatePlayer();
void showHighScore();
void jackpot();
void inputNewName();
void printCards(char *, char *, int);
int takeWager(int, int);
void playGame();
int pickNumber();
int dealerNoMatch();
int findTheAce();
void fatal(char*);
// In order to avoid passing this variable around the entirety of the program, we variable globalizing it.
struct user player;
int main(){
int choice, lastGame;
srand(time(0));
if(getPlayerData() == -1) // Then there probably isn't data for the user.
registerNewPlayer();
// Display the menu to the player.
while(choice != 7){
printf("=========Menu=========");
printf("1 - Play Pick a Number");
printf("2 - Play the Match Dealer Game");
printf("3 - Play Find the Ace");
printf("4 - View your high score");
printf("5 - Change your user name");
printf("6 - Reset your credits at 100.");
printf("7 - Quit the game\n");
printf("Name: %s", player.name);
printf("Credits: %u", player.credits);
scanf("%d", &choice);
if((choice < 1) || (choice > 7)){ // They didn't read the instructions or they're dying of boredom. (Been there).
printf("That wasn't a choice available to you.");
}else if(choice < 4){ // They want to play a game.
if(choice != lastGame){
switch(choice){
case 1: player.currentGame = pickNumber; break;
case 2: player.currentGame = dealerNoMatch; break;
case 3: player.currentGame = findTheAce; break;
}
playGame();
}
}else if(choice == 4){
showHighScore();
}else if(choice == 5){
printf("Change username:\n");
printf("Enter a new user name: ");
inputNewName();
printf("\n Your name is changed! \n");
}else if(choice == 6){
printf("Your credits have been reset to 100!");
player.credits = 100;
}
}
updatePlayer();
printf("Hope you come again soon!");
}
// This following function reads data from the file on a given user.
int getPlayerData(){
int fd, uid, readBytes;
struct user entry;
uid = getuid(); // Grabs the user id of the person starting the game.
fd = open(DATAFILE, O_RDONLY);
if(fd == -1) // The file didn't open, we may need to make one.
return -1;
readBytes = read(fd, &entry, sizeof(struct user)); // Reading the data in chunks.
while(entry.uid != uid && readBytes > 0){
readBytes = read(fd, &entry, sizeof(struct user)); /* Continues to read bytes in chunks the size of the user struct
until the file is either empty or the correct user is found. */
}
close(fd);
if(readBytes < sizeof(struct user)) // Then EOF was reached and the user is not yet in the file.
return -1;
else
player = entry;
return 1; // Code ran successfully if we get here and set the player.
}
// This is the function for a new user to register for our game.
// It will add their information to the file where information is saved.
void registerNewPlayer(){
int fd;
printf("Enter your name: ");
inputNewName();
player.uid = getuid();
player.highScore = player.credits = 100;
fd = open(DATAFILE, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
if(fd == -1)
fatal("in registerNewPlayer() while opening file.");
write(fd, &player, sizeof(struct user));
close(fd);
printf("\n Welcome to the Game of Chance %s.\n", player.name);
printf("You have been given %u credits.\n", player.credits);
}
// Function to display the user's current high score.
void showHighScore(){
unsigned int topScore = 0;
char topName[100];
struct user entry;
int fd;
printf("\n=========================|High Score|===========================\n");
fd = open(DATAFILE, O_RDONLY);
if(fd == -1)
fatal("in showHighScore() while opening file.");
while(read(fd, &entry, sizeof(struct user)) > 0){ // We continue to loop until EOF
if(entry.highScore > topScore){
topScore = entry.highScore;
strcpy(topName, entry.name);
}
}
close(fd);
if(topScore > player.highScore)
printf("You currently have the high score of all users with a total of %u credits.", player.highScore);
printf("\n=============================================================================\n\n");
}
// This is a function to award a jackpot to the player.
void jackpot(){
printf("============== You won the jackpot!! ===============");
player.credits += 100;
}
//This is the function a player uses to change and create their name.
void inputNewName(){
char *namePtr, inputChar = '\n';
while(inputChar == '\n'){ // Eat those new lines.
scanf("%c", &inputChar);
}
namePtr = (char *) &player.name;
while(inputChar != '\n'){
*namePtr = inputChar;
scanf("%c", &inputChar);
namePtr++;
}
*namePtr = 0;
}
// This function is for the find the ace game, it prints out the cards.
void printCards(char *message, char *cards, int userPick){
int count;
printf("\n\t*** %s ***\n", message);
printf(" \t._.\t._.\t._.\n");
printf("Cards:\t|%c|\t|%c|\t|%c|\n\t", cards[0], cards[1], cards[2]);
if(userPick == -1){
printf(" 1 \t 2 \t 3\n");
}else{
for(count = 0; count < userPick; count++){
printf("\t");
}
printf(" ^-- your pick\n");
}
}
// This function takes wagers from the user.
int takeWager(int availableCredits, int previousWager){
int wager, totalWager;
printf("How many of your %d credits would you like to wager? ", availableCredits);
scanf("%d", &wager);
if(wager < 1){
printf("You can't bet less than a single credit...");
return -1;
}
totalWager = previousWager + wager;
if(totalWager > availableCredits){
printf("You can't bet more credits than you have available to you.");
return -1;
}
return wager;
}
// This function contains a loop that allows the current game to be replayed.
void playGame(){
int playAgain;
int (*game)();
char selection;
while(playAgain != 0){ // So long as the player wants to play again.
if(player.currentGame() != -1){ // If the game threw an error.
if(player.credits > player.highScore)
player.highScore = player.credits;
printf("You currently have %u credits.\n", player.credits);
updatePlayer();
printf("Would you like to play again? (y/n) ");
selection = '\n';
while(selection == '\n'){
scanf("%c", &selection);
}
if(selection == 'n')
playAgain = 0;
}else{
playAgain = 0; // Here the player would have experienced an error and so
// they cannot play again.
}
}
}
// This is the pick a number game.
int pickNumber(){
int pick, winningNum;
printf("\n####### Pick a Number #######\n");
printf("This game costs 10 credits to play. Just pick a number\n between 1 and 20, if you pick the right number, you win 100 credits!");
winningNum = (rand() % 20) + 1;
if(player.credits < 10){
printf("You only have %c credits, you can reset your credits at the main menu.", player.credits);
return -1;
}
player.credits -= 10;
printf("10 credits have been deducted from your account.\n");
printf("Pick a number between 1 and 20.");
scanf("%d", &pick);
printf("The correct number was %d", winningNum);
if(pick == winningNum){
jackpot();
}else{
printf("Sorry! You lose..");
}
return 0;
}
// This is the no match dealer game.
int dealerNoMatch(){
int i, j, numbers[16], wager = -1, match = -1;
printf("\n####### No Match Dealer #######\n");
printf("In this game the player can wage as many of their credits as desired.");
printf("You will be dealt 16 random numbers between 0 and 99, then if there are no matches you win and double your wager.");
if(player.credits == 0){
printf("You don't have any credits to wager.");
return -1;
}
while(wager == -1)
wager = takeWager(player.credits, 0);
printf("::Dealing Numbers::");
for(i = 0; i < 16; i++){
numbers[i] = rand()%100;
printf("%2d\t", numbers[i]);
if(i%8 == 7)
printf("\n");
}
for(i=0; i < 15; i++){
j = i +1;
while(j < 16){
if(numbers[i] == numbers[j])
match = numbers[i];
j++;
}
}
if(match != -1){
printf("The number %d got matched, so you lose.. I'm taking those credits now.", match);
printf("%d credits have been removed from your account.", wager);
player.credits -= wager;
}else{
printf("Looks like you won! I'll add those credits to your account.");
printf("%d credits have been added to your account.", wager);
player.credits += wager;
}
return 0;
}
// The following function is the find the ace game.
int findTheAce(){
int i, ace, wager;
int invalidChoice, pick = -1, wager1 = -1, wager2 = -1;
char choiceTwo, cards[3] = {'X', 'X', 'X'};
ace = rand()%3;
printf("####### Find the Ace #######\n");
printf("In this game you can wager up to all of your credits.\n");
printf("If you correctly guess the ace's position you win, otherwise you lose your credits.\n");
printf("In the first round, one queen is revealed, at this point you can change your choice.\n");
printf("In the second round, the ace is revealed and the game is over.");
if(player.credits == 0){
printf("You don't have any credits to wager..");
return -1;
}
while(wager1 == -1){
wager1 = takeWager(player.credits, 0);
}
printCards("Dealing Cards", cards, -1);
pick = -1;//im useless
while(pick < 1 || pick > 3){
printf("Pick a card, 1, 2, or 3.");
scanf("%d", &pick);
}
pick--;
i = 0;
while(i == ace || i == pick){
i++;
}
cards[i] = 'Q';
printCards("Revealing one queen", cards, pick);
invalidChoice = 1;
while(invalidChoice == 1){
printf("Would you like to change your pick or increase your wager?");
printf("Please pick either c or i.");
choiceTwo = '\n';
while(choiceTwo == '\n'){
scanf("%c", &choiceTwo);
}
if(choiceTwo == 'i'){
}
if(choiceTwo == 'c'){
}
}
}