gpt4 book ai didi

c - 用C语言编程国际象棋中马的走法

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

嗨,分配基本上是将 K 放入骑士的输入值,然后将 X 放入棋盘的其余部分。然后,对于 Knight 可以执行的每个 Action ,我们输入数字,然后增加数字,例如如下所示:

X X X X X X X X
X X X X X X X X
X X X 1 X 2 X X
X X 4 X X X 3 X
X X X X K X X X
X X 5 X X X 6 X
X X X 7 X 8 X X
X X X X X X X X

我的代码在下面,但是当我运行它时,我得到的输出如下:4210828421082842108284210828421082842108284210828421082842108284210828421082842108284210828421082842108284210828421082842108284210828142108282421082842108284210828421082834210828421082842108284421082842108284210828421082842108284210825421082842108284210828421082842108285421082842108284210828642108284210828421082842108287421082884210828421082842108284210828421082842108284210828421082842108284210828

你能帮我修复我的代码吗?因为我是新手,所以我的 if else 条件非常蹩脚,所以如果你也能帮助我在砖 block 中添加更简单的条件,我真的可以了解它。

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

void boardDefine();
void boardDraw();

int main()

{
char board[8][8];
int i,j,row,column;
int nextMove;

printf("Please enter the position of the Knight on the board\n");
scanf("%d%d",&row,&column);
if(row<1||row>9||column<1||column>9)
{
printf("You must enter a value greater than zero");
}

boardDefine(board[8][8],i,j,row,column,nextMove);
boardDraw(board[8][8],i,j);


return 0;
}

void boardDefine(char board[8][8],int i, int j,int row,int column,int nextMove)
{
nextMove=1;
for( j=1;j<=8;j++)
{
for(i=1;i<=8;i++)
{

if(i==row&&j==column)
{
board[i][j]="K ";//Places the Knight to the position that entered by user
}
/*From here we are basicly showing where the Knight can move from its current position
for this we first check that if both row and column values are inside the board or not
after the L move if not then we put the nextMove value at that adress of the array
*/
else if(row-1<=8&&row-1>=0&&column+2<=8&&column+2>=0&&i==row-1&&j==column+2)
{
board[i][j]='0'+ nextMove;
nextMove++;
}
else if(row-1<=8&&row-1>=0&&column-2<=8&&column-2>=0&&i==row-1&&j==column-2)
{
board[i][j]='0'+ nextMove;
nextMove++;
}
else if(row+1<=8&&row+1>=0&&column+2<=8&&column+2>=0&&i==row+1&&j==column+2)
{
board[i][j]='0'+ nextMove;
nextMove++;
}
else if(row+1<=8&&row+1>=0&&column-2<=8&&column-2>=0&&i==row+1&&j==column-2)
{
board[i][j]='0'+ nextMove;
nextMove++;
}
else if(row-2<=8&&row-2>=0&&column+1<=8&&column+1>=0&&i==row-2&&j==column+1)
{
board[i][j]='0'+ nextMove;
nextMove++;
}
else if(row-2<=8&&row-2>=0&&column-1<=8&&column-1>=0&&i==row-2&&j==column-1)
{
board[i][j]='0'+ nextMove;
nextMove++;
}
else if(row+2<=8&&row+2>=0&&column-1<=8&&column-1>=0&&i==row+2&&j==column-1)
{
board[i][j]='0'+ nextMove;
nextMove++;
}
else if(row+2<=8&&row+2>=0&&column+1<=8&&column+1>=0&&i==row+2&&j==column+1)
{
board[i][j]='0'+ nextMove;
nextMove++;
}
else
{
board[i][j]="X ";//Places X to the places where Knight cant move.
}
}
printf("\n");
}

}

//Then we use this function to print
void boardDraw(char board[8][8],int i, int j)
{
for( j=1;j<=8;j++)
{
for(i=1;i<=8;i++)
{
printf("%c",board[i][j]);
}
printf("\n");
}

}

最佳答案

因此,您的代码存在几个问题,我将尽力解决尽可能多的问题,以使您的代码运行 -

首先,由于您想要存储单个可打印字符,因此应该将数组类型更改为 char 而不是 int。然后,您将向数组位置分配单个字符。因此,当分配 K 时,它将是 board[i][j] = 'k'; 并且 x 将是 board[i][j]='x';

由于您现在已将类型更改为 char,因此您必须将 printf 调用更改为

printf("%c ", board[i][j]);

最后,为了分配个位数,您不能简单地分配nextMove。您必须分配'0' + nextMove。这样您将获得数组中数字的 ASCII 等效值。

您犯的另一个错误是您已将 board[8][8] 传递给该函数。board[8][8] 仅表示数组中的单个元素。如果您想传递整个数组,则只需传递 board

最后,当数组声明为 board[8][8] 时,您运行了从 1 到 8 的循环。任何声明为 arr[x] 的数组都是从 0 到 (x-1) 而不是 1 到 x。所以你的循环应该改为

for (i = 0; i < 8; i++)

j 也类似。其余的都可以优化,但目前应该可以正常工作。

关于c - 用C语言编程国际象棋中马的走法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43439084/

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