Saturday, 30 November 2013

TIC TAC TOE PROGRAM in C++

// TIC TAC TOE PROGRAM

#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <ctime>
using namespace std;

//Winning info
struct win
{
    int Mini; // That equals 10 * row value + column value
    int Maxi; // The maximum value that you need to collect from Mini to win
    int a;
};

char board[3][3];
int turns;
win victory[2][8];
// Vitory[2] cuz there are 2 players and [8] cuz there are 8 ways to win in the board
/* counter function calculates your chance to win for example  :
if I player at the first block wich is ( 0 rows , 0 column ) this will increase the first way to win by 0 from 3 and if you played ( 0 , 1 ) it will be 1 out of 33 and if you played the last one you'll be 1 out of 1 so you won ... get it ?
*/
void counter(int x , int a , int b)
{
    victory[x][a].Mini += 10*a + b ;
    victory[x][b+3].Mini += 10*a + b ;
    victory[x][a].a ++ ;
    victory[x][b+3].a ++ ;
    if( a == b )
    {
        victory[x][6].Mini += 10*a + b ;
        victory[x][6].a ++ ;
    }
    if( a + b == 2 )
    {
        victory[x][7].Mini += 10*a + b ;
        victory[x][7].a ++ ;
    }
}
// This function do check if your input is right or not and if it's right it replaces the space by X or O depends on the player
bool check(int a , int b , int x , bool n )
{
    if ( board[a][b]!= ' '  )
        return false;
    else
    {
        if ( n == true )
        {
            counter ( x , a , b );
            if(x==0)
                board[a][b]='X';
            else
                board[a][b]='O';
        }
        return true;
    }
}
// AI intelligence
void computer()
{
    int a = 0 ,  b = 0 , f ;
    // Attack , Defence
    for(int i = 1 ; i>=0 ; i--)

        for(int j=0 ; j<=7 ; j++)

            if( victory[i][j].a == 2 )

                if ( check( (( victory[i][j].Maxi - victory[i][j].Mini ) / 10 ) , ( ( victory[i][j].Maxi - victory[i][j].Mini ) % 10 ) , 1 , true ) == true )
                    return;

    // Play strategy :D

    while ( true )
    {
        f = rand() % 4 + 1;
        if ( f == 1 )
        {
            while(true)
            {
                a = rand() % 3 ;
                b = rand() % 3 ;
                if(check(a,b,1,true)==true)
                {
                    return;
                }

            }
        }
        else if ( check (0,0,1,false) == true || check(1,1,1,false) == true || check(2,2,1,false) == true || check(2,0,1,false) == true || check(0,2,1,false) == true )
        {
            while (true)
            {
                f = rand() % 5  ;
                if( f<=2 )
                {
                    b = a = f;
                    if(check(a,b,1,true)==true)
                    {
                        return;
                    }
                }
                else if ( f == 3)
                {
                    a=0;
                    b=2;
                    if(check(a,b,1,true)==true)
                    {
                        return;
                    }
                }
                else if ( f == 4 )
                {
                    a=2;
                    b=0;
                    if(check(a,b,1,true)==true)
                    {
                        return;
                    }
                }
            }
        }
    }

}
// cleans every thing
void cleaner()
{
    for (int i = 0; i <= 1; i++)
    {

        for (int j = 0; j <= 7; j++)
        {
            victory[i][j].Mini = 0;
            victory[i][j].a    = 0;
        }
    }
    turns = 0;
    for(int i=0; i<=2; i++)
        for(int j=0; j<=2; j++)
            board[i][j]=' ';
}
// to write table and actually i can't explain it :D
void Table()
{
    system("cls");
    cout<<endl<<"TIC TAC TOE :) "<<endl<<endl;

    for (int rows = 0; rows <= 12; rows++)
    {

        if( rows  % 4 == 0)
            cout<<string(25,'-')<<"       " <<string(25,'-');
        else
        {
            for(int j=0; j<=3; j++)
            {
                if( (rows==2 || rows==6 || rows==10) && j!=3 )
                    cout<<"|   "<< board [ (rows-2) /4  ] [j]<<"   ";
                else
                    cout<<"|       ";
            }
            for(int j=0; j<=3; j++)
            {
                if( (rows==2 || rows==6 || rows==10) && j!=3 )
                    cout<<"|   "<< ( (3*rows-2) + (4*j) ) /4 <<"   ";
                else
                    cout<<"|       ";
            }
        }

        cout<<endl;
    }
}
// asks you to enter where you want to play
void InputCheck(int ys , int wf)
{
    int a , b ;
    if ( ys == 1 || wf == 0  )
    {
        string tempp="";
        while(true)
        {
            cout<<endl<<"Where would you like to play :) ? "<<endl<<endl;

            while (true)
            {
                cin>>tempp;
                if(tempp != "1" && tempp != "2" && tempp !="3" && tempp != "4" && tempp != "5" && tempp != "6" && tempp != "7" && tempp != "8" && tempp != "9")
                    cout<<"              ERROR"<<endl;
                else
                    break;
            }

            int temp;
            temp = atoi(&tempp[0]);
            if( temp <= 3  )
            {
                a=0;
                b=temp-1;
            }
            else if ( temp >= 4 && temp <= 6  )
            {
                a = 1 ;
                b = temp - 4 ;
            }
            else if ( temp >= 7 && temp <= 9 )
            {
                a = 2 ;
                b = temp - 7 ;
            }
            if (check(a,b,wf,true) == true )
            {
                return;
            }
            else
                cout<<"Already Entered";
        }
    }
    computer();
    return;
}
// checks if someone won
void WhoWins()
{
    bool c=false;

    //Player 1
    for (int i  =0; i <= 7; i++)
    {

        if(victory[0][i].a==3)
        {
            cout<<"Player 1 WON ";
            c=true;
        }
    }

    //Player 2
    for (int i = 0; i <= 7; i++)
    {

        if(victory[1][i].a==3)
        {
            cout<<"Player 2 WON " ;
            c=true;
        }
    }

    //Noone won
    if ( turns == 9 && c == false )
    {
        cout<<"Nobody WON !! " ;
        c=true;
    }

    if(c == true)
    {
        getch();
        cleaner();
        Table();
        return;
    }

}

