質問

So, I am working on a tic-tac-toe program. I have set up a boolean function and it works - it displays "Player 1 has Won!" When 3 X's are found in the given array numbers, but how can I force the program to stop?

I tried assigning i = 9 so the for loop would stop running (and thus stop allowing the input of a number to place an X or O. This is a very introductory-level program, so if you can help, please keep it as basic as possible.

#include <iostream>
#include "tictactoe.h"

using namespace std;

char board[9] {
    '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
bool winner;

void checkWinner(void) {
    bool tie;

    if (board[0] == 'X' && board[1] == 'X' && board[2] == 'X') {
        winner = 1;
        cout << "Player 1 Wins!" << endl;

    }
    else if (board[0] == 'Y' && board[1] == 'Y' && board[2] == 'Y'){
        winner = 1;
        cout << "Player 2 Wins!" << endl;
    }
}


void displayBoard(void) {

int index;
int movePosition;
    bool gameOver;

    index = 0;

    for (int i=0; i < 9; i++) {
        if (winner == 1) {
            i += 9;
            cout << "Player 1 Wins!" << endl;
        }
    cout << "Player 1 is X, player 2 is O" << endl;
    cout << endl;
    cout << board[index] << "|" << board[index+1] << "|" << board[index+2] << endl;
    cout << "-----" << endl;
    cout << board[index+3] << "|" << board[index+4] << "|" << board[index+5] << endl;
    cout << "-----" << endl;
    cout << board[index+6] << "|" << board[index+7] << "|" << board[index+8] << endl;

        cout << "Enter the space number where you would like to place X" << endl;
        checkWinner();
        if (winner == 1) {
            gameOver = true;
        }
        cin >> movePosition;

            while ((board[movePosition - 1] == 'X' || board[movePosition - 1] == 'O')) {
                cout << "This space is already taken.  Please choose an open space." << endl;
                cin >> movePosition;
            }

            board[movePosition - 1] = 'X';

            cout << "Enter the space number where you would like to place O" << endl;
            cin >> movePosition;

            while ((board[movePosition - 1] == 'X' || board[movePosition - 1] == 'O')) {
                cout << "This space is already taken.  Please choose an open space." << endl;
                cin >> movePosition;
            }

            board[movePosition - 1] = 'O';
    }
}



int main (int argc, char *argv[]) {
    displayBoard();


}
役に立ちましたか?

解決

use exit(0) after checking if player 1 is the winner, to exit the program.

Or, change the function to bool checkWinner() to be able to return a bool.

bool checkWinner(void) 
{
    bool winner = false;

    if (board[0] == 'X' && board[1] == 'X' && board[2] == 'X') {
        winner = true;
        cout << "Player 1 Wins!" << endl;

    }
    else if (board[0] == 'Y' && board[1] == 'Y' && board[2] == 'Y'){
        winner = true;
        cout << "Player 2 Wins!" << endl;
    }

    return winner;
}

In the displayBoard() function, you can then retrieve the return value, by

if ( checkWinner() )
{
    std::cout << "We have a winner" << std::end;
    return;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top