initialize a table of random values ​by forbidding to align 3 similar values

Hi all,


I have just initialized an array of random values ​​but I would like that in the case where 3 values ​​are aligned, my array is regenerated in such a way that, as of its initialization my array never comprises 3 consecutive similar values. I am a beginner in C ++, and I don't know what it is the best way to go about it, is it better to create functions that will check if my previous box in my array is similar? or do it directly in my array set up function? Below here is the creation of my table. Many thks for your help

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
void CPlateau::CreatePlateau()
{

  m_arrPlateau = new int*[m_iLignes];
      for (int ligne = 0; ligne < m_iLignes; ligne++)
      {
          m_arrPlateau[ligne] = new int[m_iColonnes];

          for (int col = 0; col < m_iColonnes; col++)
              m_arrPlateau[ligne][col] = 0;
      }
}

void CPlateau::SetupPlateau()
{ 
  if (m_arrPlateau == NULL)
      CreatePlateau();


  for (int ligne = 0; ligne < m_iLignes; ligne++)
      for (int col = 0; col < m_iColonnes; col++)
          m_arrPlateau[ligne][col] = (rand() % 7);
}

Unclear what you mean by "aligned". Do you mean adjacent in the same column?
What about adjacent in the same row?
Also what do you mean by "similar"? Do you mean identical values?

You really have to wait until you've populated the entire array before you can check for adjacent values. i.e. You can't check what will be in the next cell until that cell has been populated.
Thank you AbstractionAnon for your answer.
Yes that's exactly what I mean: adjacent in the same column and adjacent in the same row. Also it's identical values.

To contextualize my problem, I'm trying to create a match-3 game like bejeweled, so it's necessary for me to prohibit that 3 consecutive values, horizontal or vertical are equal during initialization.

Perhaps something 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdlib>

class CPlateau
{	static int 		m_iLignes = 20;
	static int		m_iColonnes = 20;
	int * 			m_arrPlateau;

public:
	void CreatePlateau ();
	void SetupPlateau ();
	bool CheckAdjacent (int row, int col);
	void CheckAligned ();
};

//	Check adjacent cells horizontally and vertically
bool CPlateau::CheckAdjacent (int row, int col)
{	//	Check adjacent columns
	if (col > 0 && m_arrPlateau[row][col-1] == m_arrPlateau[row][col]
	&& col < m_iColonnes && m_arrPlateau[row][col] == m_arrPlateau[row][col+1])
		return true;
	//	Check adjacent rows
	if (row > 0 && m_arrPlateau[row-1][col] == m_arrPlateau[row][col]
		&& row < m_iLignes && m_arrPlateau[row+1][col] == m_arrPlateau[row][col])
			return true;
	return false;
}

//	Check the grid for adjacent cells with same value
//  We iterate through the grid repeatedly until CheckAdjacent returns false for every call
void CPlateau::CheckAligned ()
{	bool done;

	do
	{	done = true;
		for (int ligne = 0; ligne < m_iLignes; ligne++)
			for (int col = 0; col < m_iColonnes; col++)
				if (CheckAdjacent (ligne, col))
				{	//	found 3 adjacent, replace this cell
					m_arrPlateau[ligne][col] = (rand() % 7);
					done = false;	//	Force another iteration
				}
	} while (! done);
}

void CPlateau::CreatePlateau()
{	m_arrPlateau = new int*[m_iLignes];
    for (int ligne = 0; ligne < m_iLignes; ligne++)
    	m_arrPlateau[ligne] = new int[m_iColonnes];
}

void CPlateau::SetupPlateau()
{ 	if (m_arrPlateau == NULL)
		CreatePlateau();

  	for (int ligne = 0; ligne < m_iLignes; ligne++)
  		for (int col = 0; col < m_iColonnes; col++)
  			m_arrPlateau[ligne][col] = (rand() % 7);
  	CheckAligned();
}
Last edited on
Topic archived. No new replies allowed.