int main()
{
    srand( time( NULL ) );
    string temp = " ";
    int yc; //Your Choice
    int wf; // Who First

    cout<<endl<<"      1 - Multiplayer "<<endl<<"      2 - Single Player " <<endl<<endl<<"      " ;

    cin >> temp;
    while (temp != "1" && temp != "2" )
    {
        cout<<"          ERROR "<<endl<<"      ";
        cin >> temp;
    }
    yc=atoi(&temp[0]);
    temp = " ";
    system("cls");

    cout<<endl<<"      1 - Player 1 first 'X' "<<endl<<"      2 - Player 2 first (or computer) 'O'"<<endl<<endl<<"      ";

    cin >> temp;
    while (temp != "1" && temp != "2" )
    {
        cout<<"          ERROR "<<endl<<"      ";
        cin >> temp;
    }

    wf=atoi(&temp[0]) - 1;

    for (int i = 0; i <= 1; i++)
    {
        victory[i][0].Maxi = 3; // first way to win play (0.0)(0,1)(0,2)
        victory[i][1].Maxi = 33; // second way to win play (1.0)(1.1)(1.3)
        victory[i][2].Maxi = 63; // and so on
        victory[i][3].Maxi = 30;
        victory[i][4].Maxi = 33;
        victory[i][5].Maxi = 36;
        victory[i][6].Maxi = 33;
        victory[i][7].Maxi = 33;
    }
    cleaner();
    for (turns = 0 ;; turns ++)
    {
        Table();
        WhoWins();
        InputCheck(yc , wf);
        wf=1-wf;
    }
}

