#include <iostream>
#include <vector>
using namespace std;
const int ROWS = 3;
const int COLS = 3;
void print_board(vector<vector<char>> xo_board) {
    for(int i = 0; i < ROWS; i++) {
        for(int j = 0; j < COLS; j++) {
            cout << xo_board[i][j] << " ";
        }
        cout << endl;
    }
}
bool valid_move(int pi, int pj, vector<vector<char>> xo_board) {
    pi--;
    pj--;
    if(pi >= 0 and pi < ROWS and pj >= 0 and pj < COLS and xo_board[pi][pj] == '.') {
        return true;
    }
    return false;
}
void set_board(int pi, int pj, char current_player, vector<vector<char>> & xo_board) {
    xo_board[pi - 1][pj - 1] = current_player;
}
bool has_anyone_won(vector<vector<char>> xo_board, char current_player) {
    if(xo_board[0][0] == current_player and xo_board[1][1] == current_player and xo_board[2][2] == current_player) {
        return true;
    }
    if(xo_board[0][2] == current_player and xo_board[1][1] == current_player and xo_board[2][0] == current_player) {
        return true;
    }
    if(xo_board[0][0] == current_player and xo_board[0][1] == current_player and xo_board[0][2] == current_player) {
        return true;
    }
    if(xo_board[1][0] == current_player and xo_board[1][1] == current_player and xo_board[1][2] == current_player) {
        return true;
    }
    if(xo_board[2][0] == current_player and xo_board[2][1] == current_player and xo_board[2][2] == current_player) {
        return true;
    }
    if(xo_board[0][0] == current_player and xo_board[1][0] == current_player and xo_board[2][0] == current_player) {
        return true;
    }
    if(xo_board[0][1] == current_player and xo_board[1][1] == current_player and xo_board[2][1] == current_player) {
        return true;
    }
    return false;
}
bool is_draw(vector<vector<char>> xo_board) {
    for(int i = 0; i < ROWS; i++) {
        for(int j = 0; j < COLS; j++) {
            if(xo_board[i][j] == '.') {
                return false;
            }
        }
    }
    return true;
}
int main() {
    vector<vector<char> > xo_board(ROWS, vector<char>(COLS, '.'));
    char current_player = 'X';
    bool game_over = false;
    
    while(!game_over) {
        print_board(xo_board);
        cout << current_player << "'s turn!" << endl;
        cout << "Where do you want to place " << current_player  <<"?" << endl;
        
        int pi, pj;
        cin >> pi >> pj;
        
        while(!valid_move(pi, pj, xo_board)) {
            cout << "Not a valid move! Please play again!" << endl;
            cin >> pi >> pj;
        }
        set_board(pi, pj, current_player, xo_board);
        
        if(has_anyone_won(xo_board, current_player)) {
            print_board(xo_board);
            cout << current_player << " has won!" << endl << "CONGRATULATIONS!!!" << endl;
            break;
        }
        if(is_draw(xo_board)) {
            cout << "It's a draw! Try again!" << endl;
            break;
        }
        if(current_player == 'X') {
            current_player = 'O';
        }
        else {
            current_player = 'X';
        }
    }
    return 0;
}
