五子棋(人与人)

#include <iostream>
#include <iomanip>
using namespace std;
int row=16;
int col=16;
int all[17][17];
bool black=true;
bool whilt;
int x,y;
int a=1;
int t=0;
int e=0;
bool win(){
	int l=all[x][y];
	if(all[x][y-1]==l&&all[x][y-2]==l&&all[x][y-3]==l&&all[x][y-4]==l){
		e=1;
	}else if(all[x][y-1]==l&&all[x][y-2]==l&&all[x][y-3]==l&&all[x][y+1]==l){
		e=1;
	}else if(all[x][y-1]==l&&all[x][y-2]==l&&all[x][y+1]==l&&all[x][y+2]==l){
		e=1;
	}else if(all[x][y-1]==l&&all[x][y+1]==l&&all[x][y+2]==l&&all[x][y+3]==l){
		e=1;
	}else if(all[x][y+1]==l&&all[x][y+2]==l&&all[x][y+3]==l&&all[x][y+4]==l){
		e=1;
	}else if(all[x-1][y]==l&&all[x-2][y]==l&&all[x-3][y]==l&&all[x-4][y]==l){
		e=1;
	}else if(all[x-1][y]==l&&all[x-2][y]==l&&all[x-3][y]==l&&all[x+1][y]==l){
		e=1;
	}else if(all[x-1][y]==l&&all[x-2][y]==l&&all[x+2][y]==l&&all[x+1][y]==l){
		e=1;
	}else if(all[x-1][y]==l&&all[x+3][y]==l&&all[x+2][y]==l&&all[x+1][y]==l){
		e=1;
	}else if(all[x+4][y]==l&&all[x+3][y]==l&&all[x+2][y]==l&&all[x+1][y]==l){
		e=1;
	}else if(all[x+1][y-1]==l&&all[x+2][y-2]==l&&all[x+3][y-3]==l&&all[x+4][y-4]==l){
		e=1;
	}else if(all[x+1][y-1]==l&&all[x+2][y-2]==l&&all[x+3][y-3]==l&&all[x-1][y+1]==l){
		e=1;
	}else if(all[x+1][y-1]==l&&all[x-3][y+3]==l&&all[x-2][y+2]==l&&all[x-1][y+1]==l){
		e=1;
	}else if(all[x+1][y-1]==l&&all[x-3][y+3]==l&&all[x-2][y+2]==l&&all[x-1][y+1]==l){
		e=1;
	}else if(all[x-4][y+4]==l&&all[x-3][y+3]==l&&all[x-2][y+2]==l&&all[x-1][y+1]==l){
		e=1;
	}else if(all[x+1][y+1]==l&&all[x+2][y+2]==l&&all[x+3][y+3]==l&&all[x+4][y+4]==l){
		e=1;
	}else if(all[x+1][y+1]==l&&all[x+2][y+2]==l&&all[x+3][y+3]==l&&all[x-1][y-1]==l){
		e=1;
	}else if(all[x+1][y+1]==l&&all[x+2][y+2]==l&&all[x-2][y-2]==l&&all[x-1][y-1]==l){
		e=1;
	}else if(all[x+1][y+1]==l&&all[x-3][y-3]==l&&all[x-2][y-2]==l&&all[x-1][y-1]==l){
		e=1;
	}else if(all[x-4][y-4]==l&&all[x-3][y-3]==l&&all[x-2][y-2]==l&&all[x-1][y-1]==l){
		e=1;
	}
}
void f5(){
    cout<<"  ";
    for(int j=1;j<=col;j++){
            cout<<setw(3)<<j;
    }
    cout<<endl;
    for(int i=1;i<=row;i++){
            cout<<setw(2)<<i;
        for(int j=1;j<=col;j++){
            if(all[i][j]==0){
                cout<<setw(3)<<"+";
            }else if(all[i][j]==1){
                cout<<setw(3)<<"黑";
            }else{
                cout<<setw(3)<<"白";
            }
             
        }
        cout<<endl;
    }
}
int main(int argc, char** argv) {
    for(int i=0;i<17;i++){
        for(int j=0;j<17;j++){
            all[i][j]=0;
        }
         
    }
    while(true){
        system("cls");
        f5();
        if(black){
            cout<<"黑棋请落子:";
            cin>>x>>y;
            if(x>16||y>16||all[x][y]!=0){
                continue;
            }
            all[x][y]=1;
            black=false;
            win();
            if(e==1){
            	system("cls");
            	f5();
            	cout<<"黑方win";
            	return 0;
			}
        }else{
            cout<<"白棋请落子:";
            cin>>x>>y;
            if(x>16||y>16||all[x][y]!=0){
                continue;
            }
            all[x][y]=2;
            win();
            if(e==1){
            	system("cls");
            	f5();
            	cout<<"白方win";
            	return 0;
			}
            black=true;
        }
    }
    return 0;
}

  

posted @ 2023-08-11 17:11  陈若麟  阅读(22)  评论(0编辑  收藏  举报