跳到正文

休闲·娱乐

论国际象棋联机版

__luo2031__约 106 分钟
原文
文章信息与版本

文章信息

收录
2026年1月10日
更新
2026年1月10日
篇幅
约 106 分钟

版本

  1. v2当前
  2. v1

alpha版的创立

前几个月班里流行国际象棋,所以我手搓了一个代码。

C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
//wasd to move,space to choose
using namespace std;
HANDLE hCon;
enum Color {BLACK, DARKBLUE = 1, DARKGREEN, DARKTEAL, \
	DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, \
	BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
void SetColor(Color ground,Color font) {
	if(hCon == NULL)
		hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hCon, ground*16+font);
}
char chess_map[9][9]={
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ','r','n','b','k','q','b','n','r'},
	{' ','p','p','p','p','p','p','p','p'},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ','P','P','P','P','P','P','P','P'},
	{' ','R','N','B','K','Q','B','N','R'}
};
int gbx,gby,xzx,xzy;
bool cmv[9][9];
bool locked,ched,ching,kkk;
void print(){
	system("cls");
	cout<<"\n\n\n\n\n\n\n\n";
	for(int i=1;i<=8;i++){
		cout<<"\t\t\t\t\t\t";
		for(int j=1;j<=8;j++){
			if(i==gby&&j==gbx){
				if(!locked)SetColor(BLUE,BLACK);
				else SetColor(GREEN,BLACK);
			}
			else if(i==xzy&&j==xzx&&ching)
				SetColor(RED,BLACK);
			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;
	}
	SetColor(BLACK,WHITE);
}
int main(){
	int ppp=0;
	while(1){
		ched=0;
		if(!kkk){
			gbx=gby=ppp*7+1;
			locked=0;
		}
		kkk=0;
		print();
		ppp=!ppp;
		while(!locked){
			char ch=getch();
			if(ch=='w'&&gby>1)gby--;
			if(ch=='a'&&gbx>1)gbx--;
			if(ch=='s'&&gby<8)gby++;
			if(ch=='d'&&gbx<8)gbx++;
			if(ch==' ')
				if((ppp==1&&chess_map[gbx][gby]>='a'&&\
					chess_map[gbx][gby]<='z')||\
					(ppp==0&&chess_map[gbx][gby]>='A'&&\
					 chess_map[gbx][gby]<='Z'))locked=1;
			print();
		}
		print();
		xzx=gbx;
		xzy=gby;
		if(chess_map[gbx][gby]=='P'){//black P
			if(gbx==7/*start*/){
				cmv[6][gby]=1;
				cmv[5][gby]=1;
			}
			else if(gbx>1)
				if(chess_map[gbx-1][gby]==' ')
					cmv[gbx-1][gby]=1;
			if(gbx>1&&gby<8&&chess_map[gbx-1][gby+1]>='a'&&\
			   chess_map[gbx-1][gby+1]<='z')cmv[gbx-1][gby+1]=1;
			if(gbx>1&&gby>1&&chess_map[gbx-1][gby-1]>='a'&&\
			   chess_map[gbx-1][gby-1]<='z')cmv[gbx-1][gby-1]=1;
		}
		if(chess_map[gbx][gby]=='p'){//white P
			if(gbx==2/*start*/){
				cmv[3][gby]=1;
				cmv[4][gby]=1;
			}
			else if(gbx<8)
				if(chess_map[gbx+1][gby]==' ')
					cmv[gbx+1][gby]=1;
			if(gbx<8&&gby>1&&chess_map[gbx+1][gby-1]>='A'&&\
			   chess_map[gbx+1][gby-1]<='Z')cmv[gbx+1][gby-1]=1;
			if(gbx<8&&gby<8&&chess_map[gbx+1][gby+1]>='A'&&\
			   chess_map[gbx+1][gby+1]<='Z')cmv[gbx+1][gby+1]=1;
		}
		if(chess_map[gbx][gby]=='R'||\
		   chess_map[gbx][gby]=='Q'){//black R,black Q
			for(int x=gbx-1;x>=1;x--){
				if(chess_map[x][gby]==' '||\
				   (chess_map[x][gby]>='a'&&chess_map[x][gby]<='z'))
					cmv[x][gby]=1;
				else break;
				if(chess_map[x][gby]>='a'&&chess_map[x][gby]<='z')break;
			}
			for(int x=gbx+1;x<=8;x++){
				if(chess_map[x][gby]==' '||\
				   (chess_map[x][gby]>='a'&&chess_map[x][gby]<='z'))
					cmv[x][gby]=1;
				else break;
				if(chess_map[x][gby]>='a'&&chess_map[x][gby]<='z')break;
			}
			for(int y=gby-1;y>=1;y--){
				if(chess_map[gbx][y]==' '||\
				   (chess_map[gbx][y]>='a'&&chess_map[gbx][y]<='z'))
					cmv[gbx][y]=1;
				else break;
				if(chess_map[gbx][y]>='a'&&chess_map[gbx][y]<='z')break;
			}
			for(int y=gby+1;y<=8;y++){
				if(chess_map[gbx][y]==' '||\
				   (chess_map[gbx][y]>='a'&&chess_map[gbx][y]<='z'))
					cmv[gbx][y]=1;
				else break;
				if(chess_map[gbx][y]>='a'&&chess_map[gbx][y]<='z')break;
			}
		}
		if(chess_map[gbx][gby]=='r'||
		   chess_map[gbx][gby]=='q'){//white R,white Q
			for(int x=gbx-1;x>=1;x--){
				if(chess_map[x][gby]==' '||\
				   (chess_map[x][gby]>='A'&&chess_map[x][gby]<='Z'))
					cmv[x][gby]=1;
				else break;
				if(chess_map[x][gby]>='A'&&chess_map[x][gby]<='Z')break;
			}
			for(int x=gbx+1;x<=8;x++){
				if(chess_map[x][gby]==' '||\
				   (chess_map[x][gby]>='A'&&chess_map[x][gby]<='Z'))
					cmv[x][gby]=1;
				else break;
				if(chess_map[x][gby]>='A'&&chess_map[x][gby]<='Z')break;
			}
			for(int y=gby-1;y>=1;y--){
				if(chess_map[gbx][y]==' '||\
				   (chess_map[gbx][y]>='A'&&chess_map[gbx][y]<='Z'))
					cmv[gbx][y]=1;
				else break;
				if(chess_map[gbx][y]>='A'&&chess_map[gbx][y]<='Z')break;
			}
			for(int y=gby+1;y<=8;y++){
				if(chess_map[gbx][y]==' '||\
				   (chess_map[gbx][y]>='A'&&chess_map[gbx][y]<='Z'))
					cmv[gbx][y]=1;
				else break;
				if(chess_map[gbx][y]>='A'&&chess_map[gbx][y]<='Z')break;
			}
		}
		if(chess_map[gbx][gby]=='N'){//black N
			int mx[8]={2,2,1,-1,-2,-2,-1,1};
			int my[8]={-1,1,2,2,1,-1,-2,-2};
			for(int i=0;i<8;i++)
				if(gbx+mx[i]>=1&&gbx+mx[i]<=8&&gby+my[i]>=1&&gby+my[i]<=8)
					if(chess_map[gbx+mx[i]][gby+my[i]]==' '||\
					   (chess_map[gbx+mx[i]][gby+my[i]]>='a'&&chess_map[gbx+mx[i]][gby+my[i]]<='z'))
						cmv[gbx+mx[i]][gby+my[i]]=1;
		}
		if(chess_map[gbx][gby]=='n'){//white N
			int mx[8]={2,2,1,-1,-2,-2,-1,1};
			int my[8]={-1,1,2,2,1,-1,-2,-2};
			for(int i=0;i<8;i++)
				if(gbx+mx[i]>=1&&gbx+mx[i]<=8&&gby+my[i]>=1&&gby+my[i]<=8)
					if(chess_map[gbx+mx[i]][gby+my[i]]==' '||\
					   (chess_map[gbx+mx[i]][gby+my[i]]>='A'&&chess_map[gbx+mx[i]][gby+my[i]]<='Z'))
						cmv[gbx+mx[i]][gby+my[i]]=1;
		}
		if(chess_map[gbx][gby]=='B'||\
		   chess_map[gbx][gby]=='Q'){//black B,black Q
			for(int i=gbx+1,j=gby+1;i<=8&&j<=8;i++,j++){
				if(chess_map[i][j]==' '||\
				   (chess_map[i][j]>='a'&&chess_map[i][j]<='z'))
					cmv[i][j]=1;
				else break;
				if(chess_map[i][j]>='a'&&chess_map[i][j]<='z')break;
			}
			for(int i=gbx-1,j=gby+1;i>=1&&j<=8;i--,j++){
				if(chess_map[i][j]==' '||\
				   (chess_map[i][j]>='a'&&chess_map[i][j]<='z'))
					cmv[i][j]=1;
				else break;
				if(chess_map[i][j]>='a'&&chess_map[i][j]<='z')break;
			}
			for(int i=gbx+1,j=gby-1;i<=8&&j>=1;i++,j--){
				if(chess_map[i][j]==' '||\
				   (chess_map[i][j]>='a'&&chess_map[i][j]<='z'))
					cmv[i][j]=1;
				else break;
				if(chess_map[i][j]>='a'&&chess_map[i][j]<='z')break;
			}
			for(int i=gbx-1,j=gby-1;i>=1&&j>=1;i--,j--){
				if(chess_map[i][j]==' '||\
				   (chess_map[i][j]>='a'&&chess_map[i][j]<='z'))
					cmv[i][j]=1;
				else break;
				if(chess_map[i][j]>='a'&&chess_map[i][j]<='z')break;
			}
		}
		if(chess_map[gbx][gby]=='b'||\
		   chess_map[gbx][gby]=='q'){//white B,white Q
			for(int i=gbx+1,j=gby+1;i<=8&&j<=8;i++,j++){
				if(chess_map[i][j]==' '||\
				   (chess_map[i][j]>='A'&&chess_map[i][j]<='Z'))
					cmv[i][j]=1;
				else break;
				if(chess_map[i][j]>='A'&&chess_map[i][j]<='Z')break;
			}
			for(int i=gbx-1,j=gby+1;i>=1&&j<=8;i--,j++){
				if(chess_map[i][j]==' '||\
				   (chess_map[i][j]>='A'&&chess_map[i][j]<='Z'))
					cmv[i][j]=1;
				else break;
				if(chess_map[i][j]>='A'&&chess_map[i][j]<='Z')break;
			}
			for(int i=gbx+1,j=gby-1;i<=8&&j>=1;i++,j--){
				if(chess_map[i][j]==' '||\
				   (chess_map[i][j]>='A'&&chess_map[i][j]<='Z'))
					cmv[i][j]=1;
				else break;
				if(chess_map[i][j]>='A'&&chess_map[i][j]<='Z')break;
			}
			for(int i=gbx-1,j=gby-1;i>=1&&j>=1;i--,j--){
				if(chess_map[i][j]==' '||\
				   (chess_map[i][j]>='A'&&chess_map[i][j]<='Z'))
					cmv[i][j]=1;
				else break;
				if(chess_map[i][j]>='A'&&chess_map[i][j]<='Z')break;
			}
		}
		if(chess_map[gbx][gby]=='K'){//black K
			int mx[8]={1,1,1,0,-1,-1,-1,0};
			int my[8]={-1,0,1,1,1,0,-1,-1};
			for(int i=0;i<8;i++)
				if(gbx+mx[i]>=1&&gbx+mx[i]<=8&&gby+my[i]>=1&&gby+my[i]<=8)
					if(chess_map[gbx+mx[i]][gby+my[i]]==' '||\
					   (chess_map[gbx+mx[i]][gby+my[i]]>='a'&&chess_map[gbx+mx[i]][gby+my[i]]<='z'))
						cmv[gbx+mx[i]][gby+my[i]]=1;
		}
		if(chess_map[gbx][gby]=='k'){//white K
			int mx[8]={1,1,1,0,-1,-1,-1,0};
			int my[8]={-1,0,1,1,1,0,-1,-1};
			for(int i=0;i<8;i++)
				if(gbx+mx[i]>=1&&gbx+mx[i]<=8&&gby+my[i]>=1&&gby+my[i]<=8)
					if(chess_map[gbx+mx[i]][gby+my[i]]==' '||\
					   (chess_map[gbx+mx[i]][gby+my[i]]>='A'&&chess_map[gbx+mx[i]][gby+my[i]]<='Z'))
						cmv[gbx+mx[i]][gby+my[i]]=1;
		}
		if(chess_map[gbx][gby]=='P'&&gbx==1){//black P change
			system("cls");
			ppp=!ppp;
			kkk=1;
			cout<<"Do you want to change this P?\n y or n\n";
			char ch=getch();
			if(ch=='y'){
				cout<<"\nChange into...(C cancel)";
				bool ok=0;
				while(!ok){
					ch=getch();
					if(ch=='C')break;
					if(ch=='R'||ch=='N'||ch=='B'||ch=='Q')ok=1;
				}
				if(ok)chess_map[gbx][gby]=ch;
			}
		}
		if(chess_map[gbx][gby]=='p'&&gbx==8){//white P change
			system("cls");
			ppp=!ppp;
			kkk=1;
			cout<<"Do you want to change this p?\n y or n\n";
			char ch=getch();
			if(ch=='y'){
				cout<<"\nChange into...(c cancel)";
				bool ok=0;
				while(!ok){
					ch=getch();
					if(ch=='c')break;
					if(ch=='r'||ch=='n'||ch=='b'||ch=='q')ok=1;
				}
				if(ok)chess_map[gbx][gby]=ch;
			}
		}
		print();
		//else ched=1;
		ching=1;
		bool ok=0;
		for(int i=1;i<=8;i++)
			for(int j=1;j<=8;j++)
				ok=ok||cmv[i][j];
		if(!ok){
			ppp=!ppp;
			continue;
		}
		while(!ched){
			char ch=getch();
			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])ched=1;
			if(ch=='q'){
				xzx=gbx;
				xzy=gby;
				ched=1;
				ppp=!ppp;
				break;
			}
			print();
		}
		ching=0;
		if(gbx!=xzx||gby!=xzy){
			chess_map[xzx][xzy]=chess_map[gbx][gby];
			chess_map[gbx][gby]=' ';
		}
    if(chess_map[xzx][xzy]=='K'){
      system("cls");
      cout<<"The White Player won!!";
      return 0;
    }
    if(chess_map[xzx][xzy]=='k'){
      system("cls");
      cout<<"The Black Player won!!";
      return 0;
    }
		memset(cmv,0,sizeof(cmv));
	}
}

