⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myframe.cpp

📁 一个人工智能的国际象棋游戏
💻 CPP
📖 第 1 页 / 共 4 页
字号:

			}
			//Ordinary Move. Repaint start and end squares and text out current piece.
			int x=startsquare[0]*SQUARE_SIZE+ORIGINX;
			int y=startsquare[1]*SQUARE_SIZE+ORIGINY;
			
			CDC *p=GetDC();
			
			
			int newx=endsquare[0]*SQUARE_SIZE+XPADDING+ORIGINX;
			int newy=endsquare[1]*SQUARE_SIZE+YPADDING+ORIGINY;
			if (undercheck==1)
			{
				x=sy*SQUARE_SIZE+ORIGINX;y=sx*SQUARE_SIZE+ORIGINY;
				newx=dy*SQUARE_SIZE+XPADDING+ORIGINX;newy=dx*SQUARE_SIZE+YPADDING+ORIGINY;
				piece=WHITE_KING;
			}
			
			p->FillRect(&(CRect(newx-XPADDING,newy-YPADDING,newx+SQUARE_SIZE-XPADDING,newy+SQUARE_SIZE-YPADDING)),&(CBrush(p->GetPixel(newx-XPADDING+2,newy-YPADDING+2))));
			COLORREF color=p->GetPixel(newx-XPADDING+2,newy-YPADDING+2);
			//Flash destination square thrice
			for (int i=0;i<3;i++)
			{
				
				p->FillRect(&(CRect(newx-XPADDING,newy-YPADDING,newx+SQUARE_SIZE-XPADDING,newy+SQUARE_SIZE-YPADDING)),&(CBrush(color)));
				SleepEx(300,FALSE);
				p->FillRect(&(CRect(newx-XPADDING,newy-YPADDING,newx+SQUARE_SIZE-XPADDING,newy+SQUARE_SIZE-YPADDING)),&highlighter);
				SleepEx(300,FALSE);		
			}
			p->FillRect(&(CRect(newx-XPADDING,newy-YPADDING,newx+SQUARE_SIZE-XPADDING,newy+SQUARE_SIZE-YPADDING)),&(CBrush(color)));
			p->SelectObject(&chessfont);
			//Text out the piece in Chess Merida font
			if (piece<10)
			{
				p->SetTextColor(RGB(255,255,255));
			}
			else
			{
				p->SetTextColor(RGB(0,0,0));
			}
			p->SetBkMode(TRANSPARENT);
			p->TextOut(newx,newy,"\t");
			p->TextOut(newx,newy,pieceName(piece));
			p->FillRect(&(CRect(x,y,x+SQUARE_SIZE,y+SQUARE_SIZE)),&(CBrush(p->GetPixel(x+2,y+2))));
			CheckCheck(board,BLACK_KING);
			
			
		}
	}
}

char *myframe::pieceName(int piece)
{
	if (piece==WHITE_PAWN)
	{
		return "o";
	}
	if (piece==BLACK_PAWN)
	{
		return "o";
	}
	if (piece%10==4) //Bishop
	{
		return "v";
	}
	if (piece%10==5) //Knight
	{
		return "m";
	}
	if (piece%10==6)  //Rook
	{
		return "t";
	}
	if (piece%10==2) //King
	{
		return "l";
	}
	if (piece%10==3) //Queen
	{
		return "w";
	}
	
	return "FA"; //Unknown
}


void myframe::ArrangeOtherPieces(CDC *p,int color)
{

	//Arrange pieces other than the pawns, according to color. Done only once.
	if (color==1)
	{
		
		p->SetTextColor(RGB(255,255,255));
		board[0][7]=board[0][0]=WHITE_ROOK;
		board[0][6]=board[0][1]=WHITE_KNIGHT;
		board[0][5]=board[0][2]=WHITE_BISHOP;
		board[0][3]=WHITE_KING;
		board[0][4]=WHITE_QUEEN;
	
	}
	else
	{


		board[7][7]=board[7][0]=BLACK_ROOK;
		board[7][6]=board[7][1]=BLACK_KNIGHT;
		board[7][5]=board[7][2]=BLACK_BISHOP;
		board[7][3]=BLACK_KING;
		board[7][4]=BLACK_QUEEN;
	
	}
	
}

