两人对战的五子棋,上下左右为wsad 回车为下棋。。。 #include<iostream> #include<conio.h> using namespace std; struct mov { int x,y; }mov[8]={-1,-1,-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1}; int map[20][20],step,X,Y; int dfs(int x,int y,int fang,int jige,int type) { if(x+mov[fang].x>=0&&x+mov[fang].x<20&&y+mov[fang].y>=0&&y+mov[fang].y<20&&map[x+mov[fang].x][y+mov[fang].y]==type) return dfs(x+mov[fang].x,y+mov[fang].y,fang,jige+1,type); else return jige+1; } int test() { int i,j,fang; for(i=0;i<20;i++) for(j=0;j<20;j++) if(map[i][j]!=0) for(fang=0;fang<4;fang++) { int lenth=dfs(i,j,fang,0,map[i][j])+dfs(i,j,(fang+4)%8,0,map[i][j])-1; if(lenth==5) goto end; } end: if(i<20&&j<20) if(map[i][j]==1) return 1; else return 2; else return 0; } void controlandshow() { int i,j; while(test()==0) { system("cls"); cout<<" "; for(i=1;i<=20;i++) printf("%2c",i+64); cout<<endl; for(i=0;i<20;i++) { cout<<" "<<char(i+97); for(j=0;j<20;j++) if(i==X&&j==Y) cout<<"¤"; else { if(map[i][j]==1) cout<<"○"; else if(map[i][j]==2) cout<<"●"; else cout<<" "; } if(i==8) cout<<" 黑棋 ○"<<" 白棋 ●"; cout<<endl; } cout<<"当前执棋者: "; if(step%2) cout<<"黑 "; else cout<<"白 "; cout<<"当前光标位置: "<<char(X+97)<<char(Y+65)<<" 当前步数: "<<step; while(1) { char ch=getch(); if(ch=='/r') { if(map[X][Y]==0) map[X][Y]=++step%2+1; return; } if(ch=='w') { for(i=(X-1)%20;i!=X;i=--i%20) { if(i<0) i+=20; if(map[i][Y]==0) break; } X=i; } if(ch=='a') { for(i=(Y-1)%20;i!=Y;i=--i%20) { if(i<0) i+=20; if(map[X][i]==0) break; } Y=i; } if(ch=='s') { for(i=(X+1)%20;i!=X;i=++i%20) { if(i<0) i+=20; if(map[i][Y]==0) break; } X=i; } if(ch=='d') { for(i=(Y+1)%20;i!=Y;i=++i%20) { if(i<0) i+=20; if(map[X][i]==0) break; } Y=i; } return; } } if(test()==1) cout<<endl<<"黑棋胜利"<<endl; else cout<<endl<<"白棋胜利"<<endl; exit(0); } int main() { step=0; X=0; Y=0; memset(map,0,sizeof(map)); while(1) controlandshow(); return 0; }