"Untitled Post" - Views: 191 · Hits: 191 - Type: Public

#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <ctime>
#include <algorithm>
using namespace std;
char input[80];
int wf = 0 ; // who's first
bool AD();   // attack defense
void table();  // draws the table
int winning(); // check the winner
bool three();  // play three blocks
bool two();   // play two blocks
bool worstcase(); // worst case
bool defense();  // defense three blocks
bool start = true; // first start
int get(int t);
void clean()
{
    for(int i = 0 ; i<= 80 ; i++)
        input[i]=' ';
    wf = 0 ;
    system("cls");
    table();
    start=true;

}
void AI()
{
    if(start == true)
    {
        start = false;
        if(input[4] == ' ')
        {
            input[4]='O';
            return;
        }
    }
    if(AD() == true)
        return ;
    if(three() == true )
        return ;
    if(defense() == true )
        return;
    if(two() == true )
        return ;
    if(worstcase() == true)
        return;
}
bool defense()
{
    int x,y;
    for(int t = 1 ; t <= 7 ; t++)
    {
        x = get(t);
        if(x<=42)
        {
            input[x]='X';
            for(int j = 1 ; j <= 7 ; j++)
            {
                y = get(j) ;
                if (y<=42)
                {
                    input[y]='X';
                    if (winning() != 0)
                    {
                        input[y]=' ';
                        input[x]='O';
                        return true;
                    }
                    input[y]=' ';
                }
            }
            input[x]=' ';
        }
    }
    return false;
}
bool worstcase()
{
    int ia[7]= {1,2,3,4,5,6,7};
    random_shuffle(&ia[0] , &ia[7]);
    int x , y;
    for(int t = 1 ; t <= 7 ; t++ )
    {
a :
        x=get(ia[t]);
        input[x]='O';
        for(int j = 1 ; j <= 7 ; j++)
        {
            y = get(j) ;
            input[y]='O';
            if(winning() != 0 )
            {
                input[y]=' ';
                if ( t < 7 )
                {
                    input[x]=' ';
                    t++;
                    goto a;
                }
                else
                    input[x]=' ';
                return false;
            }
            input[y]= ' ';
        }
        return true;
    }
}
bool two()
{
    for(int i = 1 ; i <= 42 ; i++)
    {
        if(input[i] == 'O')
        {

            if  ((i-1) - int((i-1)/7) *7 < 7 && input[i-1] == ' ' && input[i-2] == ' ' && input[i-3]==' '&& get( (i-1) - int((i-1)/7)*7 ) == i-1 && (i-1) - int((i-1)/7) < 7)
            {
                input[get(  (i+1) - ((int((i+1)/7)) *7) )] = 'O';
                return true;
            }
            if ( ( input[i+1] == ' ' && input[i+2] == ' ' && input[i+3]==' '&& get( (i+1) - int((i+1)/7)*7 ) == i+1 && (i+1) - int((i+1)/7) *7 < 7) )
            {
                input[get(  (i+1) - ((int((i+1)/7)) *7) )] = 'O';
                return true;
            }

            if ( input[i+7] == ' ' && input[i+14] == ' ' && input[i+21]==' ' && i <= 21  )
            {
                input[get( i - int(i/7)) ] = 'O';
                return true;
            }
        }
    }
}
bool three()
{
    int p[7];
    int x,y;
    int l = 0 ;
    for(int t = 1 ; t <= 7 ; t++)
    {
        x = get(t);
        if(x<=42)
        {
            input[x]='O';
            for(int j = 1 ; j <= 7 ; j++)
            {
a :
                y = get(j) ;
                if(y<=42)
                {
                    input[y]='X';
                    if(winning() != 0 )
                    {
                        input[y]=' ';
                        if(j < 7)
                        {
                            j++;
                            goto a;
                        }
                        else
                            break;
                    }
                    input[y]=' ';
                    y = get(j) ;
                    input[y]='O';
                    if (winning() != 0)
                    {
                        p[l] = x;
                        l++;
                        j=8;
                    }
                    input[y]=' ';
                }
            }
            input[x]=' ';
        }
    }
    if(l != 0)
    {
        int chances[l];
        for(int i = 0 ; i < l ; i++)
            chances[i] = 0;
        for( int i = 0 ; i < l ; i++)
        {
            input[p[i]]='O';
            for(int t = 1 ; t <= 7 ; t++)
            {
                int n = get(t);
                if (n<=42)
                {
                    input[n]='O';
                    if (winning() != 0)
                    {
                        chances[i]++;
                    }
                    input[n]=' ';
                }
            }
            input[p[i]]=' ';
        }
        int temp = 0;
        int tempp;
        for(int i=0; i<l; i++)
        {
            cout<<endl<<"DE EL I " << endl << i ;
            if(chances[i]>temp)
            {
                temp=chances[i];
                tempp = i;
            }
        }
        input[p[tempp]]='O';
        return true;
    }
    return false;
}
bool AD() // attack defence
{
    int n;
    for(int t = 1 ; t <= 7 ; t++)
    {
        n = get(t);
        if (n<=42)
        {
            input[n]='O';
            if (winning() != 0)
            {
                return true;
            }
            else
                input[n]=' ';
        }
    }
    for(int t = 1 ; t <= 7 ; t++)
    {
        int n = get(t);
        if(n<=42)
        {
            input[n]='X';
            if (winning() != 0)
            {
                input[n] = 'O';
                return true;
            }
            else
                input[n]=' ';
        }
    }
    return false;
}
int get(int t)
{
    int n;
    if(input[t] == ' ')
    {
        n = t;
    }
    else
        for(int i = 1 ; i<= 6 ; i++)
        {
            if( input[t+7*i] == ' '  )
            {
                n = t+7*i;
                break;
            }
        }
    return n;
}
int winning()
{
    for(int i = 1 ; i<= 42 ; i++)
    {
        while(true)
            if(input[i] == ' ')
                if(i < 42)
                    i++;
                else
                    return false;
            else
                break;
        if( i - int((i-1)/7) * 7 <= 4  )
            if( input[i] == input [i+1] && input[i] == input[i+2] && input[i] == input[i+3] )
                if(input[i] == 'X' )
                    return 1 ;
                else
                    return 2;
        if( i <= 21 )
            if ( input[i] == input[i+7] && input[i] == input[i+14] && input[i] == input[i+21]  )
                if(input[i] == 'X' )
                    return 1 ;
                else
                    return 2;
        if( i - int((i-1)/7) * 7 <= 4 && i<=18  )
            if(input[i] == input[i+8] && input[i] == input[i+16] && input[i]==input[i+24])
                if(input[i] == 'X' )
                    return 1 ;
                else
                    return 2;
        if( i - int((i-1)/7) * 7 >= 4 && i <= 21   )
            if(input[i] == input[i+6] && input[i] == input[i+12] && input[i]==input[i+18])
                if(input[i] == 'X' )
                    return 1 ;
                else
                    return 2;
    }
    return 0;
}
void table()
{
    cout<<endl<<"    1   "<<"    2   "<<"    3   "<<"    4   "<<"    5   "<<"    6   "<<"    7   "<<endl;

    int j = 42;
    for(int i = 0 ; i<= 23 ; i++)
    {
        if(i % 4 == 0)
            cout<<string(57,'-');
        else
        {
            if( (i - 2) % 4 == 0)
            {
                j=42-(0.25*i+0.5)*6-((0.25*i+0.5)-1) ;
                for(int i = 0 ; i<=6 ; i++)
                {
                    cout<<"|"<<"   "<<input[j]<<"   ";
                    j++;
                }
                cout<<"|";
            }
            else
            {
                for(int i = 0 ; i<=6 ; i++)
                    cout<<"|"<<string(7,' ');
                cout<<"|";
            }
        }
        cout<<endl;
    }
    cout<<string(57,'-');
}
void inputs()
{
    int sth;
    cout<<endl<<"where would you like to play"<<endl;
    while(true)
    {
a:
        cin>>sth;
        if(sth <= 7 )
            break;
        else
            cout<<"ERROR"<<endl;
    }
    if(get(sth)<=42)
        input[get(sth)] = 'X';
    else
    {
        cout<<"ERROR"<<endl;
        goto a;
    }
}
int main()
{
    srand(time(0));
    for(int i = 0 ; i<= 80 ; i++)
        input[i]=' ';
    while(true)
    {
        system("cls");
        table();
        if(winning()!=0)
        {
            if(winning() == 1)
                cout<<endl<<"YOU WON !";
            else
                cout<<endl<<"AI WON ! " ;
            getch();
            clean();
        }
        inputs();
        wf=1-wf;
        AI();
        wf=1-wf;
    }
}