Ganlai的AI版

@Ganlai造了一个AI版!

C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;

HANDLE hCon;
enum Color {BLACK, DARKBLUE = 1, DARKGREEN, DARKTEAL, \
	DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, \
	BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
int PIECE_VALUE[256] = {0};
struct Move {
	int x1, y1, x2, y2;
	Move() {
		x1 = 0; y1 = 0; x2 = 0; y2 = 0;
	}
	Move(int x1_, int y1_, int x2_, int y2_) {
		x1 = x1_; y1 = y1_; x2 = x2_; y2 = y2_;
	}
};


char chess_map[9][9]={
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ','r','n','b','k','q','b','n','r'},
	{' ','p','p','p','p','p','p','p','p'},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ','P','P','P','P','P','P','P','P'},
	{' ','R','N','B','K','Q','B','N','R'}
};

int gbx,gby,xzx,xzy;
bool cmv[9][9];
bool locked,ched,ching,kkk;
void generateLegalMoves(int x, int y, bool isWhite);
void print();
pair<int, int> findKing(bool isWhite) {
	char king = isWhite ? 'K' : 'k';
	for (int i = 1; i <= 8; i++) {
		for (int j = 1; j <= 8; j++) {
			if (chess_map[i][j] == king) {
				return {i, j};
			}
		}
	}
	return {-1, -1};
}
bool isInCheck(bool isWhite) {
	pair<int, int> kingPos = findKing(isWhite);
	if (kingPos.first == -1) return false;
	int kx = kingPos.first, ky = kingPos.second;
	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 && isupper(piece)) || (!isWhite && islower(piece))) continue;
			bool tempCmv[9][9];
			memcpy(tempCmv, cmv, sizeof(cmv));
			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;
}
bool isKingAlive(bool isWhite) {
	return findKing(isWhite).first != -1;
}
bool isMoveSafe(Move move, bool isWhite) {
	char captured = chess_map[move.x2][move.y2];
	char piece = chess_map[move.x1][move.y1];
	chess_map[move.x2][move.y2] = piece;
	chess_map[move.x1][move.y1] = ' ';
	bool safe = !isInCheck(isWhite);
	chess_map[move.x1][move.y1] = piece;
	chess_map[move.x2][move.y2] = captured;
	return safe;
}
void makeMove(Move move, bool isWhite) {
	chess_map[move.x2][move.y2] = chess_map[move.x1][move.y1];
	chess_map[move.x1][move.y1] = ' ';
	if (isWhite) {
		if (chess_map[move.x2][move.y2] == 'P' && move.x2 == 1) {
			system("cls");
			cout << "升变为(R/N/B/Q),按对应键选择:";
			char ch = ' ';
			while (ch != 'R' && ch != 'N' && ch != 'B' && ch != 'Q') {
				ch = getch();
			}
			chess_map[move.x2][move.y2] = ch;
			print();
		}
	} else {
		if (chess_map[move.x2][move.y2] == 'p' && move.x2 == 8) {
			chess_map[move.x2][move.y2] = 'q';
		}
	}
}

