Example #1
0
File: ttt.cpp Project: borfast/ttt
int main() {
    Board board;
    Computer computer;
    Game game(&board);
    IO io;

    char turn, again;
    bool playing;

    io.displayInstructions();

    while (true) {
        board.reset();
        game.setLetters(io.inputPlayerLetter());
        computer.setSmart(io.inputComputerSmartness());
        turn = whoGoesFirst();
        cout << "The " << turn << " will go first." << endl << endl;
        playing = true;
        again = '?';

        while (playing) { // The main game loop
            io.drawBoard(board);

            // Who's turn is it?
            int move;

            if (turn == 'p') {
                // player's turn
                move = getPlayerMove(board);
                if (move == 9)
                    break;
                board.setPlace(move, game.getPlayerLetter());
                cout << "You played " << move << endl;

                // Check if player won
                if (game.isWinner(game.getPlayerLetter(), board)) {
                    io.drawBoard(board);
                    cout << "Hooray! You have won the game!" << endl
                         << endl;
                    playing = false;
                } else {
                    if (board.isFull()) {
                        io.drawBoard(board);
                        cout << "The game is a tie!" << endl
                             << endl;
                        break;
                    } else {
                        turn = 'c';
                    }
                }
            } else {
                // computer's turn
                move = getComputerMove(game, board, computer);
                board.setPlace(move, game.getComputerLetter());
                cout << "The computer played " << move << endl;

                if (game.isWinner(game.getComputerLetter(), board)) {
                    io.drawBoard(board);
                    cout << "The computer has beaten you! You lose." << endl
                         << endl;
                    playing = false;
                } else {
                    if (board.isFull()) {
                        io.drawBoard(board);
                        cout << "The game is a tie!";
                        break;
                    } else {
                        turn = 'p';
                    }
                }
            }
        }

        while (again != 'y' && again != 'n') {
            cout << "Do you want to play again? (y/n): ";
            cin >> again;
        }
        if (again == 'y') {
            continue;
        } else {
            break;
        }
    }

    cout << endl << "Sorry to see you go. Perhaps a nice game of Global Thermonuclear War next time?" << endl;

    return 0;
}