版本比较
三国杀
正文
增加 107 行删除 14 行
4 行未改动5678910111213141516171885 行未改动104105106107108109110111112156 行未改动全部展开26927027127226826927027127327427527627727827928095 行未改动376377378379380381382383384385386387388389390391392393427 行未改动全部展开82182282382481081181281381482582682782882997 行未改动92792892993093193293393493593693793840 行未改动979980981982983984985986987285 行未改动全部展开127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308591 行未改动全部展开190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956186718681869195719581959196019611962196319645 行未改动1970197119721973197419751976197719781979198019811889198219831984198519863 行未改动199019911992199319011994199519961997199898 行未改动4 行未改动#include <ctime>#include <cmath>#include <cstdarg>#ifdef _WIN32#define WIN32_LEAN_AND_MEAN#include <winsock2.h>#include <ws2tcpip.h>#include <conio.h>#include <windows.h>#pragma comment(lib, "ws2_32.lib")#else#include <unistd.h>#endif#include <iostream>85 行未改动 MSG_STEAL, MSG_PINDIAN, MSG_CHAT, MSG_SYSTEM, MSG_PING, MSG_PONG, MSG_EQUIP_CARD, MSG_SKILL_USE, MSG_ROOM_BROADCAST, MSG_ROOM_QUERY,};
// ===================== 卡牌类型 =====================enum CardType {156 行未改动全部展开}
inline const char* getSuitSymbol(int suit) { switch(suit) { case SUIT_SPADE: return "♠"; case SUIT_HEART: return "♥"; case SUIT_CLUB: return "♣"; case SUIT_DIAMOND: return "♦"; case SUIT_SPADE: return "?"; case SUIT_HEART: return "?"; case SUIT_CLUB: return "?"; case SUIT_DIAMOND: return "?"; default: return "?"; }}
95 行未改动struct ChatMsg { char playerName[MAX_NAME_LEN]; char message[MAX_MSG_LEN]; };struct SystemMsg { char message[MAX_MSG_LEN]; };struct EquipMsg { CardInfo card; int seatId; char playerName[MAX_NAME_LEN]; };
struct RoomBroadcastMsg { char roomName[MAX_ROOM_NAME]; char hostName[MAX_NAME_LEN]; char hostIP[64]; unsigned short hostPort; int playerCount; int maxPlayers; bool inGame;};
inline void initMsgHeader(MsgHeader& hdr, MsgType type, const char* sender) { hdr.type = type; hdr.length = 0; if (sender) { strncpy(hdr.sender, sender, MAX_NAME_LEN - 1); hdr.sender[MAX_NAME_LEN - 1] = '\0'; } else hdr.sender[0] = '\0';427 行未改动全部展开 }};
// ===================== 网络层 =====================#ifdef _WIN32#include <winsock2.h>#include <ws2tcpip.h>#pragma comment(lib, "ws2_32.lib")#else#ifndef _WIN32#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>97 行未改动 for (int i = 0; i < peerCount; i++) if (peers[i].active) sendTo(peers[i].ip, peers[i].port, type, data, dataLen); return true; }
bool broadcastToLAN(MsgType type, const void* data, int dataLen) { return sendTo("255.255.255.255", GAME_PORT, type, data, dataLen); }
void poll() { if (!initialized) return; char buffer[4096]; struct sockaddr_in from; socklen_t fromLen = sizeof(from);40 行未改动int g_responseType = 0;int g_responseFrom = -1;bool g_isSinglePlayer = false;time_t g_turnStartTime = 0;vector<RoomBroadcastMsg> g_discoveredRooms;
// ===================== UI 函数 =====================void clearScreen() {#ifdef _WIN32285 行未改动全部展开 } break; } case MSG_PING: g_net.sendTo(senderIP, senderPort, MSG_PONG, NULL, 0); break; case MSG_ROOM_QUERY: { if (g_net.isHost && !g_inGame) { RoomBroadcastMsg rb; memset(&rb, 0, sizeof(rb)); strncpy(rb.roomName, g_net.roomName, MAX_ROOM_NAME - 1); strncpy(rb.hostName, g_net.playerName, MAX_NAME_LEN - 1); strncpy(rb.hostIP, g_net.localIP, sizeof(rb.hostIP) - 1); rb.hostPort = g_net.localPort; rb.playerCount = g_net.peerCount + 1; rb.maxPlayers = g_net.maxPlayers; rb.inGame = g_inGame; g_net.broadcastToLAN(MSG_ROOM_BROADCAST, &rb, sizeof(rb)); } break; } case MSG_ROOM_BROADCAST: { if (len >= (int)sizeof(RoomBroadcastMsg)) { RoomBroadcastMsg rb; memcpy(&rb, data, sizeof(rb)); bool found = false; for (size_t i = 0; i < g_discoveredRooms.size(); i++) { if (strcmp(g_discoveredRooms[i].hostIP, rb.hostIP) == 0) { g_discoveredRooms[i] = rb; found = true; break; } } if (!found) g_discoveredRooms.push_back(rb); if (g_discoveredRooms.size() > 10) g_discoveredRooms.erase(g_discoveredRooms.begin()); } break; } default: break; }}
591 行未改动全部展开 } hostDrawPhase();}
// ===================== 搜索房间 =====================void searchRooms() { g_discoveredRooms.clear(); g_net.broadcastToLAN(MSG_ROOM_QUERY, NULL, 0); printf("[系统] 正在搜索局域网房间...\n"); time_t start = time(NULL); while (time(NULL) - start < 2) { g_net.poll();#ifdef _WIN32 Sleep(100);#else usleep(100000);#endif } clearScreen(); printBanner(); if (g_discoveredRooms.empty()) { printf("[系统] 未发现局域网房间\n"); printf("按回车继续..."); char buf[16]; fgets(buf, sizeof(buf), stdin); return; } COL_GREEN(printf(" 发现以下房间:\n\n")); for (size_t i = 0; i < g_discoveredRooms.size(); i++) { RoomBroadcastMsg& rb = g_discoveredRooms[i]; printf(" %d. [%s] 房主:%s (%s:%d) %d/%d人 %s\n", (int)i + 1, rb.roomName, rb.hostName, rb.hostIP, rb.hostPort, rb.playerCount, rb.maxPlayers, rb.inGame ? "游戏中" : "等待中"); } printf("\n 0. 取消\n"); printf(" 请选择房间编号加入: "); char choice[16]; fgets(choice, sizeof(choice), stdin); int idx = atoi(choice) - 1; if (idx < 0 || idx >= (int)g_discoveredRooms.size()) return; RoomBroadcastMsg& selected = g_discoveredRooms[idx]; JoinRoomMsg jm; memset(&jm, 0, sizeof(jm)); strncpy(jm.playerName, g_myName.c_str(), MAX_NAME_LEN - 1); g_net.sendTo(selected.hostIP, selected.hostPort, MSG_JOIN_ROOM, &jm, sizeof(jm)); printf("[系统] 正在连接 [%s] ...\n", selected.roomName); printf("按回车继续..."); char buf[64]; fgets(buf, sizeof(buf), stdin); if (g_net.inRoom) { strncpy(g_net.hostIP, selected.hostIP, 63); g_net.hostPort = selected.hostPort; g_isSinglePlayer = false; roomLobby(); }}
// ===================== 主菜单 =====================void mainMenu() { while (true) { clearScreen(); printBanner(); COL_CYAN(printf(" 你的IP: %s:%d\n", g_net.localIP, g_net.localPort)); printf(" 名字: %s\n\n", g_myName.c_str()); printf(" 1. 创建房间(联机)\n"); printf(" 2. 加入房间(联机)\n"); printf(" 3. 单机模式(与AI对战)\n"); printf(" 4. 退出\n\n"); printf(" 2. 搜索房间(自动发现局域网房间)\n"); printf(" 3. 手动输入IP加入\n"); printf(" 4. 单机模式(与AI对战)\n"); printf(" 5. 退出\n\n"); printf(" 请选择: "); char choice[16]; fgets(choice, sizeof(choice), stdin); int c = atoi(choice); if (c == 1) {5 行未改动 g_isSinglePlayer = false; printf("\n[系统] 房间 [%s] 已创建! 等待玩家加入...\n", rn); roomLobby(); } else if (c == 2) { searchRooms(); } else if (c == 3) { printf(" 房主IP: "); char ip[64]; fgets(ip, sizeof(ip), stdin); ip[strcspn(ip, "\n")] = '\0'; JoinRoomMsg jm; memset(&jm, 0, sizeof(jm)); strncpy(jm.playerName, g_myName.c_str(), MAX_NAME_LEN - 1); g_net.sendTo(ip, GAME_PORT, MSG_JOIN_ROOM, &jm, sizeof(jm)); printf("[系统] 正在连接...\n"); printf("按回车继续..."); fgets(ip, sizeof(ip), stdin); if (g_net.inRoom) { strncpy(g_net.hostIP, ip, 63); g_net.hostPort = GAME_PORT; g_isSinglePlayer = false; roomLobby(); } } else if (c == 3) { } else if (c == 4) { // 单机模式 printf(" AI数量(1-7): "); char aiStr[8]; fgets(aiStr, sizeof(aiStr), stdin); int aiCount = atoi(aiStr); if (aiCount < 1) aiCount = 1; if (aiCount > 7) aiCount = 7; g_isSinglePlayer = true;3 行未改动 for (int i = 0; i < aiCount; i++) strncpy(names[i + 1], aiNames[i], MAX_NAME_LEN - 1); g_game.playerCount = aiCount + 1; hostStartGame(); if (g_inGame) gameLoop(); } else if (c == 4) return; } else if (c == 5) return; }}
// ===================== 房间等待 =====================98 行未改动