int myframe::validmove()
{
	int piece=board[startsquare[1]][startsquare[0]];
	int startx=startsquare[0];
	int starty=startsquare[1];
	int destx=endsquare[0];
	int desty=endsquare[1];
	int destpiece=0;
	if (piece%10==1) // Legal Moves for Pawn
	{
		if ((destpiece=ValidMove_Pawn(startx,starty,destx,desty))!=0)
		{
			DisplayCutPiece(destpiece);

		}
		else
		{
			return 0;
		}
	}	
	else if(piece%10==6)  //Legal Moves for Rook
	{
		if ((destpiece=ValidMove_Rook(startx,starty,destx,desty))!=0)
		{
			
			DisplayCutPiece(destpiece);
			
			
		}
		else
		{
			//MessageBox("Not OKay");
			return 0;
		}
	}
	else if (piece%10==2) //Legal moves for The Majesty
	{
		if ((destpiece=ValidMove_King(startx,starty,destx,desty))!=0)
		{
			DisplayCutPiece(destpiece);
			
		}
		else
		{
			return 0;
		}
	}
	else if(piece%10==3) //Checking moves for Her Majesty
	{
		if ((destpiece=ValidMove_Queen(startx,starty,destx,desty))!=0)
		{
			DisplayCutPiece(destpiece);
			
		}
		else
		{
			return 0;

		}
		
		
	}

	else if(piece%10==4)
	{
		if ((destpiece=ValidMove_Bishop(startx,starty,destx,desty))!=0)
		{
			DisplayCutPiece(destpiece);
			
		}
		else
		{
			return 0;
		}
	}
	else if(piece%10==5)
	{
		if ((destpiece=ValidMove_Knight(startx,starty,destx,desty))!=0)
		{
			
			DisplayCutPiece(destpiece);	
		}
		else
		{
			return 0;
		}
	}

		if (computer_color==WHITE && piece>10)
		{
			undo.blackmove.piece=piece;
			undo.blackmove.sourcex=startx;
			undo.blackmove.sourcey=starty;
			undo.blackmove.destx=destx;
			undo.blackmove.desty=desty;
			if (destpiece==100) destpiece=0;
			undo.cut_whitepiece=destpiece;
			
		}
		if (computer_color==BLACK && piece<10 && piece>0)
		{
			undo.whitemove.piece=piece;
			undo.whitemove.sourcex=startx;
			undo.whitemove.sourcey=starty;
			undo.whitemove.destx=destx;
			undo.whitemove.desty=desty;
			if (destpiece==100) destpiece=0;
			undo.cut_blackpiece=destpiece;

		}
		return 1;
	

	
}

inline int myframe::NoOtherPiece(int destx,int desty)
{
		if (board[desty][destx]==0)
		{
			return 100; //Dummy Value; Means Okay, No piece at destination. Just Move
		}
		else
		{
			return board[desty][destx]; //Return Piece at destination
		}
}

int myframe::Max(int x,int y)
{
	return (x>y?x:y);
}
int myframe::Min(int x,int y)
{
	return (x<y?x:y);
}



int myframe::ValidMove_Pawn(int startx,int starty,int destx,int desty)
{
	int piece=board[startsquare[1]][startsquare[0]];
	int initialposn;
	
	if ((piece==WHITE_PAWN && WHITEBASE==0) || (piece==BLACK_PAWN && BLACKBASE==0))
	{
	
		if (desty<starty) //First prevent piece from moving backwards
		{
			return 0;
		}
	
		initialposn=1;

	}
	else if ((piece==WHITE_PAWN && WHITEBASE==7) || (piece==BLACK_PAWN && BLACKBASE==7))
	{
			
		if (starty<desty) //First prevent piece from moving backwards
		{
			return 0;
		}
		initialposn=6;

	}
		
	//Can move two places for the first time
	
	if (startx==destx && starty==initialposn && abs(desty-starty)==2)
	{
		if (NoOtherPiece(destx,desty)==100) //No piece at destination or in path to destination
		{
			if ((((piece<10 && WHITEBASE==0) || (piece>10 && BLACKBASE==0)) && NoOtherPiece(destx,desty-1)==100) || (((piece>10 && BLACKBASE==7) || (piece<10 && WHITEBASE==7)) && NoOtherPiece(destx,desty+1)==100))
			{
				return 100;
			}
		
		}
	}
		
		//Normal Moves
		if ((startx==destx) )
		{
			
			if (abs(starty-desty)!=1)
				return 0;
			else
			{
				if (NoOtherPiece(destx,desty)==100)
				{
					return 100;
				}
				else
				{
					return 0;
				}
			}
		}
		//Cut opponent piece
		else if(abs(startx-destx)==1 && abs(starty-desty)==1)
		{
			int x=NoOtherPiece(destx,desty);
			if (x==100)  //Destination empty. Can't Cut
			{
				return 0;
			}
			if (piece>10) //Black pawn
			{
				if (x>10) //Don't cut if opponent is of same piece
				{
					return 0;
				}
			}
			else           //White Pawn
			{
				if (x<10)
				{
					return 0;
				}
			}
			return x;
			
		}
		
	return 0;	
	
}

