Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.8 kB
4
Indexable
Never
#include <iostream>

void initialization();
void initializationBoard(int row);
void RestBoard(char** matrix , int row);
void PrintBoard(char** matrix , int row);
void WhoPlayer(char** matrix , int row);
bool Valid_Index(char** matrix , int row , char player , int ValueRow , int ValueCol);
void CheckWhoPlayerWin(char** matrix , int row ,int player);

int main()
{
    initialization();
}
void initialization()
{
    int row = 0;
    std::cout<<"Enter size row number for size matrix -> "<<std::endl;
    std::cin>>row;
    initializationBoard(row);
}
void initializationBoard(int row)
{
    char** matrix = NULL;
    matrix = new char*[row];
    for (int i = 0; i < row; ++i)
    {
        matrix[i] = new char[row]+1;
    }
    RestBoard(matrix, row);
}
void RestBoard(char** matrix , int row)
{
    for (int i = 0; i < row; ++i)
    {
        for (int j = 0; j < row; ++j)
        {
            matrix[i][j] = '_';
        }
    }
    PrintBoard(matrix,row);
}
void PrintBoard(char** matrix , int row)
{
    std::cout<<std::endl;
    std::cout<<std::endl;
    for (int i = 0; i < row; ++i)
    {
        for (int j = 0; j < row; ++j)
        {
            std::cout<<"\t"<<matrix[i][j]<<"\t";
        }
        std::cout<<std::endl;
        std::cout<<std::endl;
    }
    std::cout<<std::endl;
    std::cout<<std::endl;
    WhoPlayer(matrix, row);
}
void WhoPlayer(char** matrix , int row)
{
    enum data {Player1 = 'F' , Player2 = 'S' , EXIT = 'E'};
    char mOption = 0;
    int ValueRow = 0;
    int ValueCol = 0;
    do
    {
        std::cout<<"Enter your choise for player1 press -> F for player2 press -> S for exit press -> E "<<std::endl;
        std::cin>>mOption;
    } while (mOption != 'F' && mOption != 'S' && mOption != 'E');
    
    switch (mOption)
    {
        case 'F':
        {
            bool flag = false;
            do
            {
                std::cout<<"Enter value row end value col to put your coin -> "<<std::endl;
                std::cin>>ValueRow>>ValueCol;
                flag = Valid_Index(matrix,row,mOption,ValueRow,ValueCol);
                
            } while (!flag);
            matrix[ValueRow][ValueCol] = 'F';
            CheckWhoPlayerWin(matrix,row,mOption);
            PrintBoard(matrix, row);
            break;
        }
        case 'S':
        {
            bool flag = false;
            do
            {
                std::cout<<"Enter value row end value col to put your coin -> "<<std::endl;
                std::cin>>ValueRow>>ValueCol;
                flag = Valid_Index(matrix,row,mOption,ValueRow,ValueCol);
                
            } while (!flag);
            matrix[ValueRow][ValueCol] = 'F';
            CheckWhoPlayerWin(matrix,row,mOption);
            PrintBoard(matrix, row);
            break;
        }
        case 'E':
        {
            std::cout<<"See You Soon -> Good Bye"<<std::endl;
            exit(0);
        }
            
            
        default:
        {
            std::cout<<"Your Choise Not Here -> Try Again "<<std::endl;
            break;
        }
    }
}
bool Valid_Index(char** matrix , int row , char player , int ValueRow , int ValueCol)
{
    if (player == 'F' || player == 'S')
    {
        if ((ValueRow < 0) || (ValueRow >= row) || (ValueCol < 0) || (ValueCol >= row))
        {
            return false;
        }
        if ((matrix[ValueRow][ValueCol] != '_'))
        {
            return false;
        }
    }
    return true;
}
void CheckWhoPlayerWin(char** matrix , int row ,int player)
{
    for (int i = 0; i < row; ++i)
    {
        for (int j = 0; j < row; ++j)
        {
            if (player == 'F')
            {
                if((matrix[0][0] == matrix[1][0]) && (matrix[1][0] == matrix[2][0]) && (matrix[2][0] == matrix[3][0]))
                {
                    std::cout<<"Great -> The Win Is Player1"<<std::endl;
                }
                else
                    if((matrix[0][j] == matrix[0][1]) && (matrix[0][1] == matrix[0][2]) && (matrix[0][2] == matrix[0][3]))
                    {
                        std::cout<<"Great -> The Win Is Player1"<<std::endl;
                    }
            }
            else
                continue;
            
            if (player == 'S')
            {
                if((matrix[0][0] == matrix[1][0]) && (matrix[1][0] == matrix[2][0]) && (matrix[2][0] == matrix[3][0]))
                {
                    std::cout<<"Great -> The Win Is Player2"<<std::endl;
                }
                else
                    if((matrix[0][0] == matrix[0][1]) && (matrix[0][1] == matrix[0][2]) && (matrix[0][2] == matrix[0][3]))
                    {
                        std::cout<<"Great -> The Win Is Player2"<<std::endl;
                    }
            }
            else
                continue;
        }
    }
}