Function swapping only one character in an array not whole string

Hi there, I'm trying to write a simple card game.

My idea is as follows: I have this char 2d array containing 52 cards where every element is written with 6 characters, for example: a shortcut for king of hearts is "kie_K", which is 6 characters long with '\0' at the end.

So, as I've already filled this deck[][] array with 52 cards, I want to shuffle them so afterwards I can hand them to players.

For now, my problem is how to swap not only one character in the name of the card but the whole name of it, because that is what I think is happening right now in the code below.

Probably it has to do something with pointers, but I don't know how to use this fact properly... I need to have access to the particular characters in the name of the card to use them after the shuffle.

Thanks for any ideas.
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
#include <iostream>
using namespace std;
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

void shuffle(char deck[PACK][CARD]);
void swap(char *a, char *b);
void print(char deck[PACK][CARD]);

#define PACK 52
#define CARD 6
int main()
{	

char deck[PACK][CARD] = 
{"pik_A", "pik_K", "pik_Q", "pik_J", "pik_0", "pik_9", "pik_8", "pik_7", "pik_6", "pik_5", "pik_4", "pik_3", "pik_2",
"kie_A", "kie_K", "kie_Q", "kie_J", "kie_0", "kie_9", "kie_8", "kie_7", "kie_6", "kie_5", "kie_4", "kie_3", "kie_2",
"tre_A", "tre_K", "tre_Q", "tre_J", "tre_0", "tre_9", "tre_8", "tre_7", "tre_6", "tre_5", "tre_4", "tre_3", "tre_2",
"kar_A", "kar_K", "kar_Q", "kar_J", "kar_0", "kar_9", "kar_8", "kar_7", "kar_6", "kar_5", "kar_4", "kar_3", "kar_2"};

	print(deck);
	shuffle(deck);
	cout << endl << "After shuffle: " << endl;
	print(deck);

	return 0;
}

void shuffle(char deck[PACK][CARD])
{
	srand((unsigned int)time(NULL));

	for (int i = PACK - 1; i > 0; i--)
	{
		int index = rand() % (i + 1);
		swap(deck[i], deck[index]);
	}
		
}

void swap(char *a, char *b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
}

void print(char deck[PACK][CARD])
{
	for (int i = 0; i < PACK; i++)
	{
		for (int j = 0; j < CARD; j++)
		{
			cout << deck[i][j];
		}
		cout << endl;
	}
}
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
#include <iostream>
#include <string>
#include <array>
#include <random>
#include <algorithm>

// string: https://cal-linux.com/tutorials/strings.html
// array: http://www.stroustrup.com/C++11FAQ.html#std-array
const std::size_t PACK_SZ = 52 ;
// pack_type is an alias for 'standard array of 52 strings'
using pack_type = std::array< std::string, PACK_SZ > ;

void shuffle( pack_type& deck );
void print( const pack_type& deck ); // const

int main()
{

    pack_type deck =
    {
        {"pik_A", "pik_K", "pik_Q", "pik_J", "pik_0", "pik_9", "pik_8", "pik_7", "pik_6", "pik_5", "pik_4", "pik_3", "pik_2",
        "kie_A", "kie_K", "kie_Q", "kie_J", "kie_0", "kie_9", "kie_8", "kie_7", "kie_6", "kie_5", "kie_4", "kie_3", "kie_2",
        "tre_A", "tre_K", "tre_Q", "tre_J", "tre_0", "tre_9", "tre_8", "tre_7", "tre_6", "tre_5", "tre_4", "tre_3", "tre_2",
        "kar_A", "kar_K", "kar_Q", "kar_J", "kar_0", "kar_9", "kar_8", "kar_7", "kar_6", "kar_5", "kar_4", "kar_3", "kar_2"}
    };

    print(deck);

    for( int i = 0 ; i < 5 ; ++i )
    {
        std::cout << "shuffle\n" ;
        shuffle(deck);
        print(deck);
    }
}

void shuffle( pack_type& deck )
{
    // https://en.cppreference.com/w/cpp/numeric/random
    static std::mt19937 rng( std::random_device{}() ) ;

    // https://en.cppreference.com/w/cpp/algorithm/random_shuffle
    // https://en.cppreference.com/w/cpp/iterator/begin
    std::shuffle( std::begin(deck), std::end(deck), rng ) ;
}

void print( const pack_type& deck )
{
    std::cout << '\n' ;

    for( std::size_t i = 0 ; i < deck.size() ; ++i )
    {
        std::cout << deck[i] << "  " ;
        if( i%13 == 12 ) std::cout << '\n' ;
    }

    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/1e8c1c39fbfc178b
Hi, thank you for your reply, but unfortunately your approach is too extensive in comparison to my knowledge. I can't use those libraries (algorithm, string, array or random), this has to be written mostly in C language. So, if there's any chance, maybe you or someone else could help me with finding solution using simple pointers? If there is this kind of solution ....
Thanks so much
it works like anything else...

1
2
3
4
5
6
char str[] = "Hello World"; //get a c-string
char tmp;   //a temporary for the classic, simple swap algorithm
tmp = str[0];   //store one of the 2 things being swapped
str[0] = str[5]; //overwrite the saved one with the other
str[5] = tmp;    //overwrite the other with the stored value because the original is already lost above
cout << str << endl; //see your hackery at work 


you can do it with pointers, but its just messy syntax for the same thing... just use array syntax unless you have a good reason not to? Even pointer allocated c-strings allow array notation...
Last edited on
this has to be written mostly in C language

Then I recommend you stick with C.

Something more like:

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
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PACK 52
#define CARD 6

void shuffle(char deck[PACK][CARD]);
void swap(char *a, char *b);
void print(char deck[PACK][CARD]);

int main()
{

char deck[PACK][CARD] =
{"pik_A", "pik_K", "pik_Q", "pik_J", "pik_0", "pik_9", "pik_8", "pik_7", "pik_6", "pik_5", "pik_4", "pik_3", "pik_2",
"kie_A", "kie_K", "kie_Q", "kie_J", "kie_0", "kie_9", "kie_8", "kie_7", "kie_6", "kie_5", "kie_4", "kie_3", "kie_2",
"tre_A", "tre_K", "tre_Q", "tre_J", "tre_0", "tre_9", "tre_8", "tre_7", "tre_6", "tre_5", "tre_4", "tre_3", "tre_2",
"kar_A", "kar_K", "kar_Q", "kar_J", "kar_0", "kar_9", "kar_8", "kar_7", "kar_6", "kar_5", "kar_4", "kar_3", "kar_2"};

	print(deck);
	shuffle(deck);
    printf("\nAfter shuffle:\n");
	print(deck);

	return 0;
}

void shuffle(char deck[PACK][CARD])
{
	srand((unsigned int)time(NULL));

	for (int i = PACK - 1; i > 0; i--)
	{
		int index = rand() % (i + 1);
		swap(deck[i], deck[index]);
	}
}

void swap(char *a, char *b)
{
    char temp[CARD];
    strcpy(temp, a);
    strcpy(a, b);
    strcpy(b, temp);
}

void print(char deck[PACK][CARD])
{
	for (int i = 0; i < PACK; i++)
	{
        if((i % 13) == 0)  // Print as 4 rows to fit on screen.
            printf("\n");
        printf("%s ", deck[i]);

	}
	printf("\n");
}
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

#define PACK 52
#define COLS 13
const char SUIT[][4] = { "pik", "kie", "tre", "kar" };
const char FACE[] = { 'A', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'J', 'Q', 'K' };

void shuffle( int deck[] );
void swap( int &a, int &b );
void print( int deck[] );


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

   int deck[PACK];
   for ( int i = 0; i < PACK; i++ ) deck[i] = i;

   print( deck );

   shuffle( deck );
   cout << "\nAfter shuffle:\n";
   print( deck );
}


void shuffle( int deck[] )
{
   for ( int i = PACK - 1; i > 0; i-- )
   {
      int index = rand() % ( i + 1 );
      swap( deck[i], deck[index] );
   }
}


void swap( int &a, int &b )
{
   int temp = a;
   a = b;
   b = temp;
}


void print( int deck[] )
{
   for ( int i = 0; i < PACK; i++ )
   {
      cout << SUIT[ deck[i]/13 ] << "_" << FACE[ deck[i]%13 ] << "  ";
      if ( !( ( i + 1 ) % COLS ) ) cout << '\n';
   }
   cout << '\n';
}


pik_A  pik_2  pik_3  pik_4  pik_5  pik_6  pik_7  pik_8  pik_9  pik_0  pik_J  pik_Q  pik_K  
kie_A  kie_2  kie_3  kie_4  kie_5  kie_6  kie_7  kie_8  kie_9  kie_0  kie_J  kie_Q  kie_K  
tre_A  tre_2  tre_3  tre_4  tre_5  tre_6  tre_7  tre_8  tre_9  tre_0  tre_J  tre_Q  tre_K  
kar_A  kar_2  kar_3  kar_4  kar_5  kar_6  kar_7  kar_8  kar_9  kar_0  kar_J  kar_Q  kar_K  


After shuffle:
pik_Q  kie_8  kie_9  tre_8  kie_K  pik_0  pik_7  tre_9  pik_9  kar_0  kie_4  kar_A  kie_Q  
pik_8  kar_5  kie_0  kar_J  kie_2  kar_K  pik_2  kie_5  pik_A  pik_3  kie_J  tre_K  pik_6  
tre_J  kie_7  kar_7  tre_2  tre_A  kie_A  tre_4  kar_6  kar_2  pik_J  kar_3  tre_7  pik_K  
tre_3  kar_4  kar_Q  tre_5  pik_4  pik_5  kar_9  kie_6  kar_8  tre_Q  tre_6  kie_3  tre_0  
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void shuffle( char deck[PACK][CARD] )
{
        // do this just once, at the beginning of main
	// srand((unsigned int)time(NULL));

	for (int i = PACK - 1; i > 0; i--)
	{
		int index = rand() % (i + 1);
		swap(deck[i], deck[index]);
	}
}

void swap( char a[CARD], char b[CARD] )
{
    for( int i = 0 ; i < CARD ; ++i )
    {
        const char temp = a[i] ;
        a[i] = b[i] ;
        b[i] = temp;
    }
}
1
2
3
4
5
6
void swap(char **a, char **b)
{
	char **temp = a;
	a = b;
	b = temp;
}
Last edited on
Wow, thank you all soo much for those answers! Now everything is clear, I really appreciate it. :)
Topic archived. No new replies allowed.