void SetColor(Color ground,Color font) {
	if(hCon == NULL)
		hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hCon, ground*16+font);
}

void print(){
	system("cls");
	cout<<"\n\n\n\n\n\n\n\n";
	for(int i=1;i<=8;i++){
		cout<<"\t\t\t\t\t\t";
		for(int j=1;j<=8;j++){
			if(i==gby&&j==gbx){
				if(!locked)SetColor(BLUE,BLACK);
				else SetColor(GREEN,BLACK);
			}
			else if(i==xzy&&j==xzx&&ching)
				SetColor(RED,BLACK);
			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黑方(AI)被将军!";
	SetColor(BLACK,WHITE);
}

void generateLegalMoves(int x, int y, bool isWhite) {
	memset(cmv, 0, sizeof(cmv));
	char piece = chess_map[x][y];
	if (piece == 'p') {
		if (x == 2) {
			if (chess_map[3][y] == ' ') {
				cmv[3][y] = 1;
				if (chess_map[4][y] == ' ') cmv[4][y] = 1;
			}
		} else if (x < 8) {
			if (chess_map[x+1][y] == ' ') cmv[x+1][y] = 1;
		}
		if (x < 8 && y > 1 && isupper(chess_map[x+1][y-1])) cmv[x+1][y-1] = 1;
		if (x < 8 && y < 8 && isupper(chess_map[x+1][y+1])) cmv[x+1][y+1] = 1;
	}
	else if (piece == 'P') {
		if (x == 7) {
			if (chess_map[6][y] == ' ') {
				cmv[6][y] = 1;
				if (chess_map[5][y] == ' ') cmv[5][y] = 1;
			}
		} else if (x > 1) {
			if (chess_map[x-1][y] == ' ') cmv[x-1][y] = 1;
		}
		if (x > 1 && y < 8 && islower(chess_map[x-1][y+1])) cmv[x-1][y+1] = 1;
		if (x > 1 && y > 1 && islower(chess_map[x-1][y-1])) cmv[x-1][y-1] = 1;
	}
	else if (piece == 'R'|| piece == 'r') {
		bool isWhitePiece = islower(piece);
		for (int i = x-1; i >= 1; i--) {
			if (chess_map[i][y] == ' ') cmv[i][y] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][y])) || (!isWhitePiece && islower(chess_map[i][y]))) {
				cmv[i][y] = 1;
				break;
			} else break;
		}
		for (int i = x+1; i <= 8; i++) {
			if (chess_map[i][y] == ' ') cmv[i][y] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][y])) || (!isWhitePiece && islower(chess_map[i][y]))) {
				cmv[i][y] = 1;
				break;
			} else break;
		}
		for (int j = y-1; j >= 1; j--) {
			if (chess_map[x][j] == ' ') cmv[x][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[x][j])) || (!isWhitePiece && islower(chess_map[x][j]))) {
				cmv[x][j] = 1;
				break;
			} else break;
		}
		for (int j = y+1; j <= 8; j++) {
			if (chess_map[x][j] == ' ') cmv[x][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[x][j])) || (!isWhitePiece && islower(chess_map[x][j]))) {
				cmv[x][j] = 1;
				break;
			} else break;
		}
	}
	else if (piece == 'B'|| piece == 'b') {
		bool isWhitePiece = islower(piece);
		for (int i = x-1, j = y+1; i >= 1 && j <= 8; i--, j++) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x+1, j = y+1; i <= 8 && j <= 8; i++, j++) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x-1, j = y-1; i >= 1 && j >= 1; i--, j--) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x+1, j = y-1; i <= 8 && j >= 1; i++, j--) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
	}
	else if (piece == 'N' || piece == 'n') {
		bool isWhitePiece = islower(piece);
		int mx[8] = {2, 2, 1, -1, -2, -2, -1, 1};
		int my[8] = {-1, 1, 2, 2, 1, -1, -2, -2};
		for (int i = 0; i < 8; i++) {
			int nx = x + mx[i];
			int ny = y + my[i];
			if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8) {
				if (chess_map[nx][ny] == ' ' || (isWhitePiece && isupper(chess_map[nx][ny])) || (!isWhitePiece && islower(chess_map[nx][ny]))) {
					cmv[nx][ny] = 1;
				}
			}
		}
	}
	else if (piece == 'K' || piece == 'k') {
		bool isWhitePiece = islower(piece);
		int mx[8] = {1, 1, 1, 0, -1, -1, -1, 0};
		int my[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
		for (int i = 0; i < 8; i++) {
			int nx = x + mx[i];
			int ny = y + my[i];
			if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8) {
				if (chess_map[nx][ny] == ' ' || (isWhitePiece && isupper(chess_map[nx][ny])) || (!isWhitePiece && islower(chess_map[nx][ny]))) {
					cmv[nx][ny] = 1;
				}
			}
		}
	}
	else if (piece == 'Q' || piece == 'q') {
		bool isWhitePiece = islower(piece);
		for (int i = x-1; i >= 1; i--) {
			if (chess_map[i][y] == ' ') cmv[i][y] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][y])) || (!isWhitePiece && islower(chess_map[i][y]))) {
				cmv[i][y] = 1;
				break;
			} else break;
		}
		for (int i = x+1; i <= 8; i++) {
			if (chess_map[i][y] == ' ') cmv[i][y] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][y])) || (!isWhitePiece && islower(chess_map[i][y]))) {
				cmv[i][y] = 1;
				break;
			} else break;
		}
		for (int j = y-1; j >= 1; j--) {
			if (chess_map[x][j] == ' ') cmv[x][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[x][j])) || (!isWhitePiece && islower(chess_map[x][j]))) {
				cmv[x][j] = 1;
				break;
			} else break;
		}
		for (int j = y+1; j <= 8; j++) {
			if (chess_map[x][j] == ' ') cmv[x][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[x][j])) || (!isWhitePiece && islower(chess_map[x][j]))) {
				cmv[x][j] = 1;
				break;
			} else break;
		}
		for (int i = x-1, j = y+1; i >= 1 && j <= 8; i--, j++) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x+1, j = y+1; i <= 8 && j <= 8; i++, j++) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x-1, j = y-1; i >= 1 && j >= 1; i--, j--) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x+1, j = y-1; i <= 8 && j >= 1; i++, j--) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
	}
}

