为什么我的十五人游戏程序打印的是“_”而不是替换的数字?

Why is my program of game of fifteen printing '_' instead of the replaced number?

我正在使用 cs50 和 pset3 十五。游戏看起来不错,除非输入数字以替换“_”。在游戏开始时,当游戏要求 number/tile 移动时,我输入了要移动到的“_”的数字,但它没有用所选数字替换“_”。只有当维度是偶数并且在游戏开始时选择“_”左侧的数字时才会发生这种情况。谢谢!

#define _XOPEN_SOURCE 500

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

// constants
#define DIM_MIN 3
#define DIM_MAX 9

// board
int board[DIM_MAX][DIM_MAX];

// dimensions
int d;

// prototypes
void clear(void);
void greet(void);
void init(void);
void draw(void);
bool move(int tile);
bool won(void);

int main(int argc, string argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        printf("Usage: fifteen d\n");
        return 1;
    }

    // ensure valid dimensions
    d = atoi(argv[1]);
    if (d < DIM_MIN || d > DIM_MAX)
    {
        printf("Board must be between %i x %i and %i x %i, inclusive.\n",
            DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
        return 2;
    }

    // open log
    FILE *file = fopen("log.txt", "w");
    if (file == NULL)
    {
        return 3;
    }

    // greet user with instructions
    greet();

    // initialize the board
    init();

    // accept moves until game is won
    while (true)
    {
        // clear the screen
        clear();

        // draw the current state of the board
        draw();

        // log the current state of the board (for testing)
        for (int i = 0; i < d; i++)
        {
            for (int j = 0; j < d; j++)
            {
                fprintf(file, "%i", board[i][j]);
                if (j < d - 1)
                {
                    fprintf(file, "|");
                }
            }
            fprintf(file, "\n");
        }
        fflush(file);

        // check for win
        if (won())
        {
            printf("ftw!\n");
            break;
        }

        // prompt for move
        printf("Tile to move: ");
        int tile = get_int();

        // quit if user inputs 0 (for testing)
        if (tile == 0)
        {
            break;
        }

        // log move (for testing)
        fprintf(file, "%i\n", tile);
        fflush(file);

        // move if possible, else report illegality
        if (!move(tile))
        {
            printf("\nIllegal move.\n");
            usleep(500000);
        }

        // sleep thread for animation's sake
        usleep(500000);
    }

    // close log
    fclose(file);

    // success
    return 0;
}

/**
 * Clears screen using ANSI escape sequences.
 */
void clear(void)
{
    printf("3[2J");
    printf("3[%d;%dH", 0, 0);
}

/**
 * Greets player.
 */
void greet(void)
{
    clear();
    printf("WELCOME TO GAME OF FIFTEEN\n");
    usleep(2000000);
}

/**
 * Initializes the game's board with tiles numbered 1 through d*d - 1
 * (i.e., fills 2D array with values but does not actually print them).  
 */
void init(void)
{
    int dimensionNum = (d * d) - 1;
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            board[i][j] = dimensionNum--;
        }
    }

    if (d % 2 == 0)
    {
        board[d - 1][d - 2] = 2;
        board[d - 1][d - 3] = 1;
    }
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (board[i][j] == 0) 
            {
                printf("  _");
            }
            else 
            {
                printf("%3i", board[i][j]);
            }
        }
        printf("\n");
    }
}

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{
    int _i, _j; 
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (board[i][j] == tile)
            {
                _i = i;
                _j = j;
            }
        }
    }

    if (board[_i + 1][_j] == 0)
    {
        board[_i + 1][_j] = board[_i][_j];
        board[_i][_j] = 0;
        return true;
    }
    else if (board[_i - 1][_j] == 0)
    {
        board[_i - 1][_j] = board[_i][_j];
        board[_i][_j] = 0;
        return true;
    }
    else if (board[_i][_j - 1] == 0)
    {
        board[_i][_j - 1] = board[_i][_j];
        board[_i][_j] = 0;
        return true;
    }
    else if (board[_i][_j + 1] == 0)
    {
        board[_i][_j + 1] = board[_i][_j];
        board[_i][_j] = 0;
        return true;
    }

    return false;
}

在移动函数中尝试找到棋盘中的棋子后,您应该检查获得的数组索引是否有效。因为,如果用户输入的数字超出允许值,则会使用一些垃圾值进行进一步的条件检查。因此,将 _i 和 _j 变量设置为 -1 或开头的其他内容,并在移动函数中的 for 循环之后检查它们是否为 - 1。如果是,说明你有非法输入,可以直接returnfalse。否则,您可以继续进行条件检查。其他代码似乎没问题。您也可以尝试使用调试器或通过演练来更好地理解。

如果这回答了您的问题,请单击绿色复选标记关闭此问题。