//服务器 #include<bits/stdc++.h> #include<winsock.h> #include<conio.h> using namespace std; //自定义部分 //接受发送部分 void prompt(string); const int MAX_LEN=30; int send_len=0,recv_len=0,len=0; char send_buf[MAX_LEN+3],recv_buf[MAX_LEN+3]; string Version=“Original_V1.0.0”; SOCKET s_server,s_accept; SOCKADDR_IN server_addr,accept_addr; #define close() system(“pause>nul”),closesocket(s_server),closesocket(s_accept),WSACleanup(),exit(0) #define exits() system(“pause>nul”),exit(1) FILE flog; void send(string s) { memset(send_buf,0,sizeof(send_buf)); fprintf(flog,“send %s\n”,s.c_str()); strncpy(send_buf,s.c_str(),s.size());send_buf[s.size()]=‘\0’; send_len=send(s_accept,send_buf,MAX_LEN,0); if(send_len<0)prompt(“发送失败!”),closesocket(s_server),closesocket(s_accept),WSACleanup(),exits(); } string receive() { memset(recv_buf,0,sizeof(recv_buf)); while(recv_buf[0]==0) { recv_len=recv(s_accept,recv_buf,MAX_LEN,0); if(recv_len<0)prompt(“接受失败!”),closesocket(s_server),closesocket(s_accept),WSACleanup(),exits(); if(recv_len==0)prompt(“对方已离开游戏!”),closesocket(s_server),closesocket(s_accept),WSACleanup(),exits(); } fprintf(flog,“received %s\n”,recv_buf); return recv_buf; } void Connect() { prompt(“正在连接到客户端……”); WORD w_req=MAKEWORD(2,2);WSADATA wsadata; int err=WSAStartup(w_req, &wsadata); if(err!=0)prompt(“初始化套接字库失败!”),exits(); if(LOBYTE(wsadata.wVersion)!=2||HIBYTE(wsadata.wHighVersion)!=2)prompt(“套接字库版本号不符!”),WSACleanup(),exits(); server_addr.sin_family=AF_INET; server_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(1234); s_server=socket(AF_INET, SOCK_STREAM,0); if(bind(s_server,(SOCKADDR)&server_addr,sizeof(SOCKADDR))==SOCKET_ERROR)prompt(“套接字绑定失败!”),exits(); if(listen(s_server,SOMAXCONN)<0)prompt(“设置监听状态失败!”),WSACleanup(),exits(); len=sizeof(SOCKADDR); s_accept=accept(s_server,(SOCKADDR*)&accept_addr,&len); if((int)s_accept==SOCKET_ERROR)prompt(“连接失败!”),WSACleanup(),exits(); prompt(“连接成功!”); flog = fopen(“log-server.txt”, “w”); } //基本函数 string ; string ToString(int x){="";+=(x/10+‘0’);+=(x%10+‘0’);return ;} int ToInt(string ){return ([0]-‘0’)*10+[1]-‘0’;} //游戏主体部分 mt19937 rnd(time(NULL)); char mp[60][200]= { ”█████ █████ █████ █████ █████”, ”█ █━━━━██ ██━━━━█ █━━━━██ ██━━━━█ █”, ”█████ █████ █████ █████ █████”, ” ┃ ┃ ┃ ┃ ┃ ”, ”█████ █████ █████ █████ █████”, ”█ █══════█ █══════█ █══════█ █══════█ █”, ”█████ █████ █████ █████ █████”, ” ║ ▔\▁ ┃ ▁/▔ ┃ ▔\▁ ┃ ▁/▔ ║ ”, ”█████ \███/ █████ \███/ █████”, ”█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █”, ”█████ /███\ █████ /███\ █████”, ” ║ ▁/▔ ┃ ▔\▁ ┃ ▁/▔ ┃ ▔\▁ ║ ”, ”█████ █████ \███/ █████ █████”, ”█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █”, ”█████ █████ /███\ █████ █████”, ” ║ ▔\▁ ┃ ▁/▔ ┃ ▔\▁ ┃ ▁/▔ ║ ”, ”█████ \███/ █████ \███/ █████”, ”█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █”, ”█████ /███\ █████ /███\ █████”, ” ║ ▁/▔ ┃ ▔\▁ ┃ ▁/▔ ┃ ▔\▁ ║ ”, ”█████ █████ █████ █████ █████”, ”█ █══════█ █══════█ █══════█ █══════█ █”, ”█████ █████ █████ █████ █████”, ” ║ ║ ║ ”, ”█████ █████ █████”, ”█ █ █ █ █ █”, ”█████ █████ █████”, ” ║ ║ ║ ”, ”█████ █████ █████ █████ █████”, ”█ █══════█ █══════█ █══════█ █══════█ █”, ”█████ █████ █████ █████ █████”, ” ║ ▔\▁ ┃ ▁/▔ ┃ ▔\▁ ┃ ▁/▔ ║ ”, ”█████ \███/ █████ \███/ █████”, ”█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █”, ”█████ /███\ █████ /███\ █████”, ” ║ ▁/▔ ┃ ▔\▁ ┃ ▁/▔ ┃ ▔\▁ ║ ”, ”█████ █████ \███/ █████ █████”, ”█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █”, ”█████ █████ /███\ █████ █████”, ” ║ ▔\▁ ┃ ▁/▔ ┃ ▔\▁ ┃ ▁/▔ ║ ”, ”█████ \███/ █████ \███/ █████”, ”█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █━━━━━━█ █”, ”█████ /███\ █████ /███\ █████”, ” ║ ▁/▔ ┃ ▔\▁ ┃ ▁/▔ ┃ ▔\▁ ║ ”, ”█████ █████ █████ █████ █████”, ”█ █══════█ █══════█ █══════█ █══════█ █”, ”█████ █████ █████ █████ █████”, ” ┃ ┃ ┃ ┃ ┃ ”, ”█████ █████ █████ █████ █████”, ”█ █━━━━██ ██━━━━█ █━━━━██ ██━━━━█ █”, ”█████ █████ █████ █████ █████”, }; int is[70];//0:普通 1:大本营 2:行营 3:山界 vector<int> e[70][3]; #define gotoxy(x,y) SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),(COORD){(short)(x),(short)(y)}) #define color(COLOR) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),COLOR); void hide_cursor() { HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO CursorInfo; GetConsoleCursorInfo(handle,&CursorInfo); CursorInfo.bVisible=false; SetConsoleCursorInfo(handle,&CursorInfo); } bool painted;//是否已经绘制了棋盘 bool turn;//谁先走 void printchess(){painted=true;for(int i=0;i<=50;++i){for(int j=0;j<=100;++j){color(mp[i][j]==”█“[0]||mp[i][j]==”█“[1]?0x77:0xf7);putchar(mp[i][j]);}puts("");}} int a[70],b[70],c[70];//a:棋子类型 b:0(平常) 1(选中) 2(光标) c:mark bool exist;//对方司令是否存活 int selected;//上一个被选择的棋子,没有则-1 int win;//赢没有 void add(int x,int y,int num){if(is[x]!=3||is[y]!=3)e[x][num].push_back(y),e[y][num].push_back(x);} void prompt(string s) { if(!painted){puts(s.c_str());return;} color(0x70); gotoxy(0,52); printf(“%s”,s.c_str()); for(int i=s.size()+1;i<=75;++i)putchar(’ ’); } void init_num() { hide_cursor(); exist=true; is[1]=is[3]=is[61]=is[63]=1; is[11]=is[13]=is[17]=is[21]=is[23]=is[41]=is[43]=is[47]=is[51]=is[53]=2; is[31]=is[33]=3; for(int i=0;i<=60;i+=5) for(int j=0;j<=3;++j)add(i+j,i+j+1,0); for(int i=0;i<=55;i+=5) for(int j=0;j<=4;++j)add(i+j,i+j+5,0); for(int i:{11,13,21,23,41,43,51,53})add(i,i-6,0),add(i,i-4,0),add(i,i+4,0),add(i,i+6,0); for(int i:{5,25,35,55}) for(int j=0;j<=3;++j)add(i+j,i+j+1,1); for(int i=5;i<=50;i+=5)add(i,i+5,2),add(i+4,i+9,2); add(27,32,2);add(37,32,2); system(“color f7”); color(0x70); gotoxy(0,52); for(int =1;<=75;_++)putchar(’ ’); gotoxy(0,0); } char ch[20][10]={” ”,” 炸弹 ”,” 司令 ”,” 军长 ”,” 师长 ”,” 旅长 ”,” 团长 ”,” 营长 ”,” 连长 ”,” 排长 ”,” 工兵 ”,” 地雷 ”,” 军旗 ”,”▓▓▓”}; void upd1(int x,int y=114514,int z=114514)//修改旗子 { int X=x%516,Y=x/54; if(y!=114514)a[x]=y; if(z!=114514)c[x]=z; if(a[x]<0)color(0xfc); if(a[x]>0)color(0xf9); if(a[x]==0)color(0xff); gotoxy(X+2,Y+1); printf(ch[a[x]<0&&(a[x]!=-12||exist)?c[x]:abs(a[x])]); gotoxy(0,51); color(0x70); } void upd2(int x,int y=114514)//修改光标 { int X=x%516,Y=x/54; if(y!=114514)b[x]=y; if(b[x]==0)color(0x77); if(b[x]==1)color(0xee); if(b[x]==2)color(0xbb); gotoxy(X+(is[x]==2)2,Y);for(int i=X+(is[x]==2)2;i<=X+9-(is[x]==2)2;++i)putchar(mp[Y][i]); gotoxy(X+(is[x]==2)2,Y+2);for(int i=X+(is[x]==2)2;i<=X+9-(is[x]==2)2;++i)putchar(mp[Y+2][i]); gotoxy(X,Y+1);printf(”█”);gotoxy(X+8,Y+1);printf(”█”); if(is[x]==1)gotoxy(X-2,Y+1),printf(”█”),gotoxy(X+10,Y+1),printf(”█”); gotoxy(0,51);color(0x70); } bool CANESC; int control(int X=0) { upd2(X,2); while(1) { int ch=getch(),chh=0; if(ch==224)chh=getch(); if(chh==75){upd2(X,selected==X?1:0);X=X/55+(X%5+4)%5;if(is[X]==3)X=X/55+(X%5+4)%5;upd2(X,2);}//L if(chh==77){upd2(X,selected==X?1:0);X=X/55+(X%5+1)%5;if(is[X]==3)X=X/55+(X%5+1)%5;upd2(X,2);}//R if(chh==72){upd2(X,selected==X?1:0);X=(X/5+12)%135+X%5;if(is[X]==3)X=(X/5+12)%135+X%5;upd2(X,2);}//U if(chh==80){upd2(X,selected==X?1:0);X=(X/5+1)%135+X%5;if(is[X]==3)X=(X/5+1)%135+X%5;upd2(X,2);}//D if(ch==97){upd2(X,selected==X?1:0);X=X/55+(X%5+4)%5;if(is[X]==3)X=X/55+(X%5+4)%5;upd2(X,2);}//A if(ch==100){upd2(X,selected==X?1:0);X=X/55+(X%5+1)%5;if(is[X]==3)X=X/55+(X%5+1)%5;upd2(X,2);}//D if(ch==119){upd2(X,selected==X?1:0);X=(X/5+12)%135+X%5;if(is[X]==3)X=(X/5+12)%135+X%5;upd2(X,2);}//W if(ch==115){upd2(X,selected==X?1:0);X=(X/5+1)%135+X%5;if(is[X]==3)X=(X/5+1)%135+X%5;upd2(X,2);}//S if(ch==13){upd2(X,selected==X?1:0);return X;}//Enter if(ch==32){upd2(X,selected==X?1:0);return X;}//空格 if(ch==27&&CANESC){upd2(X,selected==X?1:0);return -1;}//Esc } } int INITIAL[30]={1,1,2,3,4,4,5,5,6,6,7,7,8,8,8,9,9,9,10,10,10,11,11,11}; void init_game() { CANESC=1; prompt(“依次选中两个棋子来进行交换,按Enter或空格选中,按Esc结束。”); shuffle(INITIAL,INITIAL+24,rnd); bool L=rnd()%2; int cnt=0; for(int i=64;i>=35;—i) { if(is[i]==2)continue; if((L==0&&i==63)||(L==1&&i==61))upd1(i,12,13); else upd1(i,INITIAL[cnt++]); } for(int i=0;i<=29;++i)if(is[i]!=2)upd1(i,-1,13); selected=-1; int x,y=62; while(1) { x=control(y);if(x==-1)break;if(a[x]<=0)continue; selected=x;upd2(x,1); y=control(x);if(y==-1)break; if(a[y]>0&&(a[x]!=12||is[y]==1)&&(a[y]!=12||is[x]==1)) swap(a[x],a[y]),upd1(x,a[x]),upd1(y,a[y]); selected=-1;upd2(x,0); } CANESC=0; prompt(“已完成布局,等待对方完成…”); send(“done”),receive();//同步进度 for(int i=64;i>=35;i—)send(ToString(a[i])); for(int i=0;i<=29;i++) { string s=receive(); if(s.size()==1)upd1(i,-(s[0]-‘0’)); else upd1(i,-(s[1]-‘0’+(s[0]-‘0’)*10),13); } turn=rnd()%2; send(ToString(turn^1)); prompt(turn==1?”同步完成!恭喜你,抽到先手!”:“同步完成!很遗憾,你抽到了后手。”); Sleep(2000); } int compare(int x,int y) { int xx=abs(x),yy=abs(y); if(x==12||y==12)win=-1; if(x==-12||y==-12)win=1; if(xx==1||yy==1){exist&=(x!=-2)&(y!=-2);return 0;} if(xx==11)return exist&=(y!=-2),yy==10?y:0; if(yy==11)return exist&=(x!=-2),xx==10?x:0; exist&=(xx!=2||yy!=2); return xx==yy?0:(xx<yy?x:y); } void eat(int p,int x,int mx,int y,int my) { upd2(p,1); upd1(p,x,mx);Sleep(400); upd1(p,y,my);Sleep(400); upd1(p,x,mx);Sleep(400); upd1(p,y,my);Sleep(400); upd1(p,x,mx);Sleep(400); upd1(p,y,my);Sleep(400); int z=compare(x,y); if(z==x)upd1(p,z,mx); else upd1(p,z,my); if(!exist)upd1(1),upd1(3); upd2(p,0); } queue<int> Q; int pre[70]; vector<int> path; void move(vector<int> &path,bool NeedSend=1) { if(NeedSend) { send(ToString(path.size())); for(int i:path)send(ToString(i)); } int x=a[path[0]],y=a[*path.rbegin()],mx=c[path[0]],my=c[*path.rbegin()]; for(int i:path) { upd2(i,1);upd1(i,x,mx); Sleep(path.size()==2?500:200); upd2(i,0);if(i!=*path.rbegin())upd1(i,0,0); } if(y)eat(*path.rbegin(),x,mx,y,my); } bool trans(int s,int t) { path.clear(); if(s==t||a[s]>=11||a[s]<=0||a[t]>0||(is[t]==2&&a[t]))return 0; for(int v:e[s][0]) { if(v!=t)continue; path.push_back(s); path.push_back(t); return move(path),1; } queue<int> q;q.push(s); for(int i=0;i<=64;++i)pre[i]=0; while(!q.empty()) { int x=q.front();q.pop(); for(int y:e[x][1]){if(!pre[y]&&a[y]<=0){pre[y]=x;if(a[y]==0)q.push(y);}} if(a[s]==10)for(int y:e[x][2]){if(!pre[y]&&a[y]<=0){pre[y]=x;if(a[y]==0)q.push(y);}} } if(pre[t]) { path.push_back(t); while(*path.rbegin()!=s)path.push_back(pre[*path.rbegin()]); reverse(path.begin(),path.end()); return move(path),1; } if(a[s]==10)return 0; q.push(s);for(int i=0;i<=64;++i)pre[i]=0; while(!q.empty()) { int x=q.front();q.pop(); for(int y:e[x][2]){if(!pre[y]&&a[y]<=0){pre[y]=x;if(a[y]==0)q.push(y);}} } if(pre[t]) { path.push_back(t); while(*path.rbegin()!=s)path.push_back(pre[*path.rbegin()]); reverse(path.begin(),path.end()); return move(path),1; } return 0; } void game() { init_game(); int x,y=62,X;win=0; while(!win) { while(turn) { prompt(“该你走啦!”); x=control(y); if(a[x]>0) { selected=x;upd2(x,1); y=control(x); if(trans(x,y))turn^=1; selected=-1;upd2(x,0); } if(a[x]<0)upd1(x,114514,c[x]%13+1),y=x; } if(win)break; while(!turn) { prompt(“等待对方行动……”); X=ToInt(receive()); path.clear(); while(X—)path.push_back(64-ToInt(receive())); move(path,0);turn^=1; } } prompt(win==1?”游戏结束,恭喜你获得胜利!”:“游戏结束,很遗憾,你输了!”); close(); } int main() { Connect(); send(Version);if(receive()!=Version){prompt(“游戏版本号不符,请双方更新到最新版本!”);close();} system(“mode con cols=75 lines=53”); SetWindowLong(GetConsoleWindow(),GWL_STYLE,GetWindowLong(GetConsoleWindow(),GWL_STYLE)&~WS_SIZEBOX); init_num(); printchess(); game(); close(); }
剪贴板
剪贴板 70ehu2d9
剪贴板信息
剪贴板信息
- 收录
- 更新