如何在TIC TAC TOE中检查获胜者,以及如何不让两个玩家进入同一位置

How to check winner in Tic Tac Toe as well as how to not let the both players to enter at the same position?

本文关键字:两个 玩家 位置 何不 TAC TIC TOE 获胜者 检查      更新时间:2023-10-16

我现在想用我的代码做2件事。1(检查获胜者2(不要让两个球员都在同一位置进入例如,如果player1已经在板上输入值[0] [0] ='x',并且player2再次进入董事会[0] [0] ='0',我如何不让播放器再次在板上输入[0] [0]?

同样,我如何检查获胜者?

我试图通过检查

来检查获胜者
if(board[0][0]==board[0][1]==board[0][2]

然后获胜者为player1。(Player1按下他的输入后(

但是,它没有用。

我想诚实地说,我仍然没有为下一个问题而获得逻辑。

#include <cstdlib>
#include<iostream>
using namespace std;
int choice;

char board[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};

void display_board()
{
system("cls");
    cout<<"..............................................Tick Cross Game by Pakistani coder........................................"<<endl;
    cout<<"tttttPlayer1[X]ntttttPlayer2[0]n";
    cout<<"ttttt     |     |     n";
    cout<<"ttttt     |     |     n";
    cout<<"ttttt__"<<board[0][0]<<"___ __"<<board[0][1]<<"___ __"<<board[0][2]<<"n";
    cout<<"ttttt     |     |     n";
    cout<<"ttttt     |     |     n";
    cout<<"ttttt__"<<board[1][0]<<"___ __"<<board[1][1]<<"___ __"<<board[1][2]<<"n";
    cout<<"ttttt     |     |     n";
    cout<<"ttttt     |     |     n";
    cout<<"ttttt___"<<board[2][0]<<"__ __"<<board[2][1]<<"___ __"<<board[2][2]<<"n";
    cout<<"ttttt     |     |     n";
   /// cout<<"ttttt     |     |     n";
}
void player_turn()
{
        cout<<"ntPlayer1[X] turn:";
    cin>>choice;

    switch(choice)
    {
    case 1:
        board[0][0]='X';
        break;
    case 2:
        board[0][1]='X';
        break;
    case 3:
        board[0][2]='X';
        break;
    case 4:
        board[1][0]='X';
        break;
           case 5:
        board[1][1]='X';
        break;
           case 6:
        board[1][2]='X';
        break;
           case 7:
        board[2][0]='X';
        break;
           case 8:
        board[2][1]='X';
        break;
           case 9:
        board[2][2]='X';
        break;
        default:
        cout<<"invalid choice";
        break;
        ///value will go there but need to display the board also
    }
    display_board();
    //checkwin();
    cout<<"player 2 turnn";
    cin>>choice;
    switch(choice)
    {

    case 1:
        board[0][0]='0';
        break;

    case 2:
        board[0][1]='0';
        break;
    case 3:
        board[0][2]='0';
        break;
    case 4:
        board[1][0]='0';
        break;
           case 5:
        board[1][1]='0';
        break;
           case 6:
        board[1][2]='0';
        break;
           case 7:
        board[2][0]='0';
        break;
           case 8:
        board[2][1]='0';
        break;
           case 9:
        board[2][2]='0';
        break;
        default:
        cout<<"invalid choice";
        break;
    }
    display_board();
}
int main()
{
   /*for(int i=0;i<3;i++)
   {
       for(int j=0;j<3;j++)
       {
           cout<<board[i][j];
       }
   }*/
   while(1)
   {
    display_board();
player_turn();

    display_board();
    }
}

预期输出:每当两个播放器在同一数组位置板[0] [0]中输入时,就应该显示第二个插入该位置的玩家的"无效选择"。

同样,每当发生Tictactoe的获胜条件时。即,如果1(连续3行具有相同的值2(连续3个COL具有相同的值3(对角元素的值相同(3(

if(board[0][0]==board[0][1]==board[0][2]

应该是

if (board[0][0] == board[0][1] && board[0][1] == board[0][2]

第一个版本比较了board[0][0]==board[0][1]的结果(这是布尔值,true或false(和board[0][2],这显然不是您想要的。

您不能假设数学中正确的事情也可以在C 中起作用。

如果您为董事会制定了一致的索引方案,则该程序将更简单。您要求用户为广场输入一个从1到9的数字,但是在您的程序中,您的2D数组从0到2。您可以做三件事,以使您的生活更轻松

1(使用户输入两个正方形的0到2的数字。

2(更改您的程序,以使您的董事会是1到9

的数组

3(编写一个将用户输入(1至9(转化为董事会使用的功能(两个数字为0到2(。

这些选项中的任何一个都会消除您使用的这些大型开关语句的需求。选项3可能是最好的。

类似的事情(您也可以使用%/巧妙地做一些可以缩短此功能的事情,但现在保持简单(。

bool translate_coordinates(int choice, int& x, int& y)
{
    switch (choice)
    {
    case 1:
        x = 0;
        y = 0;
        return true;
    case 2:
        x = 0;
        y = 1;
        return true;
    case 3:
        x = 0;
        y = 2;
        return true;
    case 4:
        x = 1;
        y = 0;
        return true;
    case 5:
        x = 1;
        y = 1;
        return true;
    case 6:
        x = 1;
        y = 2;
        return true;
    case 7:
        x = 2;
        y = 0;
        return true;
    case 8:
        x = 2;
        y = 1;
        return true;
    case 9:
        x = 2;
        y = 2;
        return true;
    default:
        return false; // invalid choice, let the caller handle this
    }
}

然后您的代码变成了这样的东西

void player_turn()
{
    cout<<"ntPlayer1[X] turn:";
    cin>>choice;
    int x, y;
    if (translate_coordinates(choice, x, y))
        board[x][y] = 'X';
    else
        cout << "invalid choicen";
    display_board();
    //checkwin();
    cout<<"player 2 turnn";
    cin>>choice;
    if (translate_coordinates(choice, x, y))
        board[x][y] = 'O';
    else
        cout << "invalid choicen";
    ...

如何使用此代码?

    int checkwin()
{
    if (square[1] == square[2] && square[2] == square[3])
        return 1;
    else if (square[4] == square[5] && square[5] == square[6])
        return 1;
    else if (square[7] == square[8] && square[8] == square[9])
        return 1;
    else if (square[1] == square[4] && square[4] == square[7])
        return 1;
    else if (square[2] == square[5] && square[5] == square[8])
        return 1;
    else if (square[3] == square[6] && square[6] == square[9])
        return 1;
    else if (square[1] == square[5] && square[5] == square[9])
        return 1;
    else if (square[3] == square[5] && square[5] == square[7])
        return 1;
    else if (square[1] != '1' && square[2] != '2' && square[3] != '3' 
                    && square[4] != '4' && square[5] != '5' && square[6] != '6' 
                  && square[7] != '7' && square[8] != '8' && square[9] != '9')
        return 0;
    else
        return -1;
}