在构造函数中初始化值

时间:2019-06-16 17:45:23

标签: c++

我正在玩井字游戏,在构造函数中初始化属性值时遇到问题。 但是,您会注意到,从8-38行开始,我不得不再次重复该代码,因为我希望用户在确定结果后再次玩游戏。当我没有将代码从构造函数放到RunGame方法时,它就不会重复。

我试图从Run_Game方法中删除代码,但游戏结束后该面板将不再显示。

Tic_Tac_Toe::Tic_Tac_Toe()
{
    // Intializing values to attributes of class
    n = 0;
    size = 1;
    player_1 = 'X';
    player_2 = 'O';


    cout << "Welcome to the tic-tac-toe game" << endl;

}


void Tic_Tac_Toe::Run_game()
{
    //Intializing values from 2D array to board
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            matrix[0][0] = '1';
            matrix[0][1] = '2';
            matrix[0][2] = '3';
            matrix[1][0] = '4';
            matrix[1][1] = '5';
            matrix[1][2] = '6';
            matrix[2][0] = '7';
            matrix[2][1] = '8';
            matrix[2][2] = '9';
        }
    }
    int n = 0;
    int size = 1;
    char player_1 = 'X';
    char player_2 = 'O';
Draw_board(matrix,1); // Drawing the board for the game

    while (true) {
        n++;//Increments the value of n by 1 until condition is fulfils
        Input(matrix, player_1,player_2);
        Draw_board(matrix,1);
        if (Results(matrix,player_1,player_2) == player_1) {
            cout << "Player 1 wins" << endl;
            break;
        }
        else if (Results(matrix,player_1,player_2) == player_2) {
            cout << "Player 2 wins" << endl;
            break;
        }
        else if (Results(matrix,player_1,player_2) == '/' && n == 9) { // Comparing if result is a draw or not
            cout << "The game ends in a draw" << endl;
            cout << "You should play the game again to find a winner" << endl;
            break;
        }
        Set_players(&player_1, &player_2);
    }

0 个答案:

没有答案