以下是我在头文件中使用的代码:
#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
这是.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);
}
好的,显然代码不完整。我对如何使用公共函数getGrid从Tetrimino类打印出网格数组感到困惑和困惑。 Header文件是给我预先制作的(虽然我理解它)所以我不想编辑它。为什么getGrid函数首先需要一个参数?
我不能简单地调用我想打印的网格,就像我在main()中尝试一样,因为它是私有的。我只是......是的,我知道这是错的,但我不知道怎么做才是正确的。
EDIT / UPDATE: 我从getGrid()中删除了参数,并将gridOut更改为函数中的网格。但是,当我使用test.getGrid()调用函数时,打印的数组是:
-858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460
我改变的代码是:
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;
}
}
getGrid已更改为网格。
我现在正在调用这样的函数:
void main()
{
Tetrimino test(0);
test.getGrid();
}
答案 0 :(得分:0)
该方法完全错误,它被设计为打印一个参数而不是内部数据,只需删除输入参数并使用内部矩阵。
顺便说一句,你不应该使用std :: endl来打印\ n,直接使用\ n。 std :: endl将刷新fd并花费更多时间。
答案 1 :(得分:0)
请勿从您的主电话中呼叫您的私人会员,也不要从您的主电话发送任何参数。它是您的会员,您不需要任何打印出来,只需使用它,如果您对直接使用它感到困惑,请在功能中使用this
关键字
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;
}
答案 2 :(得分:0)
正如您所提到的那样,头文件是提供给您的,我想,这是一个分配。 getGrid
方法旨在为外部调用者提供接口以获取Tetrmino对象网格的副本。由于无法从函数返回数组,因此getGrid方法提供了输出参数。
使用示例:
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;
修改强> 为了扩展答案:为什么没有分配网格?
问题是,在构造函数中,您声明了一个与类成员同名的新int数组。这意味着,您没有初始化成员变量。 C ++在初始化后不允许分配给原始数组,只需复制即可。
将新变量更改为gridNew
或类似内容,然后逐个元素地从gridNew
复制到grid
,就像您现在从grid
复制到{{1}一样在gridOut
方法中。