1

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();

}
Zearia
  • 123
  • 7
  • "I don't want to edit it" - I'm afraid you're just going to have to do that if you want to print the private members. I can't say why the function was written like that, but it can't be used externally to print the class's member array. Having said that, what does `dataDump` do? – Mike Seymour Feb 03 '15 at 07:34
  • `getGrid` can be used to print any `grid`. So that this function shouldn't be member of the class. if this function were intended to print only internal `grid`, you don't need input parameter `gridOut` at all – Alexey Andronov Feb 03 '15 at 07:41
  • @MikeSeymour dataDump is just a function that I plan to use to print the internal variables for testing purposes that I haven't created yet. I agree that I'm just going to have to edit it even if I don't want to. It makes no sense to me why that has an argument in the first place. – Zearia Feb 03 '15 at 07:50
  • "I'm super stuck and confused on how to print out the grid array from the Tetrimino class " - I would think you'd be considerably more "stuck" on why your member grid remains indeterminate, and all those local-scope automatic `grid` declarations do nothing to change that. Compiling with cranked-up warnings should reveal *seven* different instances of "unused variable 'grid'" or words to that effect. – WhozCraig Feb 03 '15 at 08:15
  • @WhozCraig Yeah, I'm there now. I'm even more confused as to why that's happening. My mind is struggling to find out why that happens, but I see what you've pointed out. – Zearia Feb 03 '15 at 08:24
  • @Zearia stress the **local** automatic variables. they have nothing to do with your member array of arrays `grid`. And not to pour salt on your wounds, but `srand()` in your `randNum` function, is *not* a good idea either. That function should be called *once* per process (traditionally near the start of `main()`) unless you're purposely seeding the prng for a sequence reproduction (and you're *not*). – WhozCraig Feb 03 '15 at 08:59
  • @WhozCraig I posted expecting and hoping for people to find flaws in my coding. I'm pretty new at it and a lot of the times don't really know what I'm doing (which is probably pretty evident). With that said, looking at it again I have an idea on how that would be an issue, thanks for pointing that out and I'll have to remember that in the future. – Zearia Feb 03 '15 at 09:42
  • @Zearia you may find [**this**](http://ideone.com/nwDjRW)... interesting. Best of luck. – WhozCraig Feb 03 '15 at 09:46

3 Answers3

0

That method is simply wrong, it was designed to print one parameter not the internal data, just remove the input parameter and use the internal matrix.

Btw you shouldn't use std::endl to print only \n, use \n directly. std::endl will flush the fd and take more time.

Jose Palma
  • 756
  • 6
  • 13
  • What do you mean by use \n directly? Could you elaborate a little on how I would use \n instead of endl? – Zearia Feb 03 '15 at 07:52
  • @Zearia write `std::cout << ... << '\n'` rather than `std::cout << ... << std::endl` unless you absolutely want to have it immediately on-screen. – Quentin Feb 03 '15 at 07:54
0

Do not call your private member from your main and do not send any argument from your main. It is your member you do not need anything for print it out , you just use it and if you confusing with using it directly , use it with this keyword in your function

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 << this->grid[row][column] << " ";
      }
      //new line between rows
      cout << endl;
  }
oknsnl
  • 351
  • 1
  • 11
0

As you mention, that the header file was provided to you, I guess, this is an assignment. getGrid method is meant to provide interface for outside caller to get a copy of the Tetrmino objects grid. As you cannot return an array from function, the getGrid method provides an output parameter.

Example usage:

void Tetrimino::getGrid(int gridOut[][TETRIMINO_GRID_SIZE]) {
   for(int i = 0; i < TETRMINO_GRID_SIZE; i++) { 
      for(int j = 0; j < TETRMINO_GRID_IZE; j++ ) { 
          gridOut[i][j] = grid[i][j];
      }
   }
}

...
...

Tetrmino obj(3);

... 
... 

int grid[TETRIMINO_GRID_SIZE][TETRMINO_GRID_SIZE];
obj.getGrid(grid);
// now grid holds the copy of interal grid
for(int i = 0; i < TETRMINO_GRID_SIZE; i++) { 
   for(int j = 0; j < TETRMINO_GRID_IZE; j++ ) { 
       std::cout << grid[i][j] << " ";
   }
   std::cout << "\n";
}
std::cout << std::flush;

Edit: To expand on the answer: why grid is not assigned?

The problem is, that within your constructor you are declaring a new int array with the same name as the class member. This means, that you are not initializing the member variable. C++ does not allow to assign to raw array after its initialization, you are left just with copying.

Change the new variable to gridNew or something similar and copy from gridNew to grid element by element just like you are now copying from grid to gridOut in getGrid method.

UldisK
  • 1,619
  • 1
  • 17
  • 25
  • You're correct as to where I got the header from. So if I'm understanding correctly, in this solution the public function of getGrid only exists to extract the information of grid? – Zearia Feb 03 '15 at 08:31
  • `getGrid` is just the same method as `getColor`. The only difference is that, because raw arrays are used, it is not as simple to return a copy. – UldisK Feb 03 '15 at 08:36
  • Ah, I see. Thanks for the explanation! Now, you wouldn't happen to have any idea why grid isn't being set to the proper values would you? – Zearia Feb 03 '15 at 08:40
  • I expanded the answer – UldisK Feb 03 '15 at 09:01
  • Thanks a ton! Really great explanations and you responded quickly! – Zearia Feb 03 '15 at 09:22