Click here to Skip to main content
14,212,087 members
Rate this:
Please Sign up or sign in to vote.
void Ai()
{
//-------------------------------------------------------
    if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == '7')
         matrix[2][0] = 'O';
    else if (matrix[2][1] == 'X' && matrix[2][2] == 'X' && matrix[2][0] == '7')
        matrix[2][0] = 'O';
    else if (matrix[0][2] == 'X' && matrix[1][1] == 'X' && matrix[2][0] == '7')
        matrix[2][0] = 'O';

    if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == '9')
        matrix[2][2] = 'O';
    else if (matrix[2][1] == 'X' && matrix[2][0] == 'X' && matrix[2][2] == '9')
        matrix[2][2] = 'O';
    else if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == '9')
        matrix[2][2] = 'O';

    if (matrix[1][0] == 'X' && matrix[2][0] == 'X' && matrix[0][0] == '1')
        matrix[0][0] = 'O';
    else if (matrix[0][1] == 'X' && matrix[0][2] == 'X' && matrix[0][0] == '1')
        matrix[0][0] = 'O';
    else if (matrix[1][1] == 'X' && matrix[2][2] == 'X' && matrix[0][0] == '1')
        matrix[0][0] = 'O';

    if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == '3')
        matrix[0][2] = 'O';
    else if (matrix[1][2] == 'X' && matrix[2][2] == 'X' && matrix[0][2] == '3')
        matrix[0][2] = 'O';
    else if (matrix[1][1] == 'X' && matrix[2][0] == 'X' && matrix[0][2] == '3')
        matrix[0][2] = 'O';

    if (matrix[0][0] == 'X' && matrix[2][0] == 'X' && matrix[1][0] == '4')
        matrix[1][0] = 'O';
    else if (matrix[0][1] == 'X' && matrix[2][1] == 'X' && matrix[1][1] == '5')
        matrix[1][1] = 'O';
    else if (matrix[0][2] == 'X' && matrix[2][2] == 'X' && matrix[1][2] == '6')
        matrix[1][2] = 'O';
    else if (matrix[0][0] == 'X' && matrix[0][2] == 'X' && matrix[0][1] == '2')
        matrix[0][1] = 'O';
    else if (matrix[1][0] == 'X' && matrix[1][2] == 'X' && matrix[1][1] == '5')
        matrix[1][1] = 'O';
    else if (matrix[2][0] == 'X' && matrix[2][2] == 'X' && matrix[2][1] == '8')
         matrix[2][1] = 'O';
    else if (matrix[2][0] == 'X' && matrix[0][2] == 'X' && matrix[1][1] == '5')
         matrix[1][1] = 'O';
    else if (matrix[0][0] == 'X' && matrix[2][2] == 'X' && matrix[1][1] == '5')
         matrix[1][1] = 'O';
//----------------------------------------------------------
    if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == '7')
         matrix[2][0] = 'O';
    else if (matrix[2][1] == 'O' && matrix[2][2] == 'O' && matrix[2][0] == '7')
        matrix[2][0] = 'O';
    else if (matrix[0][2] == 'O' && matrix[1][1] == 'O' && matrix[2][0] == '7')
        matrix[2][0] = 'O';

    if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == '9')
        matrix[2][2] = 'O';
    else if (matrix[2][1] == 'O' && matrix[2][0] == 'O' && matrix[2][2] == '9')
        matrix[2][2] = 'O';
    else if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == '9')
        matrix[2][2] = 'O';

    if (matrix[1][0] == 'O' && matrix[2][0] == 'O' && matrix[0][0] == '1')
        matrix[0][0] = 'O';
    else if (matrix[0][1] == 'O' && matrix[0][2] == 'O' && matrix[0][0] == '1')
        matrix[0][0] = 'O';
    else if (matrix[1][1] == 'O' && matrix[2][2] == 'O' && matrix[0][0] == '1')
        matrix[0][0] = 'O';

    if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == '3')
        matrix[0][2] = 'O';
    else if (matrix[1][2] == 'O' && matrix[2][2] == 'O' && matrix[0][2] == '3')
        matrix[0][2] = 'O';
    else if (matrix[1][1] == 'O' && matrix[2][0] == 'O' && matrix[0][2] == '3')
        matrix[0][2] = 'O';

    if (matrix[0][0] == 'O' && matrix[2][0] == 'O' && matrix[1][0] == '4')
        matrix[1][0] = 'O';
    else if (matrix[0][1] == 'O' && matrix[2][1] == 'O' && matrix[1][1] == '5')
        matrix[1][1] = 'O';
    else if (matrix[0][2] == 'O' && matrix[2][2] == 'O' && matrix[1][2] == '6')
        matrix[1][2] = 'O';
    else if (matrix[0][0] == 'O' && matrix[0][2] == 'O' && matrix[0][1] == '2')
        matrix[0][1] = 'O';
    else if (matrix[1][0] == 'O' && matrix[1][2] == 'O' && matrix[1][1] == '5')
        matrix[1][1] = 'O';
    else if (matrix[2][0] == 'O' && matrix[2][2] == 'O' && matrix[2][1] == '8')
         matrix[2][1] = 'O';
    else if (matrix[2][0] == 'O' && matrix[0][2] == 'O' && matrix[1][1] == '5')
         matrix[1][1] = 'O';
    else if (matrix[0][0] == 'O' && matrix[2][2] == 'O' && matrix[1][1] == '5')
         matrix[1][1] = 'O';