#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <ctime>
using namespace std;

//Winning info
struct win
{
    int Mini; // That equals 10 * row value + column value
    int Maxi; // The maximum value that you need to collect from Mini to win
    int a;
};

char board[3][3];
int turns;
win victory[2][8];
// Vitory[2] cuz there are 2 players and [8] cuz there are 8 ways to win in the board
/* counter function calculates your chance to win for example  :
if I player at the first block wich is ( 0 rows , 0 column ) this will increase the first way to win by 0 from 3 and if you played ( 0 , 1 ) it will be 1 out of 33 and if you played the last one you'll be 1 out of 1 so you won ... get it ?
*/
void counter(int x , int a , int b)
{
    victory[x][a].Mini += 10*a + b ;
    victory[x][b+3].Mini += 10*a + b ;
    victory[x][a].a ++ ;
    victory[x][b+3].a ++ ;
    if( a == b )
    {
        victory[x][6].Mini += 10*a + b ;
        victory[x][6].a ++ ;
    }
    if( a + b == 2 )
    {
        victory[x][7].Mini += 10*a + b ;
        victory[x][7].a ++ ;
    }
}
// This function do check if your input is right or not and if it's right it replaces the space by X or O depends on the player
bool check(int a , int b , int x , bool n )
{
    if ( board[a][b]!= ' '  )
        return false;
    else
    {
        if ( n == true )
        {
            counter ( x , a , b );
            if(x==0)
                board[a][b]='X';
            else
                board[a][b]='O';
        }
        return true;
    }
}
// AI intelligence
void computer()
{
    int a = 0 ,  b = 0 , f ;
    // Attack , Defence
    for(int i = 1 ; i>=0 ; i--)

        for(int j=0 ; j<=7 ; j++)

            if( victory[i][j].a == 2 )

                if ( check( (( victory[i][j].Maxi - victory[i][j].Mini ) / 10 ) , ( ( victory[i][j].Maxi - victory[i][j].Mini ) % 10 ) , 1 , true ) == true )
                    return;

    // Play strategy :D

    while ( true )
    {
        f = rand() % 4 + 1;
        if ( f == 1 )
        {
            while(true)
            {
                a = rand() % 3 ;
                b = rand() % 3 ;
                if(check(a,b,1,true)==true)
                {
                    return;
                }

            }
        }
        else if ( check (0,0,1,false) == true || check(1,1,1,false) == true || check(2,2,1,false) == true || check(2,0,1,false) == true || check(0,2,1,false) == true )
        {
            while (true)
            {
                f = rand() % 5  ;
                if( f<=2 )
                {
                    b = a = f;
                    if(check(a,b,1,true)==true)
                    {
                        return;
                    }
                }
                else if ( f == 3)
                {
                    a=0;
                    b=2;
                    if(check(a,b,1,true)==true)
                    {
                        return;
                    }
                }
                else if ( f == 4 )
                {
                    a=2;
                    b=0;
                    if(check(a,b,1,true)==true)
                    {
                        return;
                    }
                }
            }
        }
    }

}
// cleans every thing
void cleaner()
{
    for (int i = 0; i <= 1; i++)
    {

        for (int j = 0; j <= 7; j++)
        {
            victory[i][j].Mini = 0;
            victory[i][j].a    = 0;
        }
    }
    turns = 0;
    for(int i=0; i<=2; i++)
        for(int j=0; j<=2; j++)
            board[i][j]=' ';
}
// to write table and actually i can't explain it :D
void Table()
{
    system("cls");
    cout<<endl<<"TIC TAC TOE :) "<<endl<<endl;

    for (int rows = 0; rows <= 12; rows++)
    {

        if( rows  % 4 == 0)
            cout<<string(25,'-')<<"       " <<string(25,'-');
        else
        {
            for(int j=0; j<=3; j++)
            {
                if( (rows==2 || rows==6 || rows==10) && j!=3 )
                    cout<<"|   "<< board [ (rows-2) /4  ] [j]<<"   ";
                else
                    cout<<"|       ";
            }
            for(int j=0; j<=3; j++)
            {
                if( (rows==2 || rows==6 || rows==10) && j!=3 )
                    cout<<"|   "<< ( (3*rows-2) + (4*j) ) /4 <<"   ";
                else
                    cout<<"|       ";
            }
        }

        cout<<endl;
    }
}
// asks you to enter where you want to play
void InputCheck(int ys , int wf)
{
    int a , b ;
    if ( ys == 1 || wf == 0  )
    {
        string tempp="";
        while(true)
        {
            cout<<endl<<"Where would you like to play :) ? "<<endl<<endl;

            while (true)
            {
                cin>>tempp;
                if(tempp != "1" && tempp != "2" && tempp !="3" && tempp != "4" && tempp != "5" && tempp != "6" && tempp != "7" && tempp != "8" && tempp != "9")
                    cout<<"              ERROR"<<endl;
                else
                    break;
            }

            int temp;
            temp = atoi(&tempp[0]);
            if( temp <= 3  )
            {
                a=0;
                b=temp-1;
            }
            else if ( temp >= 4 && temp <= 6  )
            {
                a = 1 ;
                b = temp - 4 ;
            }
            else if ( temp >= 7 && temp <= 9 )
            {
                a = 2 ;
                b = temp - 7 ;
            }
            if (check(a,b,wf,true) == true )
            {
                return;
            }
            else
                cout<<"Already Entered";
        }
    }
    computer();
    return;
}
// checks if someone won
void WhoWins()
{
    bool c=false;

    //Player 1
    for (int i  =0; i <= 7; i++)
    {

        if(victory[0][i].a==3)
        {
            cout<<"Player 1 WON ";
            c=true;
        }
    }

    //Player 2
    for (int i = 0; i <= 7; i++)
    {

        if(victory[1][i].a==3)
        {
            cout<<"Player 2 WON " ;
            c=true;
        }
    }

    //Noone won
    if ( turns == 9 && c == false )
    {
        cout<<"Nobody WON !! " ;
        c=true;
    }

    if(c == true)
    {
        getch();
        cleaner();
        Table();
        return;
    }

}

