program assigns blanks to array instead of *

closed account (1Ck93TCk)
Hi,
I'm working on a small program for practice but I've hit a wall. It's a little card matching game (i've seen over a dozen posts asking for help on this exact program, but none of them answered my problem.) The board is an array of 4x4. starting out, the board is filled with stars (cards are face down). Another board fills with random numbers but is not displayed. The user is prompted for row/col locations to pick a card.

At this point, the board should print again with the two numbers in the chosen location, and the rest still stars. And here I'm stuck. I've got my board of stars and my board of numbers, but when I try to transfer numbers from one board to the other, I just get a space. I would appreciate any help. Happy New Year!

Here's my program:

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
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

void fillStars( string upSide[4][4], char star);
void shuffleDeck(int unshuffled[4][4], int shuffled[4][4], string upSide[4][4]);
void pickCards(int &row1, int &row2, int &col1, int &col2);
void placeCard(int shuffled[4][4], string upSide[4][4], int &card1, int &card2,int &row1, int &row2, int &col1, int &col2);

int main()
{
	int unshuffled[4][4] = {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8};
	int shuffled[4][4];
        string upSide[4][4];
	int card1 = 0;
	int card2 = 0;
	int row1 = 0;
        int row2 = 0;
        int col1 = 0;
        int col2 = 0;
        char star = '*';

	fillStars(upSide, star);
	shuffleDeck(unshuffled, shuffled, upSide);
	pickCards(row1, row2, col1, col2 );
	placeCard(shuffled, upSide, card1, card2, row1, row2, col1, col2);

}
void fillStars(string upSide[4][4], char star)
{
	int i = 0;      //starts the game board by filling with stars
	int j = 0;

	for (i = 0; i < 4; i++)
	{
	    for(j = 0; j < 4; j++)
	    {
		upSide[i][j] = star;
		cout << upSide[i][j] << " ";
            }
            cout << endl;
        }
}
void shuffleDeck(int unshuffled[4][4], int shuffled[4][4], string upSide[4][4])
{
        int i = 0;
        int j = 0;          //there are 8 pairs of cards - the cards are 
                            //assigned to the board randomly

        srand (time(NULL));

	for(i = 0; i < 4; i++)
	{
            for(j = 0; j < 4; j++)
            {
                shuffled[i][j] = unshuffled[rand()% 4][rand()% 4];
                //cout << shuffled[i][j] << " ";
            }
        //cout << endl;
        }
}
void pickCards(int &row1, int &row2, int &col1, int &col2)
{

    cout << "Enter a row and column, separated with a space, for your first card: ";
    cin >> row1 >> col1;
    cout << "Enter a row and column, separated with a space, for your second card: ";
    cin >> row2 >> col2;

}
void placeCard(int shuffled[4][4], string upSide[4][4], int &card1, int &card2, int &row1, int &row2, int &col1, int &col2)
{
    int i = 0;
    int j = 0;

    card1 = shuffled[row1][col1]; //assign values to cards
    card2 = shuffled[row2][col2];

    upSide[row1][col1] = card1;
    upSide[row2][col2] = card2;  /*assigning new card value to
                                 corresponding location on upSide table
                                 this should replace a star with a number
                                 but right now gives a blank space where the 
                                      star was. */
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            cout << upSide[i][j] << " ";
        }
        cout << endl;
    }
}
Hello jmb, so here's your code which I modified a bit:
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

void fillStars( string upSide[4][4], char star);
void shuffleDeck(int unshuffled[4][4], int shuffled[4][4], string upSide[4][4]);
void pickCards(int &row1, int &row2, int &col1, int &col2);
void placeCard(int shuffled[4][4], string upSide[4][4], int &card1, int &card2,int &row1, int &row2, int &col1, int &col2);

int main()
{
    int unshuffled[4][4] = { {1,1,2,2},
	                     {3,3,4,4},
	                     {5,5,6,6},
	                     {7,7,8,8} };
    int shuffled[4][4];
    string upSide[4][4];
    int card1 = 0;
    int card2 = 0;
    int row1 = 0;
    int row2 = 0;
    int col1 = 0;
    int col2 = 0;
    char star = '*';

    fillStars(upSide, star);
    shuffleDeck(unshuffled, shuffled, upSide);
    pickCards(row1, row2, col1, col2);
    placeCard(shuffled, upSide, card1, card2, row1, row2, col1, col2);
}

void fillStars(string upSide[4][4], char star)
{
    int i = 0;      //starts the game board by filling with stars
    int j = 0;

    for (i = 0; i < 4; i++)
    {
        for(j = 0; j < 4; j++)
	{
            upSide[i][j] = star;
	    cout << upSide[i][j] << " ";
        }
        
        cout << endl;
    }
    
    cout << "\n";
}

