Juego del Blackjack

Max Speck

// El juego de Blackjack.

#include<stdlib.h>
#include<stdio.h>
#include<time.h>

/* DEFINITION OF FUNCTIONS*/
int game(int x);
int again(int x);
int inputcontol_n(int x);
char inputcontrol_c(char x);

int main()
{
int repeat, c=1, valid, won=0, lost=0, total=0, push=0, result;

printf(“\n\n\n\t\t\t !!! BLACKJACK !!! \n\n\n”);

printf(“Enter ‘h’ to hit and ‘s’ to stand. \n \n \n” );
printf(“Do you want to play? Enter ‘1’ for yes and ‘0’ for no. Or enter ‘2’ to see the full modified rules. \t”);

fflush(stdin);
scanf(“%d”, &c);
repeat = inputcontrol_n(c);

if(repeat == 2)
{
printf(“\n\n\t\t Rules of this MODIFIED VERSION: \n\nThe dealer doesn’t have a choice when he draws an ace. \n”);
printf(“If he can draw an eleven without busting himself he is going to do so.”);
printf(“Otherwise he will use the one. \nFurthermore the drawing of the cards is everytime completly at random. So you use for each draw a new deck! \n”);
printf(“\n Now it’s your turn: \n\n”);
repeat = 1;
}

/* START of the GAME */

printf(“\n\n”);

while(repeat == 1)
{
result = game(1); //Evaluation of total games
if(result == 2) push++;
else won += result;
total++;
repeat = again(1);
}

lost = total – won – push;
printf(“\n\nYou played %d times in which you won %d games, lost %d games and %d games were a push. \n \n”,total, won, lost, push);

printf(“\n\t\t\t\t\t END \n\n\n\n\n\n\n\n\n\n\n”);

return 0;
}

/* Definition of the game function */

