Skip to content

Instantly share code, notes, and snippets.

Created March 21, 2015 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/c9244365a7e5b0550b2c to your computer and use it in GitHub Desktop.
Save anonymous/c9244365a7e5b0550b2c to your computer and use it in GitHub Desktop.
gcc -o blackjack -W -Wall -Wextra -pedantic -Werror blackjack.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
#include<ncurses.h>
#define BLACKJACK 21
#define GAMEPOINTS 15
#define TRUE 1
#define FALSE 0
#define CLEAN 0
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define RESET "\033[0m"
void clear_stdin(){
int c;
while ((c = getchar()) != 0 && c != '\n');
}
struct Scores{
int start;
int roundScore;
int newScores;
int newRecord;
int rounds;
}record;
struct Players{
char playerName[20];
int year;
}player;
void setUpGameStats(){
record.start = 0;
record.rounds = 0;
}
void gameRules(){
printf("\tlet me explain you aboud this game and how it works:\n\n");
printf("\tTo win this game, you have 2 chanses:\n");
printf("\tfirst chanse: make BLACKJACK in the first Round\n");
printf("\tsecond chanse: reset your Score to 0\n\n\n");
printf("\tIf you make BLACKJACK after the second Round begins, your score will reset to 0 \n\n\n");
printf("\tyou will loose if you don't make BlackJack in 10 Rounds\n");
printf("\tYou will have to reset your score to 0 Points\n\n\n");
printf("\tIf you decide to STOP at 17, you will make 12 Points\n");
printf("\t12 points means, 21 - 17 = 4\n\n");
printf("\t4 * 3 = 12\n\n");
printf("\tIf you got 100, you lost the game\n\n");
printf("\n\n\n\n");
printf(KCYN "Please hit" RESET KYEL" \"ENTER\" " RESET KCYN"to continue:"RESET);
getchar();
}
int checkInput();
void showPlayerName(){
printf("%s\n",player.playerName);
}
int checkInput(int min, int max){
int option;
char c;
do{
printf(KCYN"\nPlease choose an Option between" RESET KYEL " %d " RESET KCYN "and " RESET KYEL "%d" RESET KCYN " >\t" RESET,min,max);
if (scanf("%d%c",&option,&c) == 0 || c != '\n'){
clear_stdin();
printf("\nPlease type a Number:\n\n\n");
}else if (option < min || option > max){
printf("The number has to be between %d and %d\n\n\n",min,max);
}else{
printf("\n\n");
break;
}
}while(TRUE);
return option;
}
int newCard(int min, int max){
int card;
srand(time(NULL));
sleep(2);
card = (rand() % min) + max;
return card;
}
int roundStart(){
int firstCard, secondCard, score;
firstCard = newCard(11,1);
printf(KGRN "The first card is:" RESET "\t%d\n",firstCard);
sleep(1);
secondCard = newCard(10,1);
printf(KGRN "The Second card is:" RESET "\t%d\n",secondCard);
score = firstCard + secondCard;
return score;
}
void showScore(char name[], int score){
sleep(1);
printf("\n\n");
printf("«««««««««««««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»\n");
printf("\n\n");
printf(KYEL "\t\t%s\t" RESET KCYN "your score is now:\t"RESET KYEL "%d" RESET "\n", name, score);
printf("\n\n");
printf("«««««««««««««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»\n");
sleep(1);
printf("\n\n");
}
void checkRound(int min, int max){
if (min < max){
do{
int b;
printf("\n\n\n");
printf(KCYN "\t\tDo you need a new Card?" RESET "\n");
printf("1) Yes >\n");
printf("2) No >\n");
printf("\n");
b = checkInput(1,2);
if (b == 1){
int a = newCard(11,1);
system("clear");
system("clear");
system("clear");
printf("\t\t ________________________\n");
printf("\t\t| You choosed a new Card |\n");
printf("\t\t|________________________|");
printf("\n\n");
printf(KGRN "Your New Card is:" RESET "\t%d\n",a);
record.roundScore += a;
printf("\n\n\n\n");
}else if(b == 2){
break;
}
showScore(player.playerName, record.roundScore);
if (record.roundScore == max){
if(record.newRecord != CLEAN){
record.newRecord +=1;
break;
}
break;
}else if (record.roundScore > max){
record.newRecord = 0;
break;
}
}while(record.roundScore != max || record.roundScore < max);
}
}
int pointsCheck(int min, int max){
int score;
if(min > max){
score = min - max;
}else{
score = max - min;
}
return score * GAMEPOINTS;
}
void gameOver(){
printf("\n\n\t\t\t»»»» -= Game Over =- ««««\n\n");
}
void goodBye(){
printf(KCYN "\n\n\t\t\t-=GoodBye=-\n\n" RESET);
}
int main(){
int startGame;
system("clear");
setUpGameStats();
do{
int checkName;
char c;
if (record.start == 0){
do{
printf(KCYN "\tPlease type your NickName: \t" RESET "");
scanf("%s",player.playerName);
clear_stdin();
if ((checkName = strtol(player.playerName, NULL, 10))){
printf(KRED "\n\n\t\tPlease type a Real NickName\n" RESET "\n");
}
}while(checkName != FALSE);
do{
printf(KCYN "\tPlease type your Age:\t" RESET "");
if (scanf("%d%c",&player.year,&c) == 0 || c != '\n'){
clear_stdin();
printf(KRED "\n\n\t\tPlease type your real Age\n" RESET "\n");
}else if (player.year > 75 && player.year < 115){
printf("\n\n\n\t\t\tYou are to old for this game LoL\n");
printf("\t\t\t\tAre you still Alive?\n\n");
goodBye();
return 1;
}else if (player.year >= 115){
printf("\n\n\n\t\t\tAlliens and Predators are not alowed in here.\n");
goodBye();
return 1;
}else if(player.year < 18){
printf(KRED "\n\n\n\t\tSorry, you are to Young for this Game\n" RESET);
goodBye();
return 1;
}
}while(1);
printf(KCYN "\n\n\n\t\t\t\tHallo" RESET KYEL" %s " RESET KCYN",welcome to BlackJack\n\n\n\n"RESET,player.playerName);
gameRules();
}else if(record.start != 0){
do{
int b;
if (record.rounds != 0){
if(record.rounds == 10 && record.roundScore != BLACKJACK ){
printf("\n\n\tYou lost %d Rounds\n",record.rounds);
gameOver();
return 1;
}
printf("\n\n\n\n");
printf(KYEL "Do you wanna play another Round?" RESET "\n");
printf("\t\t\t\t1) Yes >\n");
printf("\t\t\t\t2) No >\n");
printf("\n\n\n");
}else{
printf("\n\n\n\n");
printf(KYEL "\t\t\tDo you wanna play Again?" RESET "\n");
printf("1) Yes >\n");
printf("2) No >\n");
printf("\n");
}
b = checkInput(1,2);
if ( b == 2){
printf("Goodbye.");
return 1;
}
}while(FALSE);
}
printf("\n\n");
system("clear");
printf("\n\t\t\t\tRound %d\n\n",record.rounds+=1);
if(record.rounds == 8){
printf("\n\n\t\t\t Hurry up, you are in Round %d\n\n\n",record.rounds);
}else if(record.rounds == 9){
printf("\n\n\t\t\tHurry up, you are in Round %d\n\n\n",record.rounds);
}else if(record.rounds == 10){
printf("\n\n\t\t\tYou are in Round %d, you have to Win\n\n\n",record.rounds);
}
printf(KYEL "\t\t%s\t" RESET KCYN "you get your first two Cards\n\n" RESET,player.playerName);
sleep(3);
startGame = roundStart();
record.roundScore = startGame;
showScore(player.playerName, record.roundScore);
checkRound(record.roundScore, BLACKJACK);
if(record.roundScore < BLACKJACK){
int a = pointsCheck(record.roundScore, BLACKJACK);
record.newScores += a;
printf(KMAG"\n\n\t\t\t\tTotal points are now" RESET KYEL"\t%d\n"RESET,record.newScores);
}else if(record.roundScore == BLACKJACK){
if (record.newScores == 0){
printf("\n\n\n\t\t\t\tBlackJack\n");
printf("\n\n\t\t\t-=You are the Winner=-\n");
return 1;
}else if(record.newScores != CLEAN){
record.newScores = CLEAN;
printf("You have just reseted your Score, you have now %d Points\n", record.newScores);
record.rounds = 0;
}
}else if(record.roundScore > BLACKJACK){
int a = pointsCheck(record.roundScore, BLACKJACK);
record.newScores += a;
printf(KMAG"\n\n\t\t\t\tTotal points are now" RESET KYEL"\t%d\n"RESET,record.newScores);
printf("\n\n");
printf("\t\t\t\t\t\t" KRED "You lost this Round.\n"RESET);
sleep(3);
}
if (record.newScores > 100){
printf("\n\n\n");
printf("You lost this game\n");
gameOver();
return 1;
}
printf("\n\n\n");
record.start = 1;
}while(TRUE);
printf("\n\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment