黑白棋游戏 - 板配置和移动合法性检查C编程 [英] Reversi Game – Board Configuration and Move Legality Checking in C programming

查看:351
本文介绍了黑白棋游戏 - 板配置和移动合法性检查C编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序输入应该是下面的形式:U-对于没人住,B-黑色,白色W-被占。该计划应该要求用户配置一旦初始化板,然后打印使用用户配置董事会。然后它打印'W'和现有的移动; 'B'相应。最后一步是要求从所使用的一招,如果它与之前打印的可移动匹配,那么它打印的举动的消息是有效的,那么它打印板使用的有效举措也是最后一次。
我在这里得到一些奇怪的输出将打印配置的后板。请在这里帮忙,谢谢。
下面是一个例子输入和期望的输出:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdbool.h GT;
无效boardInitialize(炭板[26] [26],INT N);
无效printBoard(炭板[26] [26],INT N);
布尔checkLegalMovesAvailable(炭板[26] [26],INT N,字符颜色);
布尔positionInBounds(INT N,排炭,炭列);
无效printMove(炭板[26] [26],INT N);
布尔checkLegalInDirection(炭板[26] [26],INT N,排炭,炭关口,炭色,诠释deltaRow,诠释deltaCol);
布尔checkLegalInMove(炭板[26] [26],整数N,排炭,炭关口,字符颜色);诠释主要(无效){
    INT N;
    炭板[26] [26];
    的printf(请输入板尺寸:);
    scanf函数(%d个,&安培; N);
    boardInitialize(板,N);
    printBoard(板,N);
    如果(checkLegalMovesAvailable(板中,n,W))
        checkLegalMovesAvailable(板中,n,B);
    printMove(板,N);    回报(EXIT_SUCCESS);
}//函数初始化板
无效boardInitialize(炭板[26] [26],INT N){
    的printf();
    的for(int i = 0; I< N;我++){
        的printf(%C,97 + I);
    }
    的printf(\\ n);
    的for(int i = 0; I< N;我++){
        对于(INT J = 0; J< N; J ++){
            板[I] [J] ='U';
        }
    }
    板[(N / 2)-1] [(N / 2)-1] ='w';
    板[N / 2] [N / 2] ='W';
    板[(N / 2)-1] [N / 2] ='B';
    板[N / 2] [(N / 2)-1] ='B';
    的for(int i = 0; I< N;我++){
        的printf(%C,97 + I);
        对于(INT J = 0; J< N; J ++){
            的printf(%C,板[I] [J]);
        }
        的printf(\\ n);
    }
}//函数来配置后打印板
无效printBoard(炭板[26] [26],INT N){
    的printf(请输入板配置:);
    的printf(\\ n);
    炭色,行,列;
    的for(int i = 0;(颜色=放!!;&安培;!排=放'!';&安培;西=)!!;我++){
        scanf函数(%C%C%C,&安培;颜色,和放大器;行&安培; COL);
            板[行'A'] [COL-A] =颜色;
    }
    的printf();
    的for(int i = 0; I< N;我++){
        的printf(%C,97 + I);
    }
    的printf(\\ n);
    的for(int i = 0; I< N;我++){
        的printf(%C,97 + I);
        对于(INT J = 0; J< N; J ++){
            的printf(%C,板[I] [J]);
        }
        的printf(\\ n);
    }
}//功能打印配置后可移动
布尔checkLegalMovesAvailable(炭板[26] [26],INT N,字符颜色){
    诠释做= 1;
    的printf(用于%C可移动:\\ n,颜色);
    的for(int i = 0; I< N;我++){
        对于(INT J = 0; J< N; J ++){
            如果(板[I] [J] ==颜色){
                对(INT deltaRow = -1; deltaRow&下; = 1; deltaRow ++){
                    对(INT deltaCol = -1; deltaCol&下; = 1和;&放大器;!做= 0; deltaCol ++){
                        如果((positionInBounds(N,('一个'+ deltaRow),('一个'+ deltaCol)))及及(checkLegalInDirection(板,N('一个'+ deltaRow),('一个'+ deltaCol) ,颜色,deltaRow,deltaCol)))
                            做++;
                    }
                }
            }
        }
    }
    返回true;
}//检查功能,如有此举是在特定的方向法律
布尔checkLegalInDirection(炭板[26] [26],INT N,排炭,炭关口,炭色,诠释deltaRow,诠释deltaCol){
    INT R =行A,C = COL-A,数= 2;
    如果((板[R + deltaRow] [C + deltaCol] =色)及!&安培;!(板[R + deltaRow] [C + deltaCol] ='U')){
        的for(int i = 0; I< N;我++){
                如果((板[R +(计数* deltaRow)] [C +(计数* deltaCol)] =色)及!&安培;!(板[R +(计数* deltaRow)] [C +(计数* deltaCol)] ='U '))
                    算上++;
                其他
                    计数 - ;
                的printf(%C%C \\ n(行+(计数* deltaRow)),(COL +(计数* deltaCol)));
            }
            返回true;
    }
    其他
        如果(板[R + deltaRow] [C + deltaCol] == ||彩板[R + deltaRow] [C + deltaCol] =='U')
        返回false;
    }//函数检查指定的行,列位于电路板尺寸范围内
