I am trying to create a board, with different dimensions, that is, 3 x 3, 4 x 4 and 5 x 5
The problem is that I do not know if the part of the attributes of my board class is well declared. Can you tell me if it's okay? On the other hand, I was thinking about using a constructor to determine the size of my board, that is, pass parameters to it and create a matrix, but when I initialize it, there is data where it should not be. Do you know where the problem is or am I doing everything wrong?
This is the code of the board class:
#ifndef TABLERO_H
#define TABLERO_H
const int row = 1;
const int col = 1;
class Tablero
{
private:
int c;
int f;
int mat [col][row];
public:
Tablero ();
Tablero (int pC, int pF);
void mostrarT ();
void setN (int pA, int pB, int pC, int pD);
~Tablero ();
};
Tablero::Tablero ()
{
c = 5;
f = 4;
for (int i = 0; i < c; i ++ )
for (int j = 0; j < f; j ++)
mat [i][j] = 0;
mat [3][1] = 1;
}
The implementation is as follows:
Tablero::Tablero(int pC, int pF)
{
c = pC;
f = pF;
for (int i = 0; i < c; i ++ )
for (int j = 0; j < f; j ++)
mat [i][j] = 0;
mat [3][1] = 2;
}
Tablero::~Tablero()
{
}
void Tablero::mostrarT ()
{
cout << "---------------------------" << endl;
for (int i = 0; i < c; i++){
for (int j = 0; j < f; j++){
cout << mat [i] [j] << " ";
}
cout << endl;
}
}
void Tablero::setN (int pA, int pB, int pC, int pD)
{
int r;
r = rand () % (c-1);
cout << r << "<<<" << endl;
while (r == 0 || r == 2 || r == 3 || r == 5 || r == 6 || r == 8)
{
r = 0 + rand () % (c-1);
cout << r << "<<<";
}
cout << r << "<<<" << endl;
cout << pA << "<<---";
}
and the main
int main() {
srand (time (NULL));
/*Ficha x;
x.mostrar();
Ficha y;
y.mostrar();*/
Tablero w;
w.mostrarT();
Tablero a (5, 4);
a.mostrarT();
}