类中的Void Print()函数正在输出2D数组的内存地址,而不是打印值



我正在做一项作业,我们必须制作一个滑动瓷砖拼图。问题是,我打印游戏板的打印方法是输出2D阵列的内存地址,而不是实际值,但我不知道如何解决这个问题。我试着查找解决方案,我能找到的只是关于矢量的东西,我不允许使用。

来自driver.cpp:的代码

#include <iostream>
#include <conio.h>
#include "Specification.h"
#include <windows.h>    //For color in PrintBoard()
using namespace std;
// direction codes (part of the slideTile() interface)
#define SLIDE_UP        1       // pass to slideTile() to trigger UP movement
#define SLIDE_DOWN      2       // pass to slideTile() to trigger DOWN movement
#define SLIDE_LEFT      3       // pass to slideTile() to trigger LEFT movement
#define SLIDE_RIGHT     4       // pass to slideTile() to trigger RIGHT movement
int main() {
int tempHeight;
int tempWidth;
// instantiate the class
cout << "Please enter the height for the board: ";
cin >> tempHeight;
cout << "Please enter the width for the board: ";
cin >> tempWidth;
bool exit = false;
SlidingPuzzle somePuzzle(tempHeight, tempWidth);

char keyStroke = ' ';
somePuzzle.isBoardSolved();
cout << "Press any key to begin..." << endl;
_getch();
cout << endl << "Scrambling the tiles..." << endl;
somePuzzle.ScrambleBoard();
cout << "Scrambling complete, press any key to begin solving." << endl;
_getch();
somePuzzle.isBoardSolved();
do {
cout << endl << "(w = Up, s = Down, a = Left, d = Right, e = Exit Game)" << endl;
cout << "Which way to slide?" << endl;
keyStroke = _getch(); //sets the key used to continue as the move, keys that aren't options do nothing
switch (keyStroke) {
case 'w':
{
somePuzzle.SlideTile(SLIDE_UP);
break;
}
case 's':
{
somePuzzle.SlideTile(SLIDE_DOWN);
break;
}
case 'a':
{
somePuzzle.SlideTile(SLIDE_LEFT);
break;
}
case 'd':
{
somePuzzle.SlideTile(SLIDE_RIGHT);
break;
}
case 'e':
{
exit = true;
somePuzzle.~SlidingPuzzle();
break;
}
}
} while (!somePuzzle.isBoardSolved() || !exit);
// Exit
_getch();
return 0;
}

规范中的代码。h:

#ifndef __SlidingPuzzle__
#define __SlidingPuzzle__
#include <iostream>
#include <windows.h>    //For color in PrintBoard()
using namespace std;
// CLASS SlidingPuzzle
class SlidingPuzzle {
private:
int height;
int width;
int** theBoard;
int** solvedBoard;
HANDLE currentConsoleHandle;
void cls(HANDLE);
public:
// CONSTRUCTOR
SlidingPuzzle();        //Gives default values to the SlidingPuzzle's attributes
SlidingPuzzle(int, int);
//Deconstructor
~SlidingPuzzle();
//Getter
int getHeight();
int getWidth();
int** getTheBoard();
int** getSolvedBoard();
//Methods
void InitializeBoard();
bool isBoardSolved();
bool SlideTile(int);
void ScrambleBoard();
void PrintBoard();
}; // end - SlidingPuzzle
#endif

来自implementation.cpp:的代码

