14,826,846 members
1.00/5 (1 vote)
See more:
i am working on this blackjack game and i facing some problems that i can't find the soulotion , i hope someone can help me..
the code is unfisished yet, but one of the main problems i have is how to print the cards , i wrote a function that is supossed to do this but i am having a touble using it in the code..
run example:
```Welcome to CS intro Blackjack!
Start by entering a random seed:
5
You currently have 1000 fake dollars. How much do you want to bet?
500
The computer got Q and A.
You got 10 and 2.
The current sum is 12. Do you want another card? (Y / N)
Y
Extra card:5.
The current sum is 17. Do you want another card? (Y / N)
N
You have won 500 fake dollars!
You currently have 1500 fake dollars. How much do you want to bet?
500
The computer got 7 and 2.
You got J and 10.
You have won 500 fake dollars!
You currently have 2000 fake dollars. How much do you want to bet?```

What I have tried:

C++
```#include <stdio.h>
#include <stdlib.h>

#define INITIAL_MONEY 1000
#define MIN_BET 1

int rand_range (int low, int high);
void srand_range (unsigned int seed);
int bett(int money);
int special_card(int card_array[NUMOFCARDS]);
int chosecomputercards(int first_card,int second_card);
int chickcomputercards(int sumcomp,int special_card,int bet);
int choseplayercard(int first_card,int second_card);
int chickplayersum(int sumplayer,int bet,int money);
int chick_winner(int computersum,int playersum);
void printcard(int card);

int main()
{
int money=INITIAL_MONEY;
int bet;
int seed;
int sumplayer=0,sumcomp=0;
int computer_total_sum;
int player_total_sum;
int card_array[NUMOFCARDS]={0};
int winner;
printf("Welcome to CS intro Blackjack!\n");
printf("Start by entering a random seed:\n");
scanf("%d",&seed);
srand_range(seed);
while(money>=1)
{
bet=bett(money);
computer_total_sum=chickcomputercards(sumcomp,special_card,bet);
while(computer_total_sum<21)
{
player_total_sum=chickplayersum(sumplayer,bet,money);
if(player_total_sum>21)
break;
while(player_total_sum<21)
{
winner=chick_winner(computer_total_sum,player_total_sum);
}
}
}

return (0);
}

int rand_range (int low, int high) // RAND_MAX assumed to be 32767
{
static unsigned int next = 1;
next = next * 1103515245 + 12345;
return ((unsigned int)(next/65536) % 32768) % (high - low + 1) + low;
}

void srand_range (unsigned int seed)
{
for (int i = 0; (unsigned int)i < seed; i++) {
rand_range(i,i+1);
}
}

void printcard(int card)
{
switch(card){
case 1: printf("A"); break;
case 11: printf("J"); break;
case 12: printf("Q"); break;
case 13: printf("K"); break;
default:
printf("%d",card); break;
}
}

int bett(int money)
{
int riskedMoney;
printf("You currently have %d fake dollars. How much do you want to bet?\n",money );
scanf("%d",&riskedMoney);
while((riskedMoney>money)||(riskedMoney<min_bet mode="hold" />      {
printf("Your bet must be at least 1 and at most %d, try again:\n",money );
scanf("%d",&riskedMoney);
}
return(riskedMoney);
}

int special_card(int card_array[NUMOFCARDS])
{
int n=NumOfCards-1;
for (int i=NumOfCards-1;i>=0;i--)
{
if (card_array[i]<card_array[n])
{
n=i;
}
}
return(n+1);
}

int chosecomputercards(int first_card,int second_card)
{
int sum=0;
first_card=rand_range(1,13);
second_card=rand_range(1,13);
printf("The computer got");
printcards(first_card);
printf("and");
printcard(second_card);
sum=first_card+second_card;
return(sum);
}

int chickcomputercards(int sumcomp,int rare_card,int bet)/*finds the sum of cards that the cmputer has*/
{
int extra_card;
int first_computer_card=0,second_computer_card=0;
rare_card=special_card(card_array[NUMOFCARDS]);
sumcomp=chosecomputercards(first_computer_card,second_computer_card);
if(sumcomp>21)
{
printf("The computer got more than 21\n");
printf("You have won %d fake dollars!\n",bet);
}
return(sumcomp);
while((sumcomp+rare_card)<21)
{
extra_card=rand_range(1,13);
if((sumcomp+rare_card+extra_card)<21)
{
printf("Extra card:%d.\n",extra_card);
sumcomp+=extra_card;
}
else
{
break;
}

}
return(sumcomp);
}
int choseplayercard(int first_card,int second_card)
{
first_card=rand_range(1,13);
second_card=rand_range(1,13);
printf("You got)
printcards(first_card);
printcard(second_card);
return(first_card+second_card);
}

int chickplayersum(int sumplayer,int bet,int money)
{
int extra_card;
int first_player_card=0,second_player_card=0;
sumplayer=choseplayercard(first_player_card,second_player_card);
while(sumplayer<21)
{
printf("The current sum is %d. Do you want another card? (Y / N)\n",sumplayer);
{
extra_card=rand_range(1,13);
printf("Extra card:%d.\n",extra_card);
sumplayer=sumplayer+extra_card;
}
else
break;
}
return(sumplayer);
if(sumplayer>21)
{
printf("The computer has won this round… you lose %d fake dollars!\n",bet);
money=money-bet;
}
return(sumplayer);
}

int chick_winner(int computersum,int playersum)/*finds who won*/
{
int bet=0,money=0;
if((computersum>playersum)&&(computersum<=21))
{
printf("The computer has won this round… you lose %d fake dollars!\n",bet);
money-=bet;
}
else if((playersum>computersum)&&(playersum<=21))
{
printf("You have won %d fake dollars!\n",bet);
money+=bet;
}
else if (computersum==playersum)
{
printf("You have won %d fake dollars!\n",bet);
money+=bet;
}
return(0);```
Posted
Updated 19-May-16 11:29am
v7
CPallini 19-May-16 16:01pm

