如何在棋盘C中验证移动?

vlju58qv  于 2022-12-11  发布在  其他
关注(0)|答案(1)|浏览(107)

你好,我是一个编程新手,这个任务让我很沮丧,因为我仍然不能让棋子在棋盘上移动。我猜这是我的while循环函数造成的,但仍然不能让它正确。第一个用户输入应该检测要移动的角色的坐标,并显示哪些可用的点,它可以使用validateMove将棋子发送到这些点().这是我的代码,已经查了好几个星期了。

#include <stdio.h>

char board[8][8] = {
    {' ', 'B', ' ', 'B', ' ', 'B', ' ', 'B'},
    {'B', ' ', 'B', ' ', 'B', ' ', 'B', ' '},
    {' ', 'B', ' ', 'B', ' ', 'B', ' ', 'B'},
    {'*', ' ', '*', ' ', '*', ' ', '*', ' '},
    {' ', '*', ' ', '*', ' ', '*', ' ', '*'},
    {'W', ' ', 'W', ' ', 'W', ' ', 'W', ' '},
    {' ', 'W', ' ', 'W', ' ', 'W', ' ', 'W'},
    {'W', ' ', 'W', ' ', 'W', ' ', 'W', ' '}};

void printBoard()
{
    int i , j , k ; 
    
    printf("\n    "); 
    
    for(i=0;i<8;i++) 
        printf("    %d", i+1 ); 
        printf(" \n");
        
    for(k=0;k<8;k++) 
    {
        printf("     ");
        
        for(i=0;i<42;i++)
        { 
            printf("-"); 
        } 
        printf("  \n");
         
        printf("   %d ", k+1); 
        
        for(j=0;j<8;j++) 
        {
            printf("|| %c ", board[k][j]); 
        }
        printf("|| \n");
    }

    printf("     ");
    
    for(i=0;i<42;i++)
    { 
        printf("-");
    } 
    printf("  \n");
}

void validateMoveChecker(int x1, int y1, int af, int bf, int v) 
{
    int a, b;
    
    for(a=1, b=1; board[x1+ af * a][y1+ bf * b] == '*'; a++, b++) 
    {
        if((x1+af*a) == -1 || (y1+bf*b)==v)
            return;
        printf("%d%d , ", x1+af*a , y1+bf*b);
    }
}

int validateMove(int x1 , int y1) 
{
    printf( "Available coordinates to send the piece are: \n" );
    validateMoveChecker(x1, y1, -1, 1, 8);
    validateMoveChecker(x1, y1, 1, -1, -1);
    validateMoveChecker(x1, y1, 1, 1, 8);
    validateMoveChecker(x1, y1, -1, -1, -1);
}

int getChange(int x1 ,int y1 ,int x2 ,int y2 )
{
    char temp;

    temp = board[x1][y1];
    board[x1][y1] = board[x2][y2];
    board[x2][y2] = temp;
}
int check(int x, int y)
{
    switch(board[x][y])
    {
    case 'B':
        return 1; 
        break;
    default: 
        return 0;
    }
}

int main(){
    int x1, y1, x2, y2, pos1, pos2, x, y;
    char board[8][8];
    
    printBoard();
    do
    {
        printf("\nEnter the position of the piece [X,Y]: ");
        scanf("%d", &pos1); 
                    
        y1 = pos1%10;
        x1 = pos1/10;
                    
        switch(board[x1][y1])
        {
            case 'B': validateMove(x1, y1);
                      break;
            default: printf("Invalid Position. Please enter again!");
        }
    }while(x1!=0 && y1!=0);
                
    printf("\nEnter the position on where to send the piece [X,Y]: ");
    scanf("%d", &pos2);
                
    y2 = pos2%10;
    x2 = pos2/10;
                
    getChange(x1 ,y1 ,x2 ,y2);
                
    if((x2-x1==y2-y1)!=0)
    {
        getChange(x1 ,y1 ,x2 ,y2);                  
        check(x,y);
    }
}
j7dteeu8

j7dteeu81#

我修改了printBoard(),使其显示从0开始的索引(1也可以,但之后需要调整索引):

void printBoard() {
    int i , j , k ;
    printf("\n    ");
    for(i=0;i<8;i++)
        printf("    %d", i );
    printf(" \n");
    for(k=0;k<8;k++) {
        printf("     ");
        for(i=0;i<42;i++) {
            printf("-");
        }
        printf("  \n");
        printf("   %d ", k);
        for(j=0;j<8;j++) {
            printf("|| %c ", board[k][j]);
        }
        printf("|| \n");
    }
    printf("     ");
    for(i=0;i<42;i++) {
        printf("-");
    }
    printf("  \n");
}

然后在main()中进行了以下更改:
1.删除局部变量board;您的代码依赖于一个同名的全局变量。但是,将board传递给需要它的函数比使用全局变量更好。
1.将两个scanf()改为直接读取坐标。
1.在getChange()之后添加了printBoard()以验证移动是否有效。
1.(未修复)变量xy未初始化。

int main(void) {
    int x1, y1, x2, y2, x, y;
    printBoard();
    do {
        printf("\nEnter the position of the piece [X,Y]: ");
        scanf("%d,%d", &x1, &y1);
        switch(board[x1][y1]) {
            case 'B':
                validateMove(x1, y1);
                break;
            default:
                printf("Invalid Position. Please enter again!");
        }
    } while(x1 && y1);

    printf("\nEnter the position on where to send the piece [X,Y]: ");
    scanf("%d,%d", &x2, &y2);
    getChange(x1, y1 ,x2 ,y2);
    printBoard();
    if((x2-x1==y2-y1)!=0) {
        getChange(x1 ,y1 ,x2 ,y2);
        check(x, y);
    }
}

和示例会话:

0    1    2    3    4    5    6    7    
     ------------------------------------------  
   0 ||   || B ||   || B ||   || B ||   || B || 
     ------------------------------------------  
   1 || B ||   || B ||   || B ||   || B ||   || 
     ------------------------------------------  
   2 ||   || B ||   || B ||   || B ||   || B || 
     ------------------------------------------  
   3 || * ||   || * ||   || * ||   || * ||   || 
     ------------------------------------------  
   4 ||   || * ||   || * ||   || * ||   || * || 
     ------------------------------------------  
   5 || W ||   || W ||   || W ||   || W ||   || 
     ------------------------------------------  
   6 ||   || W ||   || W ||   || W ||   || W || 
     ------------------------------------------  
   7 || W ||   || W ||   || W ||   || W ||   || 
     ------------------------------------------  

Enter the position of the piece [X,Y]: 0,1
Available coordinates to send the piece are: 

Enter the position on where to send the piece [X,Y]: 0,0

        0    1    2    3    4    5    6    7 
     ------------------------------------------  
   0 || B ||   ||   || B ||   || B ||   || B || 
     ------------------------------------------  
   1 || B ||   || B ||   || B ||   || B ||   || 
     ------------------------------------------  
   2 ||   || B ||   || B ||   || B ||   || B || 
     ------------------------------------------  
   3 || * ||   || * ||   || * ||   || * ||   || 
     ------------------------------------------  
   4 ||   || * ||   || * ||   || * ||   || * || 
     ------------------------------------------  
   5 || W ||   || W ||   || W ||   || W ||   || 
     ------------------------------------------  
   6 ||   || W ||   || W ||   || W ||   || W || 
     ------------------------------------------  
   7 || W ||   || W ||   || W ||   || W ||   || 
     ------------------------------------------

相关问题