Code help, function help

This is a discussion on Code help, function help within the C Programming forums, part of the General Programming Boards category; Im making a blackjack game, there is an array cards: Code: int cards[] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,\ 8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13}; Code: void dealplayer1 (){ ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    56

    Code help, function help

    Im making a blackjack game, there is an array cards:
    Code:
    int cards[] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,\
            8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13};
    Code:
    void dealplayer1 (){
    	double R = (double)rand()/(double)RAND_MAX;
    	double P;
    	player1.card1 = cards[rand() % 52];
    	while (isalpha(player1.card1))
    		player1.card1 = cards[rand() % 52];
    	cards[player1.card1] = 'X';
    	if (player1.card1 == 11 || player1.card1 == 12 || player1.card1 == 13 )
    		player1.card1new = 10;
    	else
    		player1.card1new = player1.card1;
    	player1.card2 = cards[rand() % 52];
    	while (isalpha(player1.card2))
    		player1.card2 = cards[rand() % 52];
    	cards[player1.card2] = 'X';
    	if (player1.card2 == 11 || player1.card2 == 12 || player1.card2 == 13 )
    		player1.card2new = 10;
    	else
    		player1.card2new = player1.card2;
    	player1.total = player1.card1new + player1.card2new;
    	P = (double)1 - (((double)21 - (double)player1.total)/(double)21);
    	while (R <= P)
    		player1.extra = cards[rand() % 52];
    		while (isalpha(player1.extra))
    			player1.extra = cards[rand() % 52];
    		cards[player1.extra] = 'X';
    		if (player1.extra == 11 || player1.extra == 12 || player1.extra == 13 )
    			player1.extranew = 10;
    		player1.total += player1.extranew;
    	
    }

    This is the code for player1 of the game the problem im having is increase how many cards he can get Is there a better way to do this? looks a bit messy
    Last edited by SuperMiguel; 06-15-2012 at 08:04 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,193
    I don't know if I fully understand your intent with this code. There are some nit-picky specifics of game play I could point out, but I'm guessing that's not what you're looking for at the moment.

    I'm also not clear on what "increase implementation number" is supposed to mean.

    However, a brief analysis of your code, in addition to your indentation, leads me to conclude that you neglected to add braces to the block of code beneath your last "while" loop. I imagine you want all of those lines to execute during each iteration of the loop.

    And even if you meant to include these braces, it would yield an infinite loop if "R <= P" (not sure what values these are supposed to represent, either) because neither of those values are updated in the body of the loop (with or without the braces).

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Quote Originally Posted by Matticus View Post
    I don't know if I fully understand your intent with this code. There are some nit-picky specifics of game play I could point out, but I'm guessing that's not what you're looking for at the moment.

    I'm also not clear on what "increase implementation number" is supposed to mean.



    However, a brief analysis of your code, in addition to your indentation, leads me to conclude that you neglected to add braces to the block of code beneath your last "while" loop. I imagine you want all of those lines to execute during each iteration of the loop.

    And even if you meant to include these braces, it would yield an infinite loop if "R <= P" (not sure what values these are supposed to represent, either) because neither of those values are updated in the body of the loop (with or without the braces).
    Sorry I just did very fast, will update when I get home. Basically this is a player playing by it self, p is generated using a formula and r randomly. The while at the end is missing few things will update soon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Code (function)
    By MuzicMedia in forum C++ Programming
    Replies: 1
    Last Post: 04-04-2008, 10:51 PM
  2. I need help with one function in my code....I think
    By NicAuf in forum C++ Programming
    Replies: 12
    Last Post: 10-14-2007, 01:44 PM
  3. function code?
    By egomaster69 in forum C Programming
    Replies: 14
    Last Post: 11-17-2004, 09:37 AM
  4. Question about function code
    By SourceCode in forum C Programming
    Replies: 7
    Last Post: 02-19-2003, 01:02 PM
  5. Why does this code not function....
    By rmullen3 in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2002, 03:07 PM

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