Here's the code I'm working with in the header file:
#ifndef TETRIS_TETRIMINO
#define TETRIS_TETRIMINO
const int TETRIMINO_GRID_SIZE = 4;
struct Location {
int row;
int col;
};
class Tetrimino {
private:
int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE];
char color;
Location location;
public:
// constructor
Tetrimino(int type = 7); // valid type values are 0-6
//---------------------------------------------
//accessors
char getColor();
Location getLocation();
void getGrid(int gridOut[][TETRIMINO_GRID_SIZE]);
//---------------------------------------------
//mutators
void setLocation(Location newLocation);
void setLocation(int row, int col);
void rotateLeft();
void rotateRight();
void moveLeft();
void moveRight();
void moveDown();
void moveUp();
//---------------------------------------------
//others
void dataDump();
};
#endif
And here's the .cpp:
#include "tetrimino.h"
#include <iostream>
#include <ctime>
using namespace std;
//random number generator
int randNum()
{
int randNum;
int high = 6;
int low = 0;
srand(static_cast<unsigned int>(time(NULL)));
randNum = rand() % (high - low + 1) + low;
return randNum;
}
Tetrimino::Tetrimino(int type)
{
//check to see if type is 0-6, if not set to 7
if (type < 0 || type >= 7)
{
type = randNum();
}
//set associated type to a tetrimino
if (type == 0)
{
//set grid to i tetro
int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
{
{ 0, 0, 0, 0 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 }
};
//set color to teal
color = 't';
//initialize starting position
location.row = 0;
location.col = 0;
}
else if (type == 1)
{
//set grid to j tetro
int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
{
{ 0, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 1, 1 },
{ 0, 0, 0, 0 }
};
//set color to blue
color = 'b';
//initialize starting position
location.row = 0;
location.col = 0;
}
else if (type == 2)
{
//set grid to L tetro
int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
{
{ 0, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 1, 1, 1, 0 },
{ 0, 0, 0, 0 }
};
//set color to orange
color = 'o';
//initialize starting position
location.row = 0;
location.col = 0;
}
else if (type == 3)
{
//set grid to o tetro
int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 }
};
//set color to yellow
color = 'y';
//initialize starting position
location.row = 0;
location.col = 0;
}
else if (type == 4)
{
//set grid to s tetro
int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 1, 1, 0, 0 },
{ 0, 0, 0, 0 }
};
//set color to green
color = 'g';
//initialize starting position
location.row = 0;
location.col = 0;
}
else if (type == 5)
{
//set grid to T tetro
int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
{
{ 0, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 1, 1, 1, 0 },
{ 0, 0, 0, 0 }
};
//set color to purple
color = 'p';
//initialize starting position
location.row = 0;
location.col = 0;
}
else if (type == 6)
{
//set grid to z tetro
int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 1, 1 },
{ 0, 0, 0, 0 }
};
//set color to red
color = 'r';
//initialize starting position
location.row = 0;
location.col = 0;
}
};
//accessors
char Tetrimino::getColor()
{
return color;
}
Location Tetrimino::getLocation()
{
return location;
}
void Tetrimino::getGrid(int gridOut[][TETRIMINO_GRID_SIZE])
{
//loop goes through each row
for (int row = 0; row < TETRIMINO_GRID_SIZE; row++)
{
//goes through each col of current row
for (int column = 0; column < TETRIMINO_GRID_SIZE; column++)
{
cout << gridOut[row][column] << " ";
}
//new line between rows
cout << endl;
}
}
//mutators
//leaving these out of this for sanity
void main()
{
Tetrimino test(0);
cout << test.getColor() << endl;
test.getGrid(test.grid);
}
Ok, so obviously the code is incomplete. I'm super stuck and confused on how to print out the grid array from the Tetrimino class using the public function getGrid. The Header file was given to me pre-made (although I understand it) so I don't want to edit it. Why is the getGrid function requiring a parameter in the first place?
I can't simply call the grid I want to print like I attempted in main() because it's private. I'm just really.. yeah I know it's wrong but I have no idea how to go about doing it correct.
EDIT/UPDATE: I removed the parameter from getGrid() and changed the gridOut to simply grid in the function. However, when I call the function using test.getGrid() the array that prints is:
-858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460
The code I changed was:
void Tetrimino::getGrid()
{
//loop goes through each row
for (int row = 0; row < TETRIMINO_GRID_SIZE; row++)
{
//goes through each col of current row
for (int column = 0; column < TETRIMINO_GRID_SIZE; column++)
{
cout << grid[row][column] << " ";
}
//new line between rows
cout << endl;
}
}
The getGrid was changed to grid.
I'm now calling the function like this:
void main()
{
Tetrimino test(0);
test.getGrid();
}