🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

memory game

Started by
5 comments, last by Gotanod 2 years, 5 months ago

I am making a memory matching game using playing cards. I want to post the same card twice randomly to the screen.

int a[20] = { 0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9 };

void init()
{
	srand(time(NULL));
	random_shuffle(a, a + 20);
	cout << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << " " << a[4] << " " << a[5] << " " << a[6] << " " << a[7] << endl;
	texture[a[0]] = loadTex("C:\\Users\\Owner\\Desktop\\2_of_clubs.png");
	texture[a[1]] = loadTex("C:\\Users\\Owner\\Desktop\\3_of_clubs.png");
	texture[a[2]] = loadTex("C:\\Users\\Owner\\Desktop\\4_of_clubs.png");
	texture[a[3]] = loadTex("C:\\Users\\Owner\\Desktop\\5_of_clubs.png");
	texture[a[4]] = loadTex("C:\\Users\\Owner\\Desktop\\6_of_clubs.png");
	texture[a[5]] = loadTex("C:\\Users\\Owner\\Desktop\\7_of_clubs.png");
	texture[a[6]] = loadTex("C:\\Users\\Owner\\Desktop\\8_of_clubs.png");
	texture[a[7]] = loadTex("C:\\Users\\Owner\\Desktop\\9_of_clubs.png");
	texture[a[8]] = loadTex("C:\\Users\\Owner\\Desktop\\10_of_clubs.png");
	texture[a[9]] = loadTex("C:\\Users\\Owner\\Desktop\\jack_of_clubs2.png");
	texture[a[10]] = loadTex("C:\\Users\\Owner\\Desktop\\queen_of_clubs2.png");
	texture[a[11]] = loadTex("C:\\Users\\Owner\\Desktop\\king_of_clubs2.png");
	texture[a[12]] = loadTex("C:\\Users\\Owner\\Desktop\\ace_of_clubs.png");
	texture[a[13]] = loadTex("C:\\Users\\Owner\\Desktop\\2_of_diamonds.png");
	texture[a[14]] = loadTex("C:\\Users\\Owner\\Desktop\\3_of_diamonds.png");
	texture[a[15]] = loadTex("C:\\Users\\Owner\\Desktop\\4_of_diamonds.png");
	texture[a[16]] = loadTex("C:\\Users\\Owner\\Desktop\\5_of_diamonds.png");
	texture[a[17]] = loadTex("C:\\Users\\Owner\\Desktop\\6_of_diamonds.png");
	texture[a[18]] = loadTex("C:\\Users\\Owner\\Desktop\\7_of_diamonds.png");
	texture[a[19]] = loadTex("C:\\Users\\Owner\\Desktop\\8_of_diamonds.png");
//	texture[8] = loadTex("C:\\Users\\Owner\\Desktop\\cardBack_blue1.png");
}
Advertisement

I think it would be simpler if you separated the handling and storage of textures from whatever your deck/screen looks like.

May I suggest that you sick with one atlas for all the cards, or programmatically compose them, so you avoid repeating yourself too much.
Alternatively, you could do a bit of both, and have an atlas for the suites, and then add the values programatically.
Alternatively, you could stick with an image for each card, and solve the loading programmatically.

#include <algorithm> // swap
#include <stdint.h>

namespace PRN
{
	inline uint32_t randI (uint32_t time)
	{
		time *= 1664525;
		time ^= (time << 16);
		time ^= (time >> 16);

		time *= 16807;
		time ^= (time << 8);
		time ^= (time >> 24);

		time *= 100069;
		time ^= (time << 24);
		time ^= (time >> 8);

		time *= 1000099;
		
		return time;
	}
	
	
	template <typename T>
	inline void Shuffle (T *arr, const int n, const int seed = 0)
	{
		int32_t s = seed;
		for (int i=n; i-->0;)
		{
			int j = randI (s++) % (i + 1);
			std::swap(arr[i], arr[j]);
		}
	}
};
int a[20] = { 0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9 };

const int iterations = 5; // one would do, but randomness is increased if doing more often
for (int i=0; i<iterations; i++)
	PRN::Shuffle (a, 20, i*20);
// now the values in a are in (pseudo) random order

JoeJ said:

Obnoxious shuffle code

"randomness is increased if doing more often" is horrible advice IMO.
Use the existing std::shuffle - it's much simpler.

There is a std::shuffle?
ugh… if only i knew this before :D

SuperVGA said:
"randomness is increased if doing more often" is horrible advice IMO.

You may be right. But i use just a hash function over ordered sequence, so it isn't that great after just one run.

pbivens67 said:
int a[20] = { 0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9 };
void init() {
srand(time(NULL));
random_shuffle(a, a + 20);

@pbivens67

A couple months ago I started with C++ (coming from Java)… And that was my first project.
The steps I followed were the following:

  1. Select the number of pairs. (example 4)
  2. Build a dynamic array twice that size and add repeat each number twice. (example 0 0 1 1 2 2 3 3)
  3. Use random_suffle to suflle the array
  4. Draw the array (or the pictures matching each cardID)

Note: You will need to save in other place if the card is face up or face down, and the corresponding picture for each id. So I suggest to create a Card class to store all the details instead of using just a simple integer.

#include <random>       
#include <algorithm>   
#include <iostream>

int main() {

	// Step 1 
    int nPairs = 6;
    int nCards = nPairs * 2;
    std::vector<int> v;

	// Step 2. creates the sequence 0 0 1 1 2 2 3 3 4 4 ...
    for (int i = 0; i < nCards; i++) 
    { 
        v.push_back(i / 2); 	// or v.push_back(i >> 1);	
    }
    
    // Step 3
    // https://en.cppreference.com/w/cpp/algorithm/random_shuffle
    std::random_device rd;
    std::mt19937 g(rd());
    std::shuffle(v.begin(), v.end(), g);

	// Step 4
    for(int i = 0; i < nCards;i++)
    {
        std::cout << v[i] << " ";
    }
    std::cout << std::endl;
    
	return 0;
}

None

This topic is closed to new replies.

Advertisement