int myframe::ValidMove_King(int startx,int starty,int destx,int desty)
{
		if (abs(desty-starty)<=1 && abs(destx-startx)<=1)
		{
			x=NoOtherPiece(destx,desty);
			if (x==100)
			{
				return x;
			}
			else
			{
				if ((piece<10 && x>10) || (piece>10 && x<10))
				{
					return x;
				}
				else
				{
					return 0;
				}
			}
		}
		else  //Checking for castling move
		{
			
			int x;
			if ((x=CanCastle(startx,starty,destx,desty)))
			{
				return (Castle(x,startx,starty,destx,desty));
			}
			else
			{
				return 0;
			}

		}

}
int myframe::ValidMove_Queen(int startx,int starty,int destx,int desty)
{
	int piece=board[startsquare[1]][startsquare[0]];	
	if (startx==destx || starty==desty) //Can move along same file
		{
			if (startx==destx)
			{
				int max=Max(starty,desty);
				int min=Min(starty,desty);
				//MessageBox("X the same");
				if (min==starty || min==desty) min+=1;  //To exclude starting position
				for (int i=min;i<max;i++)   //Checking no pieces are on path
				{
					if (board[i][startx]!=0)
					{
						return 0;
					}
				}
			}
			else if(starty==desty)   //Same but for horizontal motion
			{
				int max=Max(startx,destx);
				int min=Min(startx,destx);
				if (min==startx || min==destx) min+=1; 
				for (int i=min;i<max;i++)
				{
					if (board[starty][i]!=0)
					{
						return 0;
					}
				}
			}
			x=NoOtherPiece(destx,desty);
			if (x==100)
				return x;
			else
			{
				if (piece>10 && x<10 || piece<10 && x>10)
				{
					return x;
				}
				else
				{
					return 0;
				}
			}
				
		}
		else if (abs(startx-destx)==abs(starty-desty))
		{

			if (startx<destx && starty<desty) //Right Up Diagnol
				{
					for (int i=startx+1,j=starty+1;i<destx;i++,j++)
					{
						x=NoOtherPiece(i,j);
						if (x!=100)
						{
							if ((piece<10 && x>10) || (piece>10 && x<10)) return x;
							else
								return 0;
						}
					}
				}
				else if(startx>destx && starty<desty) //Left Up Diagnol
				{
					for (int i=startx-1,j=starty+1;i>destx;i--,j++)
					{
						x=NoOtherPiece(i,j);
						if (x!=100)
							if ((piece<10 && x>10) || (piece>10 && x<10)) return x;
							else
							return 0;
					}
				}
				else if(startx<destx && starty>desty) //Right Down Diagnol
				{
					for (int i=startx+1,j=starty-1;i<destx;i++,j--)
					{
						x=NoOtherPiece(i,j);
						if (x!=100)
							if ((piece<10 && x>10) || (piece>10 && x<10)) return x;
							else
							return 0;
					}
				}
				else if(startx>destx && starty>desty) //Left Down Diagnol
				{
					for (int i=startx-1,j=starty-1;i>destx;i--,j--)
					{
						x=NoOtherPiece(i,j);
						if (x!=100)
							if ((piece<10 && x>10) || (piece>10 && x<10)) return x;
							else
							return 0;
					}
				}

				
				
			x=NoOtherPiece(destx,desty);
			if (x==100)
				return x;
			else
			{
				if (piece>10 && x<10 || piece<10 && x>10)
				{
					return x;
				}
				else
				{
					return 0;
				}
			}
	
		}
		
		else
		{
			return 0;
		}
}

int myframe::ValidMove_Knight (int startx,int starty,int destx,int desty)
{
	int piece=board[startsquare[1]][startsquare[0]];	
	if ((abs(starty-desty)==2 && abs(startx-destx)==1) ||(abs(startx-destx)==2 && abs(starty-desty)==1))
		{

			x=NoOtherPiece(destx,desty);
			if (x==100)
				return x;
			else
			{
				if ((piece>10 && x<10) || (piece<10 && x>10))
				{
					return x;
				}
				else
				{
					return 0;
				}
			}
		}
		else
		{
			return 0;
		}


}
int myframe::ValidMove_Bishop(int startx,int starty,int destx,int desty)
{
	int piece=board[startsquare[1]][startsquare[0]];
	int alreadyinc=0; 
		int alreadydec=0;
		if (abs(startx-destx)==abs(starty-desty))
		{

			
				if (startx<destx && starty<desty) //Right Up Diagnol
				{
					for (int i=startx+1,j=starty+1;i<destx;i++,j++)
					{
						x=NoOtherPiece(i,j);
						if (x!=100)
						{
							if ((piece<10 && x>10) || (piece>10 && x<10)) return x;
							else
								return 0;
						}
					}
				}
				else if(startx>destx && starty<desty) //Left Up Diagnol
				{
					for (int i=startx-1,j=starty+1;i>destx;i--,j++)
					{
						x=NoOtherPiece(i,j);
						if (x!=100)
							if ((piece<10 && x>10) || (piece>10 && x<10)) return x;
							else
							return 0;
					}
				}
				else if(startx<destx && starty>desty) //Right Down Diagnol
				{
					for (int i=startx+1,j=starty-1;i<destx;i++,j--)
					{
						x=NoOtherPiece(i,j);
						if (x!=100)
							if ((piece<10 && x>10) || (piece>10 && x<10)) return x;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -