正文
增加 151 行删除 121 行
12345678910111213141516171819202119 行未改动41424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051066 行未改动11311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618719 行未改动2072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712723 行未改动276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339**运行要求**:上方"工具"->"编译选项"->"编译时加入以下命令"```-std=c++14```、"在连接器命令行加入以下命令"```-lws2_32```**运行过程**:服务端在自己电脑上搜索```Windows PowerShell```->打开并输入```ipconfig```->找到"以太网适配器 以太网:"下方的"IPv4 地址 . . . . . . . . . . . . :"并复制其后内容发给客户端->服务端运行代码->客户端运行代码并将服务端发来的地址输入->开始游玩# 服务端```cpp#define _WIN32_WINNT 0x0600#include <iostream>#include <winsock2.h>#include <windows.h>#include <cstring>#include <random>#include <chrono>#include <conio.h>#include <algorithm>#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},19 行未改动{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 = -1, oldMeY = -1;int oldYouX = -1, oldYouY = -1;inline bool solid(int x, int y) {return x >= 0 && y >= 0 && x < W && y < H && MAP[y][x] == 0;}void drawChar(int x, int y, char c, int color) {COORD pos = { (SHORT)x, (SHORT)y };SetConsoleCursorPosition(hConsole, pos);SetConsoleTextAttribute(hConsole, color);cout << c;}void drawCell(int x, int y) {char c = (MAP[y][x] == 1) ? '#' : ' ';drawChar(x, y, c, 7);}mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());int main() {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 = {AF_INET, htons(8080)};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);const int cx = 10, cy = 10, rx = 8, ry = 8;do {meX = uniform_int_distribution<int>(0, W-1)(rng);meY = uniform_int_distribution<int>(0, H-1)(rng);} while (!solid(meX, meY) ||((meX-cx)*(meX-cx)/float(rx*rx) + (meY-cy)*(meY-cy)/float(ry*ry)) > 1.0f);do {youX = uniform_int_distribution<int>(0, W-1)(rng);youY = uniform_int_distribution<int>(0, H-1)(rng);} while (!solid(youX, youY) ||((youX-cx)*(youX-cx)/float(rx*rx) + (youY-cy)*(youY-cy)/float(ry*ry)) > 1.0f ||(youX == meX && youY == meY));string init = "[INIT|" + to_string(meX) + "|" + to_string(meY) + "|" +to_string(youX) + "|" + to_string(youY) + "]\n";send(cli, init.c_str(), init.size(), 0);system("cls");for (int y = 0; y < H; ++y)for (int x = 0; x < W; ++x)6 行未改动oldYouX = youX; oldYouY = youY;char buf[256];string cache;DWORD lastMoveTime = 0;while (true) {int r = recv(cli, buf, sizeof(buf)-1, 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;}}}}if (_kbhit()) {DWORD now = GetTickCount();if (now - lastMoveTime > 150) {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;string move = "[MOVE|" + to_string(meX) + "|" + to_string(meY) + "]\n";send(cli, move.c_str(), move.size(), 0);lastMoveTime = now;}}}Sleep(10);}closesocket(cli);closesocket(svr);WSACleanup();return 0;}```# 客户端```cpp#define _WIN32_WINNT 0x0600#include <iostream>#include <winsock2.h>#include <windows.h>#include <cstring>#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},19 行未改动{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 = -1, meY = -1;int himX = -1, himY = -1;int oldMeX = -1, oldMeY = -1;int oldHimX = -1, oldHimY = -1;bool ok = false;inline bool solid(int x, int y) {return x >= 0 && y >= 0 && x < W && y < H && MAP[y][x] == 0;}void drawChar(int x, int y, char c, int color) {COORD pos = { (SHORT)x, (SHORT)y };SetConsoleCursorPosition(hConsole, pos);SetConsoleTextAttribute(hConsole, color);cout << c;}void drawCell(int x, int y) {char c = (MAP[y][x] == 1) ? '#' : ' ';drawChar(x, y, c, 7);}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 = {AF_INET, htons(8080)};addr.sin_addr.s_addr = inet_addr(ip.c_str());connect(sock, (sockaddr*)&addr, sizeof(addr));u_long mode = 1;ioctlsocket(sock, FIONBIO, &mode);char buf[256];string cache;while (!ok) {int r = recv(sock, buf, sizeof(buf)-1, 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 aX, aY, bX, bY;if (sscanf(msg.c_str(), "[INIT|%d|%d|%d|%d]", &aX, &aY, &bX, &bY) == 4) {himX = aX; himY = aY;meX = bX; meY = bY;ok = true;}}}Sleep(10);3 行未改动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(himX, himY, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);oldMeX = meX; oldMeY = meY;oldHimX = himX; oldHimY = himY;DWORD lastMoveTime = 0;while (true) {int r = recv(sock, buf, sizeof(buf)-1, 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)) {drawCell(oldHimX, oldHimY);drawChar(x, y, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);himX = x; himY = y;oldHimX = himX; oldHimY = himY;}}}}if (_kbhit()) {DWORD now = GetTickCount();if (now - lastMoveTime > 150) {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 == himX && ny == himY)) {drawCell(oldMeX, oldMeY);drawChar(nx, ny, 'O', FOREGROUND_RED | FOREGROUND_INTENSITY);meX = nx; meY = ny;oldMeX = meX; oldMeY = meY;string move = "[MOVE|" + to_string(meX) + "|" + to_string(meY) + "]\n";send(sock, move.c_str(), move.size(), 0);lastMoveTime = now;}}}Sleep(10);}closesocket(sock);WSACleanup();return 0;}```123456789101112131415161718192019 行未改动4041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201216 行未改动12812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020119 行未改动2212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033 行未改动307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369**运行要求**:上方"工具"->"编译选项"->"编译时加入以下命令"```-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},19 行未改动{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)6 行未改动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},19 行未改动{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);3 行未改动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);}}```