Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.4 kB
4
Indexable
Never
#include <iostream>
using namespace std;

char matrix[3][3];
void initmatrix();
void print_matrix();
void get_player_matrix();
void get_computer_move();
char cheack();

int main()
{
    char XO;
    std::cout<<"This is : X -> O Game\n";
    std::cout<<"You will playing with computer\n";
    XO = '_';
    initmatrix();
    do
    {
        print_matrix();
        get_player_matrix();
        XO = cheack();
    }
    while (XO == '_');
    {
        if (XO == 'X')
        {
            std::cout<<"You Won!\n";
        }
        else
            std::cout<<"I won!\n";
        print_matrix();
    }
    return 0;
}
void initmatrix()
{
    for (int i = 0; i < 3 ; ++ i)
    {
        for (int j = 0; j < 3; ++ j)
        {
            std::cout<<(matrix[i][j] = '_');
        }
    }
}
void print_matrix()
{
    for (int i = 0; i < 3 ; ++ i)
    {
        cout << "%c | %c | %c" << endl;
        cout << matrix[0][0] << " | " << matrix[0][1] << " | " << matrix[0][2] << endl;

        if(i != 2)
        {
            cout<<"\n---|---|---\n";
        }

    }
    cout<<endl;
}
void get_player_matrix()
{
    int x;
    int y;
    cout<<"Enter your Cordiante for your mone"<<endl;
    cin>>x>>y;
    x--;
    y--;
    if (matrix[x][y] != '_')
    {
        cout<<"Invalid move -> Plasae Try Again:"<<endl;
        get_player_matrix();
    }
    else
        matrix[x][y] = x;

}
void get_computer_move()
{
    for (int i = 0; i < 3; ++ i)
    {
        for (int j = 0; j < 3; ++ j)
        {
            if (matrix[i][j] != '_')
            {
                break;
            }
            if ((i * j) == 9)
            {
                cout<<"Draw"<<endl;
                exit(0);
            }
            else
                matrix[i][j] = 'O';
        }
    }
}
char cheack()
{
    for (int i = 0; i < 3; ++ i)
    {
        if (matrix[i][0] == matrix[i][1] && matrix[i][0] == matrix[i][2])
        {
            return matrix[0][i];
        }
        if (matrix[0][i] == matrix[1][i] && matrix[0][i] == matrix[2][i])
        {
            return matrix[0][i];
        }
        if (matrix[0][0] == matrix[1][1] && matrix[0][0] == matrix[2][2])
        {
            return matrix[0][0];
        }
        if (matrix[0][2] == matrix[1][1] && matrix[0][2] == matrix[2][0])
        {
            return matrix[0][2];
        }
    }
    return '_';
}