用C编程国际象棋中骑士的动作

Programming moves of the Knight in Chess with C

Hi 分配基本上是将 K 放入 Knight 输入的值,然后将 X 放入棋盘的其余部分。然后对于 Knight 可以做的每一步,我们输入数字,然后我们增加数字,例如:

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

我的代码在下面,但是当我 运行 它时,我得到的输出是这样的: 42108284210828421082842108284210828421082842108284210828 42108284210828421082842108284210828421082842108284210828 42108284210828421082814210828242108284210828 42108284210828342108284210828421082844210828 42108284210828421082842108284210825421082842108284210828 42108284210828542108284210828421082864210828 42108284210828421082874210828842108284210828 42108284210828421082842108284210828421082842108284210828

那么你能帮我修复我的代码吗?因为我是新手,所以我的 if else 条件非常蹩脚,所以如果你也能帮我把更简单的条件放在砖块里,我真的可以理解它。

   #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");
    }

}

我不知道你的问题到底出在哪里,但你试过 switch 语句了吗? 它使您的代码简单,例如:

开关(1) {

case 1 : cout << '1'; // prints "1"

         break;       // and exits the switch
case 2 : cout << '2';

         // without break it will show 2 and after it 3 until next break

 case 3: cout << '3'; 

}

您的代码中有几处没有按您认为的那样进行。在不给你明确代码的情况下(因为这看起来像一个学校项目),我会给你一些关于如何使你拥有的东西更实用的指导。

  1. 初始化棋盘:您已经知道骑士的位置,所以您应该先将其存储在棋盘上。您的 boardDefine 函数已经迭代了棋盘中的每个元素,因此如果该位置不是骑士或骑士可以进入的位置,请尝试存储您用来表示 'X' 的任何值。
  2. 缩小您的条件语句:您的 if 语句比它们需要的复杂得多。将骑士的位置存储在某处并通过向 I 或 j 加减 2 然后从另一个加减 1 来检查 (i, j) 是否是有效的移动,如下所示:

    //The '10' can be whatever number you're using to represent the knight
    if(board[i+2][j+1] == 10) {
        board[i][j] == nextMove;
        nextMove++;
    }
    

    还要确保您的电路板的索引始终介于 0 和 7 之间,而不是 1 和 8。否则您将遇到运行时错误,指出您的索引超出范围。如果 i = 6 并且您的代码在任何地方执行 board[i+2][j],您最终会遇到运行时错误,因为 8 > 7.

  3. 为您要打印的内容赋值:因为您用一个 int 数组表示您的板,所以您应该只在数组中存储整数为了清晰和代码功能。尝试用 int 表示骑士和 X,当您打印棋盘时,确保在达到该 int 时打印 K 或 X。最后,当您打印电路板时,读取电路板的值,然后使用电路板中的值代表的字母或数字执行 printf。每次从板上读取新值时,您都可以使用 if 或 switch 来执行此操作。否则,您可能会打印出您不想打印的值。

希望这对您有所帮助,编码愉快!

所以你的代码有几个问题,我会尽可能多地解决应该得到你的代码 运行 -

首先,由于您要存储单个可打印字符,您应该将数组类型更改为 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 也类似。 其余的都可以优化,但现在应该可以正常工作。