void shuffleDeck(int unshuffled[4][4], int shuffled[4][4], string upSide[4][4])
{
    int i = 0;
    int j = 0;          //there are 8 pairs of cards - the cards are 
                        //assigned to the board randomly

    srand (time(NULL));

	for(i = 0; i < 4; i++)
	{
        for(j = 0; j < 4; j++)
        {
            shuffled[i][j] = unshuffled[rand()% 4][rand()% 4];
            cout << shuffled[i][j] << " ";
        }
        
        cout << endl;
    }
    
    cout << "\n";
}

void pickCards(int &row1, int &row2, int &col1, int &col2)
{

    cout << "Enter a row and column, separated with a space, for your first card: ";
    cin >> row1 >> col1;
    cout << "Enter a row and column, separated with a space, for your second card: ";
    cin >> row2 >> col2;
    
    //HERE DECREMENTING ROWS AND COLS TO ACCESS YOUR ARRAYS
    row1--;
    col1--;
    row2--;
    col2--;
}

void placeCard(int shuffled[4][4], string upSide[4][4], int &card1, int &card2, int &row1, int &row2, int &col1, int &col2)
{
    int i = 0;
    int j = 0;

    card1 = shuffled[row1][col1]; //assign values to cards
    card2 = shuffled[row2][col2];
    
    cout << "\nCard1: " << card1 << "\nCard2: " << card2 << "\n";

    upSide[row1][col1] = to_string(card1);   //HERE CONVERTING INT TO STRING
    upSide[row2][col2] = to_string(card2);   //HERE CONVERTING INT TO STRING
                                 
    cout << "\nupSide[" << row1 << "][" << col1 << "] is " << upSide[row1][col1]
        << "\nupSide[" << row2 << "][" << col2 << "] is " << upSide[row2][col2] << "\n\n";
    
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            cout << upSide[i][j] << " ";
        }
        
        cout << endl;
    }
}

Output:
* * * * 
* * * * 
* * * * 
* * * * 

8 7 5 3 
7 2 6 1 
5 3 6 6 
8 1 2 1 

Enter a row and column, separated with a space, for your first card: 1 1
Enter a row and column, separated with a space, for your second card: 2 3

Card1: 8
Card2: 6

upSide[0][0] is 8
upSide[1][2] is 6

8 * * * 
* * 6 * 
* * * * 
* * * *


So there were 2 things which weren't working for you:
1_ in your pickCard function, when you ask the user for the column and the row you need to decrement them by 1 since the first element of your array is accessed like so: myArr[0][0] and not like so myArr[1][1].
2_ Your upSide array in your placeCard function was showing blank spaced because upSide is an array of strings and your 2 cards are ints. So you actually need to convert cards to strings before they can show up as so when you output them (hence the use here of to_string())

Also you should restrain the user's input for numbers only between 1 and 4 otherwise you could try to access memory that's not available

EDIT: Note that the additional couts that I've added were for debugging purposes so feel free to get rid of them :-)
Last edited on
That's not really a shuffle. In fact, it's equivalent to just assigning rand() % 8 + 1 to each element. If you really need to use C rand() then it can be done like this:

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
#include <iostream>
#include <cstdlib>
#include <ctime>

const int Size = 4;

void shuffle(int board[][Size]) {
    for (int i = Size * Size; i > 1; ) {
        int r = rand() % i--;
        auto t = board[r/Size][r%Size];
        board[r/Size][r%Size] = board[i/Size][i%Size];
        board[i/Size][i%Size] = t;
    }
}

void print(int board[][Size]) {
    for (int row = 0; row < Size; row++) {
        for (int col = 0; col < Size; col++)
            std::cout << board[row][col] << ' ';
        std::cout << '\n';
    }
    std::cout << '\n';
}

void init(int board[][Size]) {
    for (int r = 0; r < Size; ++r)
        for (int c = 0; c < Size; ++c)
            board[r][c] = (r * Size + c) / 2 + 1;
}

int main() {
    int board[Size][Size];
    init(board);
    print(board);
    shuffle(board);
    print(board);
}

However, in C++ we can use std::shuffle instead.

1
2
3
4
5
6
7
8
9
#include <algorithm>
#include <random>

const int Size = 4;

void shuffle(int board[][Size]) {
    std::default_random_engine rnd(std::random_device{}());
    std::shuffle(&board[0][0], &board[Size-1][Size], rnd);
}

closed account (1Ck93TCk)
Life got busy and I haven't had the chance to come back to this post. I really appreciate all of the help! Now I'm going to sit down and get this program working correctly.
No worries buddy, hope this helped, good luck with your program :-)
Topic archived. No new replies allowed.