Code Corona

نسخه کامل: Dooz (from arash
شما در حال مشاهده نسخه ساده شده مطالب هستید. نسخه کامل را به همراه قالب بندی ببینید.
کد:
//Author:M.J
//dooz game
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <iomanip>
using namespace std;

#define Winn() Result(1)
#define Lose() Result(2)    
#define MAX 3
#define SQMAX 9
#define MAX_MENU 4

void RegisterMove(int,int);
void FRandMove();
bool FAttack();
bool FDefence();
void FGetUserMove();
void RandMove();
bool Attack();
bool Defence();
void ShowStat();
void GetUserMove();
void ShowWinn();
void ShowLose();
bool Result(int); //Winn And Lose convert to this function by using mocro
void CleanAttack();
void CleanDefence(int);
void ComMove();
void FComMove();
int Menu();

//menu action function
void PlayWithCom();
void PlayNetwork();
void PlayVS();
void Exit();

void (*MenuArray[MAX_MENU])(void)={PlayWithCom,PlayNetwork,PlayVS,Exit};


int table[MAX][MAX]={0};
bool comfirst=1;
int From,To;

int main()
{
    
    srand(time(0));
    MenuArray[Menu()]();    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void FRandMove()
{
    int n;
    From=-1;
    do
    {
        n=rand() % SQMAX;
    }
    while (table[0][n]);
    To=n;
    table[0][n]=1;
}

bool FAttack()
{
    From=-1;
    for(int i=0 ; i < SQMAX ; ++i)
        if(!table[0][i])
        {
            table[0][i]=1;
            if(Winn())
            {
                To=i;
                return true;
            }
            table[0][i]=0;
        }

    return false;
}

bool FDefence()
{
    From=-1;
    for(int i=0 ; i < SQMAX ; ++i)
        if(!table[0][i])
        {
            table[0][i]=2;
            if(Lose())
            {
                table[0][i]=1;
                To=i;
                return true;
            }
            table[0][i]=0;
        }
    return false;
}


bool Result(int x)
{
    register int i;
    for (i=0; i < MAX ; ++i)
        if(table[i][0]==x && table[i][1]==x && +table[i][2]==x)
            return true;

    for (i=0; i < MAX ; ++i)
        if(table[0][i]==x && table[1][i]==x && +table[2][i]==x)
            return true;

    if( (table[0][0]==x && table[1][1]==x && table[2][2]==x)  ||
        (table[0][2]==x && table[1][1]==x && table[2][0]==x ) )
        return true;
    
    return false;
}

bool Attack()
{
    for(int i=0 ; i <  SQMAX ; ++i)
    
        if(!table[0][i])
        {
            table[0][i]=1;
            if(Winn())
            {
                To=i;
                CleanAttack();
                return true;
            }
            else
                table[0][i]=0;
        }

        return false;
}

bool Defence()
{
    for(int i=0 ; i <  SQMAX ; ++i)
    
        if(!table[0][i])
        {
            table[0][i]=2;
            if(Lose())
            {
                table[0][i]=1;
                CleanDefence(i);
                To=i;
                return true;
            }
            else
                table[0][i]=0;
        }
        return false;
}

void RandMove()
{
    int n;
    for(;;)
    {
        n=rand() % SQMAX;
        if (!table[0][n])
            break;    
    }    
    To=n;
    table[0][n]=1;
    CleanDefence(n);
        
    

}

void CleanAttack()
{
    for(int i=0 ; i < SQMAX ; ++i)
       if(table[0][i]==1)
       {
           table[0][i]=0;
           if (Winn())
           {
               From=i;
               return;
           }
           table[0][i]=1;
       }
}

void CleanDefence(int n)
{
    for(int i=0 ; i < SQMAX ; ++i)
       if(table[0][i]==1 && n!=i)
       {
           table[0][i]=2;
           if (!Lose())
           {
               table[0][i]=0;
               From=i;
               return;
           }
           table[0][i]=1;
       }
}

void ShowWinn()
{
    system("cls");
    ShowStat();
    printf("\n\t\t\tY O U  L O S E     P L E A S E   T R Y    A G A I N \n\n\n\a\a");
}

void ShowLose()
{
    system("cls");
    ShowStat();
    cout<<"\n\t\t\tY O U  W I N N\n\n\n";
}

void ShowStat()
{
    cout<<endl<<endl;
    for(int i=0 ; i < SQMAX ; ++i)
    {
        cout<<setw(2)<<table[0][i];//<<( i%3==2 ? '\n' : NULL);
        if (i==2 || i==5 || i==8)
        if(i%3==2)
            cout<<endl;
    }

}

void FGetUserMove()
{
    int x,y,B=0;
    cout<<"your move\n";
    do
    {
        if(B)
            cout<<"\nlocation is full\n\a";
        cout<<"\nwhere do you put?\nx:";
        cin>>x;
        while(x<1 || x >MAX+1)
        {
            cout<<"\ax:";
            cin>>x;
        }

        --x;

        cout<<"y:";
        cin>>y;
        while(y < 1 && y > MAX+1)
        {
            cout<<"\ay:";
            cin>>y;
        }
        --y;
        B=1;
    }
    while (table[x][y]);

    table[x][y]=2;
}

void GetUserMove()
{
    int x,y,x1,y1,B=0;
    cout<<"your move\n";
    do
    {
        if(B)
            cout<<"\nlocation is not full\n\a";
        cout<<"\nmove from\nx:";
        cin>>x1;
        while(x1<1 || x1 >MAX+1)
        {
            cout<<"\a\ax:";
            cin>>x1;
        }

        --x1;

        cout<<"y:";
        cin>>y1;
        while(y1 < 1 || y1 > MAX+1)
        {
            cout<<"\a\a\a:y";
            cin>>y1;
        }
        --y1;
        B=1;
    }
    while (table[x1][y1]!=2);
    
    
    B=0;
    do
    {
        if(B)
            cout<<"\nlocation is full\n\a\a\a";
        cout<<"\nwhere do you put?\nx:";
        cin>>x;
        while(x<1 || x >MAX+1)
        {
            cout<<"\a\a\ax:";
            cin>>x;
        }

        --x;

        cout<<"y:";
        cin>>y;
        while(y < 1 || y > MAX+1)
        {
            cout<<"\a\a\ay:";
            cin>>y;
        }
        --y;
        B=1;
    }
    while (table[x][y]);

    table[x1][y1]=0;
    table[x][y]=2;

}

void ComMove()
{
    if ( !Attack()  && ! Defence())
        RandMove();
    RegisterMove(From,To);
}

void FComMove()
{
    if ( !FAttack()  && ! FDefence())
        FRandMove();
    RegisterMove(From,To);
}

int Menu()
{
    int x;
    system("cls");
    cout<<"Select once\n\n";
    cout<<"\t1. Play Whith Computer\n";
    cout<<"\t2. Play on network\n";
    cout<<"\t3. VS\n";
    cout<<"\t4. exit\n\n"    ;
    cout<<"your choice : ";
    cin>>x;
    while(x<1 || x>4 )
    {
        cout<<"\a\a\ayour choice must be bitween 1 - 4 \n\n";
        cout<<"your choice : ";
        cin>>x;
    }
    return --x;
}

void PlayWithCom()
{
    if (!comfirst)
    {
        ShowStat();
        FGetUserMove();
    }
        
    for (int i=0; i< 2+comfirst ; ++i)
    {
        FComMove();
        if(Winn())
        {
            ShowWinn();
            system("PAUSE");
            exit(0);    
        }
        ShowStat();
        FGetUserMove();
    }
    
    if(!comfirst)
    {
        FComMove();
        if(Winn())
        {
            ShowWinn();
            system("PAUSE");
            exit(0);
        }
        ShowStat();
        GetUserMove();

    }

    if (Lose())
    {
        ShowLose();
        system("PAUSE");
        exit(0);
    }
    

    for(;;)
    {
        ComMove();

        if(Winn())
            break;

        ShowStat();
        GetUserMove();
        
        if (Lose())
            break;    
    }
    
    ShowStat();
    if(Winn())
        ShowWinn();
    else
        ShowLose();
}
void PlayNetwork()
{
    cout<<"\a\a\anot present yet,i am sorry\n\n";
    system("pause");
    exit(0);

}
void PlayVS()
{
    cout<<"\a\a\anot present yet,i am sorry\n\n";
    system("pause");
    exit(0);
}

void Exit()
{
    exit(0);
}

void RegisterMove(int form, int to)
{
    if(form>0)
        printf("move form (%d , %d)\n",form/MAX+1,form%MAX+1);
    printf("Put in (%d , %d)\n",to/MAX+1,to%MAX+1);
}
لینک‌های مرجع