Memory game pair generating?

Hi!

I would like to make a simple cmd console memory game. I have the idea how to do the logical part behind the entire game, however, I'm kinda stuck at the beginning - at generating 3 pairs of the 3 letters I would like to use in my code (aka 1 pair of 'a', 1 pair of 'b' and 1 pair of 'c').

I have thought about using srand(time(NULL)) and rand()% and then compare if I had already generated any of the given letter, however I can't imagine how to describe that. Could you help me out, please?

Thanks in advance!!
Perhaps you would like to restate your problem in a way that made sense.
1 pair of 'a', 1 pair of 'b' and 1 pair of 'c'


Well, here's a string that fits the bill: "aabbcc"
3 pairs of the 3 letters


A pair is 2 not 3. Do you mean 3 trios of letters?

I had already generated any of the given letter


That's 9 letters. Do you want all 9 unique?
Perhaps as a starter that gives all unique letters:

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
#include <random>
#include <iostream>
#include <array>

std::mt19937 rng(std::random_device {}());

int main()
{
	constexpr unsigned nosets {3};
	constexpr unsigned noperset {3};

	bool used[26] {};
	std::uniform_int_distribution<unsigned> distrib('a', 'z');
	std::array<std::array<char, noperset>, nosets> sets;

	for (auto& s : sets)
		for (auto& e : s) {
			while (used[(e = static_cast<char>(distrib(rng))) - 'a']);
			used[e - 'a'] = true;
		}

	for (const auto& s : sets) {
		for (const auto& e : s)
			std::cout << e << ' ';

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



s v d
c t m
q o e

I meant the pair-thing like this:

a c c
b b a


or

a c b
c b a


Like in a classic memory game :D So I have 3 different type of letters and I should have 2 of each
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <random>
#include <algorithm>
#include <string>
#include <ctime>
using namespace std;

int main()
{
   mt19937 rng( time( 0 ) );
   const string letters = "aabbcc";

   for ( int i = 1; i <= 10; i++ )
   {
      string text = letters;
      shuffle( text.begin(), text.end(), rng );
      cout << text.substr(0,3) << '\n' << text.substr(3,3) << '\n' << "--------\n";
   }
}


baa
cbc
--------
bac
abc
--------
cab
cba
--------
cac
abb
--------
abc
bca
--------
aab
ccb
--------
acb
cba
--------
cba
cab
--------
cab
abc
--------
aab
cbc
--------
@vboro

Take a look at a concentration game I made awhile back for the console. It's probably a little more in depth than you're used to, but you should be able to see how I made up the pairs, etc. I guess a lot of programmers on this site don't know what the game of 'Concentration' is or how it's played. Anyway, if you have any questions, please ask.

Dropbox file:
https://www.dropbox.com/s/3apdx7ymy1wnefr/Concentration.cpp?dl=0]
(File removed}
Last edited on
const string letters = "aabbcc";

We know that every letter occurs twice. Therefore:
1
2
3
4
const string letters = "abcdefg";
// letters.size() is number of pairs
string text = letters + letters;
shuffle( text.begin(), text.end(), rng );
Thanks everyone for the answers, I got the solution :D And @whitenite1 I'll soon check out your game, thanks for sharing it with me ^^
Topic archived. No new replies allowed.