Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;Tic Tac Toe-检测领带时出现问题_C++_Arrays - Fatal编程技术网

C++ C++;Tic Tac Toe-检测领带时出现问题

C++ C++;Tic Tac Toe-检测领带时出现问题,c++,arrays,C++,Arrays,标题似乎说明了一切。我不清楚为什么Tic Tac Toe程序没有检测到领带。我附加了主功能、附加功能等。。我不确定我做错了什么。非常感谢您的帮助 #include <string> #include <iostream> #include <cstdlib> using namespace std; string displayBoard(string board[9]); // displays tic tac toe board bool isGam

标题似乎说明了一切。我不清楚为什么Tic Tac Toe程序没有检测到领带。我附加了主功能、附加功能等。。我不确定我做错了什么。非常感谢您的帮助

#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

string displayBoard(string board[9]);   // displays tic tac toe board
bool isGameOver(string board[9]);       // checks if game is over
void displayGameWelcome();              // displays welcome message

int main()
{
string board[9];          // tic tac toe, top row 0 thru 2, middle row 3 thru 5, bottom row 6 thru 8
int position = 0;         // player's position
bool gameOver = false;    // flag variable to mark end of the game
bool validMove = false;   // determines if move is valid or not

displayGameWelcome();

// initializing board with blank spaces
for (int i = 0; i < 9; i++)
{
    board[i] = " ";
}

while (!gameOver)
{
    // player #1's turn **************************
    validMove = false;


    while (!validMove)
    {
        cout << "Enter your position: ";
        cin >> position;

        if (position >= 0 && position <= 8 && board[position] == " ")
        {
            validMove = true;
            board[position] = "x";      // placing x in desired board location
        }
        else
        {
            cout << "That position is already occupied." << endl;
            cout << displayBoard(board) << endl;
        }

    }

    cout << displayBoard(board) << endl;

    if (isGameOver(board))
    {
        gameOver = true;
    }

    // player #2's turn **********************************
    validMove = false;

    while (!validMove)
    {
        cout << "Enter your position: ";
        cin >> position;

        if (position >= 0 && position <= 8 && board[position] == " ")
        {
            validMove = true;
            board[position] = "o";      // placing o in desired board position
        }
        else
        {
            cout << "That position is already occupied." << endl;
            cout << displayBoard(board) << endl;
        }

    }

    cout << displayBoard(board) << endl;

    if (isGameOver(board))
    {
        gameOver = true;
    }

}// end of validMove while loop

system("pause");
return 0;
}// end of main

// ************************** functions *************************

void displayGameWelcome()
{
    cout << "Welcome to Tic Tac Toe v3.2" << endl;\
    cout << "by Brett Singley & Nick Canarelli" << endl;
    cout << "****************************\n\n\n";
}// end of displayGameWelcome

// checks if game is over
bool isGameOver(string board[9])
{

if (board[0] == "x" && board[1] == "x" && board[2] == "x")
{
    cout << endl << "The game is over - x wins" << endl;
    return true;
}
else if (board[0] == "o" && board[1] == "o" && board[2] == "o")
{
    cout << endl << "The game is over - o wins" << endl;
    return true;
}
else if (board[3]=="x" && board[4]=="x" && board[5]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;
}
else if (board[3]=="o" && board[4]=="o" && board[5]=="o")
{
    cout <<endl << "The game is over - o wins" <<endl;
}
else if (board[0]=="x" && board[3]=="x" && board[6]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;

}
else if (board[0]=="o" && board[3]=="o" && board[6]=="o")
{
    cout <<endl << "The game is over - o wins" <<endl;
}
else if (board[6]=="x" && board[7]=="x" && board[8]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;
}
else if (board[6]=="o" && board[7]=="o" && board[8]=="o")
{
    cout <<endl << "The game is over - O wins" <<endl;
}
else if (board[1]=="x" && board[4]=="x" && board[7]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;

}
else if (board[1]=="o" && board[4]=="o" && board[7]=="o")
{
    cout <<endl << "The game is over - O wins" <<endl;
}
else if (board[2]=="x" && board[5]=="x" && board[8]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;
}
else if (board[2]=="o" && board[5]=="o" && board[8]=="o")
{
    cout <<endl << "The game is over - o wins" <<endl;
}
else if (board[0]=="x" && board[4]=="x" && board[8]=="x")
{
    cout <<endl << "The game is over - X wins" << endl; 
}
else if (board[0]=="o" && board[4]=="o" && board[8]=="o")
{
    cout <<endl << "The game is over - o wins" <<endl;
}
else if (board[2]=="x" && board[4]=="x" && board[6]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;
}
else if (board[2]=="o" && board[4]=="o" && board[6]=="o")
{
    cout <<endl << "The game is over - o wins" <<endl;
}
else
{
    cout << "WOAH!!!!! Tie Game" <<endl;
}


// more to do here (don't forget to check for draw)

return false;
}// end of isGameOver

