正文
增加 236 行删除 229 行
1234567891011121314151617181920212211 行未改动34353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220311 行未改动215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369**运行要求**:上方"工具"->"编译选项"->"编译时加入以下命令"```-std=c++14```、"在连接器命令行加入以下命令"```-lws2_32```**运行过程**:服务端在自己电脑上搜索```Windows PowerShell```->打开并输入```ipconfig```->找到"以太网适配器 以太网:"下方的"IPv4 地址 . . . . . . . . . . . . :"并复制其后内容发给客户端->服务端运行代码->客户端运行代码并将服务端发来的地址输入->开始游玩# 服务端```cpp#define _WIN32_WINNT 0x0500#include <iostream>#include <winsock2.h>#include <windows.h>#include <cstdlib>#include <ctime>#include <conio.h>#pragma comment(lib, "ws2_32.lib")using namespace std;const int W = 22;const int H = 22;const int MAP[H][W] = {{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},11 行未改动{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0}};HANDLE hConsole;int meX, meY;int youX, youY;int oldMeX, oldMeY;int oldYouX, oldYouY;bool solid(int x, int y) {if (x < 0 || y < 0 || x >= W || y >= H) return false;return MAP[y][x] == 0;}bool inSafeArea(int x, int y) {return x >= 4 && x <= 17 && y >= 4 && y <= 17;}void drawChar(int x, int y, char c, int color) {COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(hConsole, pos);SetConsoleTextAttribute(hConsole, color);cout << c;}void drawCell(int x, int y) {if (MAP[y][x])drawChar(x, y, '#', 7);elsedrawChar(x, y, ' ', 7);}int randInt(int a, int b) {return a + rand() % (b - a + 1);}int main() {srand((unsigned int)time(NULL));hConsole = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO ci;ci.dwSize = 1;ci.bVisible = FALSE;SetConsoleCursorInfo(hConsole, &ci);WSADATA wsa;WSAStartup(MAKEWORD(2,2), &wsa);SOCKET svr = socket(AF_INET, SOCK_STREAM, 0);sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(8080);addr.sin_addr.s_addr = htonl(INADDR_ANY);bind(svr, (sockaddr*)&addr, sizeof(addr));listen(svr, 1);cout << "[服务端] 等待客户端..." << endl;SOCKET cli = accept(svr, NULL, NULL);u_long mode = 1;ioctlsocket(cli, FIONBIO, &mode);// 出生点限制在中间安全区do {meX = randInt(0, W-1);meY = randInt(0, H-1);} while (!solid(meX, meY) || !inSafeArea(meX, meY));do {youX = randInt(0, W-1);youY = randInt(0, H-1);} while (!solid(youX, youY) || !inSafeArea(youX, youY) ||(youX == meX && youY == meY));char init[128];sprintf(init, "[INIT|%d|%d|%d|%d]\n", meX, meY, youX, youY);send(cli, init, strlen(init), 0);system("cls");for (int y = 0; y < H; ++y)for (int x = 0; x < W; ++x)drawCell(x, y);drawChar(meX, meY, 'O', FOREGROUND_RED | FOREGROUND_INTENSITY);drawChar(youX, youY, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);oldMeX = meX; oldMeY = meY;oldYouX = youX; oldYouY = youY;char buf[256];string cache;DWORD lastMove = 0;while (true) {int r = recv(cli, buf, 255, 0);if (r > 0) {buf[r] = 0;cache += buf;int pos;while ((pos = cache.find('\n')) != string::npos) {string msg = cache.substr(0, pos);cache.erase(0, pos + 1);int x, y;if (sscanf(msg.c_str(), "[MOVE|%d|%d]", &x, &y) == 2) {if (solid(x, y) && !(x == meX && y == meY)) {drawCell(oldYouX, oldYouY);drawChar(x, y, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);youX = x; youY = y;oldYouX = youX; oldYouY = youY;send(cli, msg.c_str(), msg.length(), 0);send(cli, "\n", 1, 0);}}}}if (_kbhit() && GetTickCount() - lastMove > 120) {char key = _getch();int nx = meX, ny = meY;if (key == 'w' || key == 'W') ny--;else if (key == 's' || key == 'S') ny++;else if (key == 'a' || key == 'A') nx--;else if (key == 'd' || key == 'D') nx++;else continue;if (solid(nx, ny) && !(nx == youX && ny == youY)) {drawCell(oldMeX, oldMeY);drawChar(nx, ny, 'O', FOREGROUND_RED | FOREGROUND_INTENSITY);meX = nx; meY = ny;oldMeX = meX; oldMeY = meY;char move[64];sprintf(move, "[MOVE|%d|%d]\n", meX, meY);send(cli, move, strlen(move), 0);lastMove = GetTickCount();}}Sleep(10);}}```# 客户端```cpp#define _WIN32_WINNT 0x0500#include <winsock2.h>#include <windows.h>#include <iostream>#include <string>#include <conio.h>#pragma comment(lib, "ws2_32.lib")using namespace std;const int W = 22;const int H = 22;const int MAP[H][W] = {{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},11 行未改动{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0}};HANDLE hConsole;int myX, myY;int enemyX, enemyY;int oldMyX, oldMyY;int oldEnemyX, oldEnemyY;bool solid(int x, int y) {if (x < 0 || y < 0 || x >= W || y >= H) return false;return MAP[y][x] == 0;}void drawChar(int x, int y, char c, int color) {COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(hConsole, pos);SetConsoleTextAttribute(hConsole, color);cout << c;}void drawCell(int x, int y) {if (MAP[y][x])drawChar(x, y, '#', 7);elsedrawChar(x, y, ' ', 7);}int main() {hConsole = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO ci;ci.dwSize = 1;ci.bVisible = FALSE;SetConsoleCursorInfo(hConsole, &ci);WSADATA wsa;WSAStartup(MAKEWORD(2,2), &wsa);SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);cout << "IP: ";string ip;getline(cin, ip);sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(8080);addr.sin_addr.s_addr = inet_addr(ip.c_str());if (connect(sock, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) {cout << "连接失败" << endl;return 0;}u_long mode = 1;ioctlsocket(sock, FIONBIO, &mode);char buf[256];string cache;bool ok = false;while (!ok) {int n = recv(sock, buf, 255, 0);if (n > 0) {buf[n] = 0;cache += buf;int pos;while ((pos = cache.find('\n')) != string::npos) {string msg = cache.substr(0, pos);cache.erase(0, pos + 1);int a, b, c, d;if (sscanf(msg.c_str(), "[INIT|%d|%d|%d|%d]", &a, &b, &c, &d) == 4) {enemyX = a; enemyY = b;myX = c; myY = d;ok = true;break;}}}Sleep(10);}system("cls");for (int y = 0; y < H; ++y)for (int x = 0; x < W; ++x)drawCell(x, y);drawChar(myX, myY, 'O', FOREGROUND_RED | FOREGROUND_INTENSITY);drawChar(enemyX, enemyY, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);oldMyX = myX; oldMyY = myY;oldEnemyX = enemyX; oldEnemyY = enemyY;DWORD lastMove = 0;while (true) {int n = recv(sock, buf, 255, 0);if (n > 0) {buf[n] = 0;cache += buf;int pos;while ((pos = cache.find('\n')) != string::npos) {string msg = cache.substr(0, pos);cache.erase(0, pos + 1);int x, y;if (sscanf(msg.c_str(), "[MOVE|%d|%d]", &x, &y) == 2) {if (!solid(x, y)) continue;// ? 关键:忽略自己发出的移动if (x == myX && y == myY) continue;drawCell(oldEnemyX, oldEnemyY);drawChar(x, y, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);enemyX = x; enemyY = y;oldEnemyX = enemyX; oldEnemyY = enemyY;}}}if (_kbhit() && GetTickCount() - lastMove > 120) {char key = _getch();int nx = myX, ny = myY;if (key == 'w' || key == 'W') ny--;else if (key == 's' || key == 'S') ny++;else if (key == 'a' || key == 'A') nx--;else if (key == 'd' || key == 'D') nx++;else continue;if (solid(nx, ny) && !(nx == enemyX && ny == enemyY)) {drawCell(oldMyX, oldMyY);drawChar(nx, ny, 'O', FOREGROUND_RED | FOREGROUND_INTENSITY);myX = nx; myY = ny;oldMyX = myX; oldMyY = myY;char move[64];sprintf(move, "[MOVE|%d|%d]\n", myX, myY);send(sock, move, strlen(move), 0);lastMove = GetTickCount();}}Sleep(10);}}```12345678910111213141516171811 行未改动3031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020111 行未改动213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376**运行要求**:上方"工具"->"编译选项"->"编译时加入以下命令"```-std=c++14```、"在连接器命令行加入以下命令"```-lws2_32```**运行过程**:服务端在自己电脑上搜索```Windows PowerShell```->打开并输入```ipconfig```->找到"以太网适配器 以太网:"下方的"IPv4 地址 . . . . . . . . . . . . :"并复制其后内容发给客户端->服务端运行代码->客户端运行代码并将服务端发来的地址输入->开始游玩# 服务端```cpp#define _WIN32_WINNT 0x0500#include <bits/stdc++.h>#include <winsock2.h>#include <windows.h>#include <conio.h>#pragma comment(lib, "ws2_32.lib")using namespace std;const int W=22,H=22;const int MAP[H][W]={{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},11 行未改动{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0}};HANDLE hConsole;int meX,meY,youX,youY;int oldMeX,oldMeY,oldYouX,oldYouY;int lastDirX=0,lastDirY=0;bool flashRed;DWORD flashStart;bool solid(int x,int y){return x>=0&&y>=0&&x<W&&y<H&&MAP[y][x]==0;}bool rayHitEnemy(int sx,int sy,int dx,int dy,int ex,int ey){int x=sx+dx,y=sy+dy;while(solid(x,y)){if(x==ex&&y==ey)return true;x+=dx;y+=dy;}return false;}void drawChar(int x,int y,char c,int color){SetConsoleCursorPosition(hConsole,{(SHORT)x,(SHORT)y});SetConsoleTextAttribute(hConsole,color);cout<<c;}void drawCell(int x,int y){drawChar(x,y,MAP[y][x]?'#':' ',7);}void fillScreen(int color){CONSOLE_SCREEN_BUFFER_INFO csbi;GetConsoleScreenBufferInfo(hConsole,&csbi);SMALL_RECT sr=csbi.srWindow;DWORD w;FillConsoleOutputAttribute(hConsole,(WORD)color,(sr.Right-sr.Left+1)*(sr.Bottom-sr.Top+1),{sr.Left,sr.Top},&w);}int main(){srand(time(NULL));hConsole=GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO ci={1,FALSE};SetConsoleCursorInfo(hConsole,&ci);WSADATA wsa;WSAStartup(MAKEWORD(2,2),&wsa);SOCKET svr=socket(AF_INET,SOCK_STREAM,0);sockaddr_in addr={0};addr.sin_family=AF_INET;addr.sin_port=htons(8080);addr.sin_addr.s_addr=htonl(INADDR_ANY);bind(svr,(sockaddr*)&addr,sizeof(addr));listen(svr,1);cout<<"[服务端] 等待客户端...\n";SOCKET cli=accept(svr,NULL,NULL);u_long mode=1;ioctlsocket(cli,FIONBIO,&mode);do{meX=rand()%W;meY=rand()%H;}while(!solid(meX,meY));do{youX=rand()%W;youY=rand()%H;}while(!solid(youX,youY)||(youX==meX&&youY==meY));char init[128];sprintf(init,"[INIT|%d|%d|%d|%d]\n",meX,meY,youX,youY);send(cli,init,strlen(init),0);system("cls");for(int y=0;y<H;++y)for(int x=0;x<W;++x)drawCell(x,y);drawChar(meX,meY,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);drawChar(youX,youY,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);oldMeX=meX;oldMeY=meY;oldYouX=youX;oldYouY=youY;char buf[256];string cache;DWORD lastMove=0;while(true){int r=recv(cli,buf,255,0);if(r>0){buf[r]=0;cache+=buf;size_t pos;while((pos=cache.find('\n'))!=string::npos){string msg=cache.substr(0,pos);cache.erase(0,pos+1);int x,y;if(sscanf(msg.c_str(),"[MOVE|%d|%d]",&x,&y)==2){if(solid(x,y)&&!(x==meX&&y==meY)){drawCell(oldYouX,oldYouY);drawChar(x,y,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);youX=x;youY=y;oldYouX=youX;oldYouY=youY;}}else if(msg=="[HIT]"){flashRed=true;flashStart=GetTickCount();fillScreen(BACKGROUND_RED);}}}if(_kbhit()&&GetTickCount()-lastMove>120){char k=_getch();int nx=meX,ny=meY,dx=0,dy=0;if(k=='w'||k=='W'){ny--;dy=-1;}else if(k=='s'||k=='S'){ny++;dy=1;}else if(k=='a'||k=='A'){nx--;dx=-1;}else if(k=='d'||k=='D'){nx++;dx=1;}else if(k==' '){if(rayHitEnemy(meX,meY,lastDirX,lastDirY,youX,youY))send(cli,"[HIT]\n",7,0);lastMove=GetTickCount();continue;}else continue;if(solid(nx,ny)&&!(nx==youX&&ny==youY)){drawCell(oldMeX,oldMeY);drawChar(nx,ny,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);meX=nx;meY=ny;oldMeX=meX;oldMeY=meY;lastDirX=dx;lastDirY=dy;sprintf(buf,"[MOVE|%d|%d]\n",meX,meY);send(cli,buf,strlen(buf),0);lastMove=GetTickCount();}}if(flashRed&&GetTickCount()-flashStart>200){flashRed=false;fillScreen(0);for(int y=0;y<H;++y)for(int x=0;x<W;++x)drawCell(x,y);drawChar(meX,meY,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);drawChar(youX,youY,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);}Sleep(10);}}```# 客户端```cpp#define _WIN32_WINNT 0x0500#include <bits/stdc++.h>#include <winsock2.h>#include <windows.h>#include <conio.h>#pragma comment(lib, "ws2_32.lib")using namespace std;const int W=22,H=22;const int MAP[H][W]={{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},11 行未改动{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0}};HANDLE hConsole;int myX,myY,enemyX,enemyY;int oldMyX,oldMyY,oldEnemyX,oldEnemyY;int lastDirX=0,lastDirY=0;bool flashRed;DWORD flashStart;bool solid(int x,int y){return x>=0&&y>=0&&x<W&&y<H&&MAP[y][x]==0;}bool rayHitEnemy(int sx,int sy,int dx,int dy,int ex,int ey){int x=sx+dx,y=sy+dy;while(solid(x,y)){if(x==ex&&y==ey)return true;x+=dx;y+=dy;}return false;}void drawChar(int x,int y,char c,int color){SetConsoleCursorPosition(hConsole,{(SHORT)x,(SHORT)y});SetConsoleTextAttribute(hConsole,color);cout<<c;}void drawCell(int x,int y){drawChar(x,y,MAP[y][x]?'#':' ',7);}void fillScreen(int color){CONSOLE_SCREEN_BUFFER_INFO csbi;GetConsoleScreenBufferInfo(hConsole,&csbi);SMALL_RECT sr=csbi.srWindow;DWORD w;FillConsoleOutputAttribute(hConsole,(WORD)color,(sr.Right-sr.Left+1)*(sr.Bottom-sr.Top+1),{sr.Left,sr.Top},&w);}int main(){hConsole=GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO ci={1,FALSE};SetConsoleCursorInfo(hConsole,&ci);WSADATA wsa;WSAStartup(MAKEWORD(2,2),&wsa);SOCKET sock=socket(AF_INET,SOCK_STREAM,0);cout<<"IP: ";string ip;getline(cin,ip);sockaddr_in addr={0};addr.sin_family=AF_INET;addr.sin_port=htons(8080);addr.sin_addr.s_addr=inet_addr(ip.c_str());if(connect(sock,(sockaddr*)&addr,sizeof(addr))==SOCKET_ERROR){cout<<"连接失败\n";return 0;}u_long mode=1;ioctlsocket(sock,FIONBIO,&mode);char buf[256];string cache;bool ok=false;while(!ok){int n=recv(sock,buf,255,0);if(n>0){buf[n]=0;cache+=buf;size_t pos;while((pos=cache.find('\n'))!=string::npos){string msg=cache.substr(0,pos);cache.erase(0,pos+1);int a,b,c,d;if(sscanf(msg.c_str(),"[INIT|%d|%d|%d|%d]",&a,&b,&c,&d)==4){enemyX=a;enemyY=b;myX=c;myY=d;ok=true;break;}}}Sleep(10);}system("cls");for(int y=0;y<H;++y)for(int x=0;x<W;++x)drawCell(x,y);drawChar(myX,myY,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);drawChar(enemyX,enemyY,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);oldMyX=myX;oldMyY=myY;oldEnemyX=enemyX;oldEnemyY=enemyY;DWORD lastMove=0;while(true){int n=recv(sock,buf,255,0);if(n>0){buf[n]=0;cache+=buf;size_t pos;while((pos=cache.find('\n'))!=string::npos){string msg=cache.substr(0,pos);cache.erase(0,pos+1);int x,y;if(sscanf(msg.c_str(),"[MOVE|%d|%d]",&x,&y)==2){if(solid(x,y)&&!(x==myX&&y==myY)){drawCell(oldEnemyX,oldEnemyY);drawChar(x,y,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);enemyX=x;enemyY=y;oldEnemyX=enemyX;oldEnemyY=enemyY;}}else if(msg=="[HIT]"){flashRed=true;flashStart=GetTickCount();fillScreen(BACKGROUND_RED);}}}if(_kbhit()&&GetTickCount()-lastMove>120){char k=_getch();int nx=myX,ny=myY,dx=0,dy=0;if(k=='w'||k=='W'){ny--;dy=-1;}else if(k=='s'||k=='S'){ny++;dy=1;}else if(k=='a'||k=='A'){nx--;dx=-1;}else if(k=='d'||k=='D'){nx++;dx=1;}else if(k==' '){if(rayHitEnemy(myX,myY,lastDirX,lastDirY,enemyX,enemyY))send(sock,"[HIT]\n",7,0);lastMove=GetTickCount();continue;}else continue;if(solid(nx,ny)&&!(nx==enemyX&&ny==enemyY)){drawCell(oldMyX,oldMyY);drawChar(nx,ny,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);myX=nx;myY=ny;oldMyX=myX;oldMyY=myY;lastDirX=dx;lastDirY=dy;sprintf(buf,"[MOVE|%d|%d]\n",myX,myY);send(sock,buf,strlen(buf),0);lastMove=GetTickCount();}}if(flashRed&&GetTickCount()-flashStart>200){flashRed=false;fillScreen(0);for(int y=0;y<H;++y)for(int x=0;x<W;++x)drawCell(x,y);drawChar(myX,myY,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);drawChar(enemyX,enemyY,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);}Sleep(10);}}```