程序在初始化板后应要求用户配置,然后使用用户配置打印板。然后它相应地打印“W”和“B”的可用移动。最后一步是询问已使用的移动,如果它与之前打印的可用移动匹配,则打印移动有效的消息,然后使用有效移动最后一次打印棋盘。我的代码工作正常,直到它打印配置的板,但之后我在这里得到一些奇怪的输出。请在这里帮忙,谢谢。在下面的程序中输入应该是:U-未占用,B-被黑色占用,W-被白色占用。这是一个示例输入和预期输出:示例
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void boardInitialize(char board[26][26], int n);
void printBoard(char board[26][26], int n);
void checkLegalMovesAvailable(char board[26][26], int N, char colour);
bool positionInBounds(int N, char row, char col);
void printMove(char board[26][26], int n);
void checkLegalInDirection(char board[26][26],int N,char row,char col,char colour,int deltaRow,int deltaCol);
bool checkLegalInMove(char board[26][26], int N, char row, char col, char colour);
int main(void){
int n;
char board[26][26];
printf("Enter the board dimension: ");
scanf("%d",&n);
boardInitialize(board,n);
printBoard(board,n);
checkLegalMovesAvailable(board,n,'W');
checkLegalMovesAvailable(board,n,'B');
printMove(board,n);
return (EXIT_SUCCESS);
}
//Function to initialize board
void boardInitialize(char board[26][26], int n){
printf(" ");
for(int i=0;i<n;i++){
printf("%c",97+i);
}
printf("\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
board[i][j]='U';
}
}
board[(n/2)-1][(n/2)-1]='W';
board[n/2][n/2]='W';
board[(n/2)-1][n/2]='B';
board[n/2][(n/2)-1]='B';
for(int i=0;i<n;i++){
printf("%c ",97+i);
for(int j=0;j<n;j++){
printf("%c",board[i][j]);
}
printf("\n");
}
}
//Function to print board after configuration
void printBoard(char board[26][26], int n){
printf("Enter board configuration:");
printf("\n");
char color,row,col;
for(int i=0;(color!='!' && row!='!' && col!='!');i++){
scanf(" %c%c%c",&color,&row,&col);
board[row-'a'][col-'a']=color;
}
printf(" ");
for(int i=0;i<n;i++){
printf("%c",97+i);
}
printf("\n");
for(int i=0;i<n;i++){
printf("%c ",97+i);
for(int j=0;j<n;j++){
printf("%c",board[i][j]);
}
printf("\n");
}
}
//function to print available moves after configuration
void checkLegalMovesAvailable(char board[26][26], int N, char colour){
printf("Available moves for %c:\n",colour);
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(board[i][j]==colour){
for(int deltaRow=-1;deltaRow<=1;deltaRow++){
for(int deltaCol=-1;deltaCol<=1;deltaCol++){
if(deltaRow==0 && deltaCol==0)
;
else
if(positionInBounds(N,('a'+i+deltaRow), ('a'+j+deltaCol)))
checkLegalInDirection(board,N,('a'+i+deltaRow),('a'+j+deltaCol),colour,deltaRow,deltaCol);
}
}
}
}
}
}
//function to check if any move is legal in a specific direction
void checkLegalInDirection(char board[26][26],int N,char row,char col,char colour,int deltaRow,int deltaCol){
int r=row-'a', c=col-'a',count=0;
while((positionInBounds(N,'a'+r+(count*deltaRow),'a'+c+(count*deltaCol))) && (board[r+(count*deltaRow)][c+(count*deltaCol)]!=colour) && (board[r+(count*deltaRow)][c+(count*deltaCol)]!='U')){
count++;
if((positionInBounds(N,'a'+r+(count*deltaRow),'a'+c+(count*deltaCol))) && (board[r+(count*deltaRow)][c+(count*deltaCol)]=='U')){
printf("%c%c\n",(row+(count*deltaRow)),(col+(count*deltaCol)));
break;
}
}
}
//function to check if the specified row,col lies within the board dimensions
bool positionInBounds(int N, char row, char col){
int p=row-'a',q=col-'a';
if(p>=0 && q>=0 && p<N && q<N)
return true;
else
return false;
}
//function to print board after a legal move
void printMove(char board[26][26], int n){
char color,row,col,temp;
printf("Enter a move:\n");
scanf(" %c%c%c",&color,&row,&col);
temp=board[row-'a'][col-'a'];
board[row-'a'][col-'a']=color;
if(checkLegalInMove(board,n,row,col,color)){
printf(" ");
for(int i=0;i<n;i++){
printf("%c",97+i);
}
printf("\n");
for(int i=0;i<n;i++){
printf("%c ",97+i);
for(int j=0;j<n;j++){
printf("%c",board[i][j]);
}
printf("\n");
}
}
else{
board[row-'a'][col-'a']=temp;
printf(" ");
for(int i=0;i<n;i++){
printf("%c",97+i);
}
printf("\n");
for(int i=0;i<n;i++){
printf("%c ",97+i);
for(int j=0;j<n;j++){
printf("%c",board[i][j]);
}
printf("\n");
}
}
}
//function to check if any specific move is legal
bool checkLegalInMove(char board[26][26], int N, char row, char col, char colour){
int r=row-'a',c=col-'a';
for(int deltaRow=-1;deltaRow<=1;deltaRow++){
for(int deltaCol=-1;deltaCol<=1;deltaCol++){
if(positionInBounds(N,row,col)){
checkLegalInDirection(board,N,('a'+r),('a'+c),colour,deltaRow,deltaCol);
printf("Valid move.\n");
return true;
}
else
printf("Invalid move.\n");
return false;
}
}
}