用c ++重启程序

时间:2017-11-28 14:08:31

标签: c++

看看这段代码,它是Tic Tac Toe的游戏 游戏结束后,程序会询问用户是否要再次播放,如果是,则该功能再次运行。但是,数组值在全局声明时仍保持更新状态。该数组必须全局声明,因为它在多个函数中使用。当用户选择再次播放选项时,有没有办法将数组的值重置为原始值?

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


    void contents(); //function to draw the tic tac toe board
    char letters[9] = {'a','b','c','d','e','f','g','h','i'}; //array containing 
    the default characters of the board
    string check(); //function to check whether the game is ongoing or has ended 
    in a win or draw
    string result; // string telling us whether the game is ongoing or has ended 
    in a win or draw





    void contents()
    {

    cout <<endl<<endl<<endl<<"   Tic Tac Toe"<<endl<<endl<<endl;


    cout << endl;

    cout << "     |     |     " << endl;
    cout << "  " << letters[0] << "  |  " << letters[1] << "  |  " << letters[2] 
    << endl;

    cout << "     |     |     " << endl;
    cout << "----- ----- -----" << endl;
    cout << "     |     |     " << endl;

    cout << "  " << letters[3] << "  |  " << letters[4] << "  |  " << letters[5] 
    << endl;

    cout << "     |     |     " << endl;
    cout << "----- ----- -----" << endl;
    cout << "     |     |     " << endl;

    cout << "  " << letters[6] << "  |  " << letters[7] << "  |  " << letters[8] 
    << endl;

    cout << "     |     |     " << endl << endl;
    }




    int main()
    {
    char selection1; //selection of the mark chosen by player 1 (X or O)
    int player = 1;
    char mark;//X or O
    char selection2; //selection of the character to replace in the array chosen 
    by the players
    char invalid; //character to enter if the user selects a character that 
    isn't present in the array
    string choice;



    while(selection1!='X' || selection1!='Y')
    {
     cout<<"Make your selection player 1 (X or O): ";
     cin>>selection1;
     if (selection1=='X' || selection1=='O')
        break;
       else
        cout<<"Make a valid selection, press enter to select again"<<endl;
        cin.ignore();
        cin.get();
    }

    do
    {
     player=(player%2)?1:2;

    if(selection1 =='X')
    mark = (player == 1) ? 'X' : 'O';

    else if (selection1=='O')
    mark = (player == 1) ? 'O' : 'X';


        contents();


        cout << "Player " << player << ", enter a letter:  ";
        cin >> selection2;


        if (selection2 == 'a' && letters[0] == 'a')

            letters[0] = mark;
        else if (selection2 == 'b' && letters[1] == 'b')

            letters[1] = mark;
        else if (selection2 == 'c' && letters[2] == 'c')

            letters[2] = mark;
        else if (selection2 == 'd' && letters[3] == 'd')

            letters[3] = mark;
        else if (selection2 == 'e' && letters[4] == 'e')

            letters[4] = mark;
        else if (selection2 == 'f' && letters[5] == 'f')

            letters[5] = mark;
        else if (selection2 == 'g' && letters[6] == 'g')

            letters[6] = mark;
        else if (selection2 == 'h' && letters[7] == 'h')

            letters[7] = mark;
        else if (selection2 == 'i' && letters[8] == 'i')

            letters[8] = mark;
        else
        {
            cout<<"Invalid move, press enter to continue: ";
            cin.ignore();
            cin.get();
            player--;


        }
        result=check();

        player++;
    }while(result=="Ongoing");
    contents();
    if(result=="Over")
{

        cout<<"Player "<<--player<<" wins ";
        cin.ignore();
        cin.get();
        cout<<"Do you want to play again?";
        cin>>choice;
        if (choice =="Yes")
        main();

}
    else
    {

        cout<<"Draw";
        cin.ignore();
        cin.get();
        cout<<"Do you want to play again?";
        cin>>choice;
        if (choice =="Yes")
        main();

    }


    return 0;
}


string check()
{
    if (letters[0] == letters[1] && letters[1] == letters[2])

    return "Over";


    else if (letters[3] == letters[4] && letters[4] == letters[5])

    return "Over";


    else if (letters[6] == letters[7] && letters[7] == letters[8])

    return "Over";


    else if (letters[0] == letters[3] && letters[3] == letters[6])

    return "Over";


    else if (letters[1] == letters[4] && letters[4] == letters[7])

    return "Over";


    else if (letters[2] == letters[5] && letters[5] == letters[8])

    return "Over";


    else if (letters[0] == letters[4] && letters[4] == letters[8])

    return "Over";


    else if (letters[2] == letters[4] && letters[4] == letters[6])

    return "Over";



    else if (letters[0] != 'a' && letters[1] != 'b' && letters[2] != 'c'
             && letters[3] != 'd' && letters[4] != 'e' && letters[5] != 'f'
             && letters[6] != 'g' && letters[7] != 'h' && letters[8] != 'i')

        return "Draw";


    else

    return "Ongoing";
}

1 个答案:

答案 0 :(得分:2)

显而易见的方法是编写一个新功能,完全符合您的要求,即将letters[0]设置为'a'等。

您稍后可能会了解课程。一个类包含多个函数,还包含多个数据成员。这样一个类的每个实例都称为一个对象,并且您将每一轮游戏都实现为一个新对象。