//---------------------------------------------------------

    else if (a == 1)
    {
        if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 2)
    {
        if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 3)
    {
        if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
        if (a == 4)
    {
        if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 5)
    {
        if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 6)
    {
        if (matrix[1][1] == '5')
            matrix[1][1] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 7)
    {
        if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 8)
    {
        if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 9)
    {
        if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
    }
}


I am 14 years old and have been learning C++ as my first language for about 2-3 months. This is my TicTacToe v2BETA_AI AI script, written in c++. Player 1 is 'X', and the AI is 'O'. Player 1 is always first. While testing the game, it runs the code blocking and completion code fine, but then makes more moves. This does not always affect the game play, as the AI will already be able to make a move to win, but then more spaces are replaced with 'O'.


For Example:
(How lay out is for game)

1 2 3

4 5 6

7 8 9

Enter number in the field you want: 5

1 2 3

O X 6

7 8 9

Enter number in the field you want: 2

O X 3

O X 6

7 8 9

Enter number in the field you want: 6

O X (O) Here this unnecessary O was added and sometimes causes problems.

O X X

O 8 9

Computer wins!!!



Whole Source Code
#include <iostream>
#include <cstdlib>
using namespace std;

char matrix[3][3] = { '1','2','3','4','5','6','7','8','9'};
char player = 'X';
int n;
int a;
int AI;
string r;

void draw()
{
    cout << endl << endl << "Player 1 = X" << endl;
    cout << "Computer = O";
    cout << endl << endl << endl;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << matrix [i][j] << " " << " ";
        }
        cout << endl << endl;
    }
}

void input()
{
    cout << endl << endl << "Press the number you want in the field: ";
    cin >> a;

    if (a == 1001)
    {
        exit(0);
    }
    else if (a == 1)
    {
        if (matrix[0][0] == '1')
            matrix[0][0] = player;
        else
        {
            cout << "Field is taken. Try again.";
            input();
        }
    }
    else if (a == 2)
    {
        if (matrix[0][1] == '2')
            matrix[0][1] = player;
        else
        {
            cout << "Field is taken. Try again.";
            input();
        }
    }
    else if (a == 3)
    {
        if (matrix[0][2] == '3')
            matrix[0][2] = player;
        else
        {
            cout << "Field is taken. Try again.";
            input();
        }
    }

     else if (a == 4)
    {
        if (matrix[1][0] == '4')
            matrix[1][0] = player;
        else
        {
            cout << "Field is taken. Try again.";
            input();
        }
    }
    else if (a == 5)
    {
        if (matrix[1][1] == '5')
            matrix[1][1] = player;
        else
        {
            cout << "Field is taken. Try again.";
            input();
        }
    }
    else if (a == 6)
    {
        if (matrix[1][2] == '6')
            matrix[1][2] = player;
        else
        {
            cout << "Field is taken. Try again.";
            input();
        }
    }

    else if (a == 7)
    {
        if (matrix[2][0] == '7')
            matrix[2][0] = player;
        else
        {
            cout << "Field is taken. Try again.";
            input();
        }
    }
    else if (a == 8)
    {
        if (matrix[2][1] == '8')
            matrix[2][1] = player;
        else
        {
            cout << "Field is taken. Try again.";
            input();
        }
    }
    else if (a == 9)
    {
        if (matrix[2][2] == '9')
            matrix[2][2] = player;
        else
        {
            cout << "Field is taken. Try again.";
            input();
        }
    }
    if (a > 9 || a < 1)
    {
        cout << endl << "Invalid Choice. Try again.";
        input();
    }
}

