Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In the following program input should be of the form: U-for unoccupied,B- occupied by black,W-occupied by white. The program should ask for user configuration once it initializes the board and then it prints the board using the user configuration. Then it prints the available moves for 'W' & 'B' accordingly. The last step is to ask for a move from the used and if it matches with the available moves printed before then it prints a message of the move is valid, then it prints the board for the last time using the valid move. I am getting some weird output here after it prints the configured board. please help here, thank you. here is an example input and expected output:

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

//Function to print board after configuration
void printBoard(char board[26][26], int n){
    printf("Enter board configuration:");
    printf("
");
    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("
");
    for(int i=0;i<n;i++){
        printf("%c ",97+i);
        for(int j=0;j<n;j++){
            printf("%c",board[i][j]);
        }
        printf("
");
    }   
}

//function to print available moves after configuration
bool checkLegalMovesAvailable(char board[26][26], int N, char colour){
    int done=1;
    printf("Available moves for %c:
",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 && done!=0;deltaCol++){
                        if((positionInBounds(N,('a'+deltaRow), ('a'+deltaCol))) && (checkLegalInDirection(board,N,('a'+deltaRow),('a'+deltaCol),colour,deltaRow,deltaCol)))
                            done++;
                    }
                }   
            }
        }
    }
    return true; 
}

//function to check if any move is legal in a specific direction
bool 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=2;
    if((board[r+deltaRow][c+deltaCol]!=colour) && (board[r+deltaRow][c+deltaCol]!='U')){
        for(int i=0;i<N;i++){
                if((board[r+(count*deltaRow)][c+(count*deltaCol)]!=colour) &&(board[r+(count*deltaRow)][c+(count*deltaCol)]!='U'))
                    count++;
                else
                    count--;
                printf("%c%c
",(row+(count*deltaRow)),(col+(count*deltaCol)));
            }
            return true;
    }
    else
        if(board[r+deltaRow][c+deltaCol]==colour || board[r+deltaRow][c+deltaCol]=='U')
        return false;
    }

//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 && p<=N){
        if(q>=0 && q<=N)
            return true;
        else
            return false;
    }
    else
        return false;
}

//function to print board after a legal move 
void printMove(char board[26][26], int n){
    char color,row,col;
    printf("Enter a move:
");
    scanf(" %c%c%c",&color,&row,&col);
    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("
");
        for(int i=0;i<n;i++){
            printf("%c ",97+i);
            for(int j=0;j<n;j++){
                printf("%c",board[i][j]);
            }
            printf("
");
        }
    }
}

//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.
");
                return true;
            }
            else{
                printf("Invalid move.
");
                return true;
            }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
192 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are a lot of problems here. Here are a few:

(1) In one of your scanf calls you forgot to take the address of the variables you want to store the result in. gcc warns about this:

scanf(" %c%c%c",color,row,col);

Try changing this to:

scanf(" %c%c%c",&color,&row,&col);

(2) In checkLegalInDirection, when printing the moves, you are subtracting 'a', which of course is a mistake since you're trying to print the characters, not the offsets. So change:

printf("%c%c
",(row+(count*deltaRow)-'a'),(col+(count*deltaCol)-'a'));

to:

printf("%c%c
",(row+(count*deltaRow)),(col+(count*deltaCol)));

(3) checkLegalMovesAvailable is supposed to return a bool, but there is no return statement anywhere in the function. This means that the caller will be picking up some random value if it tries to check the return value. Add a return statement to the function, and have it return a bool value.

I don't believe these are the only problems, but I really think you need to spend some time working on this so I'll leave you with this start. Hopefully this will help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...