C语言 我的井字游戏没有在第2列和第3列返回赢家

zbsbpyhn  于 2023-03-17  发布在  其他
关注(0)|答案(2)|浏览(134)

我有一个井字游戏的C程序,我的问题是所有的获胜条件都是好的,除了当第2列或第3列有3个“X”或“O”时,程序不会返回赢家,程序会一直运行,直到没有空格或另一个玩家(或电脑)获胜。但是,如果第一列有3个'X'或'O',游戏将停止并返回赢家。我曾尝试修复它,但我无法找出我的代码有什么问题。
我的条件是

#include <stdio.h>
#include <time.h>
char board[3][3];
const char PLAYER ='X';
const char PLAYER2 = 'O';
const char BOT ='O';

void resetBoard(){
    int i,j;
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            board[i][j]=' ';
        }
    }
}

void printWinner(char winner){
    if (winner==PLAYER){
        printf("\nPlayer 1 wins!");
    }
    else if(winner==BOT){
        printf("\nYou lose!");
    }
    else printf("\nTie!");
}

void printWinnerHuman(char winner){
    if (winner==PLAYER){
        printf("\nPlayer 1 wins!");
    }
    else if(winner==PLAYER2){
        printf("\nPlayer 2 wins!");
    }
    else printf("\nTie!");
}

void playerMove(){
    int x,y;
    do{
        printf("\nYou are X!");
        printf("\nEnter move (Ex: 0 2): ");
        scanf("%d %d",&x,&y);
        if (board[x][y]!=' '){
            printf("Space already taken\n");
            printBoard();
        }
        else {
            board[x][y]=PLAYER;
            break;
        }
    }
    while(board[x][y]!=' ');

}

void player2Move(){
    int x,y;
    do{
        printf("You are O!");
        printf("Enter your move (Ex: 1 2): ");
        scanf("%d %d",&x,&y);
        if(board[x][y]!=' '){
            printf("\nSpace already taken!");
            printBoard();
        }
        else{
            board[x][y]=PLAYER2;
            break;
        }
    }
    while(board[x][y]!=' ');
}

void botMove(){
    //create random moves for bot
    srand(time(0));
    int x,y;
    if(checkFreeSpaces()>0){
        do{
            x=rand()%3;
            y=rand()%3;
        }
        while(board[x][y]!=' ');
        board[x][y]=BOT;
    }
    else printWinner(' ');
}

char checkWinner(){
    int i;
    //check rows winner
    for(i=0;i<3;i++){
        if (board[i][0]==board[i][1]&&board[i][0]==board[i][2]){
            return board[i][0];
        }
    }
    //check columns winner
    for(i=0;i<3;i++){
        if (board[0][i]==board[1][i]&&board[0][i]==board[2][i]){
            return board[0][i];
    }
    //check diagnols winner
        if(board[0][0]==board[1][1]&&board[0][i]==board[2][2]){
            return board[0][0];
    }
    //check second diagnols winner
        if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]){
            return board[0][2];
    }
    return ' ';  //no winners
    }
}

//This is a test line to test github saving

int checkFreeSpaces(){
    int i,j;
    int freeSpaces=9;
    for(i=0;i<3;++i){
        for(j=0;j<3;++j){
            if (board[i][j]!=' '){
                freeSpaces--;
            }
        }
    }
    return freeSpaces;
}

void printBoard(){
    printf("   0   1   2");
    printf("\n |---|---|---|");
    printf("\n0| %c | %c | %c |",board[0][0],board[0][1],board[0][2]);
    printf("\n |---|---|---|");
    printf("\n1| %c | %c | %c |",board[1][0],board[1][1],board[1][2]);
    printf("\n |---|---|---|");
    printf("\n2| %c | %c | %c |",board[2][0],board[2][1],board[2][2]);
    printf("\n |---|---|---|");
}
    
