初学者C ++:Tic Tac Toe

时间:2016-03-20 02:47:32

标签: c++

我今年在学校(高中/ C ++)参加我的第一个编码课程,这是我第一次真正尝试编写除书本练习以外的任何东西。

我只是想为自己的第一个项目制作一个简单的Tic Tac Toe游戏(不是学校相关的,只是为了好玩),但我一直在努力修复过去几天的错误。

首先,我一直想知道是否有办法让变量返回到我的'Checkerboard'功能,所以我不必在每次移动后都调用它。现在我必须在每次移动后调用'棋盘',但我想知道是否有办法将X或O的位置返回到原始功能,这样每次移动后都不会弹出新的棋盘。

接下来,当我使用'CheckMove'函数时,我的两个“if”语句将不会一个接一个地运行(例如,如果我键入一个已经使用过的数字,然后是一个超出范围的数字0-10,我的程序运行不正常)。如果我只执行其中一项操作,我的程序运行正常,但只要我背靠背执行它就无法正常工作。

最后,我无法弄清楚为什么我的代码在宣布获胜者时不会停止运行(参见'CheckWinner函数)。

在旁注中,因为这是我的第一个代码,所以其他任何关于如何改进它的建议都将受到高度赞赏。

#include <iostream>
#include <conio.h>
#include <iomanip>

using namespace std;

void Checkerboard(char SpaceNumber[]);
void CheckMoveX(int Player[], int Choice, char SpaceNumber[]);
void CheckMoveO(int Player[], int Choice, char SpaceNumber[]);
int CheckWinner(char SpaceNumber[], int CheckWin);

void main()
{
    char SpaceNumber[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    int Player[2] = {1, 2}, Choice, CheckWin=0;

    cout << "          Tic Tac Toe\n\n";
    cout << "Player 1: 'X' --- Player 2: 'O'\n\n";
    Checkerboard(SpaceNumber);

    do {
        cout << "\nPlayer " << Player[0] << " pick a number to place your mark...";
        cin >> Choice;
        CheckMoveX(Player, Choice, SpaceNumber);    

        CheckWinner(SpaceNumber, CheckWin);

        cout << "\nPlayer " << Player[1] << " pick a number to place your mark..."; //The rest of the main function is a duplicate of the first half, but it is for player 2.
        cin >> Choice;
        CheckMoveO(Player, Choice, SpaceNumber);

        CheckWinner(SpaceNumber, CheckWin);
        }while (CheckWinner != 0); //Program isn't ending when winner occurs.
    _getch();
}

void Checkerboard(char SpaceNumber[])
{
    cout << setw(3) << SpaceNumber[0] << setw(3) << "|" << setw(3) << SpaceNumber[1] << setw(3) << "|" << setw(3) << SpaceNumber[2] << endl;
    cout << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << endl;

    cout << setw(3) << SpaceNumber[3] << setw(3) << "|" << setw(3) << SpaceNumber[4] << setw(3) << "|" << setw(3) << SpaceNumber[5] << endl;
    cout << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << endl;

    cout << setw(3) << SpaceNumber[6] << setw(3) << "|" << setw(3) << SpaceNumber[7] << setw(3) << "|" << setw(3) << SpaceNumber[8] << endl;
}
void CheckMoveX(int Player[], int Choice, char SpaceNumber[]) //I cannot seem to have the first/second "if" statements run back to back.
{
    do {
        if (Choice > 0 && Choice < 10)
        {
            if (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O') //This loop ensures that the same square cannot be picked twice.
            {
                do {
                    cout << "This number has already been chosen. Please pick a different number...";
                    cin >> Choice;
                } while (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O');
            }
            SpaceNumber[Choice - 1] = 'X';
            Checkerboard(SpaceNumber);
        }

        if (Choice < 1 || Choice > 9)//Forces the user to pick a number on the board.
        {
            do {
                    cout << "Invalid choice. Please pick another number...";
                    cin >> Choice;
                } while (Choice < 1 || Choice > 9);

                SpaceNumber[Choice - 1] = 'X';
                Checkerboard(SpaceNumber);
        }
    } while (SpaceNumber[Choice - 1] != 'X');
}
void CheckMoveO(int Player[], int Choice, char SpaceNumber[]) //Duplicate of CheckMoveO
{
    if (Choice > 0 && Choice < 10)
    {
        if (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O')
        {
            do {
                cout << "This number has already been chosen. Please pick a different number...";
                cin >> Choice;
            } while (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O');
        }
        SpaceNumber[Choice - 1] = 'O';
        Checkerboard(SpaceNumber);
    }

    if (Choice < 1 || Choice > 9)
    {
        do {
                cout << "Invalid choice. Please pick another number...";
                cin >> Choice;
            } while (Choice < 1 || Choice > 9);

            SpaceNumber[Choice - 1] = 'O';
            Checkerboard(SpaceNumber);
    }
}
int CheckWinner(char SpaceNumber[], int CheckWin)
{
    if (SpaceNumber[0] == 'X' && SpaceNumber[3] == 'X' && SpaceNumber[6] == 'X') //Not sure how to do this without if/else statements.
    {
        CheckWin = 0;
        cout << "Player 1 Wins!";
        return 0;
    }
    else if (SpaceNumber[1] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[7] == 'X')
    {
        CheckWin = 0;
        cout << "Player 1 Wins!";
        return CheckWin;
    }
    else if (SpaceNumber[2] == 'X' && SpaceNumber[5] == 'X' && SpaceNumber[7] == 'X')
    {
        CheckWin = 0;
        cout << "Player 1 Wins";
        return 0;
    }
    else if (SpaceNumber[0] == 'X' && SpaceNumber[1] == 'X' && SpaceNumber[2] == 'X')
    {
        CheckWin = 0;
        cout << "Player 1 Wins!";
        return 0;
    }
    else if (SpaceNumber[3] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[5] == 'X')
    {
        CheckWin = 0;
        cout << "Player 1 Wins!";
        return 0;
    }
    else if (SpaceNumber[6] == 'X' && SpaceNumber[7] == 'X' && SpaceNumber[8] == 'X')
    {
        CheckWin = 0;
        cout << "Player 1 Wins!";
        return 0;
    }
    else if (SpaceNumber[0] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[8] == 'X')
    {
        CheckWin = 0;
        cout << "Player 1 Wins";
        return 0;
    }
    else if (SpaceNumber[2] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[6] == 'X')
    {
        CheckWin = 0;
        cout << "Player 1 Wins";
        return 0;
    }

    //End Player 1 Win. Begin Player 2 Win (Duplicate Code).

    else if (SpaceNumber[0] == 'O' && SpaceNumber[3] == 'O' && SpaceNumber[6] == 'O') //Not sure how to do this without if/else statements.
    {
        CheckWin = 0;
        cout << "Player 2 Wins!";
        return 0;
    }
    else if (SpaceNumber[1] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[7] == 'O')
    {
        CheckWin = 0;
        cout << "Player 2 Wins!";
        return 0;
    }
    else if (SpaceNumber[2] == 'O' && SpaceNumber[5] == 'O' && SpaceNumber[7] == 'O')
    {
        CheckWin = 0;
        cout << "Player 2 Wins";
        return 0;
    }
    else if (SpaceNumber[0] == 'O' && SpaceNumber[1] == 'O' && SpaceNumber[2] == 'O')
    {
        CheckWin = 0;
        cout << "Player 2 Wins!";
        return 0;
    }
    else if (SpaceNumber[3] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[5] == 'O')
    {
        CheckWin = 0;
        cout << "Player 2 Wins!";
        return 0;
    }
    else if (SpaceNumber[6] == 'O' && SpaceNumber[7] == 'O' && SpaceNumber[8] == 'O')
    {
        CheckWin = 0;
        cout << "Player 2 Wins!";
        return 0;
    }
    else if (SpaceNumber[0] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[8] == 'O')
    {
        CheckWin = 0;
        cout << "Player 2 Wins";
        return 0;
    }
    else if (SpaceNumber[2] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[6] == 'O')
    {
        CheckWin = 0;
        cout << "Player 2 Wins";
        return 0;
    }

    //End player 2 win; begin cout for tie game.

    else
        return 1;
} 

3 个答案:

答案 0 :(得分:0)

你没有在while条件下调用CheckWinner,你正在比较CheckWinner的永远不会为零的地址。

答案 1 :(得分:0)

你有很多重复的代码。而不是两个CheckMove函数,只需要一个并传入'X'或'O'进行比较。您可以使用CheckWinner(第3次比较中的下标不正确)执行相同的操作。

对于您的第二个问题,CheckMove中的问题是,在do / while循环中,您获得了输入值,但未对其进行验证。

当你有一个胜利者时,你的计划没有停止有两个原因:第一个是在'X'移动后你检查胜利者但是在'O'移动之前不检查结果。第二个是你的比较,CheckWinner != 0正在检查函数的地址为零(这将永远不会发生),而不是你的CheckWin变量。如果CheckWinner返回获胜者而不是设置全局变量,这将更好。

我不确定你提出的第一个问题是什么。

答案 2 :(得分:0)

1)您没有将CheckWinner的值保存到CheckWin

CheckWinner(SpaceNumber, CheckWin);替换为CheckWin = CheckWinner(SpaceNumber, CheckWin);

2)您需要检查(CheckWin != 0)而不是(CheckWinner != 0)

3)在接受下一步的输入之前,你没有检查游戏是否结束。

替换 do.. while 函数中的<{strong> main

do {
    CheckWin = CheckWinner(SpaceNumber, CheckWin);
    if (CheckWin!=0)
    {
    cout << "\nPlayer " << Player[0] << " pick a number to place your mark...";
    cin >> Choice;
    CheckMoveX(Player, Choice, SpaceNumber);
    }

    CheckWin = CheckWinner(SpaceNumber, CheckWin);
    if (CheckWin != 0 )
    {
    cout << "\nPlayer " << Player[1] << " pick a number to place your mark...";
    cin >> Choice;
    CheckMoveO(Player, Choice, SpaceNumber);
    }

}while (CheckWin != 0);

那应该可以解决你的问题。如果您对代码有其他问题,请告诉我。