布尔positionInBounds(INT N,排炭,炭列){
    INT P =行'A',Q = COL-A;
    如果(P> = 0&放大器;&放大器; P&下; = N){
        如果(Q> = 0&放大器;&安培; Q< = N)
            返回true;
        其他
            返回false;
    }
    其他
        返回false;
}//功能后,合法的移动打印板
无效printMove(炭板[26] [26],INT N){
    炭色,行,列;
    的printf(请输入一招:\\ n);
    scanf函数(%C%C%C,&安培;颜色,和放大器;行&安培; COL);
    板[行'A'] [COL-A] =颜色;
    如果(checkLegalInMove(板,正,行,列,颜色)){
        的printf();
        的for(int i = 0; I< N;我++){
            的printf(%C,97 + I);
        }
        的printf(\\ n);
        的for(int i = 0; I< N;我++){
            的printf(%C,97 + I);
            对于(INT J = 0; J< N; J ++){
                的printf(%C,板[I] [J]);
            }
            的printf(\\ n);
        }
    }
}//检查功能如果有具体的行动是合法的
布尔checkLegalInMove(炭板[26] [26],整数N,排炭,炭关口,字符颜色){
    INT R =行A,C = COL-A;
    对(INT deltaRow = -1; deltaRow&下; = 1; deltaRow ++){
        对(INT deltaCol = -1; deltaCol&下; = 1; deltaCol ++){
            如果((positionInBounds(N,行,列))及及(checkLegalInDirection(板,N,('A'+ R),('A'+ C),颜色,deltaRow,deltaCol))){
                的printf(有效举​​措\\ n);
                返回true;
            }
            其他{
                的printf(无效的举动\\ n);
                返回true;
            }
        }
    }
}


解决方案

有很多问题在这里。这里有几个:

(1)在你的 scanf函数叫你忘了把你想要的结果存储在变量的地址之一。 GCC 提醒一下:

  scanf函数(%C%C%C,色,行,列);

试试这个更改为:

  scanf函数(%C%C%C,&安培;颜色,和放大器;行&安培; COL);

(2) checkLegalInDirection ,打印移动时,你减去'A',这当然是因为你要打印的字符错误,不偏移。所以改变:

 的printf(%C%C \\ n(行+(计数* deltaRow) - 'A'),(COL +(计数* deltaCol) - 'A'));

 的printf(%C%C \\ n(行+(计数* deltaRow)),(COL +(计数* deltaCol)));

(3) checkLegalMovesAvailable 应该返回一个布尔,但没有在函数的任何位置返回语句。这意味着,如果它试图检查返回值调用者会挑一些随机值。添加收益语句的功能,并使其返回布尔值。

我不相信这是唯一的问题,但我真的认为你需要花一些时间对这个工作,所以我会离开你这个开始。希望这会有所帮助。

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("\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
bool checkLegalMovesAvailable(char board[26][26], int N, char colour){
    int done=1;
    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 && 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\n",(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:\n");
    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("\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 true;
            }
        }
    }
}

解决方案

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\n",(row+(count*deltaRow)-'a'),(col+(count*deltaCol)-'a'));

to:

printf("%c%c\n",(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.

这篇关于黑白棋游戏 - 板配置和移动合法性检查C编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