井字棋——人工智能版

· · 休闲·娱乐

#include<bits/stdc++.h>
#include<Windows.h>
using namespace std;
char c[3][3] = {{'1','1','1'},
                {'1','1','1'},
                {'1','1','1'}};
int x , y;
int dx[]={1,0,-1,1};
int dy[]={0,1,1,1};
void hy(){
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            c[i][j] = '1';
        }
    }
}
bool cz(){
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            if(c[i][j]=='1') return false;
        }
    }
    return true;
}
int main(){
    srand((unsigned int)time(NULL));
    cout << "三子棋游戏开始!\n";
    Sleep(500);
    T:
    int i = 0;
    while(++i){
        system("cls");
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                cout << c[i][j] << " ";
            }
            cout << endl;
        } 
        if(i%2==1){
            cout << "输入坐标:";
            G:
            cin >> x >> y;
            if(x<1||x>3||y<1||y>3){
                cout << "坐标不合法,重新输入\n";
                goto G; 
            }
            else if(c[x-1][y-1]!='1'){
                cout << "此处已有棋子,重新输入\n"; 
                goto G; 
            }
            x -= 1;
            y -= 1;
        }
        else{
            int f=0;
            if(c[0][0]=='x'&&c[0][2]=='x'&&c[0][1]=='1')x=0,y=1,f=1;
                else if(c[2][0]=='o'&&c[2][2]=='o'&&c[2][1]=='1')x=2,y=1,f=1;
                else if(c[0][0]=='o'&&c[2][0]=='o'&&c[1][0]=='1')x=1,y=0,f=1;
                else if(c[0][2]=='o'&&c[2][2]=='o'&&c[2][1]=='1')x=1,y=2,f=1;
            for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
            {
                if(f)break;
                if(c[i][j]=='x')
                for(int k=0;k<3;k++)
                {
                    int xx=i+dx[k],yy=j+dy[k];
                    if(xx>=0&&yy>=0&&xx<3&&yy<3&&c[xx][yy]=='x')
                    {
                        if((i!=1||j!=0||xx!=0||yy!=1)&&(i!=0||j!=1||xx!=1||yy!=2)
                        &&(i!=1||j!=0||xx!=2||yy!=1)&&(i!=2||j!=1||xx!=1||yy!=2))
                        {
                            if(k==0)
                            if(i==0)x=2,y=j;
                            else x=0,y=j;
                            if(k==1)
                            if(yy==2)x=xx,y=0;
                            else x=xx,y=2;
                            if(k==2)
                            if(yy==2)x=2,y=0;
                            else x=0,y=2;
                            if(k==3)
                            if(i==0)x=y=2;
                            else x=y=0;
                            f=1;break;
                        }
                    }
                }
            }
            if(!f)
            for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
            {
                if(f)break;
                if(c[i][j]=='o')
                for(int k=0;k<3;k++)
                {
                    int xx=i+dx[k],yy=j+dy[k];
                    if(xx>=0&&yy>=0&&xx<3&&yy<3&&c[xx][yy]=='o')
                    {
                        if((i!=1||j!=0||xx!=0||yy!=1)&&(i!=0||j!=1||xx!=1||yy!=2)
                        &&(i!=1||j!=0||xx!=2||yy!=1)&&(i!=2||j!=1||xx!=1||yy!=2))
                        {
                            if(k==0)
                            if(i==0)x=2,y=j;
                            else x=0,y=j;
                            if(k==1)
                            if(yy==2)x=xx,y=0;
                            else x=xx,y=2;
                            if(k==2)
                            if(yy==2)x=2,y=0;
                            else x=0,y=2;
                            if(k==3)
                            if(i==0)x=y=2;
                            else x=y=0;
                            f=1;break;
                        }
                    }
                }
            }
            if(!f)
            {
                if(c[0][0]=='o'&&c[0][2]=='o'&&c[0][1]=='1')x=0,y=1,f=1;
                else if(c[2][0]=='o'&&c[2][2]=='o'&&c[2][1]=='1')x=2,y=1,f=1;
                else if(c[0][0]=='o'&&c[2][0]=='o'&&c[1][0]=='1')x=1,y=0,f=1;
                else if(c[0][2]=='o'&&c[2][2]=='o'&&c[2][1]=='1')x=1,y=2,f=1;
                else if(c[1][1]=='1')x=1,y=1,f=1;
            }
            G2:
                    if(!f)
                    {
                            if(c[1][1]=='1')x=y=f=1;
                            else  if(c[2][2]=='1')x=y=2,f=1;
                            else if(c[0][2]=='1')x=0,y=2,f=1;
                            else if(c[2][0]=='1')x=2,y=0,f=1;
                            else if(c[0][0]=='1')x=y=0,f=1;
                        if(!f) x = rand()%3;
                        if(!f) y = rand()%3;
                    }
                    if(x<0||x>2||y<0||y>2){
                    f=0;goto G2;
                    }
                    if(c[x][y]!='1'){
                    f=0;goto G2;
                    }
        }
        if(i%2){
            c[x][y] = 'o';
        }else{
            Sleep(500);
            c[x][y] = 'x';
        }
        system("cls");
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                cout << c[i][j] << " ";
            }
            cout << endl;
        } 
        if((c[0][0]=='o'&&c[0][1]=='o'&&c[0][2]=='o')||(c[1][0]=='o'&&c[1][1]=='o'&&c[1][2]=='o')||
           (c[2][0]=='o'&&c[2][1]=='o'&&c[2][2]=='o')||(c[0][0]=='o'&&c[1][1]=='o'&&c[2][2]=='o')||
           (c[0][2]=='o'&&c[1][1]=='o'&&c[2][0]=='o')||(c[0][0]=='o'&&c[1][0]=='o'&&c[2][0]=='o')||
           (c[0][1]=='o'&&c[1][1]=='o'&&c[2][1]=='o')||(c[0][2]=='o'&&c[1][2]=='o'&&c[2][2]=='o')){
            cout << "p1 wins!\n";
            break;
        }
        if((c[0][0]=='x'&&c[0][1]=='x'&&c[0][2]=='x')||(c[1][0]=='x'&&c[1][1]=='x'&&c[1][2]=='x')||
           (c[2][0]=='x'&&c[2][1]=='x'&&c[2][2]=='x')||(c[0][0]=='x'&&c[1][1]=='x'&&c[2][2]=='x')||
           (c[0][2]=='x'&&c[1][1]=='x'&&c[2][0]=='x')||(c[0][0]=='x'&&c[1][0]=='x'&&c[2][0]=='x')||
           (c[0][1]=='x'&&c[1][1]=='x'&&c[2][1]=='x')||(c[0][2]=='x'&&c[1][2]=='x'&&c[2][2]=='x')){
            cout << "p2 wins!\n";
            break;
        }
        if(cz()){
            cout << "tie\n";
            break;
        } 
    }
    cout << "是(1)否(0)再来一次?";
    cin >> i;
    if(i==1){
        hy();
        goto T;
    }else{
        return 0;
    }
    return 0;
}

此代码为FIRESTARS与User1025109联合创作