"but one of the main problems i have is how to print the cards , i wrote a function that is supossed to do this but i am having a touble using it in the code"
james7777 19-May-16 16:08pm

the function (void printcards) i want it to help me prient cards when ever i need to prient cards , for exaple instead of 1 iwant to print A ..
Matt T Heffron 19-May-16 16:09pm

So what's the problem with the way it is printing the cards?
It looks correct from your example output.
EDIT:
But not from the code! See solution below.

You probably need to add code to make sure that there are only 4 of each value of card played from each shuffle of a deck. (Or 4*n for n decks played together.)
Also, you need the "house" to "hit" until the cards total 17 or more.
james7777 19-May-16 16:18pm

the example is what is supossed to look like , and y code still doesn't show me this
and there is no need to make code to make sure there are 4 of each value, cause it doesn't matter in the specific code i am writing , i updated the code but still can't figure out how to continue. i figured out how to use it in the code but the function itself i am having a lot of problem with it..
james7777 19-May-16 17:01pm

i need another help if it is possible, in function chickcomputercards i entered to the function special_card, now i need to write a function that can find the rare card which is the card that was shoun less times till now, this card helps the computer to decide if he wants to get another card or no ,
now i have this so far but i don't think it is correct , i would be so thankful for you guys if you can help(as i really have to submet this in two hours and i am struglling not to give up ),
#define NUMOFCARDS 13
int rare (int card_array){
int n=NumOfCards-1;
for (int i=NumOfCards-1;i>=0;i--){
n=i;
}
}
return (n+1);
}

Solution 1

C++
```void printcard(int card)
{
switch(card){
case 1: printf("A"); break;
case 11: printf("J"); break;
case 12: printf("Q"); break;
case 13: printf("K"); break;
default:
printf("%d",card); break;
}
}```

and change the "forward declaration" at the top to match.
Then just call this where you want to print a card value.
james7777 19-May-16 16:25pm

thats simelar to what i did , ok i am gonna try thanks.
Matt T Heffron 19-May-16 16:30pm

Good.
Instead of replacing all of the code in the "What I have tried" section, just replace the changed code, being careful to use the character elements for the <, > and & (that's &lt; &gt; and &amp; respectively!)
That way the code will still format correctly.)
james7777 19-May-16 17:23pm

i updated my code again but now i am having a problem with the special_card function and how to call it..
Matt T Heffron 19-May-16 18:44pm

You need to go back and really learn how, and when, to pass arrays to functions.
You never modify the contents of the card_array so special_card will always just return NumOfCards (which is NOT the same as NUMOFCARDS, C is case-sensitive!)
james7777 19-May-16 18:51pm

thanks for tryieng to help, so i guess learning this would take time, and it is litrally 2:00 am where i live and the dead line is 4:00 am ,so i am not ganna make it in time i am way too tired been working on this for days , i give up :')