#include "Specification.h"
// CONSTRUCTOR
SlidingPuzzle::SlidingPuzzle() {
this->height = 0;
this->width = 0;
this->theBoard = NULL;
this->solvedBoard = NULL;
this->currentConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
}
// Overload
SlidingPuzzle::SlidingPuzzle(int newWidth, int newHeight) {
this->currentConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
if (newHeight > 0) {
this->height = newHeight;
}
else {
this->height = 0;
}
if (newWidth > 0) {
this->width = newWidth;
}
else {
this->width = 0;
}
this->theBoard = new int* [this->height];
for (int i = 0; i < this->height; i++) {
this->theBoard[i] = new int[this->width];
}
this->solvedBoard = new int* [this->height];
for (int i = 0; i < this->height; i++) {
this->solvedBoard[i] = new int [this->width];
}
}
//Getters
int SlidingPuzzle::getHeight() {
return this->height;
}
int SlidingPuzzle::getWidth() {
return this->width;
}
int** SlidingPuzzle::getTheBoard() {
return this->theBoard;
}
int** SlidingPuzzle::getSolvedBoard() {
return this->solvedBoard;
}
//Deconstructor
SlidingPuzzle::~SlidingPuzzle() {
for (int i = 0; i < this->height; ++i)
delete[] this->theBoard[i];
delete[] this->theBoard;
}
//Methods
void SlidingPuzzle::InitializeBoard() {
for (int i = 0; i < this->height; i++) {
for (int j = 0; j < this->width; j++) {
if (i == 0) {
this->theBoard[i][j] = i + j + 1;
}
else if (i == 1) {
this->theBoard[i][j] = i + j + 3;
}
else {
this->theBoard[i][j] = i + j + 5;
if (this->theBoard[i][j] == (this->width * this->height)) {
this->theBoard[i][j] = -1;
}
}
}
}
}
bool SlidingPuzzle::isBoardSolved() {
this->cls(currentConsoleHandle);
this->PrintBoard();
int correct = 0;
//Checks each position to see if the are identical
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (this->theBoard[i][j] != this->solvedBoard[i][j]) {
correct = 1;
}
}
}
if (correct == 0) {
cout << "isBoardSolved(): true" << endl;
return true;
}
else {
cout << "isBoardSolved(): false" << endl;
return false;
}
}
bool SlidingPuzzle::SlideTile(int directionCode) {
int row = 0;
int col = 0;
int rowSpace = 0;
int colSpace = 0;
//Finds the pivot space
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
if (this->theBoard[i][j] == -1) {
row = i;
col = j;
}
}
}
switch (directionCode) {
case 1:
{
rowSpace = row - 1;
colSpace = col;
break;
}
case 2:
{
rowSpace = row + 1;
colSpace = col;
break;
}
case 3:
{
rowSpace = row;
colSpace = col - 1;
break;
}
case 4:
{
rowSpace = row;
colSpace = col + 1;
break;
}
}
//Ensures that the program doesn't break from trying to move off the board
if (rowSpace >= 0 && rowSpace < height && colSpace >= 0 && colSpace < width) {
this->theBoard[row][col] = this->theBoard[rowSpace][colSpace];
this->theBoard[rowSpace][colSpace] = -1;
}
return false;
}
void SlidingPuzzle::ScrambleBoard() {
for (int i = 0; i < 10000; i++) {
int move = (rand() % 4);
move++; //Add 1 so the variable matches the values for the directions
this->SlideTile(move);
}
}
void SlidingPuzzle::PrintBoard() {  //Refuses to print, no clue why
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (this->theBoard[i][j] == -1) {
cout << " *";
}
else {
if (this->theBoard[i][j] == this->solvedBoard[i][j]) {
SetConsoleTextAttribute(hConsole, 2); //changes the color to green for correct postion
cout << " " << this->theBoard[i][j];
SetConsoleTextAttribute(hConsole, 15); //reverts color to normal
}
else {
SetConsoleTextAttribute(hConsole, 4); //changes the color to red for incorrect postion
cout << " " << this->theBoard[i][j];
SetConsoleTextAttribute(hConsole, 15); //reverts color to normal
}
}
}
cout << endl;
}
}
void SlidingPuzzle::cls(HANDLE hConsole)
{
COORD coordScreen = { 0, 0 };    /* here's where we'll home the
cursor */
BOOL bSuccess;
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
DWORD dwConSize;                 /* number of character cells in
the current buffer */
/* get the number of character cells in the current buffer */
bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
/* fill the entire screen with blanks */
bSuccess = FillConsoleOutputCharacter(hConsole, (TCHAR)' ',
dwConSize, coordScreen, &cCharsWritten);
/* get the current text attribute */
bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
/* now set the buffer's attributes accordingly */
bSuccess = FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);
/* put the cursor at (0, 0) */
bSuccess = SetConsoleCursorPosition(hConsole, coordScreen);
}

我已经使用了评论中的建议,现在我终于可以显示数组了。然而,数组中的值仍然显示为内存地址。我在这里更新了代码,以反映到目前为止所做的更改。

我忘了将somePuzzle.InitializeBoard();添加到driver.cpp中,现在我觉得自己像个傻瓜。感谢大家帮助我,很抱歉没有发布一个可复制的最小示例。感谢Igor Tandetnik和退役忍者指出我遇到的问题并帮助我解决问题。

最新更新