I've only been using c for a couple of weeks, so there will very likely be obvious errors I've overlooked. I've looked at other threads, but I don't understand a lot of what I'm reading. The program assumes an infinitely large deck.
KNOWN ISSUES:
- clearBuffer isn't currently in use, I am trying different things.
- dealerhand doesn't add values properly.
- compare function doesn't exist yet.
- Multiple non numeric inputs result in repeating of error message.
- Attempting to "hit" crashes program.
Current code:
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int deal()
{
unsigned long timenow;
timenow = time(NULL);
srand(timenow);
int deck[] ={2,3,4,5,6,7,8,9,10,10,10,10,11};
return deck[rand() % 13];
printf("%d\n", deal());
}
void clearBuffer(){
char ch;
while ((ch = getchar()) != '\n' && ch != EOF);
}
int dealerhand()
{
int dealerhand[10];
int dealertotal = 0;
while (dealertotal < 17)
{
dealertotal = deal();
printf("%d\n dx", deal());
dealertotal = dealertotal + deal();
printf("%d\n D", dealertotal);
}
}
int playerhand()
{
int playerhand [10];
int i;
for(i=0; i<1; i++)
{
playerhand[i] = deal();
printf(" %d\n", playerhand[i]);
}
}
int hit()
{
int i;
int playerhand[10];
playerhand[i] = deal();
printf(" %d", playerhand[i]);
}
int main()
{
int hold = 1;
int num;
int num2;
int money = 500;
printf("Welcome to Blackjack! You have $500. Play a hand? 1=y 2=n. ");
while (hold = 1)
{
scanf ("%d",&num);
if (num == 1)
{
printf("Great! Ante is $20, here's your hand!");
playerhand();
playerhand();
dealerhand();
money = money - 20;
printf("You have %d dollars. Hit? 1=y 2=n", money);
//clearBuffer();
scanf("%d", num2);
if (num2 = 1)
{
playerhand();
break;
}
if (num2 = 2)
{
// compare();
}
}
if (num == 2)
{
printf("Too bad, come back soon!");
break;
}
if (num != 1 || num != 2)
{
printf("Sorry what was that? Play a hand? 1=y 2=n.");
while((num = getchar()) =! EOF && num != '\n');
}
}
}