Black Jack Game

This is a discussion on Black Jack Game within the C Programming forums, part of the General Programming Boards category; This seems to work except the random number thing. I'm new to C so any help would be awesome. Code: ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    Black Jack Game

    This seems to work except the random number thing.
    I'm new to C so any help would be awesome.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    //Define All Global Variables Here
    int MinNumber,MaxNumber,Hand,Dealer,Turn;
    char action[20];
    char *HitIt = "h";
    char *Stay = "s";
    char *DealIt = "d";
    char *Quit = "q";
    
    //Define All Functions Here
    int getrand(int min, int max);
    int srand(unsigned);
    int rand();
    void Hit();
    void Deal();
    void PlayerHit();
    void DealerHit();
    void Determine();
    
    int main(int argc, const char * argv[]) {
    MaxNumber	= 11;
    MinNumber	= 0;
    Hand	 = 0;
    Dealer	 = 0;
    Turn	 = 1;
    
    srand(time(NULL));
    
    Deal();
    
    Determine();
    
    return 0;
    }
    
    // Gets two random numbers from 1 to 13 and adds them together and sets the Hand variable to it
    // Gets two random numbers from 1 to 13 and adds them together and sets the Dealer variable to it
    void Deal() {
    Hand = getrand(MinNumber,MaxNumber)+getrand(MinNumber,Max Number);
    Dealer = getrand(MinNumber,MaxNumber)+getrand(MinNumber,Max Number);
    Turn = 1;
    
    if(Dealer > 20 || Hand > 20) {
    printf("\n\nSorry but there was an error :(\n");
    printf("\t Player");
    printf("\t Dealer \t\n");
    printf("\t %i \t",Hand);
    printf("\t %i \t\n\n",Dealer);
    }
    }
    
    // Adds a random number from 1-13 to the players hand variable
    void PlayerHit() {
    if(Turn == 1) {
    Hand += getrand(MinNumber, MaxNumber);
    }
    
    else {
    printf("Sorry But there was an error");
    }
    }
    
    // Adds a random number from 1-13 to the dealers Dealer variable
    void DealerHit() {
    if(Turn == 0) {
    Dealer += getrand(MinNumber, MaxNumber);
    }
    
    else {
    printf("Sorry But there was a dealer error");
    }
    }
    
    void Determine() {
    if(action != NULL) {
    if(strcmp(action,HitIt) == 0) {
    Turn = 1;
    PlayerHit();
    }
    
    if(strcmp(action,Stay) == 0) {
    if(Dealer < 17) {
    Turn = 0;
    DealerHit();
    }
    }
    
    if(strcmp(action,DealIt) == 0) {
    printf("\nNew Hand\n\n");
    Deal();
    }
    }
    
    if(Turn == 0) {
    if(Dealer < 17) {
    DealerHit();
    Determine();
    }
    
    else if((Dealer < Hand) && (Hand <= 21)) {
    DealerHit();
    Determine();
    }
    }
    
    // Start Determining who wins
    if(Hand == 21 && Dealer != 21) {
    printf("Player:\t %i Black Jack!! you Win\n",Hand);
    printf("Dealer:\t %i",Dealer);
    printf("\nDeal(d): ");
    scanf("%s",action);
    Determine();
    }
    
    else if(Dealer == 21) {
    printf("Player:\t %i\n",Hand);
    printf("Dealer:\t %i Black Jack!! Dealer Wins",Dealer);
    printf("\nDeal(d): ");
    scanf("%s",action);
    Determine();
    }
    
    else if(Hand > 21 && Dealer < 21) {
    printf("Player:\t %i you busted!! \n",Hand);
    printf("Dealer:\t %i Dealer Wins",Dealer);
    printf("\nDeal(d): ");
    scanf("%s",action);
    Determine();
    }
    
    else if(Hand < 21 && Dealer > 21) {
    printf("Player:\t %i you Win\n",Hand);
    printf("Dealer:\t %i Dealer busted!!",Dealer);
    printf("\nDeal(d): ");
    scanf("%s",action);
    Determine();
    }
    
    else if(Hand > 21 && Dealer > 21) {
    printf("Player:\t %i you Busted!!\n",Hand);
    printf("Dealer:\t %i dealer busted!!",Dealer);
    printf("\nDeal(d): ");
    scanf("%s",action);
    Determine();
    }
    
    else {
    if(Turn == 1) {
    if(Dealer > Hand) {
    printf("Player:\t %i\n",Hand);
    printf("Dealer:\t %i",Dealer);
    printf("\nYou can Hit(h), or stay(s): ");
    scanf("%s",action);
    Determine();
    }
    
    else {
    printf("Player:\t %i\n",Hand);
    printf("Dealer:\t %i",Dealer);
    printf("\nYou can Hit(h), or stay(s): ");
    scanf("%s",action);
    Determine();
    }
    }
    }
    }
    
    // Generates a Random number between Min and Max
    int getrand(int Min,int Max){
    return(rand()%(Max-Min)+Min);
    }
    Last edited by AwesomeMMan; 05-22-2009 at 09:00 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, are you expecting this game to model a "normal" deck of cards? I've never seen a deck with cards from 0 to 10. Also, getting 21 on more than two cards is not a blackjack. And not only are you dealing zeroes, but you can't even get a blackjack since you're not allowing for aces to count as 11.

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    use the code tags please
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-07-2010, 11:08 PM
  2. Please comment on my c++ game
    By MegaManZZ in forum Game Programming
    Replies: 10
    Last Post: 01-22-2008, 10:03 AM
  3. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 12:32 PM
  4. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  5. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 11:12 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21