#include<iostream> #include<queue> #include <windows.h> #include <conio.h> #include <mmsystem.h> #pragma comment(lib,"winmm.lib") using namespace std; int wb; typedef struct { int x,y; }nn; nn node; queue<nn>local_dir[8][9][9]; void qingkong() { for(int i=1;i<9;i++) for(int j=1;j<9;j++) for(int k=0;k<8;k++) while(!local_dir[k][i][j].empty()) local_dir[k][i][j].pop(); } void chushihua(int map[][10]) { int i,j,bianjie='x'; for(i=0;i<10;i++) { map[0][i]=bianjie; map[9][i]=bianjie; map[i][0]=bianjie; map[i][9]=bianjie; } for(i=1;i<9;i++) for(j=1;j<9;j++) map[i][j]=' '; } void shuchu(int map[][10]) { int i,j; printf(" ===双人博弈黑白棋 BY 青春微凉===\n ☆为棋子位置提示\n"); printf(" A"); for(i=1;i<8;i++) printf(" %c",i+'A'); putchar(10); printf(" ┌─┬─┬─┬─┬─┬─┬─┬─┐\n"); for(i=1;i<8;i++) { printf("%d│",i); for(j=1;j<=8;j++) { if(map[i][j]=='○') printf("○│"); else if(map[i][j]=='●') printf("●│"); else if(map[i][j]=='☆') printf("☆│"); else printf(" │"); } putchar(10); printf(" ├─┼─┼─┼─┼─┼─┼─┼─┤\n"); } printf("8│"); for(j=1;j<=8;j++) { if(map[i][j]=='○') printf("○│"); else if(map[i][j]=='●') printf("●│"); else if(map[i][j]=='☆') printf("☆│"); else printf(" │"); } printf(" \n └─┴─┴─┴─┴─┴─┴─┴─┘\n"); } bool kongge(int map[][10]) { for(int i=1;i<9;i++) for(int j=1;j<9;j++) if(map[i][j]==' ') return true; return false; } bool next_wb(int map[][10],int y,int x,int wb) { if(map[y][x]==wb) return true; return false; } bool next_white_black(int map[][10]) { int dirx[]={0,1,1,1,0,-1,-1,-1}; int diry[]={-1,-1,0,1,1,1,0,-1}; int i,j,k; bool flag=0; //loop: for(i=1;i<=8;i++) { for(j=1;j<=8;j++) { if(!(map[i][j]=='●'||map[i][j]=='○')) { for(k=0;k<8;k++) { // goto loop; int y=i,x=j; while(next_wb(map,y+=diry[k],x+=dirx[k],wb=='●'?'○':'●')) { node.x=x; node.y=y; local_dir[k][i][j].push(node); } if(map[y][x]==(wb=='●'?'●':'○')&&!local_dir[k][i][j].empty()) { flag=1; map[i][j]='☆'; } else { while(!local_dir[k][i][j].empty()) local_dir[k][i][j].pop(); } } } } } return flag; } void jieshu(int map[][10]) { shuchu(map); int b=0,w=0; for(int i=1;i<9;i++) for(int j=1;j<9;j++) { if(map[i][j]=='●') b++; if(map[i][j]=='○') w++; } if(b>w) printf(" ●方赢○方%d枚棋子\n",b-w); if(b<w) printf(" ○方赢●方%d枚棋子\n",w-b); if(b==w) printf(" 和局\n"); system("pause"); system("cls"); } void fanzhuan(int map[][10],int y,int x) { HANDLE hOut; COORD pos= {0, 0}; hOut = GetStdHandle(STD_OUTPUT_HANDLE); pos.X=x*4-1; pos.Y=y*2+2; SetConsoleCursorPosition(hOut, pos); if(wb=='●') printf("●",pos.X,pos.Y); else printf("○",pos.X,pos.Y); Sleep(500); int i; for(i=0;i<8;i++) { while(!local_dir[i][y][x].empty()) { node=local_dir[i][y][x].front (); map[node.y][node.x]=wb=='●'?'●':'○'; local_dir[i][y][x].pop(); pos.X=node.x*4-1; pos.Y=node.y*2+2; SetConsoleCursorPosition(hOut, pos); if(wb=='●') printf("●",pos.X,pos.Y); else printf("○",pos.X,pos.Y); Sleep(500); } } } void fuyuan(int map[][10]) { for(int i=1;i<9;i++) for(int j=1;j<9;j++) if(map[i][j]=='☆') map[i][j]=' '; } int main() { int step=1; int map[10][10]; int yy,xx; // char xx[2]; // char local[20]; char ch1,ch2; system("color 30"); printf("==========使用说明=========\n\n1.每次出棋前会用☆提示位置,\n只需输入行列坐标即可,坐标用\n大写,中间不能有空格。\n\n\n2.若一方没棋子可下,则对方出\n棋,若都没棋子可出或棋盘已满,\n则游戏结束\n了解更多,欢迎关注官方博客 \n http://blog.csdn.net/qq909157370\n\n"); system("pause"); system("CLS"); while(1) { PlaySound("sou.wav",NULL,SND_FILENAME|SND_ASYNC); step=1; chushihua(map); map[4][4]=map[5][5]='○'; map[4][5]=map[5][4]='●'; int flag=0; while(kongge(map)&&flag<2)//(cunzai_black_white(map,'●')&&cunzai_black_white(map,'○')) { flag++; qingkong(); if(step%2==0) wb='○'; else wb='●'; if(next_white_black(map)) { flag=0; shuchu(map); wb=='○'?printf(" 请○方输入行列坐标(如5E) "):printf(" 请●方输入行列坐标(如5E) "); while(putchar(ch1=getch()),putchar(ch2=getch()),ch1<'1'||ch1>'8'||ch2<'A'||ch2>'H'||(ch1>'0'&&ch1<'9'&&ch2>='A'&&ch2<='H'&&map[ch1-'0'][ch2-'A'+1]!='☆')) { printf(" \n 输入错误!请重新输入:"); } fuyuan(map); yy=ch1-'0'; xx=ch2-'A'+1; map[yy][xx]=wb=='●'?'●':'○'; fanzhuan(map,yy,xx); system("CLS"); } step++; } jieshu(map); } }