void humanPlay(){
    resetBoard(); //reset default declaration of computer
        char winner =' ';
        while(winner == ' ' &&checkFreeSpaces()!=0){     //while there is no winner and there are still spaces
            printBoard();
            playerMove();
            winner=checkWinner();   //after every move of player, program checks if there is a winner and if there are spaces left.
            if(winner!=' ' || checkFreeSpaces()==0){
                break;
            }
            printBoard();
            player2Move();
            winner=checkWinner();
            if(winner!=' '||checkFreeSpaces()==0){          //if there is no spaces left and no winner, break loop.
                break;
            }
        }
        printBoard();
        printWinner(winner);
    }

    
void botPlay(){
    resetBoard(); //reset default declarartion of computer
        char winner =' ';
        while(winner == ' ' &&checkFreeSpaces()!=0){    //while there is no winner and there are still spaces
            printBoard();
            playerMove();
            winner=checkWinner();   //after every move of player, program checks if there is a winner and if there are spaces left.
            if(winner!=' ' || checkFreeSpaces()==0){
                break;
            }
            botMove();
            winner=checkWinner();
            if(winner!=' ' || checkFreeSpaces()==0){
                break;
            }
        }
        printBoard();
        printWinner(winner);
    }

void menuMain(){
    int choice;
    printf("*-------MAIN MENU-------*");
    printf("\n1. Play with other players");
    printf("\n2. Play with BOT");
    printf("\n3. Replay");
    printf("\n4. Player's information'");
    printf("\n5. Guide");
    printf("\n6. Exit");
    printf("\nPress number to choose: "); scanf("%d",&choice);

    
    switch(choice){
        case 1:
            humanPlay();
            break;
        case 2:
            botPlay();
            break;
        case 3:
            
            break;
        case 4:
            
            break;
        case 5:
            printf("\n1.The game is played on a grid that's 3 squares by 3 squares");
            printf("\n2.You are X, your friend (or the computer in this case) is O. Players take turns putting their marks in empty squares.");
            printf("\n3.The first player to get 3 of her marks in a row (up, down, across, or diagonally) is the winner.");
            printf("\n3.The first player to get 3 of her marks in a row (up, down, across, or diagonally) is the winner.");
            printf("\n4.When all 9 squares are full, the game is over.");
            break;
        case 6:
            printf("Thank you for playing");
            break;
    }
}

int main(){
    
    menuMain();
    
    
    return 0;
}

谢谢!
我尝试从i更改为i

0g0grzrc

0g0grzrc1#

在checkWinners函数中,for循环中有一个return语句:

char checkWinner(){
    int i;
    //check rows winner
    for(i=0;i<3;i++){
        if (board[i][0]==board[i][1]&&board[i][0]==board[i][2]){
            return board[i][0];
        }                // MISSING THIS CURLY BRACE FOR THE COLUMN WINNERS CHECK
    }
    //check columns winner
    for(i=0;i<3;i++){
        if (board[0][i]==board[1][i]&&board[0][i]==board[2][i]){
            return board[0][i];
    }               // OOPS
    //check diagnols winner
        if(board[0][0]==board[1][1]&&board[0][i]==board[2][2]){
            return board[0][0];
    }
    //check second diagnols winner
        if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]){
            return board[0][2];
    }
    return ' ';  //no winners
    }               // SEE THIS, THIS IS A HINT SOMETHING IS WRONG
}

乍一看,您不会注意到for语句在if条件之后仍然继续。
你的意思可能是:

char checkWinner(){
    int i;
    //check rows winner
    for(i=0;i<3;i++){
        if (board[i][0]==board[i][1]&&board[i][0]==board[i][2]){
            return board[i][0];
        }
    }
    //check columns winner
    for(i=0;i<3;i++){
        if (board[0][i]==board[1][i]&&board[0][i]==board[2][i]){
            return board[0][i];
        } // ADD THIS
    }

    //check diagnols winner
    if(board[0][0]==board[1][1]&&board[0][i]==board[2][2]){
        return board[0][0];
    }

    //check second diagnols winner
    if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]){
        return board[0][2];
    }
    return ' ';  //no winners
}
kmbjn2e3

kmbjn2e32#

为了便于演示,假设电路板是这样的:

XO
 XO
 X

然后checkWinner将在第0列中看到3个相同的字符(' '),并返回该字符,humanPlaybotPlay正确地忽略了该字符。

相关问题