int evaluateBoard() {
	int score = 0;
	for (int i = 1; i <= 8; i++) {
		for (int j = 1; j <= 8; j++) {
			score += PIECE_VALUE[(unsigned char)chess_map[i][j]];
		}
	}
	if (isInCheck(false)) score -= 5000;
	if (isInCheck(true)) score += 5000;
	return score;
}

int minimax(int depth, int alpha, int beta, bool isMaximizingPlayer) {
	if (depth == 0) {
		return evaluateBoard();
	}
	vector<Move> allMoves;
	for (int x = 1; x <= 8; x++) {
		for (int y = 1; y <= 8; y++) {
			char piece = chess_map[x][y];
			if ((isMaximizingPlayer && islower(piece)) || (!isMaximizingPlayer && isupper(piece))) {
				generateLegalMoves(x, y, isMaximizingPlayer);
				for (int nx = 1; nx <= 8; nx++) {
					for (int ny = 1; ny <= 8; ny++) {
						if (cmv[nx][ny]) {
							allMoves.push_back(Move(x, y, nx, ny));
						}
					}
				}
				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);
			maxEval = max(maxEval, eval);
			chess_map[move.x1][move.y1] = chess_map[move.x2][move.y2];
			chess_map[move.x2][move.y2] = temp;
			alpha = max(alpha, eval);
			if (beta <= alpha) break;
		}
		return maxEval;
	} else {
		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);
			minEval = min(minEval, eval);
			chess_map[move.x1][move.y1] = chess_map[move.x2][move.y2];
			chess_map[move.x2][move.y2] = temp;
			beta = min(beta, eval);
			if (beta <= alpha) break;
		}
		return minEval;
	}
}