int main()
{
    srand( time( NULL ) );
    string temp = " ";
    int yc; //Your Choice
    int wf; // Who First

    cout<<endl<<"      1 - Multiplayer "<<endl<<"      2 - Single Player " <<endl<<endl<<"      " ;

    cin >> temp;
    while (temp != "1" && temp != "2" )
    {
        cout<<"          ERROR "<<endl<<"      ";
        cin >> temp;
    }
    yc=atoi(&temp[0]);
    temp = " ";
    system("cls");

    cout<<endl<<"      1 - Player 1 first 'X' "<<endl<<"      2 - Player 2 first (or computer) 'O'"<<endl<<endl<<"      ";

    cin >> temp;
    while (temp != "1" && temp != "2" )
    {
        cout<<"          ERROR "<<endl<<"      ";
        cin >> temp;
    }

    wf=atoi(&temp[0]) - 1;

    for (int i = 0; i <= 1; i++)
    {
        victory[i][0].Maxi = 3; // first way to win play (0.0)(0,1)(0,2)
        victory[i][1].Maxi = 33; // second way to win play (1.0)(1.1)(1.3)
        victory[i][2].Maxi = 63; // and so on
        victory[i][3].Maxi = 30;
        victory[i][4].Maxi = 33;
        victory[i][5].Maxi = 36;
        victory[i][6].Maxi = 33;
        victory[i][7].Maxi = 33;
    }
    cleaner();
    for (turns = 0 ;; turns ++)
    {
        Table();
        WhoWins();
        InputCheck(yc , wf);
        wf=1-wf;
    }
}

No comments:

Post a Comment