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

📄 myframe.cpp

📁 一个人工智能的国际象棋游戏
💻 CPP
📖 第 1 页 / 共 4 页
字号:
			mcount++;
			movelist.AddString(move);
		}
		
		//Display cut pieces.
		char cutpieces[16];
		in.getline(cutpieces,20,'\n'); //Dummy
		
		in.getline(cutpieces,16,'\n');
		whitepieces.SetWindowText(cutpieces);

		in.getline(cutpieces,16,'\n');
		blackpieces.SetWindowText(cutpieces);

		
		//Get the turn info.
		computer_color=newgame.computer_color;
		if (computer_color==BLACK)
		{
			lastmoved=BLACK;
		}
		else
		{
			lastmoved=WHITE;
		}
		firsttime=0;
		playing=1;end_of_game=0;
		//Get Castling info.
		if (computer_color==WHITE)
		{
			blackkingmoved=newgame.CastlingInfo.kingmoved;
			queen_blackrook=newgame.CastlingInfo.queenrookmoved;
			king_blackrook=newgame.CastlingInfo.kingrookmoved;
		}
		else
		{
			whitekingmoved=newgame.CastlingInfo.kingmoved;
			queen_whiterook=newgame.CastlingInfo.queenrookmoved;
			king_whiterook=newgame.CastlingInfo.kingrookmoved;
		}
		
		


	}


}

void myframe::unmakemove()
{
		
	int lastpiece;	int rookpiece;int sx,sy,dx,dy;
	can_undo=0;
	//If undone while previous move was made before rotating, exchange piece colors.
	if (rotated)
	{
		undo.blackmove.piece-=10;undo.whitemove.piece+=10;
	}
	//Next, special case for Castling where two pieces of same color and one of opp color need to be rearranged.
	if (computer_color==WHITE)
	{
			lastpiece=undo.blackmove.piece;
			
			//MessageBox(itoa(lastpiece,buf,10),"",0);
			rookpiece=BLACK_ROOK;
			
			sx=undo.blackmove.sourcex;
			sy=undo.blackmove.sourcey;
			dx=undo.blackmove.destx;
			dy=undo.blackmove.desty;
	}
	else
	{
		lastpiece=undo.whitemove.piece;
		rookpiece=WHITE_ROOK;
		sx=undo.whitemove.sourcex;
		sy=undo.whitemove.sourcey;
		dx=undo.whitemove.destx;
		dy=undo.whitemove.desty;
	}
	//If castling move
	if (lastpiece%10==2)
	{
		int squaresmoved=dx-sx;
		if (abs(squaresmoved)==2) //Means castling move
		{
			if (squaresmoved<0)
			{
				board[dy][dx+1]=0;
				
				board[dy][0]=rookpiece;
			}
			else
			{
				board[dy][dx-1]=0;
				board[dy][7]=rookpiece;
			}
			
		
		}
		if (lastpiece==BLACK_KING) blackkingmoved=0; else whitekingmoved=0;
		if (squaresmoved<0)
		{
			
			if (lastpiece==WHITE_KING)
			{
				king_whiterook=0;
			}
			else
			{
				king_blackrook=0;
				
			}
		}
		else
		{
			if (lastpiece==WHITE_KING)
			{
				queen_whiterook=0;
			}
			else
			{
				queen_blackrook=0;
			}
		}
	}
		//Actual undo code
		if (computer_color==WHITE)
		{
			
			
			board[undo.whitemove.sourcey][undo.whitemove.sourcex]=undo.whitemove.piece;
			if (undo.cut_blackpiece)
			{
				
				if (undo.whitemove.desty!=undo.blackmove.desty || undo.whitemove.destx!=undo.blackmove.destx)
				{
					board[undo.whitemove.desty][undo.whitemove.destx]=undo.cut_blackpiece;
				}
			}
			else
			{
				board[undo.whitemove.desty][undo.whitemove.destx]=0;
			}
			board[undo.blackmove.sourcey][undo.blackmove.sourcex]=undo.blackmove.piece;
			if (undo.cut_whitepiece)
			{
				board[undo.blackmove.desty][undo.blackmove.destx]=undo.cut_whitepiece;
			}
			else
			{
				board[undo.blackmove.desty][undo.blackmove.destx]=0;
			}
			
		}
		else
		{
			board[undo.blackmove.sourcey][undo.blackmove.sourcex]=undo.blackmove.piece;
			if (undo.cut_whitepiece)
			{
				if (undo.blackmove.desty!=undo.whitemove.desty || undo.blackmove.destx!=undo.whitemove.destx)
				{
					board[undo.blackmove.desty][undo.blackmove.destx]=undo.cut_whitepiece;
				}
			}
			else
			{
				board[undo.blackmove.desty][undo.blackmove.destx]=0;
			}
			if (undo.cut_blackpiece)
			{
				board[undo.whitemove.desty][undo.whitemove.destx]=undo.cut_blackpiece;
			}
			else
			{
				board[undo.whitemove.desty][undo.whitemove.destx]=0;
			}
			board[undo.whitemove.sourcey][undo.whitemove.sourcex]=undo.whitemove.piece;
		}

	//Remove Cut piece, if any from cut pieces list and remove last 2 entries from movelist.
	movelist.DeleteString(movecount-1);movelist.DeleteString(movecount-2);movecount-=2;
	if (undo.cut_whitepiece!=0)
	{
		CString earliertext;
		whitepieces.GetWindowText(earliertext);
		earliertext=earliertext.Left(earliertext.GetLength()-2); 
		whitepieces.SetWindowText(earliertext);
	}
	if (undo.cut_blackpiece!=0)
	{
		CString earliertext;
		blackpieces.GetWindowText(earliertext);
		earliertext=earliertext.Left(earliertext.GetLength()-2);
		blackpieces.SetWindowText(earliertext);
	}
	undo.cut_whitepiece=0;
	undo.cut_blackpiece=0;
	Refresh();
	

}
void myframe::ReverseMove()
{
	MessageBox("Sorry. Not a valid move. Please see the statusbar to know why","Erroneous move",0);
	board[startsquare[1]][startsquare[0]]=board[endsquare[1]][endsquare[0]];
	board[endsquare[1]][endsquare[0]]=destpiece;
	Refresh();
	if (piececut<10)
	{
		CString earliertext;
		whitepieces.GetWindowText(earliertext);
		earliertext=earliertext.Left(earliertext.GetLength()-2); 
		whitepieces.SetWindowText(earliertext);
	}
	else 
	{
		CString earliertext;
		blackpieces.GetWindowText(earliertext);
		earliertext=earliertext.Left(earliertext.GetLength()-2);
		blackpieces.SetWindowText(earliertext);
	}
	piececut=0;
}
void myframe::Refresh(CDC *q)
{
	CDC *p;int x,y;
	if (q==NULL)
	{
	
		p=GetDC();
	}
	else
	{
		p=q;
	}
	//Draw Outer rectangle
	p->Rectangle(ORIGINX,ORIGINY,ORIGINX+8*SQUARE_SIZE,ORIGINY+8*SQUARE_SIZE);		
	
	x=ORIGINX;y=ORIGINY;
	int rowcount=1;
	for (int i=0;i<64;i++)
	{
		
		if (i!=0 && i%8==0)
		{
			x=ORIGINX;
			y=y+SQUARE_SIZE;
			rowcount+=1;
		}
		if (i%2==0)
			if (rowcount%2==0) style=SS_GRAYRECT; else style=SS_WHITERECT; 
			else
				if (rowcount%2==0) style=SS_WHITERECT; else style=SS_GRAYRECT; 
				//Draw color squares
				if (style==SS_GRAYRECT)
				{
					p->FillRect(&(CRect(x,y,x+SQUARE_SIZE,y+SQUARE_SIZE)),&(CBrush(BLACKSQUARE)));
				}
				else
				{
					p->FillRect(&(CRect(x,y,x+SQUARE_SIZE,y+SQUARE_SIZE)),&(CBrush(WHITESQUARE)));
				}
				x=x+SQUARE_SIZE;
				
	}
	
	p->SelectObject(&chessfont);
	p->SetBkMode(TRANSPARENT);
	//Draw chess pieces.
	for (i=0;i<8;i++)
	{
		for (int j=0;j<8;j++)
		{
			if (board[i][j]<10 && board[i][j]!=0)
			{
				p->SetTextColor(RGB(255,255,255));
				p->TextOut(j*SQUARE_SIZE+XPADDING+ORIGINX,i*SQUARE_SIZE+YPADDING+ORIGINY,pieceName(board[i][j]));
			}
			else if (board[i][j]>10)
			{
				p->SetTextColor(RGB(0,0,0));
				p->TextOut(j*SQUARE_SIZE+XPADDING+ORIGINX,i*SQUARE_SIZE+YPADDING+ORIGINY,pieceName(board[i][j]));
			}
			
		}
	}
	
	ReleaseDC(p);
	
}