// displays tic tac toe board in the format
// |0 1 2|
// |3 4 5|
// |6 7 8|
string displayBoard(string board[9])
{
string str = "";

for (int i = 0; i < 9; i++)
{

    if (i == 0)
    {
        str = str  + "|" + board[i];
    }
    else if (i == 2 || i == 5)
    {
        str = str + board[i] + "|\n|";
    } 
    else if (i == 8)
    {
        str = str +  board[i] + "|\n";
    } 
    else
    {
        str = str + board[i];
    }

}

return str;
}// end of displayBoard
#包括
#包括
#包括
使用名称空间std;
字符串显示板(字符串板[9]);//显示tic tac趾板
bool-isGameOver(字符串板[9]);//检查游戏是否结束
void displayGameWelcome();//显示欢迎信息
int main()
{
弦板[9];//tic tac toe,最上面的第0行到第2行,中间的第3行到第5行,最下面的第6行到第8行
int position=0;//玩家的位置
bool gameOver=false;//标记游戏结束的标志变量
bool validMove=false;//确定移动是否有效
displayGameWelcome();
//用空格初始化电路板
对于(int i=0;i<9;i++)
{
板[i]=“”;
}
而(!gameOver)
{
//轮到玩家1次**************************
validMove=false;
而(!validMove)
{
cout>位置;

如果(position>=0&&position,您可以通过使用
for
循环并意识到三个空间中的值相等(与初始值不同)来简化对赢家的检查。此外,由于有3行和3列,因此可以在同一
for
循环中检查行和列:

const char initial_value = ' ';
const unsigned int max_rows_or_columns = 3;
bool Is_Game_Over(int board[9], char& winner)
{
    for (unsigned int i = 0; i < max_rows_or_columns; ++i)
    {
        // Check the row
        if (   (board[(i * 3) + 0] == board[(i * 3) + 1])
            && (board[(i * 3) + 1) == board[(i * 3) + 2]))
        {
           // By transitive property, board[(i * 3) + 0] == board[(i * 3) + 2]
           winner = board[(i * 3) + 0];
           if (winner != initial_value)
           {
               break;
           }
        }
        else // Check the columns
        {
            if (   (board[(0 * 3) + i] == board[(1 * 3) + i])  
                && (board[(1 * 3) + i] == board[(2 * 3) + i]))
            {
                winner = board[(0 * 3) + i];
                if (winner != initial_value)
                {
                    break;
                }
            }
        }
    bool winner_found = true;
    if (i >= max_rows_or_columns)
    {
        winner_found = false;
    }
    return winner_found;
}
const char初始值=“”;
常量无符号整数最大行数或列数=3;
布尔游戏结束了(智力板[9],char和winner)
{
for(无符号整数i=0;i=最大行数或列数)
{
winner_found=假;
}
返回你找到的;
}

检查对角线是留给读者的一个练习。

好吧,你可能想先完成isGameOver函数,或者发布你的最新代码。返回缺失你介意指导我正确的方向吗?我不确定遗漏了什么。游戏完成后,你在所有情况下都不会返回true…你应该创造一个乐趣动作进行移动,你给出一个参数谁是要移动的玩家,避免重复代码…@nackolous抱歉,因为你有几次
返回true
,我假设你刚刚发布了错误的代码。如果游戏结束,你的函数返回true,如果没有,则返回FALSE,对吗?但是,平局游戏和一些获胜案例没有e a
返回true
。它们根据您提供的代码返回false。我敢打赌,当您返回函数时,您刚刚忘记了它们。这发生在我们中最好的人身上