为什么我的十五个游戏程序打印'_'而不是被替换的数字?

时间:2017-07-22 21:00:48

标签: c algorithm cs50

我正在服用cs50和pset3十五岁。游戏看起来很好,除非输入一个数字来代替'_'。在游戏开始时要求移动一个数字/牌,我输入要移动的'_'的数字,但它不会用所选的数字替换'_'。这仅在尺寸均匀并且在游戏开始时选择“_”左侧的数字时才会发生。谢谢!

#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("\033[2J");
    printf("\033[%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;
}

1 个答案:

答案 0 :(得分:1)

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

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