Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.9 kB
1
Indexable
Never
void initialization3();
void initializationboard(int row);
void resetboard(char** matrix , int row);
void PrintBoard(char** matrix , int row);
void WhoPlayer(char**& matrix , int row);
bool cheakboard(char** matrix , int row ,char player , int NumberRow , int NumberCol);
void CheakWin(char** matrix , int row , int player);

int main()
{
    initialization3();
    return 0;

}

void initialization3()
{
    int row = 0;
    std::cout<<"Enter row for size to 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];
    }
    resetboard(matrix, row);
}

void resetboard(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<<"This is your board -> "<<std::endl;
    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)
{
    int NumberRow = 0;
    int NumberCol = 0;
    enum mOptions { player1 = 'F', player2 = 'S', EXIT = 'E' };
    char data = 0;
    do {
        std::cout << "Enter your choice for player1 press -> F for player2 press -> S for exit press -> E" << std::endl;
        std::cin >> data;
    } while (data != 'F' && data != 'S' && data != 'E');
    switch (data)
    {
    case 'F':
        {
            bool inputValid = false;
            do
            {
                std::cout << "Enter number row and number col for input coin -> " << std::endl;
                std::cin >> NumberRow >> NumberCol;
                inputValid = cheakboard(matrix, row, data, NumberRow, NumberCol);
            } while (!inputValid);
            matrix[NumberRow][NumberCol] = 'F';
            PrintBoard(matrix, row);
            CheakWin(matrix, row, data);
            break;
        }

    case 'S':
        {
            bool inputValid = false;
            do
            {
                std::cout << "Enter number row and number col for input coin -> " << std::endl;
                std::cin >> NumberRow >> NumberCol;
                inputValid = cheakboard(matrix, row, data, NumberRow, NumberCol);
            } while (!inputValid);
            matrix[NumberRow][NumberCol] = 'S';
            PrintBoard(matrix, row);
            CheakWin(matrix, row, data);
            break;
        }
        case 'E':
        {
            std::cout<<"Hope You Enjoyed -> Good Bay"<<std::endl;
            exit(0);
            break;
        }

    default:
        {
            std::cout<<"The Choise Not Find -> Try Again"<<std::endl;
            break;
        }
        
    }
}
bool cheakboard(char** matrix , int row ,char player , int NumberRow , int NumberCol)
{
    if (player == 'F' || player == 'S')
    {
        if ((NumberRow < 0) || (NumberRow >= row) || (NumberCol < 0) || (NumberCol >= row))
        {
            return false;
        }
        if (matrix[NumberRow][NumberCol] != '_' )
        {
            return false;
        }
    }
    return true;
}
void CheakWin(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<<"The Player1 Win -> Good Luck"<<std::endl;
                    break;
                }
                if (matrix[0][0] == matrix[0][1] && matrix[0][1] == matrix[0][2] && matrix[0][2] == matrix[0][3])
                {
                    std::cout<<"The Player1 Win -> Good Luck"<<std::endl;
                    break;

                }
            }
            if (player == 'S')
            {
                    if (matrix[0][0] == matrix[1][0] && matrix[1][0] == matrix[2][0] && matrix[2][0] == matrix[3][0])
                    {
                        std::cout<<"The Player2 Win -> Good Luck"<<std::endl;
                        break;
                    }
                    if (matrix[0][0] == matrix[0][1] && matrix[0][1] == matrix[0][2] && matrix[0][2] == matrix[0][3])
                    {
                        std::cout<<"The Player2 Win -> Good Luck"<<std::endl;
                        break;

                    }
            }
            else
                continue;
        }
    }
}