骑士每次在棋盘上的不同位置上移动c

时间:2018-08-27 15:02:13

标签: c

此代码应在8x8的棋盘上移动一个骑士,并且应该向我显示每次的总移动量。最后,它显示了140次移动,但是如果骑士每次在新位置上最多只能在棋盘上移动64次,那怎么可能?请回答我,让我知道我做错了什么。谢谢。

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

int main(){

    int board[8][8]={0};
    int currentRow=4, currentColumn=4;
    int cont=0, moveNumber, i, j, flag;

    while(moveNumber>=0 && moveNumber<=7){
        moveNumber=rand()%8;
        switch(moveNumber){
            case 0:
                currentRow--;
                currentColumn+=2;
                if(board[currentRow][currentColumn]==1 || currentRow>7 || currentColumn>7){
                    currentRow++;
                    currentColumn-=2;
                    flag=0;
                    break;
                }else{
                    board[currentRow][currentColumn]=1;
                    cont++;
                    flag=1;
                }
                break;
            case 1:
                currentRow-=2;
                currentColumn++;
                if(board[currentRow][currentColumn]==1 || currentRow>7 || currentColumn>7){
                    currentRow+=2;
                    currentColumn--;
                    flag=0;
                    break;
                }else{
                    board[currentRow][currentColumn]=1;
                    cont++;
                    flag=1;
                }
                break;
            case 2:
                currentRow-=2;
                currentColumn--;
                if(board[currentRow][currentColumn]==1 || currentRow>7 || currentColumn>7){
                    currentRow+=2;
                    currentColumn++;
                    flag=0;
                    break;
                }else{
                    board[currentRow][currentColumn]=1;
                    cont++;
                    flag=1;
                }
                break;
            case 3:
                currentRow--;
                currentColumn-=2;
                if(board[currentRow][currentColumn]==1 || currentRow>7 || currentColumn>7){
                    currentRow++;
                    currentColumn+=2;
                    flag=0;
                    break;
                }else{
                    board[currentRow][currentColumn]=1;
                    cont++;
                    flag=1;
                }
                break;
            case 4:
                currentRow++;
                currentColumn-=2;
                if(board[currentRow][currentColumn]==1 || currentRow>7 || currentColumn>7){
                    currentRow--;
                    currentColumn+=2;
                    flag=0;
                    break;
                }else{
                    board[currentRow][currentColumn]=1;
                    cont++;
                    flag=1;
                }
                break;
            case 5:
                currentRow+=2;
                currentColumn--;
                if(board[currentRow][currentColumn]==1 || currentRow>7 || currentColumn>7){
                    currentRow-=2;
                    currentColumn++;
                    flag=0;
                    break;
                }else{
                    board[currentRow][currentColumn]=1;
                    cont++;
                    flag=1;
                }
                break;
            case 6:
                currentRow+=2;
                currentColumn++;
                if(board[currentRow][currentColumn]==1 || currentRow>7 || currentColumn>7){
                    currentRow-=2;
                    currentColumn--;
                    flag=0;
                    break;
                }else{
                    board[currentRow][currentColumn]=1;
                    cont++;
                    flag=1;
                }
                break;
            case 7:
                currentRow++;
                currentColumn+=2;
                if(board[currentRow][currentColumn]==1 || currentRow>7 || currentColumn>7){
                    currentRow--;
                    currentColumn-=2;
                    flag=0;
                    break;
                }else{
                    board[currentRow][currentColumn]=1;
                    cont++;
                    flag=1;
                }
                break;
        }
        if(flag==1){
        for(i=0; i<8; i++){
            for(j=0; j<8; j++){
                printf("%d ", board[i][j]);
            }
            printf("\n");
        }
        printf("Total moves: %d\n",cont);
        }
    }

    return 0;
}

2 个答案:

答案 0 :(得分:1)

我认为您缺少某些东西。 在这一行:

if(board[currentRow][currentColumn]==1 || currentRow>7 || currentColumn>7)

您应该添加

currentRow<0 || currentColumn<0

答案 1 :(得分:1)

您观察到未定义行为的影响。您正在使用检查板中已分配的位置和不完整的范围更改。这会导致访问board数组超出范围。

如果您转储有趣的信息而不是公告板,则可以观看此内容。添加一行

printf("moveNumber: %d board[%d][%d]: %d\n", 
    moveNumber, currentRow, currentColumn, board[currentRow][currentColumn]);

在板单元之前的每次分配之前。会给您这样的输出:

moveNumber: 1 board[2][5]: 0
moveNumber: 3 board[1][3]: 0
moveNumber: 6 board[3][4]: 0
moveNumber: 4 board[4][2]: 0
moveNumber: 1 board[2][3]: 0
moveNumber: 4 board[3][1]: 0
moveNumber: 6 board[5][2]: 0
moveNumber: 6 board[7][3]: 0
moveNumber: 0 board[6][5]: 0
moveNumber: 1 board[4][6]: 0
moveNumber: 1 board[2][7]: 0
moveNumber: 3 board[1][5]: 0
moveNumber: 1 board[-1][6]: -858993460
moveNumber: 3 board[-2][4]: 3

最后两行显示索引无效的访问。这会导致不确定的行为。

您可以用不同的方式改进程序。您可以扩展范围更改并检查负数。您也可以将currentRowcurrentColumn的类型更改为unsigned,在这种情况下,您的支票就足够了。

如果您的伪随机数生成器创建了一个序列,那么您到达电路板的所有字段都取决于实现方式。我怀疑通常的伪随机生成器是否可以填充整个电路板。