Move computerFindBestMove() {
	int bestScore = -1e9;
	Move bestMove;
	vector<Move> allMoves;
	for (int x = 1; x <= 8; x++) {
		for (int y = 1; y <= 8; y++) {
			char piece = chess_map[x][y];
			if (islower(piece)) {
				generateLegalMoves(x, y, true);
				for (int nx = 1; nx <= 8; nx++) {
					for (int ny = 1; ny <= 8; ny++) {
						if (cmv[nx][ny]) {
							allMoves.push_back(Move(x, y, nx, ny));
						}
					}
				}
				memset(cmv, 0, sizeof(cmv));
			}
		}
	}
	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);
		chess_map[move.x1][move.y1] = chess_map[move.x2][move.y2];
		chess_map[move.x2][move.y2] = temp;
		if (currentScore > bestScore) {
			bestScore = currentScore;
			bestMove = move;
		}
	}

	return bestMove;
}

void computerMakeMove() {
	Move bestMove = computerFindBestMove();
	if (bestMove.x1 == 0) return;
	makeMove(bestMove, false);
	print();
}

int main(){
	PIECE_VALUE['P'] = 100;   PIECE_VALUE['p'] = -100;
	PIECE_VALUE['N'] = 320;   PIECE_VALUE['n'] = -320;
	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");
			cout << "\n\t\t白方王被吃,黑方(AI)胜利!\n";
			break;
		}
		if (!isKingAlive(false)) {
			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--;
				if(ch=='a'&&gbx>1)gbx--;
				if(ch=='s'&&gby<8)gby++;
				if(ch=='d'&&gbx<8)gbx++;
				if(ch==' ')
					if(isupper(chess_map[gbx][gby])) locked=1;
				print();
			}
			print();
			xzx=gbx;
			xzy=gby;
			generateLegalMoves(gbx, gby, false);
			bool ok=0;
			for(int i=1;i<=8;i++)
				for(int j=1;j<=8;j++)
					ok=ok||cmv[i][j];
			if(!ok){
				ppp=!ppp;
				continue;
			}
			ching=1;
			while(!ched){
				char ch=getch();
				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])ched=1;
				if(ch=='q'){
					xzx=gbx;
					xzy=gby;
					ched=1;
					ppp=!ppp;
					break;
				}
				print();
			}
			ching=0;
			if(gbx!=xzx||gby!=xzy){
				makeMove(Move(gbx, gby, xzx, xzy), true);
			}
		} else {
			cout << "\n\t\t电脑思考中...\n";
			computerMakeMove();
		}

		memset(cmv,0,sizeof(cmv));
		ppp=!ppp;
	}
	cout << "\n\t\t按任意键退出...";
	getch();
	return 0;
}

联机版

V1.0.0

可以联机了,但有很多 bug 。

V1.0.1

更新了显示 bug 。

V1.0.2&V1.0.3

优化了一下,现在移动后不会卡在原地。

V1.0.4

更新了胜负制度。最新版!

C++
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <bits/stdc++.h>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <conio.h>
#pragma comment(lib, "Ws2_32.lib")  // 链接Winsock库
using namespace std;

// ===================== 全局常量与类型定义 =====================
const int MAX_LEN = 100;          // 网络数据最大长度
const int PORT = 1234;            // 指定通信端口
const string VERSION = "V1.0.4";  // 版本号
HANDLE hCon = NULL;               // 控制台句柄,初始化为NULL
int win=-1;                       // true表示白方胜利,false表示黑方胜利

// 颜色枚举
enum Color {
	BLACK, DARKBLUE = 1, DARKGREEN, DARKTEAL,
	DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY,
	BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE
};

// 移动指令结构体(用于网络传输)
struct MoveCmd {
	int gbx, gby;  // 选中位置
	int xzx, xzy;  // 目标位置
	char promote;  // 兵升变的棋子(无则为' ')
};

// ===================== 服务器命名空间 =====================
namespace Server {
	SOCKET s_server = INVALID_SOCKET, s_accept = INVALID_SOCKET;
	SOCKADDR_IN server_addr, accept_addr;
	int len = sizeof(SOCKADDR);
	FILE* flog = nullptr;
	bool Send(const string& data);
	string Receive();
	void Close();

	// 初始化服务器
	bool Init() {
		// 初始化Winsock
		WORD w_req = MAKEWORD(2, 2);
		WSADATA wsadata;
		int err = WSAStartup(w_req, &wsadata);
		if (err != 0) {
			cout << "初始化套接字库失败!错误码:" << err << endl;
			return false;
		}
		if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wHighVersion) != 2) {
			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);
		if (bind(s_server, (SOCKADDR*)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
			cout << "套接字绑定失败!端口可能被占用,错误码:" << WSAGetLastError() << endl;
			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);
		if (s_server != INVALID_SOCKET) closesocket(s_server);
		WSACleanup();
		cout << "服务器已关闭!" << endl;
	}
}

// ===================== 客户端命名空间 =====================
namespace Client {
	SOCKET s_client = INVALID_SOCKET;
	SOCKADDR_IN server_addr;
	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);
		WSADATA wsadata;
		int err = WSAStartup(w_req, &wsadata);
		if (err != 0) {
			cout << "初始化套接字库失败!错误码:" << err << endl;
			return false;
		}
		if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wHighVersion) != 2) {
			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);
		WSACleanup();
		cout << "客户端已关闭!" << endl;
	}
}

// ===================== 国际象棋核心逻辑 =====================
// 棋盘(1-8行/列,0行0列弃用)
char chess_map[9][9] = {
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ','r','n','b','k','q','b','n','r'},
	{' ','p','p','p','p','p','p','p','p'},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ',' ',' ',' ',' ',' ',' ',' ',' '},
	{' ','P','P','P','P','P','P','P','P'},
	{' ','R','N','B','K','Q','B','N','R'}
};

int gbx, gby, xzx, xzy;  // 选中位置、目标位置
bool cmv[9][9] = {false};// 可移动位置标记
bool locked = false, ched = false, ching = false, kkk = false;
char promote_piece = ' ';// 升变棋子

// 设置控制台颜色
void SetColor(Color ground, Color font) {
	if (hCon == NULL)
		hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hCon, ground * 16 + font);
}

