A方
#include<iostream>
#include<cstring>
#include<winsock2.h>
#include<windows.h>
using namespace std;
#define MAX_PLAYERS 10
#define MAP_WIDTH 120
#define MAP_HEIGHT 40
const char GAME_MAP[MAP_HEIGHT][MAP_WIDTH+1] = {
"############# ############食堂############### #######武器室#######",
"#...........###########################.............................# #..................#",
"#-------+...........................................................# #..................#",
"# 反应堆|...........................................................#######..................#",
"# 上 |...########...################......................................................#",
"#-------+...# #...# #......................................................#",
"#...........# #...# #......................................................#",
"#####...##### ###...#### #.............................#######..................#",
" #...# #........# #.............................# #####...############",
" #...# ##### #........# #.............................# #...# ",
"#主控台## #...# #(@)# #........# #.............................# #...# ",
"#.......# #...####...# #........# #.............................# #...# ##导航室##",
"#--+....# #..........# #........# #.............................# #...# #........#",
"# |....# #..........# ##医疗室## #.............................# #...###........#",
"# |....# #...####...# #.............................# #..............#",
"# |....#####...# 监控# #.............................# #..............#",
"# |............# #############...############### #..............#",
"# |............# ##################### #...# #...###........#",
"# |....####....# #...................# #...# ####氧气室###### #...# #........#",
"# |....# #...# #---------------+...# #...# #..............######...# #........#",
"#--+....# #...# #........电力室.|...# #...# #.......................# #........#",
"#.......# #...# #...............|...# #...# #.......................# ##########",
"######### #...# #---------------+...# #...# #..............######...# ",
" #...# #...................# #...# ################ #...# ",
" #...# ################...## #...# #...# ",
" #####...##### #...# #########...##仓库### ###########...###### ",
" #...........# #...# #...................# #..................# ",
" #-------+...# #...# #...................# #..................# ",
" # 反应堆|...###################...#########...................#######..................# ",
" # 下 |..............................................................................# ",
" #-------+..............................................................................# ",
" #...........######################...######............................................# ",
" ############# #...# #...................#######..................# ",
" #################...## #...................# #..................# ",
" #..|.................# #...................# ######淘汰仓######## ",
" #..|.................# #...................# ",
" #..+-----------------# #...................# ",
" #....................# ##################### ",
" ########通讯室######## "
};
struct Player {
SOCKET sock;
char name[101];
int x, y;
bool isImposter;
bool isAlive;
};
Player players[MAX_PLAYERS];
int playerCount = 0;
CRITICAL_SECTION cs;
void broadcast(const char* msg, int excludeIndex = -1) {
EnterCriticalSection(&cs);
for (int i = 0; i < playerCount; i++)
if (i != excludeIndex && players[i].isAlive)
send(players[i].sock, msg, strlen(msg), 0);
LeaveCriticalSection(&cs);
}
void handleMovement(int playerIndex, char direction) {
EnterCriticalSection(&cs);
int& x = players[playerIndex].x;
int& y = players[playerIndex].y;
switch (direction) {
case 'W': if (y > 0 && GAME_MAP[y-1][x] != '#') y--; break;
case 'S': if (y < MAP_HEIGHT-1 && GAME_MAP[y+1][x] != '#') y++; break;
case 'A': if (x > 0 && GAME_MAP[y][x-1] != '#') x--; break;
case 'D': if (x < MAP_WIDTH-1 && GAME_MAP[y][x+1] != '#') x++; break;
}
char updateMsg[256];
sprintf(updateMsg, "POS:%d:%d:%d", playerIndex, x, y);
broadcast(updateMsg);
LeaveCriticalSection(&cs);
}
DWORD WINAPI playerThread(LPVOID param) {
int playerIndex = *(int*)param;
delete (int*)param;
char recvBuf[1024];
while (true) {
int recvLen = recv(players[playerIndex].sock, recvBuf, sizeof(recvBuf)-1, 0);
if (recvLen <= 0) {
EnterCriticalSection(&cs);
players[playerIndex].isAlive = false;
LeaveCriticalSection(&cs);
char disconnectMsg[256];
sprintf(disconnectMsg, "DISCON:%d:%s", playerIndex, players[playerIndex].name);
broadcast(disconnectMsg);
break;
}
recvBuf[recvLen] = '\0';
char cmd[10];
int args[5];
char text[1000];
if (sscanf(recvBuf, "%s", cmd) == 1) {
if (strcmp(cmd, "MOVE") == 0) {
char dir;
if (sscanf(recvBuf, "MOVE:%c", &dir) == 1) {
handleMovement(playerIndex, dir);
}
} else if (strcmp(cmd, "CHAT") == 0) {
if (sscanf(recvBuf, "CHAT:%[^\n]", text) == 1) {
char chatMsg[1024];
sprintf(chatMsg, "CHAT:%d:%s:%s", playerIndex, players[playerIndex].name, text);
broadcast(chatMsg);
}
}
}
}
closesocket(players[playerIndex].sock);
return 0;
}
int main() {
InitializeCriticalSection(&cs);
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
cout << "连接失败!" << endl;
return 1;
}
SOCKET serverSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSock == INVALID_SOCKET) {
cout << "连接失败!" << endl;
WSACleanup();
return 1;
}
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = INADDR_ANY;
if (bind(serverSock, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
cout << "连接失败!" << endl;
closesocket(serverSock);
WSACleanup();
return 1;
}
if (listen(serverSock, MAX_PLAYERS) == SOCKET_ERROR) {
cout << "连接失败!" << endl;
closesocket(serverSock);
WSACleanup();
return 1;
}
cout << "Among Us服务器启动成功,等待玩家连接..." << endl;
cout << "请输入你的名字:";
cin >> players[0].name;
players[0].x = 10;
players[0].y = 10;
players[0].isImposter = false;
players[0].isAlive = true;
playerCount = 1;
while (playerCount < MAX_PLAYERS) {
sockaddr_in clientAddr;
int addrLen = sizeof(clientAddr);
SOCKET clientSock = accept(serverSock, (sockaddr*)&clientAddr, &addrLen);
if (clientSock == INVALID_SOCKET) {
cout << "连接失败!" << endl;
continue;
}
char name[101];
int recvLen = recv(clientSock, name, sizeof(name)-1, 0);
if (recvLen <= 0) {
closesocket(clientSock);
continue;
}
name[recvLen] = '\0';
EnterCriticalSection(&cs);
players[playerCount].sock = clientSock;
strcpy(players[playerCount].name, name);
players[playerCount].x = 20 + playerCount*5;
players[playerCount].y = 10;
players[playerCount].isImposter = false;
players[playerCount].isAlive = true;
char mapMsg[MAP_HEIGHT*(MAP_WIDTH+2) + 100];
sprintf(mapMsg, "MAP:%d:%d\n", MAP_WIDTH, MAP_HEIGHT);
for (int i = 0; i < MAP_HEIGHT; i++) {
strcat(mapMsg, GAME_MAP[i]);
strcat(mapMsg, "\n");
}
send(clientSock, mapMsg, strlen(mapMsg), 0);
for (int i = 0; i < playerCount; i++) {
char posMsg[256];
sprintf(posMsg, "POS:%d:%d:%d", i, players[i].x, players[i].y);
send(clientSock, posMsg, strlen(posMsg), 0);
}
char joinMsg[256];
sprintf(joinMsg, "JOIN:%d:%s:%d:%d", playerCount, name, players[playerCount].x, players[playerCount].y);
broadcast(joinMsg, playerCount);
playerCount++;
LeaveCriticalSection(&cs);
int* index = new int(playerCount-1);
CreateThread(NULL, 0, playerThread, (LPVOID)index, 0, NULL);
cout << name << " 已加入游戏,当前玩家数:" << playerCount << endl;
int x=MessageBox(NULL,"是否不继续加人,剩下的人将由AI人机代替","系统提示",1);
if(x==1)
break;
}
cout << "玩家已满,游戏即将开始!" << endl;
broadcast("START");
while (true) {
//敬请期待
Sleep(1000);
}
closesocket(serverSock);
WSACleanup();
DeleteCriticalSection(&cs);
}B方
#include<iostream>
#include<cstring>
#include<winsock2.h>
#include<windows.h>
#include<conio.h>
using namespace std;
#define MAP_WIDTH 120
#define MAP_HEIGHT 30
char gameMap[MAP_HEIGHT][MAP_WIDTH+1];
char playerName[101];
int playerIndex = -1;
int playerX = 0, playerY = 0;
struct OtherPlayer {
char name[101];
int x, y;
bool isAlive;
};
OtherPlayer otherPlayers[10];
int otherPlayerCount = 0;
bool gameStarted = false;
CRITICAL_SECTION cs;
void drawGame() {
system("cls");
EnterCriticalSection(&cs);
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
bool isPlayer = false;
if (x == playerX && y == playerY) {
cout << '@';
isPlayer = true;
} else
for (int i = 0; i < otherPlayerCount; i++)
if (otherPlayers[i].x == x && otherPlayers[i].y == y && otherPlayers[i].isAlive) {
cout << 'P';
isPlayer = true;
break;
}
if (!isPlayer)
cout << gameMap[y][x];
}
cout << endl;
}
LeaveCriticalSection(&cs);
cout << "\n玩家:" << playerName << " | 位置:(" << playerX << "," << playerY << ")" << endl;
cout << "操作:WASD移动 | Enter聊天 | ESC退出" << endl;
}
DWORD WINAPI recvThread(LPVOID param) {
SOCKET sock = *(SOCKET*)param;
char recvBuf[4096];
while (true) {
int recvLen = recv(sock, recvBuf, sizeof(recvBuf)-1, 0);
if (recvLen <= 0) {
cout << "\n与服务器断开连接!" << endl;
exit(0);
}
recvBuf[recvLen] = '\0';
char cmd[10];
if (sscanf(recvBuf, "%s", cmd) != 1) continue;
EnterCriticalSection(&cs);
if (strcmp(cmd, "MAP") == 0) {
int w, h;
if (sscanf(recvBuf, "MAP:%d:%d\n", &w, &h) == 2) {
char* mapData = strchr(recvBuf, '\n') + 1;
char* line = strtok(mapData, "\n");
for (int y = 0; y < h && line != NULL; y++) {
strncpy(gameMap[y], line, w);
gameMap[y][w] = '\0';
line = strtok(NULL, "\n");
}
}
} else if (strcmp(cmd, "POS") == 0) {
int idx, x, y;
if (sscanf(recvBuf, "POS:%d:%d:%d", &idx, &x, &y) == 3) {
if (idx == playerIndex) {
playerX = x;
playerY = y;
} else {
for (int i = 0; i < otherPlayerCount; i++) {
if (otherPlayers[i].x == idx) {
otherPlayers[i].x = x;
otherPlayers[i].y = y;
break;
}
}
}
}
} else if (strcmp(cmd, "JOIN") == 0) {
int idx, x, y;
char name[101];
if (sscanf(recvBuf, "JOIN:%d:%[^:]:%d:%d", &idx, name, &x, &y) == 4) {
otherPlayers[otherPlayerCount].x = idx;
strcpy(otherPlayers[otherPlayerCount].name, name);
otherPlayers[otherPlayerCount].x = x;
otherPlayers[otherPlayerCount].y = y;
otherPlayers[otherPlayerCount].isAlive = true;
otherPlayerCount++;
}
} else if (strcmp(cmd, "START") == 0) {
gameStarted = true;
} else if (strcmp(cmd, "CHAT") == 0) {
int idx;
char name[101], text[1000];
if (sscanf(recvBuf, "CHAT:%d:%[^:]:%[^\n]", &idx, name, text) == 3) {
cout << "\n[" << name << "]:" << text << endl;
cout << "按任意键继续...";
_getch();
}
} else if (strcmp(cmd, "DISCON") == 0) {
int idx;
char name[101];
if (sscanf(recvBuf, "DISCON:%d:%s", &idx, name) == 2) {
for (int i = 0; i < otherPlayerCount; i++) {
if (otherPlayers[i].x == idx) {
otherPlayers[i].isAlive = false;
break;
}
}
}
}
LeaveCriticalSection(&cs);
if (gameStarted) {
drawGame();
}
}
return 0;
}
int main() {
InitializeCriticalSection(&cs);
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
cout << "WSA初始化失败!" << endl;
return 1;
}
SOCKET clientSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSock == INVALID_SOCKET) {
cout << "创建套接字失败!" << endl;
WSACleanup();
return 1;
}
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
cout << "正在连接服务器..." << endl;
if (connect(clientSock, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
cout << "连接服务器失败!" << endl;
closesocket(clientSock);
WSACleanup();
return 1;
}
cout << "连接成功!请输入你的名字:";
cin >> playerName;
send(clientSock, playerName, strlen(playerName), 0);
CreateThread(NULL, 0, recvThread, (LPVOID)&clientSock, 0, NULL);
cout << "等待游戏开始..." << endl;
while (!gameStarted) {
Sleep(100);
}
while (true) {
if (_kbhit()) {
char key = _getch();
char sendBuf[1024];
EnterCriticalSection(&cs);
switch (key) {
case 'W': case 'w':
sprintf(sendBuf, "MOVE:W");
send(clientSock, sendBuf, strlen(sendBuf), 0);
break;
case 'S': case 's':
sprintf(sendBuf, "MOVE:S");
send(clientSock, sendBuf, strlen(sendBuf), 0);
break;
case 'A': case 'a':
sprintf(sendBuf, "MOVE:A");
send(clientSock, sendBuf, strlen(sendBuf), 0);
break;
case 'D': case 'd':
sprintf(sendBuf, "MOVE:D");
send(clientSock, sendBuf, strlen(sendBuf), 0);
break;
case '\r':
cout << "\n输入消息:";
char text[1000];
cin.ignore();
cin.getline(text, sizeof(text));
sprintf(sendBuf, "CHAT:%s", text);
send(clientSock, sendBuf, strlen(sendBuf), 0);
break;
case 27:
cout << "\n退出游戏..." << endl;
closesocket(clientSock);
WSACleanup();
DeleteCriticalSection(&cs);
return 0;
}
LeaveCriticalSection(&cs);
}
Sleep(50);
}
closesocket(clientSock);
WSACleanup();
DeleteCriticalSection(&cs);
return 0;
}进展
2026.05.15 21:43 初始化完成 yyc:连个初始化都要200多行,那么正常代码…我晕…