2 位玩家井字游戏,玩家 1 获胜......但奇怪的是玩家 2 没有

时间:2021-02-25 10:54:40

标签: c++

我已经使用非常基本的逻辑实现了它,即在二维数组中存储输入

但我不明白为什么在轮到玩家 2 之后 SomeoneWonCondition() 不执行,而它对玩家 1 来说却是完美的。我什至计算了网格值,它们存储得非常好很好。 但由于某种奇怪的原因,它无法决定玩家 2 的获胜者。

这是我的代码,

#include <iostream>

using namespace std;

void emptyLine();
void displayNumpad();
void storingGridData(int input, int playerNumberType);
void displayGrid();
void checkAllboxes();
void player1InputIfNotFilled();
void player2InputIfNotFilled();
void someoneWonCondition();
void checkingWinner();
void playerNames(string &player1, string &player2);
void setTick1();void setTick2();void setTick3();void setTick4();void setTick5();
void setCircle1();void setCircle2();void setCircle3();void setCircle4();void setCircle5();


int playerNumberType; string player1, player2; int userInput;
bool gameOver = false, someoneWon = false; bool boxFilled1=0, boxFilled2=0, boxFilled3=0, boxFilled4=0, boxFilled5=0, boxFilled6=0, boxFilled7=0, boxFilled8=0, boxFilled9=0;
bool inputReceived=0; // for the input loop condition
int grid[3][3] = {7,8,9,6,5,4,3,6,3}; //assigning 0 for empty, 1 for tick and 2 for circle
//random values in grid for winning logic to work properly

int main()
{
    playerNames(player1, player2); //inputting names
    while(!gameOver)
    {

    displayGrid();
    displayNumpad();
    player1InputIfNotFilled(); inputReceived=0; //the latter for resetting the former's condition
    checkingWinner();
    someoneWonCondition(); if(someoneWon){break;} else{cout<<"";}

    checkAllboxes(); if(gameOver){system("cls");displayGrid();break;} else {cout <<"";} //checking if all boxes are filled, if so then it'll exit loop.
    system("cls");

    displayGrid(); displayNumpad();

    player2InputIfNotFilled(); inputReceived=0; //the latter for resetting the former's condition
    checkingWinner();
    someoneWonCondition(); if(someoneWon){break;} else{cout<< "";}

    checkAllboxes();//checking if all boxes are filled, if so then it'll exit loop
    }

    cout << grid[0][0] << "|" <<grid[0][1] << "|" << grid[0][2]<< endl;
    cout<< "------" << endl;
    cout << grid[1][0] << "|" <<grid[1][1] << "|" << grid[1][2]<< endl;
    cout<< "------" << endl;
    cout << grid[2][0] << "|" <<grid[2][1] << "|" << grid[2][2]<< endl;
return 0;
}

void setCircle1(){cout << "       ###         ";}
void setCircle2(){cout << "      #   #        ";}
void setCircle3(){cout << "     #     #       ";}
void setCircle4(){cout << "      #   #        ";}
void setCircle5(){cout << "       ###         ";}

void setTick1(){cout << "         #         ";}
void setTick2(){cout << "        #          ";}
void setTick3(){cout << "  #    #           ";}
void setTick4(){cout << "   #  #            ";}
void setTick5(){cout << "    ##             ";}

void emptyLine()
{
    cout << "                   ";
}
void displayNumpad()
{
    cout <<"~-------------~\n";
    cout <<"|  7 | 8 | 9  |\n";
    cout <<"| ----------- |\n";
    cout <<"|  4 | 5 | 6  |\n";
    cout <<"| ----------- |\n";
    cout <<"|  1 | 2 | 3  |\n";
    cout <<"~-------------~\n";
}

void playerNames(string &player1, string &player2)
{
    cout << "Enter name of player 1: "; cin >> player1;
    cout << "Enter name of player 2: "; cin >> player2;
}

void storingGridData(int input, int playerNumberType) // assigning according to numpad of keyboard
{
     if(input == 1){grid[2][0] = {playerNumberType}; boxFilled1=1;}
else if(input == 2){grid[2][1] = {playerNumberType}; boxFilled2=1;}
else if(input == 3){grid[2][2] = {playerNumberType}; boxFilled3=1;}
else if(input == 4){grid[1][0] = {playerNumberType}; boxFilled4=1;}
else if(input == 5){grid[1][1] = {playerNumberType}; boxFilled5=1;}
else if(input == 6){grid[1][2] = {playerNumberType}; boxFilled6=1;}
else if(input == 7){grid[0][0] = {playerNumberType}; boxFilled7=1;}
else if(input == 8){grid[0][1] = {playerNumberType}; boxFilled8=1;}
else if(input == 9){grid[0][2] = {playerNumberType}; boxFilled9=1;}
}

void displayGrid() //logic for grid structure, each box is 20 characters apart
{

    cout << "|";if(grid[0][0] == 1){setTick1();} else if(grid[0][0] == 2){setCircle1();} else{emptyLine();}cout<<"|"; if(grid[0][1] == 1){setTick1();} else if(grid[0][1] == 2){setCircle1();} else{emptyLine();} cout << "|"; if(grid[0][2] == 1){setTick1();} else if(grid[0][2] == 2){setCircle1();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[0][0] == 1){setTick2();} else if(grid[0][0] == 2){setCircle2();} else{emptyLine();}cout<<"|"; if(grid[0][1] == 1){setTick2();} else if(grid[0][1] == 2){setCircle2();} else{emptyLine();} cout << "|"; if(grid[0][2] == 1){setTick2();} else if(grid[0][2] == 2){setCircle2();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[0][0] == 1){setTick3();} else if(grid[0][0] == 2){setCircle3();} else{emptyLine();}cout<<"|"; if(grid[0][1] == 1){setTick3();} else if(grid[0][1] == 2){setCircle3();} else{emptyLine();} cout << "|"; if(grid[0][2] == 1){setTick3();} else if(grid[0][2] == 2){setCircle3();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[0][0] == 1){setTick4();} else if(grid[0][0] == 2){setCircle4();} else{emptyLine();}cout<<"|"; if(grid[0][1] == 1){setTick4();} else if(grid[0][1] == 2){setCircle4();} else{emptyLine();} cout << "|"; if(grid[0][2] == 1){setTick4();} else if(grid[0][2] == 2){setCircle4();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[0][0] == 1){setTick5();} else if(grid[0][0] == 2){setCircle5();} else{emptyLine();}cout<<"|"; if(grid[0][1] == 1){setTick5();} else if(grid[0][1] == 2){setCircle5();} else{emptyLine();} cout << "|"; if(grid[0][2] == 1){setTick5();} else if(grid[0][2] == 2){setCircle5();} else{emptyLine();}cout<<"|\n";
    cout << "|"<<"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|"<< endl;
    cout << "|";if(grid[1][0] == 1){setTick1();} else if(grid[1][0] == 2){setCircle1();} else{emptyLine();}cout<<"|"; if(grid[1][1] == 1){setTick1();} else if(grid[1][1] == 2){setCircle1();} else{emptyLine();} cout << "|"; if(grid[1][2] == 1){setTick1();} else if(grid[1][2] == 2){setCircle1();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[1][0] == 1){setTick2();} else if(grid[1][0] == 2){setCircle2();} else{emptyLine();}cout<<"|"; if(grid[1][1] == 1){setTick2();} else if(grid[1][1] == 2){setCircle2();} else{emptyLine();} cout << "|"; if(grid[1][2] == 1){setTick2();} else if(grid[1][2] == 2){setCircle2();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[1][0] == 1){setTick3();} else if(grid[1][0] == 2){setCircle3();} else{emptyLine();}cout<<"|"; if(grid[1][1] == 1){setTick3();} else if(grid[1][1] == 2){setCircle3();} else{emptyLine();} cout << "|"; if(grid[1][2] == 1){setTick3();} else if(grid[1][2] == 2){setCircle3();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[1][0] == 1){setTick4();} else if(grid[1][0] == 2){setCircle4();} else{emptyLine();}cout<<"|"; if(grid[1][1] == 1){setTick4();} else if(grid[1][1] == 2){setCircle4();} else{emptyLine();} cout << "|"; if(grid[1][2] == 1){setTick4();} else if(grid[1][2] == 2){setCircle4();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[1][0] == 1){setTick5();} else if(grid[1][0] == 2){setCircle5();} else{emptyLine();}cout<<"|"; if(grid[1][1] == 1){setTick5();} else if(grid[1][1] == 2){setCircle5();} else{emptyLine();} cout << "|"; if(grid[1][2] == 1){setTick5();} else if(grid[1][2] == 2){setCircle5();} else{emptyLine();}cout<<"|\n";
    cout << "|"<<"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|"<< endl;
    cout << "|";if(grid[2][0] == 1){setTick1();} else if(grid[2][0] == 2){setCircle1();} else{emptyLine();}cout<<"|"; if(grid[2][1] == 1){setTick1();} else if(grid[2][1] == 2){setCircle1();} else{emptyLine();} cout << "|"; if(grid[2][2] == 1){setTick1();} else if(grid[2][2] == 2){setCircle1();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[2][0] == 1){setTick2();} else if(grid[2][0] == 2){setCircle2();} else{emptyLine();}cout<<"|"; if(grid[2][1] == 1){setTick2();} else if(grid[2][1] == 2){setCircle2();} else{emptyLine();} cout << "|"; if(grid[2][2] == 1){setTick2();} else if(grid[2][2] == 2){setCircle2();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[2][0] == 1){setTick3();} else if(grid[2][0] == 2){setCircle3();} else{emptyLine();}cout<<"|"; if(grid[2][1] == 1){setTick3();} else if(grid[2][1] == 2){setCircle3();} else{emptyLine();} cout << "|"; if(grid[2][2] == 1){setTick3();} else if(grid[2][2] == 2){setCircle3();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[2][0] == 1){setTick4();} else if(grid[2][0] == 2){setCircle4();} else{emptyLine();}cout<<"|"; if(grid[2][1] == 1){setTick4();} else if(grid[2][1] == 2){setCircle4();} else{emptyLine();} cout << "|"; if(grid[2][2] == 1){setTick4();} else if(grid[2][2] == 2){setCircle4();} else{emptyLine();}cout<<"|\n";
    cout << "|";if(grid[2][0] == 1){setTick5();} else if(grid[2][0] == 2){setCircle5();} else{emptyLine();}cout<<"|"; if(grid[2][1] == 1){setTick5();} else if(grid[2][1] == 2){setCircle5();} else{emptyLine();} cout << "|"; if(grid[2][2] == 1){setTick5();} else if(grid[2][2] == 2){setCircle5();} else{emptyLine();}cout<<"|\n";

}


void player1InputIfNotFilled()
{
    playerNumberType=1;
 while (inputReceived != 1)
 {
            cout << player1 << "'s turn:- "; cin >> userInput;
    if(userInput == 1 && boxFilled1==0)
    {
    storingGridData(userInput, playerNumberType); inputReceived=1;
    }
    else if(userInput == 2 && boxFilled2==0)
    {
    storingGridData(userInput, playerNumberType); inputReceived=1;
    }
    else if(userInput == 3 && boxFilled3==0)
    {
    storingGridData(userInput, playerNumberType); inputReceived=1;
    }
    else if(userInput == 4 && boxFilled4==0)
    {
    storingGridData(userInput, playerNumberType); inputReceived=1;
    }
    else if(userInput == 5 && boxFilled5==0)
    {
    storingGridData(userInput, playerNumberType);  inputReceived=1;
    }
    else if(userInput == 6 && boxFilled6==0)
    {
    storingGridData(userInput, playerNumberType);  inputReceived=1;
    }
    else if(userInput == 7 && boxFilled7==0)
    {
    storingGridData(userInput, playerNumberType);  inputReceived=1;
    }
    else if(userInput == 8 && boxFilled8==0)
    {
    storingGridData(userInput, playerNumberType);  inputReceived=1;
    }
    else if(userInput == 9 && boxFilled9==0)
    {
    storingGridData(userInput, playerNumberType);  inputReceived=1;
    }
    else{cout<< "Box is already filled, choose different! \n"; continue;}

 }

}
void player2InputIfNotFilled()
{
    playerNumberType=2;
 while (inputReceived != 1)
 {
            cout << player2 << "'s turn:- "; cin >> userInput;
    if(userInput == 1 && boxFilled1==0)
    {
    storingGridData(userInput, playerNumberType); system("cls"); inputReceived=1;
    }
    else if(userInput == 2 && boxFilled2==0)
    {
    storingGridData(userInput, playerNumberType); system("cls"); inputReceived=1;
    }
    else if(userInput == 3 && boxFilled3==0)
    {
    storingGridData(userInput, playerNumberType); system("cls"); inputReceived=1;
    }
    else if(userInput == 4 && boxFilled4==0)
    {
    storingGridData(userInput, playerNumberType); system("cls"); inputReceived=1;
    }
    else if(userInput == 5 && boxFilled5==0)
    {
    storingGridData(userInput, playerNumberType); system("cls"); inputReceived=1;
    }
    else if(userInput == 6 && boxFilled6==0)
    {
    storingGridData(userInput, playerNumberType); system("cls"); inputReceived=1;
    }
    else if(userInput == 7 && boxFilled7==0)
    {
    storingGridData(userInput, playerNumberType); system("cls"); inputReceived=1;
    }
    else if(userInput == 8 && boxFilled8==0)
    {
    storingGridData(userInput, playerNumberType); system("cls"); inputReceived=1;
    }
    else if(userInput == 9 && boxFilled9==0)
    {
    storingGridData(userInput, playerNumberType); system("cls"); inputReceived=1;
    }
    else{cout<< "Box is already filled, choose different! \n"; continue;}

 }

}

