#include <iostream>#include <vector>#include <cstdlib>#include <iomanip>using namespace std;int winningCombos[8][3] = {{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};int wins1;int wins2;class game{private:    int currentPlayer;    vector <int> spaces;    int winner;    int numTurns;public:    void play();    void update(){ system(clear); } // Clear the screen    void makeBoard();   // Display the current board    void setup();   // Set up the game    void updateSpaces(int icon, int pos);    // updates the gameboard vector    bool checkState();  // Checks to see if the game is over};void game::makeBoard(){    update();    int pos = 0;    for (unsigned int row = 0; row < 3; row++){        for (unsigned int col = 0; col < 3; col++){            if (col != 2){                if (spaces[pos] == 88){                    cout <<     << \e[1mX\e[0m << setw(4) << |;                }else if (spaces[pos] == 79){                    cout <<     << \e[1mO\e[0m << setw(4) <<|;                }else{                    cout << setw(4) << spaces[pos] << setw(4) << |;                }            }else{                if (spaces[pos] == 88){                    cout <<     << \e[1mX\e[0m;                }else if (spaces[pos] == 79){                    cout <<     << \e[1mO\e[0m;                }else{                    cout << setw(4) << spaces[pos];                }            }            pos++;        }        cout << endl;        if (row != 2){            cout << ----------------------- << endl;        }    }}void game::setup(){    // Reset the board and number of turns    spaces.clear();    numTurns = 0;    // Set up a new gameboard    for (unsigned int i = 0; i < 10; i++){        spaces.push_back(i+1);    }    // Decide who moves first (player1 = X, player2 = O)    /*int num = rand() % 100 + 1;     cout << num;     if (num % 2 == 0){     currentPlayer = 1;     }else{     currentPlayer = 2;     }*/    int choice;    do {        cout << endl;        cout << Who is moving first? << endl;        cout << 1. Player 1 (X) << endl;        cout << 2. Player 2 (O) << endl;        cin >> choice;        switch(choice){            case 1: currentPlayer = 1;                break;            case 2: currentPlayer = 2;                break;            default: cout << Invalid Option. Please Enter Again. << endl;        }    }while(choice != 1 && choice != 2);}void game::updateSpaces(int icon, int pos){    spaces[pos-1] = icon;    makeBoard();}bool game::checkState(){    for (unsigned int i = 0; i < 8; i++){        if (spaces[winningCombos[i][0]-1] == spaces[winningCombos[i][1]-1] && spaces[winningCombos[i][1]-1] == spaces[winningCombos[i][2]-1]){            winner = currentPlayer;            return true;        }else if(numTurns == 9){            winner = -1;            return true;        }    }    return false;}void game::play(){    // Setup functions and variables    setup();    makeBoard();    int pos;    bool validMove;    do {        validMove = false;        do {            cout << endl;            // Prompt player for their move            if (currentPlayer == 1){                cout << Player 1 choose a position. << endl;            }else{                cout << Player 2 choose a position << endl;            }            cin >> pos;            // Check if the move is valid            if (spaces[pos-1] == 88 || spaces[pos-1] == 79){                cout << This position is already filled! << endl;            }else if(pos <= 9 && pos >= 1){                validMove = true;            }else{                cout << That is not a valid position! Valid positions are 1-9. << endl;            }        } while(!validMove);        // Update the game board according to the current player        if (currentPlayer == 1){            updateSpaces(88,pos);            currentPlayer = 2;        }else{            updateSpaces(79,pos);            currentPlayer = 1;        }        numTurns++;    } while(!checkState());    // Print the winner of the game    if (winner == 2){        cout << Player 1 Wins! << endl;        wins1++;    }else if (winner == 1){        cout << Player 2 Wins! << endl;        wins2++;    }else{        cout << The game ended in a draw... << endl;    }    // Update Scores    cout << endl;    cout << ----------------------------- << endl;    cout << Player 1 Wins | Player 2 Wins << endl;    cout << ----------------------------- << endl;    cout <<         << wins1 <<       |         << wins2 << endl;    cout << ----------------------------- << endl;}int main(){    // Main Menu Variables    bool quit = false;    int choice;    game currentGame;    do {        // Main Menu        cout << 1. Play Game << endl;        cout << 2. Quit << endl;        cout << endl;        cin >> choice;        switch (choice){            case 1: currentGame.play();                break;            case 2: quit = true;                wins1 = 0;                wins2 = 0;                break;            default: cout << Invalid choice. Please choose from the list below. << endl;        }        cout << endl;    } while (!quit);    return 0;}
Creating Tic-Tac-Toe in C++ with classes
c++;object oriented;console;tic tac toe