C语言 奇怪的错误,似乎可以通过添加多余的代码行来解决.十五人游戏



这是一款益智游戏,在 4x4 网格中,必须按顺序排列 15 个编号的瓷砖。在大多数情况下,程序运行良好。但是,当将"1"位交换到第 n 行第 n 列第 n 列时,程序似乎会出错并复制数字 1。

这是问题所在。当我添加一行随机代码时,说

int blah = 0;

printf("abc");

问题就神奇地消失了。

因为我无法找到问题的根源,所以我必须发布整个问题。

若要查看问题,请在没有任何命令行参数的情况下运行代码,然后输入 2 后跟 1。

当我在main()函数的末尾添加随机代码行时,问题就消失了。请尝试一下,并帮助我找出正在发生的事情;这真的很令人困惑。

#include <stdio.h>
#include <stdlib.h>
int n=4;
int win(int board[n][n]);
void print(int board[n][n]);
int main(int argc, char * argv[])
{
    if(argc != 2)
    {
        printf("No valid number accepted. Board size set as 4x4.n");
    }
    else if(argc == 2)
    {
        n = atoi(argv[1]);
        if(n<2 || n>5)
        {
            printf("No valid number accepted. Board size set as 4x4.n");
        }
        else
        {
            printf("Preparing board of size %dx%dn",n,n);
        }
    }
    int board[n][n];
    printf("n The aim of the game is to sort the board so that it runs in ascending order, from 1 to %d, from left to right and up to down starting from the top left square. To make a move, enter the number of the tile you want to move. No diagonal movement is allowed.nn",n*n-1);
    int c = n*n-1;
    for(int x = 0;x<n;x++)
    {
        for(int y=0;y<n;y++)
        {
            board[x][y] = c;
            c--;
        }
    }
    if(n%2==0)
    {
        int temp1 = board[n-1][n-2];
        board[n-1][n-2] = board[n-1][n-3];
        board[n-1][n-3] = temp1;
    }
    print(board);
    int spacex = n-1;
    int spacey = n-1;
    char buffer[10];
    while(win(board) == 0)
    {
        printf("To move, enter the number you wish to move. Take note that this number must be adjacent to the blank space. Diagonal movement is not allowed.nYour move: ");
        fgets(buffer,10,stdin);
        int move;
        char temp[20];
        if(sscanf(buffer," %d %s",&move,temp)!= 1)
        {
            printf("Enter a number please.n");
            continue;
        }
        if(move == board[spacex+1][spacey])
        {
            board[spacex][spacey] = board[spacex+1][spacey];
            board[spacex+1][spacey] = 0;
            spacex++;
        }
        else if(move == board[spacex-1][spacey])
        {
            board[spacex][spacey] = board[spacex-1][spacey];
            board[spacex-1][spacey] = 0;
            spacex--;
        }
        else if(move == board[spacex][spacey+1])
        {
            board[spacex][spacey] = board[spacex][spacey+1];
            board[spacex][spacey+1] = 0;
            spacey++;
        }
        else if(move == board[spacex][spacey-1])
        {
            board[spacex][spacey] = board[spacex][spacey-1];
            board[spacex][spacey-1] = 0;
            spacey--;
        }
        else if(move == 0)
        {
            printf("Enter a valid digit please.n");
            continue;
        }
        else
        {
            printf("Enter a valid number please.n");
            continue;
        }
        printf("n");
        print(board);
    }
    printf("You won!n");
}
///////////////////////////////////////////////////////
void print(int board[n][n])
{
    for(int x=0;x<n;x++)
    {
        for(int y=0;y<n;y++)
        {
            if(board[x][y] == 0)
            {
                printf("__ ");
            }
            else
                printf("%2d ",board[x][y]);
        }
        printf("nn");
    }
}
///////////////////////////////////////////////////////
int win(int board[n][n])
{
    int check = 1;
    for(int x=0;x<n;x++)
    {
        for(int y=0;y<n;y++)
        {
            if(board[x][y] != check)
            {
                if(x==n-1 && y == n-1);
                else
                {
                    return 0;
                }
            }
            check++;
        }
    }
    return 1;
}

关于代码的任何其他评论也将不胜感激。提前感谢!

代码读取越界。

这两个变量指向数组board的最后一个元素:

   int spacex = n-1;
   int spacey = n-1;

但在所有 if 语句中都用错了。 每当使用 +1 时,它们都会读取越界或读取不正确的元素:

if(move == board[spacex+1][spacey])
{
    board[spacex][spacey] = board[spacex+1][spacey];
    board[spacex+1][spacey] = 0;
    spacex++;
}
else if(move == board[spacex-1][spacey])
{ 
...
else if(move == board[spacex][spacey+1])
{
    board[spacex][spacey] = board[spacex][spacey+1];
...

最新更新