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

📄 myframe.cpp

📁 一个人工智能的国际象棋游戏
💻 CPP
📖 第 1 页 / 共 4 页
字号:
							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_Rook(int startx,int starty,int destx,int desty)
{
		int x;
		
		if (WHITEBASE==0 )
		{
			if (!king_blackrook && startx==0 && starty==7)
				king_blackrook=1;
			if (!queen_blackrook && startx==7 && starty==7)
				queen_blackrook=1;
			if (!king_whiterook && startx==0 && starty==0)
				king_whiterook=1;
			if (!queen_whiterook && startx==7 && starty==0)
				queen_whiterook=1;
			
		}
		else
		{
			if (!king_blackrook && startx==7 && starty==0)
				king_blackrook=1;
			if (!queen_blackrook && startx==0 && starty==0)
				queen_blackrook=1;
			if (!king_whiterook && startx==0 && starty==7)
				king_whiterook=1;
			if (!queen_whiterook && startx==7 && starty==7)
				queen_whiterook=1;
		}

		if (startx==destx || starty==desty) //Can move along same file
		{
			if (startx==destx)
			{

				int max=Max(starty,desty);
				int min=Min(starty,desty);

				if (min==starty) min+=1; //To exclude starting position
				if (min==desty) min+=1;
				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
		{
			return 0;
		}

}

void myframe::DisplayCutPiece(int piece)
{
	if (piece==100) //Means no piece has been cut
	{
		piececut=0;
		return;
	}
	piececut=piece;
	CString earliertext;
	if (piece<10)	//Place cut piece in white cut piece edit box
	{
		char *pname=pieceName(piece);		

		whitepieces.GetWindowText(earliertext);
		if (strcmp(pname,"v")==0) pname="b";
		else if (strcmp(pname,"w")==0) pname="q";
		else if (strcmp(pname,"l")==0) pname="k";
		else if (strcmp(pname,"o")==0) pname="p";
		else if (strcmp(pname,"m")==0) pname="n";
		else if (strcmp(pname,"t")==0) pname="r" ;			
		whitepieces.SetWindowText(earliertext+(CString)(pname[0]));
		
		if (sndenable)
		{
			if (computer_color==WHITE) 
			{
				
				PlaySound(GetMusicDirectory()+"\\Wav\\Yow.wav",NULL,0);
				
			}
			else
			{
				
				PlaySound(GetMusicDirectory()+"\\Wav\\Yippee.wav",NULL,0);
			}
		}

	}
	else	//Place cut piece in white cut piece edit box
	{
		blackpieces.GetWindowText(earliertext);
		blackpieces.SetWindowText(earliertext+pieceName(piece));
		if (sndenable)
		{
		
			if (computer_color==BLACK)
			{
				
				PlaySound(GetMusicDirectory()+"\\Wav\\Yow.wav",NULL,0);
				
			}
			else
			{
				
				PlaySound(GetMusicDirectory()+"\\Wav\\Yippee.wav",NULL,0);
			}
		}
			
	}

}


void myframe::ComputerPlay()
{	
	//Driver routine for starting search
	AfxBeginThread(StartSearch,this,THREAD_PRIORITY_NORMAL);
}

void myframe::ControlMusic()
{
	//if music_play has been set, start playing music. Otherwise stop
	if (music_play)
	{
		keep_music_playing=1;
		MusicPlayer m;
	}
	else
	{
		char ret[10];
		keep_music_playing=0;
		
		mciSendString("stop aria",ret,10,NULL);
		mciSendString("close aria",ret,lstrlen(ret),NULL);
		
		statusbar.SetText("Stopped playing Music",2,0);
	}

}

void myframe::ShowHideMusicPlay(CCmdUI *item)
{
	//Alternates between Play/Stop music. Disabled if autoplay disabled. Otherwise, depending on current status.
	if (autoplay==1)
	{
		char ret[10];
		mciSendString("status aria mode",ret,10,NULL);
		
		if (strcmp(ret,"playing")==0)
		{
			item->SetText("Stop Music");
			music_play=0;
		}
		else
		{
			item->SetText("Play Music");
			music_play=1;
		}
		item->Enable(TRUE);
	}
	else
	{
		item->Enable(FALSE);
	}
	DrawMenuBar();
}


int myframe::CanCastle(int startx,int starty,int destx,int desty)
{
	int kingpiece,rookpiece;
	int testcond;int rookcond;
	int newboard[8][8];
	memmove(newboard,board,BOARDSIZE);
	//Initialization
	if (piece<10)
	{
		kingpiece=WHITE_KING;rookpiece=WHITE_ROOK;
		testcond=(whitekingmoved==0);
		if (WHITEBASE==0)
		{
			if (startx>destx ) 
			{	
				rookcond=king_whiterook;
				castlecondition="KWR";
				if (newboard[desty][destx-1]!=rookpiece) return 0;
			}
			else 
			{
				if (newboard[desty][destx+1]!=rookpiece) return 0;
				rookcond=queen_whiterook;
				castlecondition="QWR";
			}
		}
		else
		{
			if (startx>destx)
			{
				if (newboard[desty][destx-1]!=rookpiece) return 0;
				rookcond=queen_whiterook;
				castlecondition="QWR";
			}
			else
			{
				if (newboard[desty][destx+1]!=rookpiece) return 0;
				rookcond=king_whiterook;
				castlecondition="KWR";
			}
		}
		lastmoved=BLACK;
	}
	else
	{
		
		kingpiece=BLACK_KING;rookpiece=BLACK_ROOK;
		testcond=(blackkingmoved==0);

		if (WHITEBASE==0)
		{
			if (startx>destx ) 
			{	
				if (newboard[desty][destx-1]!=rookpiece) return 0;
				rookcond=king_blackrook;
				castlecondition="KBR";
			}
			else 
			{
				if (newboard[desty][destx+1]!=rookpiece) return 0;
				rookcond=queen_blackrook;
				castlecondition="QBR";
			}
		}
		else
		{
			if (startx>destx)
			{
				if (newboard[desty][destx-1]!=rookpiece) return 0;
				rookcond=queen_blackrook;
				castlecondition="QBR";
			}
			else
			{
				if (newboard[desty][destx+1]!=rookpiece) return 0;
				rookcond=king_blackrook;
				castlecondition="KBR";
			}
		}
		
		
		lastmoved=WHITE;
	}
	//If condition is okay and king not in check now
	if (testcond && !CheckCheck(newboard,kingpiece,0))
	{
		
		if (((WHITEBASE==0 && startx-destx==2) || (BLACKBASE==0 && destx-startx==2)) && !rookcond)
		{
			
			
			if ((WHITEBASE==0 && (newboard[starty][startx-1]==0) && (newboard[starty][startx-2]==0)) || (BLACKBASE==0 && newboard[starty][startx+1]==0 && newboard[starty][startx+2]==0))
			{
				if (WHITEBASE==0)
				{
					newboard[starty][startx-1]=newboard[starty][startx];
					newboard[starty][startx]=0;
				}
				else
				{
					newboard[starty][startx+1]=newboard[starty][startx];
					newboard[starty][startx]=0;
				}
				//Move king one square to the left and check for check
				if (!CheckCheck(newboard,kingpiece,0))
				{
					
					if (WHITEBASE==0)
					{
						newboard[starty][startx-2]=newboard[starty][startx-1];
						newboard[starty][startx-1]=0;
					}
					else
					{
						newboard[starty][startx+2]=newboard[starty][startx+1];
						newboard[starty][startx+1]=0;
					}
					//Moved king to destination and checked for check
					if (!CheckCheck(newboard,kingpiece,0))
					{
						
						return 1;
					}
				}
			}
		}
		else 
		{
			//If rook and king have not been moved already
			if ((WHITEBASE==0 && (newboard[starty][startx+1]==0) && (newboard[starty][startx+2]==0) && newboard[starty][startx+3]==0) || (BLACKBASE==0 && newboard[starty][startx-1]==0 && newboard[starty][startx-2]==0 && newboard[starty][startx-3]==0))
			{
			
				if (WHITEBASE==0)
				{
					newboard[starty][startx+1]=newboard[starty][startx];
					newboard[starty][startx]=0;
				}
				else
				{
					newboard[starty][startx-1]=newboard[starty][startx];
					newboard[starty][startx]=0;
				}
				//Moved king one square and checked for check
				if (!CheckCheck(newboard,kingpiece,0))
				{
					
					if (WHITEBASE==0)
					{
						newboard[starty][startx+2]=newboard[starty][startx+1];
						newboard[starty][startx+1]=0;
					}
					else
					{
						newboard[starty][startx-2]=newboard[starty][startx-1];
						newboard[starty][startx-1]=0;
					}
					//Moved king two squares and checked for check
					if (!CheckCheck(newboard,kingpiece,0))
					{
						
						if (WHITEBASE==0)
						{
							newboard[starty][startx+3]=newboard[starty][startx+2];
							newboard[starty][startx+2]=0;
						}
						else
						{
							newboard[starty][startx-3]=newboard[starty][startx-2];
							newboard[starty][startx-2]=0;
						}
						//Moved king to destination squares and checked for check
						if (!CheckCheck(newboard,kingpiece,0))
						{
							
							return 2;
						}
					}
				}
			}
			
		}
		
			}
			
			Refresh();
			return 0;
			
}


int myframe::Castle(int x,int startx,int starty,int destx,int desty)
{
	
	int kingpiece,rookpiece;
	//Select correct color
	if (piece<10)
	{
		kingpiece=WHITE_KING;rookpiece=WHITE_ROOK;
	}
	else
	{
		kingpiece=BLACK_KING;rookpiece=BLACK_ROOK;
	}
	//If CanCastle was successful
	if (x==1)
	{
		castling_move=1;
		//Move king according to queen side or king side castling
		if (WHITEBASE==0)
		{
			board[starty][startx-1]=rookpiece;
			board[starty][startx-2]=kingpiece;
			board[starty][startx-3]=0;
			board[starty][startx]=0;
			
		}
		else
		{
			board[starty][startx+2]=kingpiece;
			board[starty][startx+1]=rookpiece;
			board[starty][startx]=0;
			board[starty][startx+3]=0;
		}
		
		Refresh();
		if (piece<10)
		{
			//Move king according to queen side or king side castling
			whitekingmoved=1;
			if (castlecondition=="QWR")
			{
				queen_whiterook=1;
			}
			else
			{
				king_whiterook=1;
			}
		}
		else
		{
			blackkingmoved=1;
			if (castlecondition=="QBR")
			{
				queen_blackrook=1;
			}
			else
			{
				king_blackrook=1;
			}
		}
		
		
		return 100;
	}
	else
	{
		castling_move=1;
		if (WHITEBASE==0)
		{
			board[starty][startx+2]=kingpiece;
			board[starty][startx+1]=rookpiece; 
			board[starty][startx+3]=0;
			board[starty][startx+4]=0;
		}
		else
		{
			board[starty][startx-2]=kingpiece;
			board[starty][startx-1]=rookpiece;
			board[starty][startx-3]=0;
			board[starty][startx-4]=0;
		}
		
		Refresh();
		if (piece<10)
		{
			whitekingmoved=1;
			if (castlecondition=="QWR")
			{
				queen_whiterook=1;
			}
			else
			{
				king_whiterook=1;
			}
		}
		else
		{	
			blackkingmoved=1;
			if (castlecondition=="QBR")
			{
				queen_blackrook=1;
			}
			else
			{
				king_blackrook=1;
			}
		}
		
		return 100;
		

	}
	return 0;
}



int myframe::CanCastle_Save(Game *game)
{
	if (computer_color==WHITE)
	{
		game->CastlingInfo.kingmoved=blackkingmoved;
		game->CastlingInfo.kingrookmoved=king_blackrook;
		game->CastlingInfo.queenrookmoved=queen_blackrook;
		
	}
	else
	{
		game->CastlingInfo.kingmoved=whitekingmoved;
		game->CastlingInfo.kingrookmoved=king_whiterook;
		game->CastlingInfo.queenrookmoved=queen_whiterook;
	}
	return 1;
}

⌨️ 快捷键说明

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