void checkAllboxes()
{
    if (boxFilled1==1 && boxFilled2==1 && boxFilled3==1 && boxFilled4==1 && boxFilled5==1 && boxFilled6==1 && boxFilled7==1 && boxFilled8==1 && boxFilled9==1)
    {
     gameOver=true;
    }
    else{gameOver = false;}
}
void checkingWinner()
{
    if((grid[0][0] == grid[0][1]== grid[0][2] )|| (grid[1][0] == grid[1][1] == grid[1][2]) || (grid[2][0] == grid[2][1] == grid[2][2]) || (grid[0][0] == grid[1][0] == grid[2][0]) || (grid[0][1] == grid[1][1] == grid[2][1]) || (grid[0][2] == grid[1][2] == grid[2][2]) || (grid[0][0] == grid[1][1] == grid[2][2]) || (grid[0][2] == grid[1][1] == grid[2][0]))
    {
        someoneWon = true;
    }
    else{someoneWon = false;}
}
void someoneWonCondition()
{
    if(someoneWon && playerNumberType == 1){system("cls"); displayGrid(); cout <<player1 << " is the winner!\n";}

        else if(someoneWon && playerNumberType == 2){system("cls"); displayGrid(); cout <<player2 << " is the winner! \n";}
}

感谢任何帮助! 谢谢

1 个答案:

答案 0 :(得分:2)

稍微改变 void checkingWinner() 中的条件即可修复它,这是更改后的代码,

void checkingWinner()
{
    if((grid[0][0] == grid[0][1] && grid[0][1] == grid[0][2] )|| (grid[1][0] == grid[1][1] && grid[1][1] == grid[1][2]) || (grid[2][0] == grid[2][1] && grid[2][1] == grid[2][2]) || (grid[0][0] == grid[1][0] && grid[1][0] == grid[2][0]) || (grid[0][1] == grid[1][1] && grid[1][1] == grid[2][1]) || (grid[0][2] == grid[1][2] && grid[1][2] == grid[2][2]) || (grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2]) || (grid[0][2] == grid[1][1] && grid[1][1] == grid[2][0]))
    {
        someoneWon = true;
    }
    else{someoneWon = false;}
}