| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Black Jack C - Code
Hi !!
Im doing this program just for fun, and have a problem which is that ill like to split my hand. But can't make it work. Is trying "Stuct" now so... If any one could help my out it would be great. Code: Code:
// *Black Jack***Version 1.05***By: Dan L *
// ****************************************
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
struct hander {int kort[25];};
struct hander kortet(void)
{
struct hander;
int kortet[25],spelare[216],i=1;
kortet[i]=spelare[i];
printf("\nHand %d: %d %d\n",i,spelare[i],spelare[i]);
i++;
}
void main (void)
{
start:
clrscr();
int val=3,pengar=500;
slut:
// menu:
printf(" Meny: \n \n1:Spela \n2:Regler \n3:Avsluta\n");
scanf("%d",&val);
switch (val)
{
case 3: goto slut;
case 2: printf("Vi spelar efter dom regel som g„ller p† kasino \n");
printf("1. Vid splittning av ess f†r man endast ett kort\n");
printf("2. Dubbling vid 7,8,9,10 och 11\n");
printf("3. Om vid splittning, spelare f†r en summa av 7,8,9,10 eller 11\n s† kan inte dubbling anv„ndas\n");
printf("4. Dealer m†ste dra kort p† 16, men stanna p† 17\n\n");goto start;
case 1: goto spelet;
}
spelet:
int kort,i,j=0,kortlek[14],spelare[216]={0},summa[5]={0},sats,h=0;
char sval;
struct hander kortet;
srand(time(0));
FILE *spel; // One text file for 6 card decks
spel=fopen("kortlek.txt","wt");
for(i=1;i<=14;i++)
{
kortlek[i]=0; // All to '0'
}
do
{
kort=(rand()%9)+2; /* ramdomizes 2-10
if(kortlek[kort]<24) and puts all arrays to 24 */ {
fprintf(spel,"%1d \n",kort);
(kortlek[kort])++;
j++;
}
}while(j<216);
spel=fopen("kortlek.txt","rt"); // reads textfile
for (i=0;i<=216;i++)
{
fscanf(spel,"%d",&kort);
spelare[i]=kort;
}
if (pengar<=0)
{
goto start;
}
clrscr(); // The GAME !
printf("'Black Jack !' Du har: %d\n-------------\n",pengar);
printf("Vad vill du satsa?: ");
scanf("%d",&sats);
printf("Spelare: %d %d \nDealer: %d \n",spelare[1],spelare[2],spelare[3]);
summa[1]=spelare[1]+spelare[2];
summa[2]=spelare[3];
printf("______________\nDu har: %d\n",summa[1]);
i=4;
if ((summa[1]==7) || (summa[1]==8) || (summa[1]==9) || (summa[1]==10) || (summa[1]==11))
{
gotoxy(1,8);
printf("<D>ouble "); // Dubbling
}
if (spelare[1]==spelare[2])
{
gotoxy(1,8);
printf("S<p>litt "); // Splitt
}
do
{
printf("<H>it <S>tand: "); // Hit or Stand
sval=getche();
if ((sval=='h') || (sval=='H'))
{
printf("\nDu fick: %d\n",spelare[i]);
summa[1]=spelare[i]+summa[1];
summa[3]=spelare[i]+summa[3]; // summa[1] = player 1
printf("Du har: %d\n",summa[1]);
i++;
}
if ((sval=='d') || (sval=='D'))
{
printf("\nDu fick: %d\n",spelare[i]);
summa[1]=spelare[i]+summa[1];
printf("Du har: %d\n",summa[1]);
sats=sats+sats;
}
if ((sval=='p') || (sval=='P'))
{ /// THIS IS THE PROBLEM !!
kortet;
}
}while ((sval=='h') || (sval=='H') || (sval=='p'));
do
{
spelare[i]; // Dealer får kort
summa[2]=spelare[i]+summa[2];
printf("\nDealer har: %d\n",summa[2]);
i++;
//summa[2] = summan av dealer
}while (summa[2]<=16);
// Här kollas vem som vinner omgången
printf("%d %d",summa[1],summa[2]);
if ((summa[2]>summa[1] && summa[2]<21 || summa[2]==21) || (summa[1]>21))
{
printf("\n Dealer: VANN !!");
pengar=pengar-sats;
}
if ((summa[2]>21) || (summa[1]>summa[2] && summa[1]<21 || summa[1]==21))
{
printf("\n Spelare: VANN !!");
pengar=pengar+sats;
}
getch();
goto spelet;
}
Sorry that the text is in Swedish =) /D Last edited by B-Con : September 21st, 2005 at 06:18 PM. Reason: added [code] tags ;) |
|
#2
|
|||
|
|||
|
umm..
as u would expect here are questions are u doing this in C or C++ cause my recent topics i got blackjack in C++ so just look at my other topics to see how i did it also not to be rude or anything but have you heard of using namespace std; |
|
#3
|
||||
|
||||
|
Well first off I wish I knew Swedish because then maybe I could help you a little more. Well first things first, and this all depends on what compiler you are using I believe but with mine which is Dev-C++ I get a combination of errors. First suggestion is to make "void main (void)" just int main(void) that way it doesn't require you to return an integer. Then it says clrscr(); first use in function. That is because you just have it sitting on top of the statements and should probably look more like this.
Code:
void clrscr()
{
printf("'Black Jack !' Du har: %d\n-------------\n",pengar);
printf("Vad vill du satsa?: ");
scanf("%d",&sats);
printf("Spelare: %d %d \nDealer: %d \n",spelare[1],spelare[2],spelare[3]);
summa[1]=spelare[1]+spelare[2];
summa[2]=spelare[3];
printf("______________\nDu har: %d\n",summa[1]);
}
That way when you call it as a function the compiler knows what your asking, you are using it more like a goto statement. But just remember once you change it you must put a global declaration of it outside the main block and what I mean by that if your not sure of what I mean is declare it like this. Code:
void clrscr(); Put that after the included statements... Now about your goto statements most Programmers will tell you don't get accustomed to using them because there only the lazy mans way of doing things but with me on the other hand I don't mind them as long as they are well commented on. In your case it probably still wouldn't have helped me because you use a different language than mine and if I wasn't American than I probably would know more than one lol. On to the next error, your use of srand or your random number error could be fixed by adding the #include<ctime> library. Now as far as the last error I get I'm not sure what the heck it does as of yet so I'm not sure how to correct it, its the statement 'gotoxy(1,8);' now I'm not sure what you are attempting here but, with your previous uses of the goto statement I'm assuming you are trying to goto something with a couple of stipulation, but I'm not sure if its that or just Swedish for something else =). So, anyways This will not solve the fact of splitting the deck or anything but it should get you past some of those errors to let you continue figuring this out. I hope this helps a little. I will try to look your code over a little more to see if I can figure it out a little better. |
|
#4
|
|||
|
|||
|
Ok !!
Hi
it's build in C code !! im using Boralnd C++ 5.02 so that is why its some more problems in dev !! know that im probably in the wrong forum for that matter but but !! It help a bit but !! thnx anyway /D |
|
#5
|
|||
|
|||
|
Hi HKD,
It seems that none of the others has answered your actual question. When you started the program you did not take this into account, so now you know why it is good to make some kind of 'functional design' before starting the C-editor. Not that I'm doing that all the time but you should always make your program as flexible as possible.As a possible solution... At this moment you are using summa[1] for the player and summa[2] for the dealer. Since you start with element 1 you have element 0 still available. I suggest you initialize this with summa[0]=0 at the start of the game (e.g. line 82). When the user wants to split you can do the following: Code:
if ((sval=='p') || (sval=='P'))
{
if(i == 4) // only split when no other cards are dealt
{
// split the hand
summa[0]=spelare[1];
summa[1]=spelare[2];
// hit twice
printf("\nDu fick: %d & %d\n",spelare[i], spelare[i+1]);
summa[0]=spelare[i++]+summa[0];
summa[1]=spelare[i++]+summa[1];
printf("Du har: %d & %d\n", summa[0], summa[1]);
}
}
and adjust the 'Hit' part: Code:
if ((sval=='h') || (sval=='H'))
{
// Check to see if the hand is split
if(summa[0]==0)
{
// No split, normal hit
printf("\nDu fick: %d\n",spelare[i]);
summa[1]=spelare[i]+summa[1];
printf("Du har: %d\n",summa[1]);
i++;
}
else
{
// Split hand, hit twice
printf("\nDu fick: %d & %d\n",spelare[i], spelare[i+1]);
summa[0]=spelare[i++]+summa[0];
summa[1]=spelare[i++]+summa[1];
printf("Du har: %d & %d\n", summa[0], summa[1]);
}
}
I'm sure you can work the rest out yourself (double and winning parts) As for the other remarks: a goto is indeed 'not done'. The program is not very readable and maintainable. In fact, it even slows down your program. Your code could easilly be cleaned by using functions for each goto. Also, the last if-statements could use a pair of brackets. Never use && and || without brackets: Code:
if ( ( (summa[2] > summa[1]) &&
( (summa[2] < 21) ||
(summa[2] == 21) ) ) ||
(summa[1] > 21) )
or just ;-) Code:
if ( (summa[1] > 21) ||
( (summa[2] > summa[1]) &&
(summa[2] <= 21) ) )
Note that when both the dealer and the player have > 21, the dealer wins in your game. Don't hit the dealer when the player has > 21. Last but not least, your program does not take into account that a Ace can be 1 or 11. At this moment the player or dealer will have 22 points with two aces which should be 12 or even 2. Good luck with your progress!!! To Geo: this is a 'DOS' game. clrscr() is a function to clear the screen. gotoxy() is a function to move the cursor to a specific place on the screen, before printf a text. Since the age of Windows, this is not used anymore. But to learn programming, one can still use it... ![]() |
|
#6
|
|||
|
|||
|
Thnx
Great..
That will help me allot thnx MichaelSoft.. Yes i know that about the aces, ill take that last =) great, ive had allto of propblams thinking out how to make the hit function too know if it´s a splitt or not =) I feel so stupid...but yehh yehh...We all do some time !! Thnx...I'll post the game when it's finnish will I hope is in about 4 weeks, do to some school work =) /D |
|
#7
|
|||
|
|||
|
OK...One big problem left then tho
Yepp
And MichaelSoft, i've really want to change the "goto" to som other function, but can't figure out what !! The biggest problem for me now is the splitting, it wokrs just like MichaelSoft wrote, but i wan't the program to know if the next hand also can splitt ex. Hand 1: 8 8 => splitt Hand 1: 8 7 Hand 2: 8 8 => splitt hand 2... and so on !! I'm thinking of something like an array that keeps track of the hands..but can't get to work !! |
|
#8
|
|||
|
|||
|
Oh ... is splitting not limited to 1 hand? :-o
Regarding the goto: In stead of putting all code into main() and use 'goto spelet'. Put the code after spelet: in a function named Spelet(), and call this in main(). I'm going home now, but maybe I've some time later... Regards! |
|
#9
|
|||
|
|||
|
Splitt...
Hmm....Not what i know, the rules are based on "swedish bar's"-rules...and there u can splitt as many times as u want.
Trying with the spelet() and main()...I'll se if it works. Thnx for any help i can get ! /D |
|
#10
|
|||
|
|||
|
Okay ... I've had some thought about this, but I'm not sure if everything is possible due to the setup of your program. Maybe it is wise to start all over when the program is almost finished.
I suggest you switch to a structure containing the hand of a player. This way you can also update to a multiple player game. The structure can be something like: Code:
typedef struct {
int iHands; // Number of 'hands'
int iSum[4]; // Maximum of 4 hands; cannot split more then this
int iAces[4]; // Counts the aces in a hand
} HAND;
With this you would be able to solve the problems. Do something like this: Code:
HAND Player, Dealer;
memset(Player, 0, sizeof(HAND)); // Set all to zero
memset(Dealer, 0, sizeof(HAND)); // Set all to zero
Player.iHands=1; // Start with 1 hand
Dealer.iHands=1; // Start with 1 hand
Hit(&Player, 0, spelare, &i); // First Card for first hand
if(Hit(&Player, 0, spelare, &i)) // Second Card for first hand
{
Split(&Player, 0, spelare, &i))
}
Code:
bool Hit(HAND *Player, int iHand, int *spelare, int *i)
{
bool bRet = (Player->iSum[iHand] == spelare[*i]); // Determine if the cards are the same
// Keep track of the Aces
if(spelare[*i] == 11)
{
Player->iAces[iHand]++;
}
// Hit one card, and go to next card
Player->iSum[iHand] += spelare[*i];
(*i)++;
return bRet;
}
Code:
void Split(HAND *Player, int iHand, int *spelare, int *i)
{
/*
Ask for split, if not return without doing anything
*/
int *CurHand = Player->iSum[iHand]; // Current Hand
int *NewHand = Player->iSum[Player->iHand]; // Next Hand
// Split the hand
*NewHand = *CurHand/2;
*CurHand = *NewHand;
// Following order is important!
// Deal a new card and check for split option
if(Hit(Player, Player->iHand, spelare, &i))
{
Split(Player, Player->iHand++, spelare, &i))
}
// Deal a new card and check for split option
if(Hit(Player, iHand, spelare, &i))
{
Split(Player, iHand, spelare, &i))
}
}
Then later in the game for the hit Code:
int hand;
for(hand=0; hand<Player.iHands; hand++)
{
do
{
/*
show sum for current hand: Player.iSum[hand]
ask for hit or stand
when hit:
*/
if ((sval=='h') || (sval=='H'))
{
Hit(&Player, hand, spelare, &i);
/*
Check for > 21
*/
}
}
while((sval=='h') || (sval=='H')) // Continue while not stand
}
{/CODE]
This must be done for the dealer also. Since he can split to!
As for the Aces I suggest two functions:
[CODE]
void ShowSum(HAND *Player, int iHand)
{
int i;
// First check the overflow
while((Player->iSum[iHand] > 21) &&
(Player->iAces[iHand]))
{
// Sum > 21: Use an Ace as 1
Player->iSum[iHand] -= 10;
Player->iAces[iHand]--;
}
// Print the possible Aces combinations (start with all 11, then work down
printf("Du har:");
for(i=0; i<=Player->iAces[iHand]; i++)
{
printf(" %d", Player->iSum[iHand]-(i*10));
}
printf("\n-------------\n",pengar);
}
The Sum will now always contain the largest valid value Hope you can use this idea.... ![]() |
|
#11
|
|||
|
|||
|
Great Thnx...
I'll try to understand it all and do some configuation to my program...
Hmm, did put in your code, but the program got angry with your code... For ex: memset(Player,0,sizeof(HAND)); it want to have a ")" after Player....and some other damn **** =) Did attach it so u can test ur self...didnät put my own code in there yet so ! Thnx allot // regards // Dan ![]() |
|
#12
|
|||
|
|||
|
You have copied my suggested code right into a cpp file ... the code isn't even in a main() function what so ever!
The meaning was that you tried to understand the code, and modify your program accordingly. But okay .. find below a more workable compilation. You must insert the card-randomizer, determine the winner and the doubler yourself... No idea if the code works, but at least it compiles on my machine ![]() Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct
{
int iHands; // Antal händer
int iSum[24]; // Max händer
int iAces[24]; // Håller koll på Äss
} HAND;
bool Hit(HAND *Player,int iHand, int *spelare, int *i)
{
bool bRet = (Player->iSum[iHand]==spelare[*i]); // Kollar om korten är lika
if(spelare[*i]==11)
{
Player->iAces[iHand]++;
}
// Ger ett kort och sedan går till nästa
Player->iSum[iHand]+=spelare[*i];
(*i)++;
return bRet;
}
void Splitt(HAND *Player, int iHand, int *spelare, int *i)
{
// Frågar om splitt
int *CurHand = &(Player->iSum[iHand]); // 1:a Handen
int *NewHand = &(Player->iSum[Player->iHands]); // 2:a Handen
// Splitt
*NewHand = *CurHand/2;
*CurHand = *NewHand;
// Ger nya kort och kollar för ny splittning
if(Hit(Player,Player->iHands,spelare,i))
{
Splitt(Player,Player->iHands++,spelare,i);
}
// Nytt kort och kollar igen för splitt
if(Hit(Player,iHand,spelare,i))
{
Splitt(Player,iHand,spelare,i);
}
}
void ShowSum(HAND *Player,int iHand)
{
int i;
// Kollar först om det är över 21
while((Player->iSum[iHand]>21) &&
(Player->iAces[iHand])) // Om summan är över 21 blir Äss = 1
{
Player->iSum[iHand]-=10;
Player->iAces[iHand]--;
}
for(i=0;i<=Player->iAces[iHand];i++)
{
if(i) printf(" or ");
printf("%d",Player->iSum[iHand]-(i*10));
}
}
void Spelet(int pengar)
{
int kort,iCard,j=0,spelare[216]={0},sats,h=0;
HAND Player,Dealer;
char sval;
bool bStand=false;
/*
* Randomize the card deck here
*/
while(pengar > 0)
{
clrscr(); // The GAME !
printf("'Black Jack !' Du har: %d\n-------------\n",pengar);
printf("Vad vill du satsa?: ");
scanf("%d",&sats);
memset(&Player,0,sizeof(HAND)); // Sätter alla till 0
memset(&Dealer,0,sizeof(HAND)); // Sätter alla till 0
Player.iHands=1; // Man startar med 1 hand
Dealer.iHands=1; // Man startar med 1 hand
iCard = 1; // Start with second card...
// Deal the cards for the Dealer
printf("Dealer: %d \n",spelare[iCard]);
Hit(&Dealer, 0, spelare, &iCard); // Första kortet
if (Hit(&Dealer, 0, spelare, &iCard)) // Andra kortet
{
Splitt(&Dealer,0,spelare,&iCard);
}
// Deal the cards for the Player
printf("Spelare: %d %d \n",spelare[iCard],spelare[iCard+1]);
Hit(&Player, 0, spelare, &iCard); // Första kortet
if (Hit(&Player, 0, spelare, &iCard)) // Andra kortet
{
Splitt(&Player,0,spelare,&iCard);
}
// Player cards
int hand;
for(hand=0; hand<Player.iHands;hand++)
{
printf("______________\nHand %d:\n", hand+1);
do
{
printf("Du har: ");
ShowSum(&Player, hand);
printf("<H>it <S>tand: ");
sval=getche();
if ((sval=='h') || (sval=='H'))
{
Hit(&Player,hand,spelare,&iCard); // Kollar om det är större än 21
if(Player.iSum[hand]>21)
{
printf("Busted: %d\n", Player.iSum[hand]);
break;
}
}
}while((sval=='h') || (sval=='H')); // Fortsätter om det inte är "Stand"
}
// Dealer cards
for(hand=0; hand<Dealer.iHands;hand++)
{
printf("______________\nDealer Hand %d:\n", hand+1);
while(1)
{
printf("Dealer har: ");
ShowSum(&Dealer, hand);
if(Dealer.iSum[hand]>16)
{
break;
}
Hit(&Dealer,hand,spelare,&iCard); // Kollar om det är större än 21
if(Dealer.iSum[hand]>21)
{
printf("Busted: %d\n", Dealer.iSum[hand]);
break;
}
}while(Dealer.iSum[hand] <= 16); // Fortsätter om det inte är "Stand"
}
/* Determine winning hands */
getch();
}
}
// ****************************************
void main (void)
{
int val=3;
do
{
clrscr();
int pengar=500;
// menu:
printf(" Meny: \n \n1:Spela \n2:Regler \n3:Avsluta\n");
scanf("%d",&val);
switch (val)
{
case 1: Spelet(pengar);
case 2: printf("Vi spelar efter dom regel som g„ller p† kasino \n");
printf("1. Vid splittning av ess f†r man endast ett kort\n");
printf("2. Dubbling vid 7,8,9,10 och 11\n");
printf("3. Om vid splittning, spelare f†r en summa av 7,8,9,10 eller 11\n s† kan inte dubbling anv„ndas\n");
printf("4. Dealer m†ste dra kort p† 16, men stanna p† 17\n\n");goto start;
}
}
while(val != 3);
}
|
|
#13
|
|||
|
|||
|
=)
Sorry, for my stupidity
But i haven't really learn about struct so far =)...and get any book to really learn me that so I thank u allot !! U have been a great help to me !! Will try to fix something the program to work =) Regards /D |
|
#14
|
|||
|
|||