gpt4 book ai didi

c - 奇怪的错误似乎可以通过添加多余的代码行来解决。十五人游戏

转载 作者:行者123 更新时间:2023-11-30 15:11:19 25 4
gpt4 key购买 nike

这是一款益智游戏,玩家必须在 4x4 网格中按顺序排列 15 个编号的图 block 。大多数情况下,程序运行良好。然而,当将“1”数字交换到第 n 行、第 n-2 列时,程序似乎出现错误并重复了数字 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%d\n",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.\n\n",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("\n\n");
}
}
///////////////////////////////////////////////////////
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;

but 在所有 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];
...

关于c - 奇怪的错误似乎可以通过添加多余的代码行来解决。十五人游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35700001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com