void myframe::OnLButtonDown(UINT flags,CPoint point)
{

	//Get starting square	
	startsquare[0]=(point.x-ORIGINX)/(SQUARE_SIZE);
	startsquare[1]=(point.y-ORIGINY)/(SQUARE_SIZE);
	//If square within board
	if (startsquare[0]<0 || startsquare[1]<0 || startsquare[0]>7 || startsquare[1]>7 || endsquare[0]<0 || endsquare[1]<0 || endsquare[0]>7 || endsquare[1]>7) return;
	{
		//If computer has finished thinking
		if (lastmoved==computer_color)
		{
			piece=board[startsquare[1]][startsquare[0]];	
			pieceselected=1;
			first=1;
		}
	}
}
void myframe::OnMouseMove(UINT flags,CPoint point)
{
	static int prevx=0;
	static int prevy=0;
	
	if (pieceselected==1 && playing)
	{
		//Get square positions
		int x=((point.x-ORIGINX)/(SQUARE_SIZE))*SQUARE_SIZE+ORIGINX;
		int y=((point.y-ORIGINX)/(SQUARE_SIZE))*SQUARE_SIZE+ORIGINX;
		CDC *p=GetDC();
		//Draw highlight square
		if (!first)
		{
			p->DrawDragRect(CRect(x,y,x+SQUARE_SIZE,y+SQUARE_SIZE),CSize(2,2),CRect(prevx,prevy,prevx+SQUARE_SIZE,prevy+SQUARE_SIZE),CSize(2,2),&highlighter,NULL);
		}
		else
		{
			 
			p->DrawDragRect(CRect(x,y,x+SQUARE_SIZE,y+SQUARE_SIZE),CSize(0,0),NULL,CSize(0,0),NULL,NULL);
			first=0;
		}
		prevx=x;
		prevy=y;
	}
}

void myframe::OnLButtonUp(UINT nflags,CPoint point)
{

	//Get ending square
	endsquare[0]=(point.x-ORIGINX)/(SQUARE_SIZE);
	endsquare[1]=(point.y-ORIGINY)/(SQUARE_SIZE);
	//Reset pieceselected
	pieceselected=0;first=1;
	//If squares within board boundaries
	if (startsquare[0]<0 || startsquare[1]<0 || startsquare[0]>7 || startsquare[1]>7 || endsquare[0]<0 || endsquare[1]<0 || endsquare[0]>7 || endsquare[1]>7) return;
	//If no move (startsquare=endsquare) return
	if (startsquare[0]==endsquare[0] && startsquare[1]==endsquare[1]) return;
	//Get piece id
	piece=board[startsquare[1]][startsquare[0]];	
	char buf[100];	
	//If move is valid
	if (!end_of_game && validmove() && !searching && playing)
	{
		//Kill ponder thread
		if (ponder_thread!=NULL)
		{
			::TerminateThread(ponder_thread->m_hThread,0);
			ponder_thread=NULL;
			pondering=0;
			statusbar.SetText("Pondering Over",1,0);
		}
		//Reset rotate on rotatecount=0
		if (rotated && rotatecount==0) { rotated=0;} else {rotatecount--;}
		//If move has been made by the correct colored side or this is the first move
		if ((lastmoved==0 && ((computer_color==WHITE && piece>10) || (computer_color==BLACK && piece<10 && piece>0))) || (lastmoved==WHITE && piece>10) || (lastmoved==BLACK && piece<10))
		{
			playing=1;can_undo=1;

			if (!castling_move)
			{
				//Move piece in board and display
				movePieceInRealBoard();
				CString move;
				
				//Generate algebraic notation
				move+=(CString)itoa(++movecount,buf,10)+". ";
				if ((computer_color==WHITE && WHITEBASE==0) || (computer_color==BLACK && BLACKBASE==0))
				{
					move+=(char)((startsquare[0])+97);move+=itoa(8-(startsquare[1]),buf,10);move+=" - ";move+=(char)(endsquare[0]+97);move+=itoa(8-(endsquare[1]),buf,10);
				}
				else
				{
					move+=(char)((startsquare[0])+97);move+=itoa(8-(startsquare[1]),buf,10);move+=" - ";move+=(char)(endsquare[0]+97);move+=itoa(8-(endsquare[1]),buf,10);
				
				}
				movelist.AddString(move);
				
			}
			else
			{
				//Generate special message for Castling
				CString move;
				move="Castling";
				movelist.AddString(move);
				castling_move=0;
				if (computer_color==WHITE)
				{
					piece=BLACK_KING;
				}
				else
				{
					piece=WHITE_KING;
				}

			}
			lastmoved=(piece<10?WHITE:BLACK);
			
				
				int val=CheckCheck(board,(lastmoved==WHITE)?BLACK_KING:WHITE_KING); //This is for checking whether a check has occurred after opponent moved.
				if (lastmoved==BLACK && computer_color==WHITE)
				{
					timeover=1;
					if (piece==BLACK_KING) blackkingmoved=1;// Set king moved. Cannot castle any more.
					
					if (CheckCheck(board,BLACK_KING)) //Checking to see if move of Black avoids Check on Black King. If not, don't allow move;
					{
						ReverseMove();
						lastmoved=WHITE;
						statusbar.SetText("",0,0);
						return;
					}
					
					
					ComputerPlay();
					
					statusbar.SetText("",0,0);  //Clearing message in statusbar
				}
				else if(lastmoved==WHITE && computer_color==BLACK)
				{
					if (piece==WHITE_KING) whitekingmoved=1;
					if (CheckCheck(board,WHITE_KING)) //Checking to see if move of Black avoids Check on Black King. If not, don't allow move;
					{
						ReverseMove();
						lastmoved=BLACK;
						statusbar.SetText("",0,0);
						return;
					}
				
					
					ComputerPlay();
				}
		}

		
	}
		
	else
	{
		statusbar.SetText("Illegal Move",0,0);
		Refresh();
	}
}	

void myframe::initializeBoard(int count,CDC *p)
{
		//Create round rectangle,row numbers and column alphabets.			
		CBrush mybrush;
		mybrush.CreateSolidBrush(RGB(100,100,210));
		p->SelectObject(mybrush);
		p->RoundRect(CRect(0,0,2*ORIGINX+SQUARE_SIZE*8,2*ORIGINY+SQUARE_SIZE*8),CPoint(20,20));
		p->SetBkColor(RGB(0,0,0));
		p->Rectangle(ORIGINX-2,ORIGINY-2,SQUARE_SIZE*8+ORIGINX+2,SQUARE_SIZE*8+ORIGINY+2);		
		CFont temp;
		temp.CreateFont(15,5,0,0,0,0,0,0,0,0,0,0,0,"Verdana");
		char buf[10];
		int stx=ORIGINX;int sty=ORIGINY+SQUARE_SIZE*8+10;
		int changey=sty-30;;
		for (char i=97;i<105;i++)
		{
			p->SetBkMode(TRANSPARENT);
			p->SelectObject(&temp);
			p->TextOut(stx+20,sty,i);
			stx+=SQUARE_SIZE;
			p->TextOut(ORIGINX-20,changey,itoa((int)(i-96),buf,10));
			changey-=SQUARE_SIZE;
		}
		if (count==0)
		{
			//First time. So arrange pieces in board and refresh display
			p->SelectObject(&chessfont);
			p->SetBkMode(TRANSPARENT);
			p->SetTextColor(RGB(0,0,0));
			for (int z=0;z<8;z++)
			{
				for (int b=0;b<8;b++)
				{
					board[z][b]=0;
				}
			}
			for (int j=0;j<8;j++)        //Set Pawns
			{
				board[1][j]=WHITE_PAWN;
				board[6][j]=BLACK_PAWN;
			}
			//Set Other Pieces
			ArrangeOtherPieces(p,1);
			ArrangeOtherPieces(p,2);
			
			Refresh(p);
		//Display Area for showing cut pieces
		}
		else   //Simply Refreshing display after OnPaint();
		{
			CFont temp;
			
			temp.CreateFont(15,10,0,0,0,00,0,0,0,0,0,0,0,"Arial");
			statusbar.SetFont(&temp,0);
			
			Refresh(p);
			
			
			
		}		
		
}

void myframe::movePieceInRealBoard()
{
	
	//Storing destpiece and replacing piece in dest square with currentpiece
	destpiece=board[endsquare[1]][endsquare[0]];	
	piece=board[startsquare[1]][startsquare[0]];
	board[startsquare[1]][startsquare[0]]=0;
	board[endsquare[1]][endsquare[0]]=piece;
	
	movePieceInViewBoard(0);
}

void myframe::movePieceInViewBoard (int undercheck,int sx,int sy,int dx,int dy)
{
	
	//Special case for pawn promotion. Displays a dialog box and replaces pawn with selected piece
	//if computer is playing from top of board
	if ((((piece==BLACK_PAWN && computer_color==WHITE  && WHITEBASE==0) || (piece==WHITE_PAWN && computer_color==BLACK && BLACKBASE==0)) &&  endsquare[1]==0)) //Pawn promotion
	{
		mydialog d(IDD_DIALOG3);
		d.DoModal();
		piece=d.GetSelected();
		if (piece==WHITE_PAWN) piece-=10;
		board[endsquare[1]][endsquare[0]]=piece;
		Refresh();

	}
	else
	{
		//If computer is playing from bottom of board
		
		if (((piece==WHITE_PAWN && computer_color==BLACK && WHITEBASE==0) || (piece==BLACK_PAWN && computer_color==WHITE && BLACKBASE==0)) && endsquare[1]==7)
		{
			mydialog d(IDD_DIALOG3);
			d.DoModal();
			piece=d.GetSelected();
			if (piece==WHITE_PAWN) piece-=10;
			board[endsquare[1]][endsquare[0]]=piece;
			Refresh();
		}
		else
		{
			//If pawn played by computer has reached last rank, replace with queen
			if ((computer_color==WHITE && piece==WHITE_PAWN) && ((WHITEBASE==0 && endsquare[1]==7) || (WHITEBASE==7 && endsquare[1]==0)))
			{
				piece=WHITE_QUEEN;
				board[endsquare[1]][endsquare[0]]=piece;
			}
			else if(computer_color==BLACK && piece==BLACK_PAWN && ((BLACKBASE==0 && endsquare[1]==7) || (BLACKBASE==7 && endsquare[1]==0)))
			{
				piece=BLACK_QUEEN;
				board[endsquare[1]][endsquare[0]]=piece;

⌨️ 快捷键说明

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