void Ai()
{
//-------------------------------------------------------
    if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == '7')
         matrix[2][0] = 'O';
    else if (matrix[2][1] == 'X' && matrix[2][2] == 'X' && matrix[2][0] == '7')
        matrix[2][0] = 'O';
    else if (matrix[0][2] == 'X' && matrix[1][1] == 'X' && matrix[2][0] == '7')
        matrix[2][0] = 'O';

    if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == '9')
        matrix[2][2] = 'O';
    else if (matrix[2][1] == 'X' && matrix[2][0] == 'X' && matrix[2][2] == '9')
        matrix[2][2] = 'O';
    else if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == '9')
        matrix[2][2] = 'O';

    if (matrix[1][0] == 'X' && matrix[2][0] == 'X' && matrix[0][0] == '1')
        matrix[0][0] = 'O';
    else if (matrix[0][1] == 'X' && matrix[0][2] == 'X' && matrix[0][0] == '1')
        matrix[0][0] = 'O';
    else if (matrix[1][1] == 'X' && matrix[2][2] == 'X' && matrix[0][0] == '1')
        matrix[0][0] = 'O';

    if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == '3')
        matrix[0][2] = 'O';
    else if (matrix[1][2] == 'X' && matrix[2][2] == 'X' && matrix[0][2] == '3')
        matrix[0][2] = 'O';
    else if (matrix[1][1] == 'X' && matrix[2][0] == 'X' && matrix[0][2] == '3')
        matrix[0][2] = 'O';

    if (matrix[0][0] == 'X' && matrix[2][0] == 'X' && matrix[1][0] == '4')
        matrix[1][0] = 'O';
    else if (matrix[0][1] == 'X' && matrix[2][1] == 'X' && matrix[1][1] == '5')
        matrix[1][1] = 'O';
    else if (matrix[0][2] == 'X' && matrix[2][2] == 'X' && matrix[1][2] == '6')
        matrix[1][2] = 'O';
    else if (matrix[0][0] == 'X' && matrix[0][2] == 'X' && matrix[0][1] == '2')
        matrix[0][1] = 'O';
    else if (matrix[1][0] == 'X' && matrix[1][2] == 'X' && matrix[1][1] == '5')
        matrix[1][1] = 'O';
    else if (matrix[2][0] == 'X' && matrix[2][2] == 'X' && matrix[2][1] == '8')
         matrix[2][1] = 'O';
    else if (matrix[2][0] == 'X' && matrix[0][2] == 'X' && matrix[1][1] == '5')
         matrix[1][1] = 'O';
    else if (matrix[0][0] == 'X' && matrix[2][2] == 'X' && matrix[1][1] == '5')
         matrix[1][1] = 'O';
//----------------------------------------------------------
    if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == '7')
         matrix[2][0] = 'O';
    else if (matrix[2][1] == 'O' && matrix[2][2] == 'O' && matrix[2][0] == '7')
        matrix[2][0] = 'O';
    else if (matrix[0][2] == 'O' && matrix[1][1] == 'O' && matrix[2][0] == '7')
        matrix[2][0] = 'O';

    if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == '9')
        matrix[2][2] = 'O';
    else if (matrix[2][1] == 'O' && matrix[2][0] == 'O' && matrix[2][2] == '9')
        matrix[2][2] = 'O';
    else if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == '9')
        matrix[2][2] = 'O';

    if (matrix[1][0] == 'O' && matrix[2][0] == 'O' && matrix[0][0] == '1')
        matrix[0][0] = 'O';
    else if (matrix[0][1] == 'O' && matrix[0][2] == 'O' && matrix[0][0] == '1')
        matrix[0][0] = 'O';
    else if (matrix[1][1] == 'O' && matrix[2][2] == 'O' && matrix[0][0] == '1')
        matrix[0][0] = 'O';

    if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == '3')
        matrix[0][2] = 'O';
    else if (matrix[1][2] == 'O' && matrix[2][2] == 'O' && matrix[0][2] == '3')
        matrix[0][2] = 'O';
    else if (matrix[1][1] == 'O' && matrix[2][0] == 'O' && matrix[0][2] == '3')
        matrix[0][2] = 'O';

    if (matrix[0][0] == 'O' && matrix[2][0] == 'O' && matrix[1][0] == '4')
        matrix[1][0] = 'O';
    else if (matrix[0][1] == 'O' && matrix[2][1] == 'O' && matrix[1][1] == '5')
        matrix[1][1] = 'O';
    else if (matrix[0][2] == 'O' && matrix[2][2] == 'O' && matrix[1][2] == '6')
        matrix[1][2] = 'O';
    else if (matrix[0][0] == 'O' && matrix[0][2] == 'O' && matrix[0][1] == '2')
        matrix[0][1] = 'O';
    else if (matrix[1][0] == 'O' && matrix[1][2] == 'O' && matrix[1][1] == '5')
        matrix[1][1] = 'O';
    else if (matrix[2][0] == 'O' && matrix[2][2] == 'O' && matrix[2][1] == '8')
         matrix[2][1] = 'O';
    else if (matrix[2][0] == 'O' && matrix[0][2] == 'O' && matrix[1][1] == '5')
         matrix[1][1] = 'O';
    else if (matrix[0][0] == 'O' && matrix[2][2] == 'O' && matrix[1][1] == '5')
         matrix[1][1] = 'O';

