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

📄 movesbackup.cpp

📁 一个人工智能的国际象棋游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "Global.h"
#include "OneMove.h"
#include "Moves.h"
#include "TransTableEntry.h"
#include "TranspositionTable.h"
#include "MoveQueue.h"

extern int WHITEBASE;
extern int BLACKBASE;
extern TranspositionTable table;
extern int movecount;
int search_firsttime=0;


MoveQueue movequeue;
OneMove *allmoves;

Moves::Moves()
	{
		head=new OneMove();
		//head->next=NULL;
	
		attackhead=new OneMove();
		normalhead=new OneMove();
		//head=attackhead;
		size=0;
		
		//current=head;
		acurrent=attackhead;
		ncurrent=normalhead;
		attackhead->next=NULL;
		normalhead->next=NULL;
	}
	
	
	
	
	
	
	
	/*void add(int p,int source,int sourcey,int dx,int dy,int noa)
	{
		//current=head;
		//while (current->next!=NULL) current=current->next;
		OneMove *newnode=new OneMove();
		newnode->sourcex=source;
		newnode->sourcey=sourcey;
		newnode->destx=dx;
		newnode->desty=dy;
		newnode->piece=p;
		//newnode->norm_or_attack=noa;
		newnode->next=NULL;
		current->next=newnode;
		current=newnode;
		size++;
	}*/
void Moves::add(int p,int source,int sourcey,int dx,int dy,int noa)
	{
		//current=head;
		//while (current->next!=NULL) current=current->next;
		/*if (sourcey>7 || dy>7)
		{
			char buf[10];
			MessageBox(NULL,"Added Wrong","",0);
			MessageBox(NULL,itoa(p,buf,10),"",0);
		}*/
	
		OneMove *newnode=new OneMove();
		newnode->sourcex=source;
		newnode->sourcey=sourcey;
		newnode->destx=dx;
		newnode->desty=dy;
		newnode->piece=p;
		//newnode->norm_or_attack=noa;
		newnode->next=NULL;
		//MessageBox(NULL,"Join called","",0);
		
		if (noa==1)
		{
			acurrent->next=newnode;
			acurrent=newnode;
	
			//acurrent->next=normalhead;
		}
		else
		{
			ncurrent->next=newnode;
			ncurrent=newnode;
	
		}
		size++;
	}
	
void Moves::combine()
	{
		
	
		if (attackhead->next!=NULL)
		{
	
			head=attackhead;
			acurrent->next=normalhead->next;
		}
		else
		{
	
			head=normalhead;
		}
	}
	
