//Diego Martinez					CSC5				   Chapter 7, P.492, #18
/*******************************************************************************
*  PLAY TIC-TAC-TOE
* ______________________________________________________________________________
* This program allows two players to play a game of tic-tac-toe, taking turns
* placing Xs and Os. It determines a winner or if it ends in a tie.
* 
* Computation is based on the Formula:
*	Row match
*	Column match
*	Diagonal Match
*______________________________________________________________________________
* INPUT
*	Row and column positions (0-2)
*	
* OUTPUT
*	Invalid moves
*	Winning Player
*	Tie game messsage
*	
*******************************************************************************/
#include <iostream>
using namespace std;

const int SIZE = 3;

// ===== FUNCTION PROTOTYPES =====
void initializeBoard(char board[SIZE][SIZE]);
void displayBoard(char board[SIZE][SIZE]);
bool placeMove(char board[SIZE][SIZE], int row, int col, char player);
bool checkWin(char board[SIZE][SIZE], char player);
bool checkTie(char board[SIZE][SIZE]);

// ===== MAIN FUNCTION =====
int main()
{
    // ===== DATA DICTIONARY =====
    char board[SIZE][SIZE];   // game board
    int row, col;             // player input
    char currentPlayer = 'X'; // Player 1 starts
    bool gameOver = false;

    initializeBoard(board);

    // ===== GAME LOOP =====
    while (!gameOver)
    {
        displayBoard(board);

        cout << "\nPlayer " << currentPlayer << " turn\n";
        cout << "Enter row and column (0-2): ";
        cin >> row >> col;

        if (!placeMove(board, row, col, currentPlayer))
        {
            cout << "Invalid move. Try again.\n";
            continue;
        }

        if (checkWin(board, currentPlayer))
        {
            displayBoard(board);
            cout << "\nPlayer " << currentPlayer << " wins!\n";
            gameOver = true;
        }
        else if (checkTie(board))
        {
            displayBoard(board);
            cout << "\nIt's a tie!\n";
            gameOver = true;
        }
        else
        {
            // switch player
            if (currentPlayer == 'X')
                currentPlayer = 'O';
            else
                currentPlayer = 'X';
        }
    }

    return 0;
}

// ===== FUNCTION DEFINITIONS =====

// Initialize board with '*'
void initializeBoard(char board[SIZE][SIZE])
{
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            board[i][j] = '*';
        }
    }
}

// Display board
void displayBoard(char board[SIZE][SIZE])
{
    cout << "\nTic-Tac-Toe Board:\n";
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            cout << board[i][j] << " ";
        }
        cout << endl;
    }
}

// Place move if valid
bool placeMove(char board[SIZE][SIZE], int row, int col, char player)
{
    if (row < 0 || row >= SIZE || col < 0 || col >= SIZE)
        return false;

    if (board[row][col] != '*')
        return false;

    board[row][col] = player;
    return true;
}

// Check win condition
bool checkWin(char board[SIZE][SIZE], char player)
{
    // rows & columns
    for (int i = 0; i < SIZE; i++)
    {
        if (board[i][0] == player && board[i][1] == player && board[i][2] == player)
            return true;

        if (board[0][i] == player && board[1][i] == player && board[2][i] == player)
            return true;
    }

    // diagonals
    if (board[0][0] == player && board[1][1] == player && board[2][2] == player)
        return true;

    if (board[0][2] == player && board[1][1] == player && board[2][0] == player)
        return true;

    return false;
}

// Check tie condition
bool checkTie(char board[SIZE][SIZE])
{
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            if (board[i][j] == '*')
                return false;
        }
    }
    return true;
}