简体   繁体   English

Tic-Tac-Toe Loop&If问题

[英]Tic-Tac-Toe Loop & If issues

I need to make a two player tic tac toe game that uses a 1-9 grid. 我需要制作一个使用1-9网格的双人tic tac toe游戏。 The player enters in the number they want there letter to represent. 玩家输入他们想要的字母代表的数字。 However I can't get it to recognize who wins. 但是我不能认识到谁赢了。 After every number entered it says that the last player that entered in a number won. 在输入的每个数字之后,它表示输入数字的最后一个玩家赢了。

#include <iostream>

using namespace std;

int main()
{

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

cout << "Please enter a number on the board that is the spot you wish to use" << endl;

cout << "Board:\n";

cout << board[0] << " " << board[1] << " " << board[2] << endl;
cout << board[3] << " " << board[4] << " " << board[5] << endl;
cout << board[6] << " " << board[7] << " " << board[8] << endl;

do
{
for (turn=1; turn<10; turn++)
    if (!(turn % 2) == 0)
    {
        cout << "\nPlayer X's turn." << endl;
        cin >> Xp;
        board[Xp-1] = 'X';

cout << "Current Board:\n";

cout << board[0] << " " << board[1] << " " << board[2] << endl;
cout << board[3] << " " << board[4] << " " << board[5] << endl;
cout << board[6] << " " << board[7] << " " << board[8] << endl;




if((board[0]&&board[1]&&board[2] == 'X') || (board[3]&&board[4]&&board[5] == 'X') || (board[6]&&board[7]&&board[8] == 'X') || 
 (board[0]&&board[3]&&board[6] == 'X') || (board[1]&&board[4]&&board[7] == 'X') || (board[2]&&board[5]&&board[8] == 'X') || 
 (board[0]&&board[4]&&board[8] == 'X') || (board[6]&&board[4]&&board[2] == 'X'));
 {          
   cout << "Player X wins!!!!" << endl;
 }      
    else
    {

        cout << "\nPlayer O's turn." << endl;

        cin >> Op;
        board[Op-1] = 'O';

cout << "Current Board:\n";

cout << board[0] << " " << board[1] << " " << board[2] << endl;
cout << board[3] << " " << board[4] << " " << board[5] << endl;
cout << board[6] << " " << board[7] << " " << board[8] << endl;

if((board[0]&&board[1]&&board[2] == 'O') || (board[3]&&board[4]&&board[5] == 'O') || (board[6]&&board[7]&&board[8] == 'O') || 
 (board[0]&&board[3]&&board[6] == 'O') || (board[1]&&board[4]&&board[7] == 'O') || (board[2]&&board[5]&&board[8] == 'O') || 
 (board[0]&&board[4]&&board[8] == 'O') || (board[6]&&board[4]&&board[2] == 'O'));
{           
cout << "Player O wins!!!!" << endl;
}

    }
}
while(turn<10);

cout << "We have a tie!!!";

return 0;
}

The problem is with the if statements that look like this: 问题是if语句看起来像这样:

if((board[0]&&board[1]&&board[2] == 'X') || (board[3]&&board[4]&&board[5] == 'X') || (board[6]&&board[7]&&board[8] == 'X') || 
 (board[0]&&board[3]&&board[6] == 'X') || (board[1]&&board[4]&&board[7] == 'X') || (board[2]&&board[5]&&board[8] == 'X') || 
 (board[0]&&board[4]&&board[8] == 'X') || (board[6]&&board[4]&&board[2] == 'X'));
 {          
   cout << "Player X wins!!!!" << endl;
 } 

You have a ; 你有一个; before the block of code that you intend to be the body of the if statement. 在您打算成为if语句主体的代码块之前。 The ; ; ends the statement making the following block no longer part of the if, but simply a block of code to be executed regardless of the outcome of the condition. 结束语句使下一个块不再是if的一部分,而只是一个代码块,无论条件的结果如何都要执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM