Custom Search
A | A | A | Report content





#include <iostream>
// Tic, tac, toe

int main()
{
    // Get players name
    std::string player1 = "player1", player2 = "player2";
    std::cout << "Type player ones name: " << std::endl;
    std::cin >> player1;
    std::cout << "Type player twoes name: " << std::endl;
    std::cin >> player2;

    // chars to represent places
    char one = '1';
    char two = '2';
    char three = '3';
    char four = '4';
    char five = '5';
    char six = '6';
    char seven = '7';
    char eight = '8';
    char nine = '9';
    //Getting input
    short choice = '0';
    short check = 0;

    // Board
    std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
            << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n" << "  " <<  seven << " | " << eight << " | " << nine << std::endl;
    // Continue
    while (true) {
        // input for player1
        if (check == 0) {
            std::cout << "\n\nIts " << player1 << " turn" << std::endl;
        }
        while (check == 0 && std::cin >> choice) {
            std::cout << std::string(50, '\n');
            std::cout << player1 << " turn" << std::endl;
            if (choice == 1) {
                one = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 2) {
                two = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 3) {
                three = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 4) {
                four = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 5) {
                five = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 6) {
                six = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 7) {
                seven = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 8) {
                eight = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 9) {
                nine = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            if (one == 'X' && five == 'X' && nine == 'X') {
            std::cout << "We`ve got a winner:" << player1 << std::endl;
            break;
            }
                    if (one == 'X' && five == 'X' && nine == 'X') {
            std::cout << "We`ve got a winner:" << player1 << std::endl;
            break;
            }
            if (one == 'X' && five == 'X' && nine == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }
            if (one == 'X' && five == 'X' && nine == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }
            if (one == 'X' && four == 'X' && seven == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }


            if (one == 'O' && five == 'O' && nine == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (two == 'O' && five == 'O' && eight == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (four == 'O' && five == 'O' && six == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (one == 'O' && four == 'O' && seven == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (one != '1' && two != '2' && three != '3' && four != '4' && five != '5' && six != '6' && seven != '7' && eight != '8' && nine != '9') {
                std::cout << "Draw" << std::endl;
                break;
            }

            check = 1;
        }

        if (check == 1) {
            std::cout << "\n\nIts " << player2 << " turn" << std::endl;
        }
        // input for player2
        while (check == 1 && std::cin >> choice) {
            std::cout << std::string(50, '\n');
            if (choice == 1) {
                one = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 2) {
                two = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 3) {
                three = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 4) {
                four = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 5) {
                five = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 6) {
                six = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 7) {
                seven = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 8) {
                eight = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 9) {
                nine = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            if (one == 'X' && five == 'X' && nine == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }
            if (two == 'X' && five == 'X' && eight == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }
            if (four == 'X' && five == 'X' && six == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }
            if (one == 'X' && four == 'X' && seven == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }


            if (one == 'O' && five == 'O' && nine == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (two == 'O' && five == 'O' && eight == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (four == 'O' && five == 'O' && six == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (one == 'O' && four == 'O' && seven == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (one != '1' && two != '2' && three != '3' && four != '4' && five != '5' && six != '6' && seven != '7' && eight != '8' && nine != '9') {
                std::cout << "Draw" << std::endl;
                break;
            }
            check = 0;
        }
    }
    return 0;
}; | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 4) {
                four = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 5) {
                five = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 6) {
                six = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 7) {
                seven = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 8) {
                eight = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 9) {
                nine = 'X';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            if (one == 'X' && five == 'X' && nine == 'X') {
            std::cout << "We`ve got a winner:" << player1 << std::endl;
            break;
            }
                    if (one == 'X' && five == 'X' && nine == 'X') {
            std::cout << "We`ve got a winner:" << player1 << std::endl;
            break;
            }
            if (one == 'X' && five == 'X' && nine == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }
            if (one == 'X' && five == 'X' && nine == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }
            if (one == 'X' && four == 'X' && seven == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }


            if (one == 'O' && five == 'O' && nine == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (two == 'O' && five == 'O' && eight == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (four == 'O' && five == 'O' && six == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (one == 'O' && four == 'O' && seven == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (one != '1' && two != '2' && three != '3' && four != '4' && five != '5' && six != '6' && seven != '7' && eight != '8' && nine != '9') {
                std::cout << "Draw" << std::endl;
                break;
            }

            check = 1;
        }

        if (check == 1) {
            std::cout << "\n\nIts " << player2 << " turn" << std::endl;
        }
        // input for player2
        while (check == 1 && std::cin >> choice) {
            std::cout << std::string(50, '\n');
            if (choice == 1) {
                one = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 2) {
                two = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 3) {
                three = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 4) {
                four = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 5) {
                five = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 6) {
                six = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 7) {
                seven = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 8) {
                eight = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            else if (choice == 9) {
                nine = 'O';
                std::cout << "\n  " << one << " | " << two << " | " << three << "\n  ----------\n"
                    << "  " <<  four << " | " << five << " | " << six <<  "\n  ---------- \n"
                    << "  " <<  seven << " | " << eight << " | " << nine << std::string(14, '\n') << std::endl;
            }
            if (one == 'X' && five == 'X' && nine == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }
            if (two == 'X' && five == 'X' && eight == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }
            if (four == 'X' && five == 'X' && six == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }
            if (one == 'X' && four == 'X' && seven == 'X') {
                std::cout << "We`ve got a winner:" << player1 << std::endl;
                break;
            }


            if (one == 'O' && five == 'O' && nine == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (two == 'O' && five == 'O' && eight == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (four == 'O' && five == 'O' && six == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (one == 'O' && four == 'O' && seven == 'O') {
                std::cout << "We`ve got a winner:" << player2 << std::endl;
                break;
            }
            if (one != '1' && two != '2' && three != '3' && four != '4' && five != '5' && six != '6' && seven != '7' && eight != '8' && nine != '9') {
                std::cout << "Draw" << std::endl;
                break;
            }
            check = 0;
        }
    }
    return 0;
}