// 查找国王位置
pair<int, int> findKing(bool isWhite) {
	char king = isWhite ? 'K' : 'k';
	for (int i = 1; i <= 8; i++) {
		for (int j = 1; j <= 8; j++) {
			if (chess_map[i][j] == king) {
				return {i, j};
			}
		}
	}
	return {-1, -1};
}

// 生成合法移动
void generateLegalMoves(int x, int y, bool isWhite) {
	memset(cmv, 0, sizeof(cmv));
	char piece = chess_map[x][y];
	if (piece == 'p') {
		if (x == 2) {
			if (chess_map[3][y] == ' ') {
				cmv[3][y] = 1;
				if (chess_map[4][y] == ' ') cmv[4][y] = 1;
			}
		} else if (x < 8) {
			if (chess_map[x+1][y] == ' ') cmv[x+1][y] = 1;
		}
		if (x < 8 && y > 1 && isupper(chess_map[x+1][y-1])) cmv[x+1][y-1] = 1;
		if (x < 8 && y < 8 && isupper(chess_map[x+1][y+1])) cmv[x+1][y+1] = 1;
	}
	else if (piece == 'P') {
		if (x == 7) {
			if (chess_map[6][y] == ' ') {
				cmv[6][y] = 1;
				if (chess_map[5][y] == ' ') cmv[5][y] = 1;
			}
		} else if (x > 1) {
			if (chess_map[x-1][y] == ' ') cmv[x-1][y] = 1;
		}
		if (x > 1 && y < 8 && islower(chess_map[x-1][y+1])) cmv[x-1][y+1] = 1;
		if (x > 1 && y > 1 && islower(chess_map[x-1][y-1])) cmv[x-1][y-1] = 1;
	}
	else if (piece == 'R'|| piece == 'r') {
		bool isWhitePiece = islower(piece);
		for (int i = x-1; i >= 1; i--) {
			if (chess_map[i][y] == ' ') cmv[i][y] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][y])) || (!isWhitePiece && islower(chess_map[i][y]))) {
				cmv[i][y] = 1;
				break;
			} else break;
		}
		for (int i = x+1; i <= 8; i++) {
			if (chess_map[i][y] == ' ') cmv[i][y] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][y])) || (!isWhitePiece && islower(chess_map[i][y]))) {
				cmv[i][y] = 1;
				break;
			} else break;
		}
		for (int j = y-1; j >= 1; j--) {
			if (chess_map[x][j] == ' ') cmv[x][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[x][j])) || (!isWhitePiece && islower(chess_map[x][j]))) {
				cmv[x][j] = 1;
				break;
			} else break;
		}
		for (int j = y+1; j <= 8; j++) {
			if (chess_map[x][j] == ' ') cmv[x][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[x][j])) || (!isWhitePiece && islower(chess_map[x][j]))) {
				cmv[x][j] = 1;
				break;
			} else break;
		}
	}
	else if (piece == 'B'|| piece == 'b') {
		bool isWhitePiece = islower(piece);
		for (int i = x-1, j = y+1; i >= 1 && j <= 8; i--, j++) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x+1, j = y+1; i <= 8 && j <= 8; i++, j++) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x-1, j = y-1; i >= 1 && j >= 1; i--, j--) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x+1, j = y-1; i <= 8 && j >= 1; i++, j--) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
	}
	else if (piece == 'N' || piece == 'n') {
		bool isWhitePiece = islower(piece);
		int mx[8] = {2, 2, 1, -1, -2, -2, -1, 1};
		int my[8] = {-1, 1, 2, 2, 1, -1, -2, -2};
		for (int i = 0; i < 8; i++) {
			int nx = x + mx[i];
			int ny = y + my[i];
			if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8) {
				if (chess_map[nx][ny] == ' ' || (isWhitePiece && isupper(chess_map[nx][ny])) || (!isWhitePiece && islower(chess_map[nx][ny]))) {
					cmv[nx][ny] = 1;
				}
			}
		}
	}
	else if (piece == 'K' || piece == 'k') {
		bool isWhitePiece = islower(piece);
		int mx[8] = {1, 1, 1, 0, -1, -1, -1, 0};
		int my[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
		for (int i = 0; i < 8; i++) {
			int nx = x + mx[i];
			int ny = y + my[i];
			if (nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8) {
				if (chess_map[nx][ny] == ' ' || (isWhitePiece && isupper(chess_map[nx][ny])) || (!isWhitePiece && islower(chess_map[nx][ny]))) {
					cmv[nx][ny] = 1;
				}
			}
		}
	}
	else if (piece == 'Q' || piece == 'q') {
		bool isWhitePiece = islower(piece);
		for (int i = x-1; i >= 1; i--) {
			if (chess_map[i][y] == ' ') cmv[i][y] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][y])) || (!isWhitePiece && islower(chess_map[i][y]))) {
				cmv[i][y] = 1;
				break;
			} else break;
		}
		for (int i = x+1; i <= 8; i++) {
			if (chess_map[i][y] == ' ') cmv[i][y] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][y])) || (!isWhitePiece && islower(chess_map[i][y]))) {
				cmv[i][y] = 1;
				break;
			} else break;
		}
		for (int j = y-1; j >= 1; j--) {
			if (chess_map[x][j] == ' ') cmv[x][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[x][j])) || (!isWhitePiece && islower(chess_map[x][j]))) {
				cmv[x][j] = 1;
				break;
			} else break;
		}
		for (int j = y+1; j <= 8; j++) {
			if (chess_map[x][j] == ' ') cmv[x][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[x][j])) || (!isWhitePiece && islower(chess_map[x][j]))) {
				cmv[x][j] = 1;
				break;
			} else break;
		}
		for (int i = x-1, j = y+1; i >= 1 && j <= 8; i--, j++) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x+1, j = y+1; i <= 8 && j <= 8; i++, j++) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x-1, j = y-1; i >= 1 && j >= 1; i--, j--) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
		for (int i = x+1, j = y-1; i <= 8 && j >= 1; i++, j--) {
			if (chess_map[i][j] == ' ') cmv[i][j] = 1;
			else if ((isWhitePiece && isupper(chess_map[i][j])) || (!isWhitePiece && islower(chess_map[i][j]))) {
				cmv[i][j] = 1;
				break;
			} else break;
		}
	}
}

// 检查是否被将军
bool isInCheck(bool isWhite) {
	pair<int, int> kingPos = findKing(isWhite);
	if (kingPos.first == -1) return false;
	int kx = kingPos.first, ky = kingPos.second;
	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 && isupper(piece)) || (!isWhite && islower(piece))) continue;
			bool tempCmv[9][9];
			memcpy(tempCmv, cmv, sizeof(cmv));
			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;
}