void Moves::destroy()
	{
		for (OneMove *temp=head;temp!=NULL;)
		{
			OneMove *tmp=temp->next;
			delete temp;
			temp=tmp;
			
		}
		
	if (attackhead->next==NULL) delete attackhead;
			if (head!=normalhead) delete normalhead;
	
	}
	
	/*void sortMoves(int board[8][8],int color)
	{
		OneMove *temp=head;OneMove *p;int min;
		OneMove *temp2;
		int newboard1[8][8];int newboard2[8][8];
		for (temp=head->next;temp!=NULL;temp=temp->next)
		{
			
			
				for (int i=0;i<8;i++)
				{
					for (int j=0;j<8;j++)
					{
						newboard1[i][j]=board[i][j];
					}
				}
				executeMove(newboard1,temp);
				min=eval(newboard1,color);
				p=temp;
			
			
			for (temp2=temp;temp2!=NULL;temp2=temp2->next )
			{
				for (int i=0;i<8;i++)
				{
					for (int j=0;j<8;j++)
					{
						newboard2[i][j]=board[i][j];
					}
				}
				executeMove(newboard2,temp2);
				int val=eval(newboard2,color);
				if ((color==1 && val>min) || (color==0 && val<min))
				{
					min=val;
					p=temp2;
				}
			}
			int piece=temp->piece;int sx=temp->sourcex;int sy=temp->sourcey;int dx=temp->destx;int dy=temp->desty;
			temp->piece=p->piece;temp->sourcex=p->sourcex;temp->sourcey=p->sourcey;temp->destx=p->destx;temp->desty=p->desty;
			p->piece=piece;
			p->sourcex=sx;
			p->sourcey=sy;
			p->destx=dx;
			p->desty=sy;
		}
			
		
					
	
	}

	
	int eval(int currentboard[8][8],int color)
	{
		//ofstream in("C:\\Hello.txt",ios::app);
		int value=0;
		int oppplayer_value=0;
			for (int i=0;i<8;i++)
			{
				for (int j=0;j<8;j++)
				{
					
					if (currentboard[i][j]<10 && currentboard[i][j]!=0)
					{
						switch (currentboard[i][j]%10)
						{
							case 1:
							{
		
								value+=100;
							}
							break;
							case 2:
							{
								value+=10000;
							}
							break;
							case 3:
							{
								value+=900;
							}
							break;
							case 4:
							{
								value+=325;
							}
							break;
							case 5:
							{
								value+=300;
							}break;
							case 6:
							{
								value+=500;
							}
							break;
						}
					}
					
				}
			}
		
		
			for (i=7;i>=0;i--)
			{
				for (int j=7;j>=0;j--)
				{
					
					if (currentboard[i][j]>10)
					{
						switch(currentboard[i][j]%10)
						{
							case 1:
							{
								oppplayer_value+=100;
							}
							break;
							case 2:
							{
								oppplayer_value+=10000;
							}break;
							case 3:
							{
								oppplayer_value+=900;
							}break;
							case 4:
							{
								oppplayer_value+=325;
							}
							break;
							case 5:
							{
								oppplayer_value+=300;
							}break;
							case 6:
							{
								oppplayer_value+=500;
							}
					
						}
					}
				}
			}
		
		*/
		//char buf[255];
		//MessageBox(NULL,"Pawn Count = "+(CString)itoa(w_pawncount,buf,10),"",0);
		/*srand((unsigned)time(NULL));
		return rand();*/
		/*fstream in("C:\\Hello.txt",ios::app);
		
		in<<"\t"<<(value)-oppplayer_value<<endl;
		in.close();
		*/
		
		//if (color==1)return ((value*2)-oppplayer_value);
		//else return -((oppplayer_value*2)-value);
	 	
	//}

	
	
	
	
	
	
	/*void executeMove(int currentboard[8][8],OneMove *temp)
	{
		currentboard[temp->desty][temp->destx]=currentboard[temp->sourcey][temp->sourcex];
		currentboard[temp->sourcey][temp->sourcex]=0;		
	}*/
#define cond(color,check) (color==WHITE?(check>10 ):(check<10  && check!=0))
	
		
	
	
void Moves::GeneratePawnMoves(int board[8][8],int color)
	{
		
		int piece;
		piece=(color==WHITE?WHITE_PAWN:BLACK_PAWN);
		if ((color==WHITE && WHITEBASE==0)	|| (color==BLACK && BLACKBASE==0))
		{
		
			for (int i=0;i<8;i++)
			{
				for (int j=0;j<8;j++)
				{
					
					if (board[i][j]==piece)
					{
						
						if (i<7 && j<7 && cond(color,board[i+1][j+1]))
						{
							//fstream out("C:\\Hello.txt",ios::app);
							//out<<"White Pawn at "<<i<<","<<j<<" and piece at dest "<<board[i+1][j+1]<<endl;
							add(piece,j,i,j+1,i+1,1);
						}
						if (i<7 && j>0 && cond(color,board[i+1][j-1]))
						{
							add(piece,j,i,j-1,i+1,1);
						}
						if (i<7 && board[i+1][j]==0)
						{
							add(piece,j,i,j,i+1,0);
						}
						
						
						if (i==1)
						{
							if (board[i+2][j]==0 && board[i+1][j]==0)
							{
								add(piece,j,i,j,i+2,0);
							}
						}
						
						
					}
				}
			}
		}
		else if( (color==WHITE && WHITEBASE==7) || (color==BLACK && BLACKBASE==7))
		{
			
			
			
			
			for (int i=0;i<8;i++)
			{
				for (int j=0;j<8;j++)
				{
					if (board[i][j]==piece)
					{
						if (i==6)
						{
							if (board[i-2][j]==0 && board[i-1][j]==0)
							{
								add(piece,j,i,j,i-2,0);
							}
						}
						if (i>0 && board[i-1][j]==0 )
						{
							add(piece,j,i,j,i-1,0);
						}
						if (i>0 && j>0 && cond(color,board[i-1][j-1]))
						{
								add(piece,j,i,j-1,i-1,1);
						}
						if (i>0 && j<7 && cond(color,board[i-1][j+1]))
						{
							add(piece,j,i,j+1,i-1,1);
						}
						

					}
				}
			}
			
		}
	}
