Thread: Need help again...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    18

    Need help again...

    Heres my whole code.

    I already tried fixing its errors and the compiler said its error-free...

    But still it wont work when I run it.

    And one thing i dont have a main function.

    Is that the cause of it?

    The code is too long sorry...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    #include <process.h>
    
    
    int player_card1, player_card2;
    int dealer_card1, dealer_card2;
    int lost=0, won=0, tied=0;
    int w, x, y, z, a=0;
    int cards[53];
    int player_total, dealer_total;
    int player_ace=0, dealer_ace=0;
    int test=0;
    char choice3='j';
    char choice2='j';
    char names2[53][25];
    char names[53][25] = {
    							"Zero",
    "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades",
    "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades",
    "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades",
    "King of Spades",
    
    "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts",
    "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts",
    "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts",
    "King of Hearts",
    
    "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs",
    "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs",
    "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs",
    "King of Clubs",
    
    "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds",
    "Five of Diamonds", "Six of Diamonds",	"Seven of Diamonds",	"Eight of Diamonds",
    "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds",
    "King of Diamonds"
    					  };
    
    void game();
    void shuffle();
    char hit_or_stand();
    void dealt();
    void deal_cards();
    void card_values();
    void out();
    void start();
    
    void start()
    {
    		system("cls");
    		shuffle();
    		deal_cards();
    		game();
    }
    
    void deal_cards()
    {
    	if(a==53)
    	{
    		printf("Deck was just re-shuffled. \n");
    		shuffle();
    		a=0;
    	}
    
    	 a++;
    	 
    	 if(a==53)
    	{
    		printf("Deck was just re-shuffled. \n");
    		shuffle();
    		a=1;
    	}
    
    	player_card1 = cards[a];
    	strcpy(names2[1],names[a]);
    		w=a;
    	a++;
    
    	if(a==53)
    	{
    		printf("Deck was just re-shuffled. \n");
    		shuffle();
    		a=1;
    	}
    
    	player_card2 = cards[a];
    	strcpy(names2[2],names[a]);
    		x=a;
    	a++;
    
    	player_total= player_card1 + player_card2;
    
    	if(a==53)
    	{
    		printf("Deck was just re-shuffled. \n");
    		shuffle();
    		a=1;
    	}
    
    	dealer_card1 = cards[a];
    	strcpy(names2[3],names[a]);
    		y=a;
    	a++;
    
    	if(a==53)
    	{
    		printf("Deck was just re-shuffled. \n");
    		shuffle();
    		a=1;
    	}
    
    	dealer_card2 = cards[a];
    	strcpy(names2[4],names[a]);
    		z=a;
    
    	dealer_total = dealer_card1 + dealer_card2;
    
    
    	if(player_card1==1)
    	{
    		player_ace = 1;
    		player_card1 = player_card1 + 10;
    		player_total = player_card1 + player_card2;
    	}
    
    
    	if(player_card2==1)
    	{
    
    		if(player_card1 != 11)
    		{
    			player_ace = 1 ;
    			player_card2 = player_card2 + 10;
    			player_total = player_card1 + player_card2;
    		}
    
    		else
    		{
    			player_card2 = 1 ;
    			player_total = player_card1 + player_card2;
    		}
    	}
    
    	if(dealer_card1==1)
    	{
    		dealer_ace = 1;
    		dealer_card1 = dealer_card1 + 10;
    		dealer_total = dealer_card1 + dealer_card2;
    	}
    
    
    	if(dealer_card2==1)
    	{
    		if(dealer_card1 != 11)
    		{
    			dealer_ace = 1;
    			dealer_card2 = dealer_card2 + 10;
    			dealer_total =dealer_card1 + dealer_card2;
    		}
    
    		else
    		{
    			dealer_card2=1;
    			dealer_total = dealer_card1 + dealer_card2;
    		}
    	}
    	
    	dealt(&player_total, &dealer_total) ;
    	game(test, &player_total, &dealer_total);
    
    }
    
    void dealt()
    {
    	printf("You are dealt with %s and %s for a total of %d.\n",names2[1], names2[2], player_total);
    	printf("Dealer is dealt a %s and a %s for a total of %d.\n",names2[3], names2[4], dealer_total);
    }
    
    char hit_or_stand()
    {
    	 char choice;
    	 
    	 printf("Would you like to Hit or Stand? (Press H or S)\n");
    	 scanf("%c", &choice);
    	 return(choice);
    }
    
    void game()
    {
    	if(test==0)
    	{
    
    		if((player_total==21) && (dealer_total!=21))
    		{
    			dealt() ;
    			printf("You have BlackJack! You win!\n");
    			out();
    		}
    
    		else if((dealer_total==21) && (player_total!=21))
    		{ 
    			dealt() ;
    			printf("Dealer has BlackJack1! You Lose.\n");
    			out();
    		}
    
    		else if((dealer_total==21) && (player_total==21))
    		{
    			dealt() ;
    			printf("You both have BlackJack! It's a tie!\n");
    			out();
    		}
    
    
    		else
    		{
    			test=1;
    			dealt();
    			choice2 = hit_or_stand(choice3);
    		}
    	}
    
    	 if(((choice2 == 'H') || (choice2 == 'h')) && (test==1))
    	{
    		a++;
    		if(a==53)
    		{
    			printf("Deck was just re-shuffled. \n");
    			shuffle();
    			a=1;
    		}
    
    
    		if(cards[a]==1)
    		{ 
    			if(player_total<=10)
    			{
    				player_ace=1;
    				player_total=player_total+11;
    			}
    
    
    			else
    			{
    				player_total=player_total + 1;
    			}
    		}
    
    		else
    			player_total= player_total + cards[a];
    
    
    		if((player_total>=22) && (player_ace==1))
    		{
    			player_total= player_total-10;
    			player_ace=0;
    		}
    
    
    		if(player_total>=22)
    		{
    			printf("You are dealt a %s for a total of %d\n",names[a], player_total);
    			printf("You are over. Dealer wins.\n");
    			out();
    		}
    
    		else if(player_total==21)
    		{
    			printf("You are dealt a %s for a total of %d\n",names[a], player_total);
    			printf("You have BlackJack! You win!\n");
    			out();
    		}
    
    		else if(player_total<=20)
    		{
    			printf("You are dealt a %s for a total of %d \n",names[a], player_total);
    			choice2=hit_or_stand(choice2);
    			game();
    		}
    
    	}
    
    
    	else if(((choice2 =='S') || (choice2 == 's')) && (test==1))
    	{
    
    		while(dealer_total<=16)
    		{
    			a++;
    			
    				if(a==53)
    			{
    				printf("Deck was just re-shuffled. \n");
    				shuffle();
    				a=1;
    			}
    
    
    
    			if(cards[a]==1)
    			{ 
    
    				if(dealer_total<=10)
    				{
    					dealer_ace=1;
    					dealer_total=dealer_total+11;
    				}
    
    
    				else
    				{
    					dealer_total= dealer_total + 1;
    				}
    			}
    
    			else
    				dealer_total= dealer_total + cards[a];
    
    
    			if((dealer_total>=22) && (dealer_ace==1))
    			{
    				dealer_total= dealer_total-10;
    				dealer_ace=0;
    			}
    
    
    			printf("Dealer is dealt a %s for a total of %d\n", names[a], dealer_total);
    		}
    
    
    		if(dealer_total>=22)
    		{
    			 printf("Dealer has a total of %d \n",dealer_total);
    			 printf("Dealer busts. You win!\n");
    			 out();
    		}
    
    		else if(dealer_total==21)
    		{
    			 printf("Dealer has BlackJack. You lose.\n");
    			 out();
    		}
    
    		else if((dealer_total>player_total) && (dealer_total<=20))
    		{ 
    
    			printf("Dealer stands with a %d \n",dealer_total);
    			printf("Dealer wins\n");
    			out();
    		}
    
    		else if(dealer_total==player_total)
    		{
    			printf("Dealer stands with a %d \n",dealer_total);
    			printf("You tied\n");
    			out();
    		}
    
    		else if(dealer_total<player_total)
    		{
    			printf("Dealer stands with a %d \n",dealer_total);
    			printf("You win!\n");
    			out();
    		}
    	}
    
    	else if((choice2 !='S') && (choice2 != 's') && (test==1) && (choice2 !='H') && (choice2 != 'h') && (choice2 !='\r'))
    	{
    		printf("Sorry that is not an option. Please try again.\n");
    		choice2=hit_or_stand(choice2);
    		game();
    	}
    
    }
    
    void out()
    {
    	char choice4;
    	
    	printf("\n");
    	printf("Would you like to play again? (Press Y or N) \n");
    	scanf("%c",&choice4);
    
    	while((choice4!='Y') && (choice4!='y') && (choice4!='N') && (choice4!='n'))
    	{
    		printf("\n");
    		printf("Sorry that is not an option. Please try again.\n");
    		scanf("%c",&choice4);
    	}
    
    
    	if((choice4 == 'Y') || (choice4 == 'y'))
    	{
    		printf("\n");
    		test=0;
    		player_ace=0;
    		dealer_ace=0;
    		deal_cards();
    		game();
    	}
    
    	else if((choice4 == 'N') || (choice4 == 'n'))
    	{
    		printf("\n");
    		printf("Thank you for playing \n");
    		printf("\n");
    	}
    	
    }
    
    void shuffle()
    {
    	int  j, k, temp;
    	char temp2[25];
    
    
    	srand((unsigned) time(NULL));
    	
    	j = 52;
    	while(j > 0)
    	{
    	  k = rand() % j +1;
    
    	  temp = cards[j];
    	  strcpy(temp2,names[j]);
    
    	  cards[j] = cards[k];
    	  strcpy(names[j],names[k]);
    
    	  cards[k] = temp;
    	  strcpy(names[k],temp2);
    
    	  j--;
    	}
    
    	
    }
    
    void card_values()
    {
    	int index, l;
    
    
    	for(index=1; index<=10; index++)
    	{
    		cards[index] = index;
    	}
    
    	cards[11]=10;
    	cards[12]=10;
    	cards[13]=10;
    
    	l=1;
    
    	for(index=14; index<=23; index++)
    	{
    		cards[index]=l;
    		l++;
    	}
    
    	cards[24]=10;
    	cards[25]=10;
    	cards[26]=10;
    
    	l=1;
    
    	for(index=27; index<=36; index++)
    	{
    		cards[index]=l;
    		l++;
    	}
    
    	cards[37]=10;
    	cards[38]=10;
    	cards[39]=10;
    
    	l=1;
    
    	for(index=40; index<=49; index++)
    	{
    		cards[index]=l;
    		l++;
    	}
    
    	cards[50]=10;
    	cards[51]=10;
    	cards[52]=10;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you don't have a main() no executable file was produced, so you have no program to run.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    18
    Thankies I really didn't know that.

    Anyway i tried making start function the main.

    Code:
    void main()
    {
    
    	int option;
    
    
    	card_values();
    
    	printf("Press 1 To play black jack\n");
    	printf("Press 2 To exit\n");
    	scanf("%d", &option);
    
    	while((option!=1) && (option!=2))
    	{
    		printf("\n");
    		printf("Sorry that is not an option. Please try again.\n");
    		printf("\n");
    		printf("Press 1 To play black jack\n");
    		printf("Press 2 To exit\n");
    		scanf("%d", &option);
    	}
    
    
    	if (option==1)
    	{
    		system("cls");
    		shuffle();
    		deal_cards();
    		game();
    	}
    
    	else if(option==2)
    	{
    		out();
    	}
    
    }
    heres what happens:
    http://i166.photobucket.com/albums/u102/xetehkah/c.jpg

    the printf always repeats itself.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    All dealt does is print the hands. Since you call it approximately 432 times as you go through the course of a hand, you're going to see it a lot, I would think. (For example, it's the last thing that happens in deal_cards, and the first thing that happens in game.)

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    18
    oh so thats why...

    ill try fixing it thanks for pointing it out...

Popular pages Recent additions subscribe to a feed