// 检查是否将死
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;
}

// 打印棋盘
void print() {
	system("cls");
	cout << "\n\n\n\n\n\n\n\n";
	for (int i = 1; i <= 8; i++) {
		cout << "\t\t\t\t\t\t";
		for (int j = 1; j <= 8; j++) {
			// 选中位置
			if (i == gby && j == gbx) {
				if (!locked) SetColor(BLUE, BLACK);
				else SetColor(GREEN, BLACK);
			}
			else if (i == xzy && j == xzx) {
				SetColor(RED,WHITE);
			}
			// 将军标记
			else if (i == xzy && j == xzx && ching)
				SetColor(RED, BLACK);
			// 可移动位置
			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);
}

// 执行移动指令(用于同步棋盘)
void ExecuteMove(const MoveCmd& cmd) {
	// 处理兵升变
	if (cmd.promote != ' ') {
		chess_map[cmd.gbx][cmd.gby] = cmd.promote;
	}
	// 移动棋子
	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;  // 白方国王被吃,黑方胜利
	}
	else if(findKing(false)==make_pair(-1,-1)) {
		win = true;   // 黑方国王被吃,白方胜利
	}
	// 检查是否将死
	else if (isCheckmate(true)) {
		win = false;  // 白方被将死,黑方胜利
	}
	else if (isCheckmate(false)) {
		win = true;   // 黑方被将死,白方胜利
	}
	else {
		// 未分胜负,继续游戏
		print();
		return;
	}
	// 游戏结束提示
	SetColor(RED, YELLOW);
	if (win) {
		cout << "\n\n\t\t\t\t\t\t========== 游戏结束 ==========" << endl;
		cout << "\t\t\t\t\t\t        白方获胜!" << endl;
	} else {
		cout << "\n\n\t\t\t\t\t\t========== 游戏结束 ==========" << endl;
		cout << "\t\t\t\t\t\t        黑方获胜!" << endl;
	}
	SetColor(BLACK, WHITE);
	print();
}

