Need help on array initializiation (Class attribute)

Hi, I recently started a personnal project : A Match-3 type of game (Where you match 3 of the same type and they go away).

I am aware I might not be able to finish the full thing, but I still wanted to give it a shot to learn stuff and practice.

So here's where I have a problem : I have 2 classes for this, one class for the gameBoard if you will and one for the tokens. The gameBoard class has an array of the tokens type, like this , 'Formes' is the tokens class :


 
 Formes m_gameBoard[5][5];


So when I initialize the tokens into the array, I've made a random constructor that sets a type and a position to each token object in it. Here's my problem :

The types and positions of ALL the objects in the array are the same, and I was expecting each token object to have a random position and type. They do go random, but all the same, if that makes sense.

Anywway, here's the 2 classes (.h and .cpp of each, respectively)

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
#pragma once
#include "regles.h"


class Formes
{
public:
	// Constructeurs
	Formes();
	// Destructeurs
	~Formes();
	// Méthodes
	bool moveIsValid(Formes p_autreForme);
	void deplacerFormes(Formes& p_autreForme);
	// Getters
	std::string getPosition();
	std::string getType();
	std::string getSymbole();
	static int getNbFormes();
	//Setters
	void setPosition(const std::string& p_nouvellePosition);

private:
	std::string m_position;
	std::string m_type;
	std::string m_symbole;
	static int nbFormes;
	
};



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
#include "Formes.h"

int Formes::nbFormes = 0;
// Constructeur par default
Formes::Formes()
{
	nbFormes++;

	srand(time(NULL));
	int rand5 = rand() % 5;
	int rand25 = rand() % 25;

	m_symbole = regles::listeFormes[rand5];
	m_position = regles::listeDeplacementsValides[rand25];
}

// Destructeur
Formes::~Formes()
{
	nbFormes--;
}

bool Formes::moveIsValid(Formes p_autreForme)
{
	bool deplacementHorizontal = false;
	bool deplacementVertical = false;

	// Vérification si même Position
	if (m_position == p_autreForme.m_position)
	{
		return false;
	}
	// Mouvement Horizontal
	else if (m_position[0] == p_autreForme.m_position[0])
	{	
		deplacementHorizontal = true;
		
		if ((int)(m_position[1]) == ((int)(p_autreForme.m_position[1]) + 1) || (int)(m_position[1]) == ((int)(p_autreForme.m_position[1]) - 1))
		{
			std::cout << (int)(m_position[1]) << std::endl;
			std::cout << (int)(p_autreForme.m_position[1]) << std::endl;
			return true;
		}
		
	}
	// Mouvement Vertical
	else if (m_position[1] == p_autreForme.m_position[1])
	{
		deplacementVertical = true;

		if ((int)(m_position[0]) == ((int)(p_autreForme.m_position[0]) + 1) || (int)(m_position[0]) == ((int)(p_autreForme.m_position[0]) - 1))
		{
			std::cout << (int)(m_position[0]) << std::endl;
			std::cout << (int)(p_autreForme.m_position[0]) << std::endl;
			return true;
		}
	}
	
	return false;
}

void Formes::deplacerFormes(Formes& p_autreForme)
{
	if (moveIsValid(p_autreForme) == true)
	{
		this->m_position.swap(p_autreForme.m_position);
	}
}

// Getters
std::string Formes::getPosition()
{
	return m_position;
}

std::string Formes::getType()
{
	return m_type;
}

std::string Formes::getSymbole()
{
	return m_symbole;
}

int Formes::getNbFormes()
{
	return nbFormes;
}

// Setters
void Formes::setPosition(const std::string& p_nouvellePosition)
{
	m_position = p_nouvellePosition;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include "Formes.h"

class Tableau
{
public:
	Tableau(std::string p_nomGameBoard);
	~Tableau();

	void afficherGameBoard();

private:
	Formes m_gameBoard[5][5];
	std::string m_nomGameBoard;
	double m_temps;
};




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
#include "Tableau.h"



Tableau::Tableau(std::string p_nomGameBoard = "Match-3") :  m_nomGameBoard(p_nomGameBoard)
{
	
}


Tableau::~Tableau()
{
}

void Tableau::afficherGameBoard()
{
	system("cls");
	for (int i = 0; i < 5; i++)
	{
		std::cout << " | ";

		for (int j = 0; j < 5; j++)
		{
			std::cout << m_gameBoard[i][j].getSymbole() << " | ";
		}

		std::cout << std::endl;
	}
}


srand should be called once per program invocation, not once per object instantiated. Call it in main, then use rand where needed.
Yeah that's what I did and it fixed it ! WOOT
Last edited on
Topic archived. No new replies allowed.