[Ask]Card combine and shuffle

Hi everyone. I got a problem with combine
I have project to make card game without GUI(CMD view), for 4 players(me + 3 CPU)
The card have 4 elements : Diamonds, Clubs, Hearts, Spade
This is part of my code

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main( void ){
	
	char Diamond[13][15] = {"2 Diamond", "3 Diamond", "4 Diamond", "5 Diamond", "6 Diamond", "7 Diamond", "8 Diamond", "9 Diamond", "10 Diamond", "Jack Diamond", "Queen Diamond", "King Diamond", "As Diamond"};
	char Club[13][15] = {"2 Club", "3 Club", "4 Club", "5 Club", "6 Club", "7 Club", "8 Club", "9 Club", "10 Club", "Jack Club", "Queen Club", "King Club", "As Club"};
	char Heart[13][15] = {"2 Heart", "3 Heart", "4 Heart", "5 Heart", "6 Heart", "7 Heart", "8 Heart", "9 Heart", "10 Heart", "Jack Heart", "Queen Heart", "King Heart", "As Heart"};
	char Spade[13][15] = {"2 Spade", "3 Spade", "4 Spade", "5 Spade", "6 Spade", "7 Spade", "8 Spade", "9 Spade", "10 Spade", "Jack Spade", "Queen Spade", "King Spade", "As Spade"};

Next step is combine these 4 elements into 1, and shuffle it with random (rand)...
The question is how to combine and random it and split for 4 players(each 13 cards) ?
I try with strcat but it doesn't work...
Last edited on
You could have a char deck[52][15] and strcpy them all, but that would be a lot of work. You could also have a char* deck[52]; and for(int i = 0; i < 13; i++) deck[i] = Diamond[i]; and so on.
Then to shuffle you'd have to choose two random integers [0;52) and swap deck[i1] with deck[i2]. Repeat it a bunch of times.
Did you try searching for an answer before posting here? On the very first page of a google search for "std random shuffle" http://www.cplusplus.com/reference/algorithm/random_shuffle/
Thx quirkyusername
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    srand( time(NULL) );

    char Diamond[13][15] =
    {
        "2 Diamond", "3 Diamond", "4 Diamond", "5 Diamond", "6 Diamond",
        "7 Diamond", "8 Diamond", "9 Diamond", "10 Diamond",
        "Jack Diamond", "Queen Diamond", "King Diamond","Ace Diamond"
    };

    char Club[13][15] =
    {
        "2 Club", "3 Club", "4 Club", "5 Club", "6 Club",
        "7 Club", "8 Club", "9 Club", "10 Club", "Jack Club",
        "Queen Club", "King Club", "Ace Club"
    };

    char Heart[13][15] =
    {
        "2 Heart", "3 Heart", "4 Heart", "5 Heart", "6 Heart",
        "7 Heart", "8 Heart", "9 Heart", "10 Heart",
        "Jack Heart", "Queen Heart", "King Heart", "Ace Heart"
    };

    char Spade[13][15] =
    {
        "2 Spade", "3 Spade", "4 Spade", "5 Spade", "6 Spade",
        "7 Spade", "8 Spade", "9 Spade", "10 Spade",
        "Jack Spade", "Queen Spade", "King Spade", "Ace Spade"
    };


    char* Deck[52];
    int i;

    //put on deck
    for (i=0; i<13; i++)
    {
        Deck[i]    = Diamond[i];
        Deck[13+i] = Club[i];
        Deck[26+i] = Heart[i];
        Deck[39+i] = Spade[i];
    }

    //print
    for (i=0; i<52; i++)
    {
        printf("%16s\t", Deck[i]);
        if ((i) % 2)
        {
            printf("\n");
        }

    }


    char buf[256];
    gets(buf);
    printf("\n\n");


    //shuffle
    for (i=0; i<52; i++)
    {
        int r = rand()%52;
        char* tmp = Deck[i];
        Deck[i] = Deck[r];
        Deck[r] = tmp;
    }

    //print
    for (i=0; i<52; i++)
    {
        printf("%16s\t", Deck[i]);
        if ((i) % 2)
        {
            printf("\n");
        }

    }

    return 0;
}

http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
http://www.cplusplus.com/reference/clibrary/cstdio/gets/
http://www.cplusplus.com/reference/clibrary/cstdio/printf/
blackcode41 you are genius !! WOOO...
You do not even use strcat or strcpy
Thank you very much with this all problem cleared^^
Last edited on
Topic archived. No new replies allowed.