未收到用户的3x3滑动瓷砖幻灯片输入

时间:2019-03-26 21:54:33

标签: c++ arrays pointers 8-puzzle

我正在尝试创建一个3x3的滑动瓷砖拼图游戏,我以2d阵列制作了该拼图,现在我正试图让该拼图滑动瓷砖。

我是调试的新手,无法找出程序为什么没有收到用户输入的幻灯片。

#define SLIDE_UP        1       
#define SLIDE_DOWN      2       
#define SLIDE_LEFT      3       
#define SLIDE_RIGHT     4    

void InitializeBoard(int[NUM_ROWS][NUM_COLS]);
void PrintBoard(int[NUM_ROWS][NUM_COLS]);
bool slideTile(int[NUM_ROWS][NUM_COLS], int);
void scrambleBoard(int[NUM_ROWS][NUM_COLS]);        
bool isBoardSolved(int[NUM_ROWS][NUM_COLS]);
void DeallocateMemory(int[NUM_ROWS][NUM_COLS]);                                                 
int** ppRootPointer = NULL;

int main() {

int slidingBoard[NUM_ROWS][NUM_COLS];       
char keyStroke = ' ';                        
int directionCode = UNSET;  
int slideDirection = 0;

InitializeBoard(slidingBoard);
PrintBoard(slidingBoard);
slideTile(slidingBoard,slideDirection);
PrintBoard(slidingBoard);

_getch();                                       
DeallocateMemory(slidingBoard);

return 0;
}

void InitializeBoard(int theBoard[NUM_ROWS][NUM_COLS]) {

ppRootPointer = new(int*[NUM_COLS]);

for (int i = 0; i < NUM_COLS; i++) {
    ppRootPointer[i] = new(int[NUM_ROWS]);
}

int counter = 1;
int i = 0, j = 0;
for (i = 0; i < NUM_COLS; i++) {
    for (j = 0; j < NUM_ROWS; j++) {
        ppRootPointer[i][j] = counter++;
    }
}
ppRootPointer[i-1][j-1] = PIVOT;


}

void PrintBoard(int theBoard[NUM_ROWS][NUM_COLS]) {
    cout << left;
    for (int i = 0; i < NUM_COLS; i++) {
        for (int j = 0; j < NUM_ROWS; j++) {
            if (ppRootPointer[i][j] != PIVOT) {
                cout << setw(3) << ppRootPointer[i][j];
            }
            else {
                cout << setw(3) << (char)PIVOT;
            }
        }
        cout << endl;
    }
    cout << endl << endl;
}

 bool slideTile(int theBoard[NUM_ROWS][NUM_COLS], int slideDirection) {
    int pivotRow=0;
    int pivotCol=0;
    bool slidebool;
    //once I declare i and j before the loop they go up to 3 instead of 2 
    //which causes the loop to not work
    int i = 0;
    int j = 0;
    for (i = 0; i < NUM_COLS; i++) {
        for (j = 0; j < NUM_ROWS; j++) {

            if (theBoard[i][j] == (char)PIVOT) {
                pivotCol = i;
                pivotRow = j;
                break;
            }
        }
    }
    //These are to assign pivotRow and Col because they dont get assigned 
    //inside of the loop
pivotCol = i;
pivotRow = j;

    cout << "please enter a number to slide the tile (1 = up, 2 = down, 3 = 
left, 4 = right" << endl;
    cin >> slideDirection;

    if (slideDirection == SLIDE_UP) {
        if (pivotRow + 1 > NUM_ROWS) {
            slidebool = false;
        }
        else {
            ppRootPointer[pivotRow + 1][pivotCol] = PIVOT;
            ppRootPointer[pivotRow][pivotCol] = ppRootPointer[pivotRow + 1] 
[pivotCol];
            slidebool = true;

        }
    }

。我希望用户能够输入一个表示该幻灯片幻灯片的1-4的整数。这将允许我构建加扰板功能并更接近完成代码。感谢阅读本文并尝试提供帮助的任何人!

0 个答案:

没有答案