-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchall.c
More file actions
122 lines (103 loc) · 2.79 KB
/
Copy pathchall.c
File metadata and controls
122 lines (103 loc) · 2.79 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
#include <stdio.h>
#include <stdlib.h>
#include <sys/random.h>
void win() { puts(getenv("FLAG")); }
void game_banner() {
puts("------------------------");
puts("BLACK JACK");
puts("------------------------");
puts("In blackjack, players aim to achieve a hand value as close to 21 as "
"possible without exceeding it.");
puts("Numbered cards hold their face value, face cards are worth 10, and "
"an Ace can be valued at 1 or 11.");
puts("The objective is to beat the dealer by having a higher hand without "
"busting, and a natural");
puts("blackjack (an Ace and a 10-value card) is the strongest hand.");
}
void banner() {
puts("------------------------");
puts("WELCOME TO FERRO'S CASINO");
puts("------------------------");
puts("insert 1 to play blackjack.");
puts("insert any other number to quit.");
}
// This is needed for the rendering of the challenge. You can skip this.
// It is irrelevant to solve the challenge.
void buffering() {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}
unsigned long blackjack(unsigned long money) {
long balance = 0, bet = 0;
int sumUser = 0, choice = 0, sumDeck = 0;
game_banner();
if (money > 1000) {
win();
}
while (1) {
printf("your current money are %lu\n", money);
puts("what's your bet now?");
scanf("%ld", &bet);
// TODO(dev): check this limit
if (bet > money + 10) {
printf("NOT ENOUGH MONEY :| \n");
continue;
}
break;
}
unsigned int seed = 0;
if (getrandom(&seed, sizeof(seed), GRND_NONBLOCK) < 0) {
puts("sky's falling, cannot initialize seed");
exit(1);
}
srand(seed);
int userCard = rand() % 10 + 1;
int deckCard = rand() % 10 + 1;
sumUser += userCard;
printf("your first card is %d \n", userCard);
printf("deck first card is %d \n", deckCard);
for (;;) {
printf("1. card 2.stay ");
scanf("%d", &choice);
switch (choice) {
case 1:
sumUser += rand() % 10 + 1;
printf("this is the sum of your cards %d \n", sumUser);
if (sumUser > 21) {
printf("you lost :| \n");
return money - bet;
}
continue;
case 2:
sumDeck =
21; // this is not how blackjack works but fck it, find the bug :)
if (sumUser < sumDeck) {
puts("you lost :|");
return money - bet;
}
printf("you won, nice! now you have %ld", (long)money);
return money + (bet * 2);
default:
puts("invalid choice!");
continue;
}
}
}
int main() {
buffering();
int choice;
unsigned long money = 10;
while (1) {
banner();
scanf("%d", &choice);
switch (choice) {
case 1:
money = blackjack(money);
break;
default:
puts("bye bye");
return 0;
}
}
}