版本比较
论国际象棋联机版
正文
增加 100 行删除 100 行
349 行未改动全部展开35035135235335435435535635735857 行未改动416417418419420420421422423424278 行未改动全部展开7037047057067077077087097107117117127137147157167177177187197207218 行未改动73073173273373473473573673773829 行未改动7687697707717727727737747757763 行未改动7807817827837847847857867877889 行未改动7987998008018028028038048058064 行未改动81181281381481581581681781881982082182282382382482582682741 行未改动86987087187287387387487587687752 行未改动9309319329339349349359369379387 行未改动9469479489499509509519529539549559569579589589599609619629629639649659662 行未改动969970971972973973974975976977978979980981981982983983984985986987988989990991992992993994994995996997998998999100010011002100310041005100610071008100810091010101110111012101310141015101610171018101810191020102110221022102310241025102610271028102910301030103110321033103410351036103710371038103910401041104210431044104410451046104710471048104910501051105210531053105410551056105710581058105910601061106210631063106410651066106710681068106910701071107210 行未改动10831084108510861087108710881089109010917 行未改动10991100110111021103110311041105110611071108110911101111111111121113111411151116111611171118111911201121112211231124112511251126112711271128112911301131113111321133113411351136113711381139114011411141114211431144114411451146114711481149115011511151115211531154115511551156115711581159116011611162116311631164116511661167116811691170117011711172117311741175117611771177117811791180118011811182118311841185118611861187118811891190119111911192119311941195119611961197119811991200120112011202120312041205235 行未改动全部展开14411442144314441445144514461447144814492 行未改动14521453145414551456145614571458145914601461146214631463146414651466146714681469146914701471147214721473147414751476147714781479148014811481148214831484148414851486148714881488148914901491149214931494149514961497149814981499150015011502150315041504150515061507150818 行未改动1527152815291530153115311532153315341535153615371538153815391540154115421543154315441545154615478 行未改动155615571558155915601560156115621563156434 行未改动159916001601160216031603160416051606160710 行未改动161816191620162116221622162316241625162616261627162816291630158 行未改动全部展开178917901791179217931793179417951796179717981799180018001801180218031804180518061807180818081809181018111812181318141814181518161817181818191820182118211822182318241824182518261827182818291830183118321832183318341835183611 行未改动184818491850185118521852185318541855185518561857185818591860186118621862186318641865186518661867186818691869187018711872187318741874187518761877187818791879188018811882188318841884188518861887188818891890189118911892189318941894189518961897189818991900190019011902190319041905190619071908190819091910191119121913191319141915191619161917191819191920192019211922192319241925192619271927192819291930193019311932193319341935193519361937193819391940194019411942194319441945194519461947194819491950195119511952195319541955195619571957195819591960196119621963196419651966196619671968196919702 行未改动349 行未改动全部展开}
```# Ganlai的AI版@[__Ganlai__](www.luogu.com.cn/user/1234009)造了一个AI版! @[__Ganlai__](www.luogu.com.cn/user/1234009)造了一个AI版!```cpp#include<bits/stdc++.h>#include<windows.h>#include<conio.h>57 行未改动 memset(cmv, 0, sizeof(cmv)); generateLegalMoves(i, j, !isWhite); bool canAttackKing = cmv[kx][ky]; memcpy(cmv, tempCmv, sizeof(cmv));
if (canAttackKing) return true; } } return false;278 行未改动全部展开 memset(cmv, 0, sizeof(cmv)); } } }
if (allMoves.empty()) { return isMaximizingPlayer ? -1e9 : 1e9; }
if (isMaximizingPlayer) { int maxEval = -1e9; for (size_t i = 0; i < allMoves.size(); i++) { Move& move = allMoves[i]; if (!isMoveSafe(move, false)) continue;
char temp = chess_map[move.x2][move.y2]; chess_map[move.x2][move.y2] = chess_map[move.x1][move.y1]; chess_map[move.x1][move.y1] = ' '; int eval = minimax(depth - 1, alpha, beta, false);8 行未改动 int minEval = 1e9; for (size_t i = 0; i < allMoves.size(); i++) { Move& move = allMoves[i]; if (!isMoveSafe(move, true)) continue;
char temp = chess_map[move.x2][move.y2]; chess_map[move.x2][move.y2] = chess_map[move.x1][move.y1]; chess_map[move.x1][move.y1] = ' '; int eval = minimax(depth - 1, alpha, beta, true);29 行未改动 } for (size_t i = 0; i < allMoves.size(); i++) { Move& move = allMoves[i]; if (!isMoveSafe(move, false)) continue;
char temp = chess_map[move.x2][move.y2]; chess_map[move.x2][move.y2] = chess_map[move.x1][move.y1]; chess_map[move.x1][move.y1] = ' '; int currentScore = minimax(3, -1e9, 1e9, false);3 行未改动 bestScore = currentScore; bestMove = move; } }
return bestMove;}
void computerMakeMove() {9 行未改动 PIECE_VALUE['B'] = 330; PIECE_VALUE['b'] = -330; PIECE_VALUE['R'] = 500; PIECE_VALUE['r'] = -500; PIECE_VALUE['Q'] = 900; PIECE_VALUE['q'] = -900; PIECE_VALUE['K'] = 20000; PIECE_VALUE['k'] = -20000;
int ppp=0; while(1){ if (!isKingAlive(true)) { system("cls");4 行未改动 system("cls"); cout << "\n\t\t黑方王被吃,白方(玩家)胜利!\n"; break; }
ched=0; if(!kkk){ gbx=gby=ppp*7+1; locked=0; } kkk=0; print();
if (ppp == 0) { while(!locked){ char ch=getch(); if(ch=='w'&&gby>1)gby--;41 行未改动 } else { cout << "\n\t\t电脑思考中...\n"; computerMakeMove(); }
memset(cmv,0,sizeof(cmv)); ppp=!ppp; } cout << "\n\t\t按任意键退出...";52 行未改动 FILE* flog = nullptr; bool Send(const string& data); string Receive(); void Close();
// 初始化服务器 bool Init() { // 初始化Winsock WORD w_req = MAKEWORD(2, 2);7 行未改动 cout << "套接字库版本号不符!" << endl; WSACleanup(); return false; }
// 创建服务器套接字 s_server = socket(AF_INET, SOCK_STREAM, 0); if (s_server == INVALID_SOCKET) { cout << "创建套接字失败!错误码:" << WSAGetLastError() << endl; WSACleanup(); return false; }
// 设置套接字选项,允许端口复用 int opt = 1; setsockopt(s_server, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
// 绑定地址与端口 server_addr.sin_family = AF_INET; server_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); // 监听所有网卡 server_addr.sin_port = htons(PORT);2 行未改动 closesocket(s_server); WSACleanup(); return false; }
// 开始监听 if (listen(s_server, SOMAXCONN) == SOCKET_ERROR) { cout << "设置监听状态失败!错误码:" << WSAGetLastError() << endl; closesocket(s_server); WSACleanup(); return false; }
cout << "服务器已启动,监听端口 " << PORT << ",等待客户端连接..." << endl;
// 接受客户端连接 s_accept = accept(s_server, (SOCKADDR*)&accept_addr, &len); if (s_accept == INVALID_SOCKET) { cout << "接受客户端连接失败!错误码:" << WSAGetLastError() << endl; closesocket(s_server); WSACleanup(); return false; }
cout << "客户端已连接!IP:" << inet_ntoa(accept_addr.sin_addr) << endl;
// 初始化日志文件 flog = fopen("log-server.txt", "w"); if (!flog) cout << "日志文件创建失败!" << endl;
// 版本校验 Send(VERSION); string client_version = Receive(); if (client_version != VERSION) { cout << "版本号不匹配!客户端版本:" << client_version << ",服务器版本:" << VERSION << endl; Close(); return false; } cout << "版本校验通过!" << endl;
return true; }
// 发送数据(通用字符串) bool Send(const string& data) { if (s_accept == INVALID_SOCKET) { cout << "未连接到客户端!" << endl; return false; }
char send_buf[MAX_LEN] = {0}; strncpy(send_buf, data.c_str(), min((int)data.size(), MAX_LEN - 1)); int send_len = send(s_accept, send_buf, MAX_LEN, 0);
if (flog) fprintf(flog, "发送:%s\n", data.c_str()); if (send_len <= 0) { cout << "发送失败!错误码:" << WSAGetLastError() << endl; return false; } return true; }
// 发送移动指令 bool SendMove(const MoveCmd& cmd) { string data = to_string(cmd.gbx) + "," + to_string(cmd.gby) + "," + to_string(cmd.xzx) + "," + to_string(cmd.xzy) + "," + cmd.promote; return Send(data); }
// 接收数据(通用字符串) string Receive() { if (s_accept == INVALID_SOCKET) { cout << "未连接到客户端!" << endl; return ""; }
char recv_buf[MAX_LEN] = {0}; int recv_len = recv(s_accept, recv_buf, MAX_LEN, 0);
if (recv_len <= 0) { if (recv_len == 0) cout << "客户端已断开连接!" << endl; else cout << "接收失败!错误码:" << WSAGetLastError() << endl; return ""; }
string data = recv_buf; if (flog) fprintf(flog, "接收:%s\n", data.c_str()); return data; }
// 接收移动指令 bool ReceiveMove(MoveCmd& cmd) { string data = Receive(); if (data.empty()) return false;
// 解析指令:gbx,gby,xzx,xzy,promote sscanf(data.c_str(), "%d,%d,%d,%d,%c", &cmd.gbx, &cmd.gby, &cmd.xzx, &cmd.xzy, &cmd.promote); return true; }
// 关闭服务器 void Close() { if (flog) fclose(flog); if (s_accept != INVALID_SOCKET) closesocket(s_accept);10 行未改动 FILE* flog = nullptr; bool Send(const string& data); string Receive(); void Close();
// 初始化客户端(参数:服务器IP) bool Init(const string& server_ip) { // 初始化Winsock WORD w_req = MAKEWORD(2, 2);7 行未改动 cout << "套接字库版本号不符!" << endl; WSACleanup(); return false; }
// 创建客户端套接字 s_client = socket(AF_INET, SOCK_STREAM, 0); if (s_client == INVALID_SOCKET) { cout << "创建套接字失败!错误码:" << WSAGetLastError() << endl; WSACleanup(); return false; }
// 设置服务器地址 server_addr.sin_family = AF_INET; server_addr.sin_addr.S_un.S_addr = inet_addr(server_ip.c_str()); server_addr.sin_port = htons(PORT);
// 连接服务器 cout << "正在连接服务器 " << server_ip << ":" << PORT << "..." << endl; if (connect(s_client, (SOCKADDR*)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) { cout << "连接服务器失败!请检查IP和端口,错误码:" << WSAGetLastError() << endl; closesocket(s_client); WSACleanup(); return false; }
cout << "连接服务器成功!" << endl;
// 初始化日志文件 flog = fopen("log-client.txt", "w"); if (!flog) cout << "日志文件创建失败!" << endl;
// 版本校验 string server_version = Receive(); if (server_version != VERSION) { cout << "版本号不匹配!服务器版本:" << server_version << ",客户端版本:" << VERSION << endl; Close(); return false; } Send(VERSION); cout << "版本校验通过!" << endl;
return true; }
// 发送数据(通用字符串) bool Send(const string& data) { if (s_client == INVALID_SOCKET) { cout << "未连接到服务器!" << endl; return false; }
char send_buf[MAX_LEN] = {0}; strncpy(send_buf, data.c_str(), min((int)data.size(), MAX_LEN - 1)); int send_len = send(s_client, send_buf, MAX_LEN, 0);
if (flog) fprintf(flog, "发送:%s\n", data.c_str()); if (send_len <= 0) { cout << "发送失败!错误码:" << WSAGetLastError() << endl; return false; } return true; }
// 发送移动指令 bool SendMove(const MoveCmd& cmd) { string data = to_string(cmd.gbx) + "," + to_string(cmd.gby) + "," + to_string(cmd.xzx) + "," + to_string(cmd.xzy) + "," + cmd.promote; return Send(data); }
// 接收数据(通用字符串) string Receive() { if (s_client == INVALID_SOCKET) { cout << "未连接到服务器!" << endl; return ""; }
char recv_buf[MAX_LEN] = {0}; int recv_len = recv(s_client, recv_buf, MAX_LEN, 0);
if (recv_len <= 0) { if (recv_len == 0) cout << "服务器已断开连接!" << endl; else cout << "接收失败!错误码:" << WSAGetLastError() << endl; return ""; }
string data = recv_buf; if (flog) fprintf(flog, "接收:%s\n", data.c_str()); return data; }
// 接收移动指令 bool ReceiveMove(MoveCmd& cmd) { string data = Receive(); if (data.empty()) return false;
// 解析指令:gbx,gby,xzx,xzy,promote sscanf(data.c_str(), "%d,%d,%d,%d,%c", &cmd.gbx, &cmd.gby, &cmd.xzx, &cmd.xzy, &cmd.promote); return true; }
// 关闭客户端 void Close() { if (flog) fclose(flog); if (s_client != INVALID_SOCKET) closesocket(s_client);235 行未改动全部展开 memset(cmv, 0, sizeof(cmv)); generateLegalMoves(i, j, !isWhite); bool canAttackKing = cmv[kx][ky]; memcpy(cmv, tempCmv, sizeof(cmv));
if (canAttackKing) return true; } } return false;2 行未改动// 检查是否将死bool isCheckmate(bool isWhite) { // 如果没有被将军,不是将死 if (!isInCheck(isWhite)) return false;
// 检查该方所有棋子是否有合法移动 for (int i = 1; i <= 8; i++) { for (int j = 1; j <= 8; j++) { char piece = chess_map[i][j]; if (piece == ' ') continue; if ((isWhite && islower(piece)) || (!isWhite && isupper(piece))) continue;
// 保存当前状态 char temp_map[9][9]; memcpy(temp_map, chess_map, sizeof(temp_map)); bool temp_cmv[9][9]; memcpy(temp_cmv, cmv, sizeof(temp_cmv));
// 生成该棋子的合法移动 generateLegalMoves(i, j, isWhite);
// 检查每个合法移动是否能解除将军 for (int x = 1; x <= 8; x++) { for (int y = 1; y <= 8; y++) { if (cmv[x][y]) { // 模拟移动 char captured = chess_map[x][y]; chess_map[x][y] = chess_map[i][j]; chess_map[i][j] = ' ';
// 检查移动后是否还被将军 bool stillInCheck = isInCheck(isWhite);
// 恢复棋盘 chess_map[i][j] = chess_map[x][y]; chess_map[x][y] = captured;
// 如果有任何移动能解除将军,就不是将死 if (!stillInCheck) { memcpy(chess_map, temp_map, sizeof(temp_map)); memcpy(cmv, temp_cmv, sizeof(temp_cmv)); return false; } } } }
// 恢复状态 memcpy(chess_map, temp_map, sizeof(temp_map)); memcpy(cmv, temp_cmv, sizeof(temp_cmv)); } }
// 没有任何合法移动能解除将军,是将死 return true;}
18 行未改动 // 可移动位置 else if (cmv[j][i]) SetColor(YELLOW, BLACK); else SetColor(BLACK, WHITE);
// 绘制棋盘格子 if (chess_map[j][i] != ' ') cout << '('; else cout << '['; cout << chess_map[j][i]; if (chess_map[j][i] != ' ') cout << ')'; else cout << ']';
SetColor(BLACK, WHITE); } cout << endl; }
if (isInCheck(true)) cout << "\n\t\t\t\t\t\t白方被将军!"; if (isInCheck(false)) cout << "\n\t\t\t\t\t\t黑方被将军!"; SetColor(BLACK, WHITE);}8 行未改动 chess_map[cmd.xzx][cmd.xzy] = chess_map[cmd.gbx][cmd.gby]; chess_map[cmd.gbx][cmd.gby] = ' '; gbx=gby=xzx=xzy=0;locked=0; memset(cmv, 0, sizeof(cmv));
// 检查国王是否被吃掉(胜负判定) if(findKing(true)==make_pair(-1,-1)) { win = false; // 白方国王被吃,黑方胜利 }34 行未改动 locked = false; } kkk = false; print();
// 选择棋子 while (!locked) { char ch = getch(); if (ch == 'w' && gby > 1) gby--;10 行未改动 } } print(); }
print(); xzx = gbx; xzy = gby;
// 棋子移动规则判断 // 兵(黑) if (chess_map[gbx][gby] == 'P') { if (gbx == 7) {158 行未改动全部展开 if (ch == 'w' && xzy > 1) xzy--; if (ch == 'a' && xzx > 1) xzx--; if (ch == 's' && xzy < 8) xzy++; if (ch == 'd' && xzx < 8) xzx++;
// 空格确认移动 if (ch == ' ') { if (cmv[xzx][xzy]) { locked = false; cmd.xzx = xzx; cmd.xzy = xzy;
// 处理兵升变逻辑 if ((chess_map[gbx][gby] == 'p' && xzx == 8) || (chess_map[gbx][gby] == 'P' && xzx == 1)) { cout << "\n\n请选择升变棋子 (q/r/b/n): "; cin >> promote_piece; cmd.promote = promote_piece; chess_map[gbx][gby]=promote_piece; }
// 执行移动 ExecuteMove(cmd); break; } }
// ESC取消选择 if (ch == 27) { locked = false; kkk = true; break; }
print(); }
return cmd;}
// 获取本机IP地址string getIPAddress() { WSADATA wsaData; char hostName[256];
if (WSAStartup(MAKEWORD(2, 0), &wsaData) == 0) { if (gethostname(hostName, sizeof(hostName)) == 0) { hostent* host = gethostbyname(hostName); if (host != nullptr) {11 行未改动 cout << "请选择模式:" << endl; cout << "1. 服务器模式(黑方)" << endl; cout << "2. 客户端模式(白方)" << endl; cout << "输入选择(1/2):";
int choice; cin >> choice;
if (choice == 1) { // 服务器模式(黑方) if (!Server::Init()) { system("pause"); return 1; }
while (true) { // 检查游戏是否结束 if (findKing(true)==make_pair(-1,-1) || findKing(false)==make_pair(-1,-1) || if (findKing(true)==make_pair(-1,-1) || findKing(false)==make_pair(-1,-1) || isCheckmate(true) || isCheckmate(false)) { break; }
// 服务器方下棋 cout << "\n你的回合(黑方),使用WASD移动,空格选择/确认,ESC取消" << endl; MoveCmd cmd = LocalPlay(1); if (kkk) continue;
// 发送移动指令给客户端 if (!Server::SendMove(cmd)) { break; }
// 检查游戏是否结束 if (win == false) { // 黑方胜利 break; }
// 等待客户端回应 cout << "\n等待对方回合..." << endl; MoveCmd client_cmd; if (!Server::ReceiveMove(client_cmd)) { break; }
// 执行客户端的移动 ExecuteMove(client_cmd);
// 检查游戏是否结束 if (win == true) { // 白方胜利 break; } }
Server::Close(); } else if (choice == 2) { // 客户端模式(白方) string server_ip; cout << "请输入服务器IP地址:"; cin >> server_ip;
if (!Client::Init(server_ip)) { system("pause"); return 1; }
while (true) { // 检查游戏是否结束 if (findKing(true)==make_pair(-1,-1) || findKing(false)==make_pair(-1,-1) || if (findKing(true)==make_pair(-1,-1) || findKing(false)==make_pair(-1,-1) || isCheckmate(true) || isCheckmate(false)) { break; }
// 等待服务器方下棋 cout << "\n等待对方回合..." << endl; MoveCmd server_cmd; if (!Client::ReceiveMove(server_cmd)) { break; }
// 执行服务器的移动 ExecuteMove(server_cmd);
// 检查游戏是否结束 if (win == 0) { // 黑方胜利 break; }
// 客户端方下棋 cout << "\n你的回合(白方),使用WASD移动,空格选择/确认,ESC取消" << endl; MoveCmd cmd = LocalPlay(0); if (kkk) continue;
// 发送移动指令给服务器 if (!Client::SendMove(cmd)) { break; }
// 检查游戏是否结束 if (win == 1) { // 白方胜利 break; } }
Client::Close(); } else { cout << "无效的选择!" << endl; }
// 游戏结束后的提示 if (win == 1) { cout << "\n\n游戏结束!白方获得胜利!" << endl; } else if (win != -1 && (findKing(true)==make_pair(-1,-1) || isCheckmate(true))) { cout << "\n\n游戏结束!黑方获得胜利!" << endl; } else { cout << "\n\n游戏结束!平局!" << endl; }
system("pause"); return 0;}```2 行未改动