Hello everyone. Thank you again for all the help... At this point, my eyes are bleeding on this project.....My code runs and does everything this card memory matching game is supposed to do, except I've tried to no avail to "shuffle" the cards between iterations (i suspect). I have read many posts, tutorials etc. on rand functions, and even had an example from someone(which is not working for me- ALL COMMENTED OUT) and still don't quite get it.... My text offers several random generators, a random(n), which will ostensibly return a "pseudorandom integer >=0 & <= n-1. Then the int rand(), and a void srand(unsigned int?)
Anyway, I'm lost again, thanks for any help on this last leg of my project::::::::::::

#include <iostream>
#include <cstdlib>
using namespace std;

void player_input(int card_value[][4], int &choice1x, int &choice1y, int &choice2x, int &choice2y);
int card_face[4][4];
int card_value[4][4];
int fill_values();
int card_defacer();
int show_grid(int card_value[][4], bool card[][4]);
//int disarray(int card_value[][4]);
//int locum(int card_value[][4]);
char Continue = 1;

int main(){
	int choice1x, choice1y, choice2x, choice2y;

	player_input(card_value, choice1x, choice1y, choice2x, choice2y);

   int fill_values() //Defining Function establishes 16 cards with paired numbers from 1-8

	 return 0;


int show_grid(int card_value[][4], bool card[][4])  // Function that will show the "playing table" and output values of only "pairs" other values will be "*******"


	cout<<"        " << "1         2         3         4  " << endl; //Header for (y) coordinates
        cout <<"    " <<"----------------------------------"<< endl; 
		cout <<endl;
	    for(int x = 0; x < 4; x++)
			cout <<"  "<<x+1<<"|";			


		for (int y = 0; y < 4; y++){

			if (card[x][y] == true)
				cout << "    "<< card_value[x][y]<<  "     ";
				cout << "    "<< "*"<<  "     ";

	 cout << endl;
	 cout << endl;
return 0;


 void player_input(int card_value[][4], int &choice1x, int &choice1y, int &choice2x, int &choice2y)


	bool card[4][4];
    show_grid(card_value, card);


	cout << "Player, enter a coordinate choice for your first card:  " << endl;
	cin >> choice1x >> choice1y;
	cout << "Player, enter your second choice:  " << endl;
	cin >> choice2x >> choice2y;

	choice1x = choice1x - 1;
	choice1y = choice1y - 1;
	choice2x = choice2x - 1;
	choice2y = choice2y - 1;

	if (card_value[choice1x][choice1y] == card_value[choice2x][choice2y]){
		card[choice1x][choice1y] = true;
		card[choice2x][choice2y] = true;

		card[choice1x][choice1y] = true;
		card[choice2x][choice2y] = true;

		show_grid(card_value, card);


        card[choice1x][choice1y] = false;
		card[choice2x][choice2y] = false;

		}show_grid(card_value, card);

        cout << "Do you want to continue, enter 1 for yes: "<< endl;
	    cin >> Continue;

        }while(Continue == '1');


        //int locum(int card_value[][4])
		//	int locumizer2, locumizer3, locumizer4, momentarily;

		//int locumizer1 =(rand()%3 + 1,  locumizer2 = (rand()%3 + 1,  locumizer3 = (rand()%3 + 1,  locumizer4 = (rand()%3 + 1, momentarily[4][4];
		//momentarily[locumizer1][locumizer2] = momentarily[locumizer2][locumizer1];
		//card_value[locumizer1][locumizer2] = card_value[locumizer3][locumizer4];
        //card_value[locumizer3][locumizer4] = momentarily[locumizer1][locumizer2];

		//return 0;

		//int disarray(int card_value[][4])
		//	locum(card_value);
		//	locum(card_value);
		//	locum(card_value);
		//	locum(card_value);
		//	locum(card_value);
		//	locum(card_value);
		//	locum(card_value);
		//return 0;

int random = rand()%3;
printf("%s", card_value[][4]);
9 Years
Discussion Span
Last Post by guitarrick

You need srand() to initialize the random generator (use once at the program's start) and:

Walt, thanks, but I'm not sure what or where to write the shuffle routine....it was called disarray(int card_value[][4]), but I commented it all out.....I thought there may be an easier way of going about this.....And when you say format, are you referring to my code?? Or my messaging - I do realize probably both are a bit of a mess. Sorry, I will try to do better


Well, disarray() is meaningless, isn't it? Call it shuffle() :icon_wink:

Make the function to shuffle the cards in a new program. When you get the shuffle function working, simply add it to you program.

Pass what you want shuffled (the source array) into the function.
Use rand() to move that array into a temporary array as the shuffling mechanism.
Move the temp array back into the source array.


And when you say format, are you referring to my code?? Or my messaging...

Did you look at the link? That's why the words were in blue :icon_rolleyes:


Depending on restrictions there is a function in the algorithm header file of the standard template library that will "automagically" shuffle a given group of items. However, that would take the fun and learning experience out of implementing the routine on your own.


Thanks Lerner,
No restrictions I don't think --I was also wondering how difficult it would be to write a function that would shuffle?


Random shuffling can achieved by...

Populating an array with bunch of numbers going up sequentially.

Choose two random numbers where each number is less that the maximum number of things in your array.

Use those two number to swap the indexes of your array.

Keep doing this until you are satisfied.


Not that difficult.

One option would be to declare a shuffled array inside the function. If you're passing in a 2 dimensional array then declare two int variables that would range from 0 to whatever is appropriate and would act as indexes into the passed in array. Assign a random value to each int variable that is within desired range. Do this within a nested loop. Place an asterix or other default value at the random indexes in the original array after assigning the original value into the current spot in the shuffled array. If the cell is already transfered to the shuffled array, then go to the next unstransfered value. Then copy the shuffled array back into the passed in array so the new values are visible back in the calling function.

Alternatively, you could declare four ints in the function. One pair would randomly determine which cell was to move and the other would determine where it would go. Switch the two cells. Repeat 10000 times or so and it should be pretty random.

Edit: This appears to be a variant of iamthwees suggestion.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.