// 本地下棋逻辑(返回移动指令)
MoveCmd LocalPlay(int ppp) {
	MoveCmd cmd = {0, 0, 0, 0, ' '};
	ched = false;
	if (!kkk) {
		gbx = gby = !ppp * 7 + 1;
		locked = false;
	}
	kkk = false;
	print();

	// 选择棋子
	while (!locked) {
		char ch = getch();
		if (ch == 'w' && gby > 1) gby--;
		if (ch == 'a' && gbx > 1) gbx--;
		if (ch == 's' && gby < 8) gby++;
		if (ch == 'd' && gbx < 8) gbx++;
		// 空格选中棋子(区分黑白方)
		if (ch == ' ') {
			if ((ppp == 1 && chess_map[gbx][gby] >= 'a' && chess_map[gbx][gby] <= 'z') ||
				(ppp == 0 && chess_map[gbx][gby] >= 'A' && chess_map[gbx][gby] <= 'Z')) {
				locked = true;
				cmd.gbx = gbx;
				cmd.gby = gby;
			}
		}
		print();
	}

	print();
	xzx = gbx;
	xzy = gby;

	// 棋子移动规则判断
	// 兵(黑)
	if (chess_map[gbx][gby] == 'P') {
		if (gbx == 7) {
			cmv[6][gby] = 1;
			cmv[5][gby] = 1;
		} else if (gbx > 1 && chess_map[gbx-1][gby] == ' ')
			cmv[gbx-1][gby] = 1;
		if (gbx>1 && gby<8 && chess_map[gbx-1][gby+1]>='a' && chess_map[gbx-1][gby+1]<='z')
			cmv[gbx-1][gby+1] = 1;
		if (gbx>1 && gby>1 && chess_map[gbx-1][gby-1]>='a' && chess_map[gbx-1][gby-1]<='z')
			cmv[gbx-1][gby-1] = 1;
	}
	// 兵(白)
	if (chess_map[gbx][gby] == 'p') {
		if (gbx == 2) {
			cmv[3][gby] = 1;
			cmv[4][gby] = 1;
		} else if (gbx < 8 && chess_map[gbx+1][gby] == ' ')
			cmv[gbx+1][gby] = 1;
		if (gbx<8 && gby>1 && chess_map[gbx+1][gby-1]>='A' && chess_map[gbx+1][gby-1]<='Z')
			cmv[gbx+1][gby-1] = 1;
		if (gbx<8 && gby<8 && chess_map[gbx+1][gby+1]>='A' && chess_map[gbx+1][gby+1]<='Z')
			cmv[gbx+1][gby+1] = 1;
	}
	// 车/后(黑)
	if (chess_map[gbx][gby] == 'R' || chess_map[gbx][gby] == 'Q') {
		for(int x=gbx-1;x>=1;x--){
			if(chess_map[x][gby]==' '||(chess_map[x][gby]>='a'&&chess_map[x][gby]<='z'))
				cmv[x][gby]=1;
			else break;
			if(chess_map[x][gby]>='a'&&chess_map[x][gby]<='z')break;
		}
		for(int x=gbx+1;x<=8;x++){
			if(chess_map[x][gby]==' '||(chess_map[x][gby]>='a'&&chess_map[x][gby]<='z'))
				cmv[x][gby]=1;
			else break;
			if(chess_map[x][gby]>='a'&&chess_map[x][gby]<='z')break;
		}
		for(int y=gby-1;y>=1;y--){
			if(chess_map[gbx][y]==' '||(chess_map[gbx][y]>='a'&&chess_map[gbx][y]<='z'))
				cmv[gbx][y]=1;
			else break;
			if(chess_map[gbx][y]>='a'&&chess_map[gbx][y]<='z')break;
		}
		for(int y=gby+1;y<=8;y++){
			if(chess_map[gbx][y]==' '||(chess_map[gbx][y]>='a'&&chess_map[gbx][y]<='z'))
				cmv[gbx][y]=1;
			else break;
			if(chess_map[gbx][y]>='a'&&chess_map[gbx][y]<='z')break;
		}
	}
	// 车/后(白)
	if (chess_map[gbx][gby] == 'r' || chess_map[gbx][gby] == 'q') {
		for(int x=gbx-1;x>=1;x--){
			if(chess_map[x][gby]==' '||(chess_map[x][gby]>='A'&&chess_map[x][gby]<='Z'))
				cmv[x][gby]=1;
			else break;
			if(chess_map[x][gby]>='A'&&chess_map[x][gby]<='Z')break;
		}
		for(int x=gbx+1;x<=8;x++){
			if(chess_map[x][gby]==' '||(chess_map[x][gby]>='A'&&chess_map[x][gby]<='Z'))
				cmv[x][gby]=1;
			else break;
			if(chess_map[x][gby]>='A'&&chess_map[x][gby]<='Z')break;
		}
		for(int y=gby-1;y>=1;y--){
			if(chess_map[gbx][y]==' '||(chess_map[gbx][y]>='A'&&chess_map[gbx][y]<='Z'))
				cmv[gbx][y]=1;
			else break;
			if(chess_map[gbx][y]>='A'&&chess_map[gbx][y]<='Z')break;
		}
		for(int y=gby+1;y<=8;y++){
			if(chess_map[gbx][y]==' '||(chess_map[gbx][y]>='A'&&chess_map[gbx][y]<='Z'))
				cmv[gbx][y]=1;
			else break;
			if(chess_map[gbx][y]>='A'&&chess_map[gbx][y]<='Z')break;
		}
	}
	// 马(黑)
	if (chess_map[gbx][gby] == 'N') {
		int mx[8]={2,2,1,-1,-2,-2,-1,1};
		int my[8]={-1,1,2,2,1,-1,-2,-2};
		for(int i=0;i<8;i++)
			if(gbx+mx[i]>=1&&gbx+mx[i]<=8&&gby+my[i]>=1&&gby+my[i]<=8)
				if(chess_map[gbx+mx[i]][gby+my[i]]==' '||(chess_map[gbx+mx[i]][gby+my[i]]>='a'&&chess_map[gbx+mx[i]][gby+my[i]]<='z'))
					cmv[gbx+mx[i]][gby+my[i]]=1;
	}
	// 马(白)
	if (chess_map[gbx][gby] == 'n') {
		int mx[8]={2,2,1,-1,-2,-2,-1,1};
		int my[8]={-1,1,2,2,1,-1,-2,-2};
		for(int i=0;i<8;i++)
			if(gbx+mx[i]>=1&&gbx+mx[i]<=8&&gby+my[i]>=1&&gby+my[i]<=8)
				if(chess_map[gbx+mx[i]][gby+my[i]]==' '||(chess_map[gbx+mx[i]][gby+my[i]]>='A'&&chess_map[gbx+mx[i]][gby+my[i]]<='Z'))
					cmv[gbx+mx[i]][gby+my[i]]=1;
	}
	// 象/后(黑)
	if (chess_map[gbx][gby] == 'B' || chess_map[gbx][gby] == 'Q') {
		for(int i=gbx+1,j=gby+1;i<=8&&j<=8;i++,j++){
			if(chess_map[i][j]==' '||(chess_map[i][j]>='a'&&chess_map[i][j]<='z'))
				cmv[i][j]=1;
			else break;
			if(chess_map[i][j]>='a'&&chess_map[i][j]<='z')break;
		}
		for(int i=gbx-1,j=gby+1;i>=1&&j<=8;i--,j++){
			if(chess_map[i][j]==' '||(chess_map[i][j]>='a'&&chess_map[i][j]<='z'))
				cmv[i][j]=1;
			else break;
			if(chess_map[i][j]>='a'&&chess_map[i][j]<='z')break;
		}
		for(int i=gbx+1,j=gby-1;i<=8&&j>=1;i++,j--){
			if(chess_map[i][j]==' '||(chess_map[i][j]>='a'&&chess_map[i][j]<='z'))
				cmv[i][j]=1;
			else break;
			if(chess_map[i][j]>='a'&&chess_map[i][j]<='z')break;
		}
		for(int i=gbx-1,j=gby-1;i>=1&&j>=1;i--,j--){
			if(chess_map[i][j]==' '||(chess_map[i][j]>='a'&&chess_map[i][j]<='z'))
				cmv[i][j]=1;
			else break;
			if(chess_map[i][j]>='a'&&chess_map[i][j]<='z')break;
		}
	}
	// 象/后(白)
	if (chess_map[gbx][gby] == 'b' || chess_map[gbx][gby] == 'q') {
		// 右下斜向
		for(int i=gbx+1,j=gby+1;i<=8&&j<=8;i++,j++){
			if(chess_map[i][j]==' '||(chess_map[i][j]>='A'&&chess_map[i][j]<='Z'))
				cmv[i][j]=1;
			else break;
			if(chess_map[i][j]>='A'&&chess_map[i][j]<='Z')break;
		}
		// 右上斜向
		for(int i=gbx-1,j=gby+1;i>=1&&j<=8;i--,j++){
			if(chess_map[i][j]==' '||(chess_map[i][j]>='A'&&chess_map[i][j]<='Z'))
				cmv[i][j]=1;
			else break;
			if(chess_map[i][j]>='A'&&chess_map[i][j]<='Z')break;
		}
		// 左下斜向
		for(int i=gbx+1,j=gby-1;i<=8&&j>=1;i++,j--){
			if(chess_map[i][j]==' '||(chess_map[i][j]>='A'&&chess_map[i][j]<='Z'))
				cmv[i][j]=1;
			else break;
			if(chess_map[i][j]>='A'&&chess_map[i][j]<='Z')break;
		}
		// 左上斜向
		for(int i=gbx-1,j=gby-1;i>=1&&j>=1;i--,j--){
			if(chess_map[i][j]==' '||(chess_map[i][j]>='A'&&chess_map[i][j]<='Z'))
				cmv[i][j]=1;
			else break;
			if(chess_map[i][j]>='A'&&chess_map[i][j]<='Z')break;
		}
	}
	bool ok=0;
	for(int i=1;i<=8;i++)for(int j=1;j<=8;j++)ok=ok||cmv[i][j];
	if(!ok)return LocalPlay(ppp);
	// 选择目标位置
	while (locked) {
		char ch = getch();
		// 移动光标选择目标位置
		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) {
				return inet_ntoa(*(struct in_addr*)*host->h_addr_list);
			}
		}
	}
	return "获取 IP 失败";
}

// 主函数 - 选择服务器/客户端模式
int main() {
	system("mode con cols=75 lines=53");
	cout << "===== 国际象棋网络对战 " << VERSION << " =====" << "\t\t\tIP:" << getIPAddress() << endl;
	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) ||
				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) ||
				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;
}

V1.0.5

更新中 …

原文