Thread: How to join this two together?

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

    How to join this two together?

    I have about 6 players... and calling this function for each player.. Is there a way to joint it together? So i can just call it depending on the player?

    like dealplayer(1); or dealplayer(2);


    Code:
    void dealplayer1 (){
        player1.count++;
        double R = (double)rand()/(double)RAND_MAX;
        double P;
        player1.card = cards[rand() % 52];
        while (isalpha(player1.card))
            player1.card = cards[rand() % 52];
        cards[player1.card] = 'X';
        if (player1.card >= 11)
            player1.cardnew = 10;
        else
            player1.cardnew = player1.card;
        strcpy(player1a[player1.count], cardnames[player1.card]);
        player1.total += player1.cardnew;
        P = (double)1 - (((double)21 - (double)player1.total)/(double)21);
        if (R >= P)
            player1.play = 0;
        else
            player1.play = 1;
        if (player1.total >= 21)
            player1.play = 1;
    }
    
    
    void dealplayer2 (){
        player2.count++;
        double R = (double)rand()/(double)RAND_MAX;
        double P;
        player2.card = cards[rand() % 52];
        while (isalpha(player2.card))
            player2.card = cards[rand() % 52];
        cards[player2.card] = 'X';
        if (player2.card >= 11)
            player2.cardnew = 10;
        else
            player2.cardnew = player2.card;
        strcpy(player2a[player2.count], cardnames[player2.card]);
        player2.total += player2.cardnew;
        P = (double)1 - (((double)21 - (double)player2.total)/(double)21);
        if (R >= P)
            player2.play = 0;
        else
            player2.play = 1;
        if (player2.total >= 21)
            player2.play = 1;
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Yes, and you'll want to use an array to hold all of the players instead of having individual players like player1 and player2. See Arrays in C - Cprogramming.com to learn about arrays.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    so i can have
    Code:
    char PLAYER [4][7] = {"player1", "player2", "player3", "player4"};
    Code:
    void dealplayer (){ int i=0; PLAYER[i].count++; double R = (double)rand()/(double)RAND_MAX; double P; PLAYER[i].card = cards[rand() % 52]; while (isalpha(PLAYER[i].card)) PLAYER[i].card = cards[rand() % 52]; cards[PLAYER[i].card] = 'X'; if (PLAYER[i].card >= 11) PLAYER[i].cardnew = 10; else PLAYER[i].cardnew = PLAYER[i].card; strcpy(PLAYER[i]a[PLAYER[i].count], cardnames[PLAYER[i].card]); PLAYER[i].total += PLAYER[i].cardnew; P = (double)1 - (((double)21 - (double)PLAYER[i].total)/(double)21); if (R >= P) PLAYER[i].play = 0; else PLAYER[i].play = 1; if (PLAYER[i].total >= 21) PLAYER[i].play = 1; }



  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    char PLAYER [4][7] = {"player1", "player2", "player3", "player4"};
    This array just holds the players' names. If you also wanted to hold data, you'd have to use a different approach. Your code indicates the usage of a structure - do you know how to use these? An array of structures can allow you to hold variables of different data types for each "player."

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Quote Originally Posted by Matticus View Post
    Code:
    char PLAYER [4][7] = {"player1", "player2", "player3", "player4"};
    This array just holds the players' names. If you also wanted to hold data, you'd have to use a different approach. Your code indicates the usage of a structure - do you know how to use these? An array of structures can allow you to hold variables of different data types for each "player."
    I have a struct that contains values used in the previous function:
    player1.count
    player1.card
    player1.play

    etc

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by SuperMiguel View Post
    I have a struct that contains values used in the previous function:
    player1.count
    player1.card
    player1.play

    etc
    Then you will want to make an array of struct, something like this:
    Code:
    struct player players[6];

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Quote Originally Posted by christop View Post
    Then you will want to make an array of struct, something like this:
    Code:
    struct player players[6];
    i have this struct:
    Code:
    struct blackjack
    {
    	int card, cardnew, total, play, result, count, countnew, ace;
    	char playornot;
    } you, dealer, player1, player2, player3, player4;
    thats where all the variables are being held

    so you are saying to do

    Code:
    structblackjack players[6];
    and to access the variables do playernames[i].total ??

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You don't have to make an array of structs, but it is certainly beneficial and an advisable way to do it.
    Alternatively, you can declare an array of pointers to struct and initialise those pointers to point to your individual struct variables.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fork/join
    By modulo_crt in forum C Programming
    Replies: 10
    Last Post: 06-08-2009, 05:29 PM
  2. How do I know if I should join a proyect?
    By jalnewbie in forum C++ Programming
    Replies: 4
    Last Post: 12-31-2006, 02:31 AM
  3. join two different programs
    By moemen ahmed in forum Windows Programming
    Replies: 1
    Last Post: 02-06-2002, 07:45 PM