#define gencond(color,check) (color==WHITE?((check==0 || check>10)):(check < 10))
	
void Moves::GenerateKnightMoves(int board[8][8],int color)
	{
		int dummy;
		
		int piece;
		piece=(color==WHITE?WHITE_KNIGHT:BLACK_KNIGHT);
		
		for (int i=0;i<8 ;i++)
		{
			for (int j=0;j<8;j++)
			{
				if (board[i][j]==piece)
				{
					
					if (i<6 && j<7 && gencond(color,board[i+2][j+1]))
					{
						if (board[i+2][j+1]==0)
						{
							dummy=0;
						}
						else
						{
							dummy=1;
						}
						
						add(piece,j,i,j+1,i+2,dummy);
					}
					if (i<6 && j>0 && gencond(color,board[i+2][j-1]))
					{
						if (board[i+2][j-1]==0)
						{
							dummy=0;
						}
						else
						{
							dummy=1;
						}
						
						add(piece,j,i,j-1,i+2,dummy);
					}
					if (i>1 && j<7 && gencond(color,board[i-2][j+1]))
					{
						if (board[i-2][j+1]==0)
						{
							dummy=0;
						}
						else
						{
							dummy=1;
						}
						
						add(piece,j,i,j+1,i-2,dummy);
					}
					if (i>1 && j>0 && gencond(color,board[i-2][j-1]))
					{
						if (board[i-2][j-1]==0)
						{
							dummy=0;
						}
						else
						{
							dummy=1;
						}
						
						add(piece,j,i,j-1,i-2,dummy);
					}
					//
					if (i<7 && j<6 && gencond(color,board[i+1][j+2]))
					{
						if (board[i+1][j+2]==0)
						{
							dummy=0;
						}
						else
						{
							dummy=1;
						}
						
						add(piece,j,i,j+2,i+1,dummy);
					}
					if (i<7 && j>1 && gencond(color,board[i+1][j-2]))
					{
						if (board[i+1][j-2]==0)
						{
							dummy=0;
						}
						else
						{
							dummy=1;
						}
						
						add(piece,j,i,j-2,i+1,dummy);
					}
					if (i>0 && j<6 && gencond(color,board[i-1][j+2]))
					{
						if (board[i-1][j+2]==0)
						{
							dummy=0;
						}
						else
						{
							dummy=1;
						}
						
						add(piece,j,i,j+2,i-1,dummy);
					}
					if (i>0 && j>1 && gencond(color,board[i-1][j-2]))
					{
						if (board[i-1][j-2]==0)
						{
							dummy=0;
						}
						else
						{
							dummy=1;
						}
						
						add(piece,j,i,j-2,i-1,dummy);
					}

				}
			}
		}
			
	}

#define cond2(color,check) (color==WHITE?(check > 10):(check < 10))
	void Moves::GenerateBishopMoves(int board[8][8],int color)
	{
		
		int piece;int k,m;
		piece=(color==WHITE?WHITE_BISHOP:BLACK_BISHOP);
		
		for (int i=0;i<8;i++)
		{
			for (int j=0;j<8;j++)
			{
				if (board[i][j]==piece)
				{
					
					for (k=i-1,m=j-1;k>=0 && m>=0;k--,m--)
					{

⌨️ 快捷键说明

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