//---------------------------------------------------------

    else if (a == 1)
    {
        if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 2)
    {
        if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 3)
    {
        if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
        if (a == 4)
    {
        if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 5)
    {
        if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 6)
    {
        if (matrix[1][1] == '5')
            matrix[1][1] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 7)
    {
        if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 8)
    {
        if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
    }
    else if (a == 9)
    {
        if (matrix[2][1] == '8')
            matrix[2][1] = 'O';
        else if (matrix[1][1] == '5')
            matrix[1][1] == 'O';
        else if (matrix[1][2] == '6')
            matrix[1][2] = 'O';
        else if (matrix[2][2] == '9')
            matrix[2][2] = 'O';
        else if (matrix[0][0] == '1')
            matrix[0][0] = 'O';
        else if (matrix[0][1] == '2')
            matrix[0][1] = 'O';
        else if (matrix[2][0] == '7')
            matrix[2][0] = 'O';
        else if (matrix[1][0] == '4')
            matrix[1][0] = 'O';
        else if (matrix[0][2] == '3')
            matrix[0][2] = 'O';
    }
}

char win()
{
    if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')
        return 'X';
    if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')
        return 'X';
    if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')
        return 'X';
    if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
        return 'X';
    if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
        return 'X';
    if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
        return 'X';
    if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')
        return 'X';
    if (matrix[0][2] == 'X' && matrix[1][1] == 'X' && matrix[2][0] == 'X')
        return 'X';

    if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')
        return 'O';
    if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')
        return 'O';
    if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')
        return 'O';
    if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
        return 'O';
    if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
        return 'O';
    if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
        return 'O';
    if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')
        return 'O';
    if (matrix[0][2] == 'O' && matrix[1][1] == 'O' && matrix[2][0] == 'O')
        return 'O';

    return '/';
}

int main()
{
    int n = 0;
    cout << "Tic Tac Toe v2 BETA" << endl;
    cout << "-------------------" << endl;
    system("TITLE Tic Tac Toe");
    system("COLOR a");
    draw();
    while (1)
    {
        n++;

        input();

        Ai();

        system("cls");

        draw();

        if (win() == 'X')
        {
            cout << "Player 1 Wins!!!" << endl << endl;
            break;
        }
        if (win() == 'O')
        {
            cout << "Computer Wins!!!" << endl << endl;
            break;
        }
        else if (win() == '/' && n == 5)
        {
            cout << "It's a Draw!!!" << endl << endl;
            break;
        }
    }
    system("PAUSE");
    return 0;
}


I tested the game more and it actually does interfere quite a bit. I played 3 moves and the rest of the field was covered with O's.

This also may help:

0,0 (1)| 0,1 (2)| 0,2 (3)
-------------------------
1,0 (4)| 1,1 (5)| 1,2 (6)
-------------------------
2,0 (7)| 2,1 (8)| 2,2 (9)

What I have tried:

I have tried to change all the if statements to switch statements, but nothing.
Posted
Updated 28-Dec-16 9:36am
v3
Comments
Philippe Mori 28-Dec-16 16:49pm
   
You should refactor your code... For example, the win function has 2 parts that are identical except they check for X or O. You can make a function that would take a parameter for the character (X or O) and returns true or false. Also code for testing each row is similar so you can have a function to test specified row. You can do the same for columns... Afterwards, you might do similar refactoring for the AI and then use loops. Once you have improved code, it would be easier to fix it.
Rate this:
Please Sign up or sign in to vote.

Solution 1

In the AI method, you're allowing multiple choices since there are more than one if blocks. So any of the if blocks can evaluate to true, setting the additional 'O'.

You have potentially 10 points due to the 10 if blocks, most likely they all would not evaluate as true (this includes the else if parts) but always the chance.

You should
return
whenever the value is set to 'O' or 'X' in the AI method to force the single action.

Ex.

void Ai()
{
//-------------------------------------------------------
    if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == '7') {
         matrix[2][0] = 'O';
         return;
    }
    else if (matrix[2][1] == 'X' && matrix[2][2] == 'X' && matrix[2][0] == '7') {
        matrix[2][0] = 'O';
        return;
    }
    else if (matrix[0][2] == 'X' && matrix[1][1] == 'X' && matrix[2][0] == '7') {
        matrix[2][0] = 'O';
        return;
    }
   
Comments
TheTrappedMonkey 28-Dec-16 12:25pm
   
I think this works, except you would have to change void to char. Now you can return it to X or O. Thanks
Ramza360 30-Dec-16 13:43pm
   
Thats a good idea as well. Congrats on learning to code. It is so much fun!
Rate this:
Please Sign up or sign in to vote.

Solution 2

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 28 Dec 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100