int game(int x)
{
int dea[21], pla[21], sumd=0, sump1=0, sump2=0, result_g;

srand(time(NULL));
dea[0] = rand()%10 +1; //Dealer draws cards
dea[1] = rand()%10 +1;

sumd = dea[0]+dea[1];

// Dealer BLACKJACK control
if (sumd == 21)
{
printf(“\n\nDealer has Blackjack. You lose! \n”);
result_g = 0;
return result_g;
}

pla[0] = rand()%10 +1; // Player draws cards
pla[1] = rand()%10 +1;
int k = 0;
for(k=2;k<=21;k++) pla[k]=0;

int ace = 0;
char next = ‘h’;

//Begin of the first draw

if (pla[0] == 1)
{
sump1 = pla[1]+1;
sump2 = pla[1]+11;
if(sump1 == 21 || sump2 == 21)
{
next = ‘s’;
}
else
{
printf(“\nThe Dealer drew a %d. \nYou drew an ace and a %d. Your score is now either %d or %d. What would you like to do next? \t\t”, dea[0], pla[1], sump1, sump2);
ace = 1;
}

}
else if (pla[1] == 1)
{
sump1 = pla[0]+1;
sump2 = pla[0]+11;
if(sump1 == 21 || sump2 == 21)
{
next = ‘s’;
}
else
{
printf(“\nThe Dealer drew a %d. \nYou drew an ace and a %d. Your score is now either %d or %d. What would you like to do next? \t\t”, dea[0], pla[0], sump1, sump2);
ace = 1;
}
}
else
{
sump1 = pla[0]+pla[1];
sump2 = sump1;
if(sump1 == 21)
{
next = ‘s’;

}
else
{
printf(“\nThe Dealer drew a %d. \nYou drew an %d and a %d. Your score is now %d. What would you like to do next? \t\t\t\t”, dea[0], pla[0], pla[1], sump1);
}
}

int i=1; // WHILE LOOP OF GAME after the first draw without ace

if(ace == 0)
{
if (next != ‘s’)
{
fflush(stdin);
scanf(“%c”, &next);
next=inputcontrol_c(next);
}
while(next == ‘h’ && ace == 0)
{
// Hit (drawing a new card)
i++;
pla[i] = rand()%10 +1;
if (pla[i] == 1)
{
sump1 = sump1+1;
sump2 = sump1+10;
if(sump1 == 21 || sump2 == 21) next = ‘s’;
else
{
printf(“\nYou drew an ace. Your score is now either %d or %d. What would you like to do next? \t\t\t”, sump1, sump2);
ace = 1;
}
}
else
{
sump1 += pla[i];
if(sump1>=21)
{
printf(“\nYou drew a %d.”, pla[i]);
break;
}
if(sump1 == 21 || sump2 == 21)
{
next = ‘s’;
break;
}
printf(“\nYou drew a %d. Your score is now %d. What would you like to do next? \t\t\t\t\t”, pla[i], sump1);

fflush(stdin);
scanf(“%c”, &next);
next=inputcontrol_c(next);
}
}
}

if(ace ==1) // WHILE LOOP OF GAME with ace
{
fflush(stdin);
scanf(“%c”, &next);
next = inputcontrol_c(next);

if( next == ‘h’)
{
while(next == ‘h’ && sump2<=21)
{
// Hit (drawing a new card)
i++;
pla[i] = rand()%10 +1;
sump1 += pla[i];
sump2 += pla[i];
if(sump1 == 21 || sump2 == 21)
{
next = ‘s’;
break;
}
if(sump1>=21 && sump2>=21)
{
printf(“\nYou drew a %d. Your score is now %d or %d.”, pla[i], sump1, sump2); // Hier ist es ein wenig schwierig mit den beiden scores, falls einer grš§er 21 ist..
next = ‘s’;
break;
}
printf(“\nYou drew a %d. Your score is now %d or %d. What would you like to do next? \t\t\t\t”, pla[i], sump1, sump2);

fflush(stdin);
scanf(“%c”, &next);
next=inputcontrol_c(next);

}
while(next == ‘h’ && sump1 <=20)
{
// NEW CARD / HIT
i++;
pla[i] = rand()%10 +1;
sump1 += pla[i];
if(sump1>=21)
{
printf(“\nYou drew a %d. Your score is now %d.”, pla[i], sump1);
break;
}
printf(“\nYou drew a %d. Your score is now %d. What would you like to do next? \t\t\t\t\t\t”, pla[i], sump1);

fflush(stdin);
scanf(“%c”, &next);
next=inputcontrol_c(next);
}
}
}

// DEALER CONTROL
int j=2;
while (sumd<=16)
{
dea[j] = rand()%10 +1;

if (dea[j]==1)
{
if (sumd+11<=21) sumd+=10;
}
sumd += dea[j];
j++;
}

printf(“\n”);

// determine best score
int sump3 = 0;
if(sump2>= sump1 && sump2 <=21) sump3 = sump2;
else sump3 = sump1;

// Evaluation of the game

if(sump3 == 21)
{
printf(“\n\n !!! BLACKJACK !!! \a\a\a \n\n You win! You scored %d and the dealer scored %d. \n”, sump3, sumd);
result_g = 1;
return result_g;
}
else if(sump3 == sumd && sump3<=21)
{
printf(“Its a push! You and the dealer scored %d. \n”, sump3);
result_g = 2;
return result_g;
}
else if (sumd >=22 && sump3 <= 21)
{
printf(“You win! You scored %d and the dealer scored %d. \n”, sump3, sumd);
result_g = 1;
return result_g;
}
else if (sump3<sumd || sump3>=22 )
{
printf(“You lose! The dealer scored %d and you scored %d. \n”, sumd, sump3);
result_g = 0;
return result_g;
}
else if (sump3>sumd)
{
printf(“You win! You scored %d and the dealer scored %d. \n”, sump3, sumd);
result_g = 1;
return result_g;
}
}

// Again?

int again(int x)
{
int z, valido;
printf(“\n\nDo you want to play again? Enter ‘1’ for yes and ‘0’ for no. \t\t\t\t\t\t”);
do
{
fflush(stdin);
scanf(“%d”,&z);
valido = (z == 0 || z == 1);
if (!valido) printf(“No valid input! \n”);

} while (!valido);

return z;
}

// INPUTCONTROLS

int inputcontrol_n(int x)
{
int valid;
do
{
valid = (x == 0 || x == 1 || x ==2);
if (!valid)
{
printf(“No valid input! Reenter: \n”);

fflush(stdin);
scanf(“%d”,&x);
}
} while (!valid);
return x;
}

char inputcontrol_c(char x)
{
int valid;
valid = (x ==’h’ || x ==’s’);
do
{
valid = (x ==’h’ || x ==’s’);
if (!valid)
{
fflush(stdin);
scanf(“%c”, &x);
valid = (x ==’h’ || x ==’s’);
if(valid) break;
printf(“No valid input456! Reenter: \t\t\t\t”);

}
} while (!valid);
return x;
}

Anuncios

Acerca de programacion1z

Curso de introducción a la programación en C
Esta entrada fue publicada en Informática e Internet. Guarda el enlace permanente.