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

📄 heibaiqi.java

📁 java编的黑白棋,基本功能全部实现,算法较好
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

		this.state=i;
		if(i==HeiBaiButton.white) setIcon(image1);
		if(i==HeiBaiButton.black) setIcon(image2);
		if(i==HeiBaiButton.blacking) setIcon(image3);
		if(i==HeiBaiButton.whiting) setIcon(image4);
		if(i==HeiBaiButton.blank) setIcon(image5);
	}
public void addtochange(HeiBaiButton thebutton)
	{
			arraylist.add(thebutton);
	}
	public void clearList()
	{
		arraylist.clear();
	}
}
class RunThread implements Runnable,Serializable
{
	ArrayList<HeiBaiButton> arraylist1;
	ArrayList<HeiBaiButton> arraylist2;
	HeiBaiButton result=null;
	HeiBaiButton[][] thebutton;
	Caculate caculate=null;
	int m;
  static int time1=0;
  static int time2=0;
	JLabel text;
	 static boolean flag;
   static boolean AI;
	static boolean sequence=true;
	public RunThread(ArrayList<HeiBaiButton> arraylist1,ArrayList<HeiBaiButton> arraylist2,HeiBaiButton[][] thebutton,Caculate caculate,int m,boolean flag,boolean AI,JLabel text)
	{
  this.AI=AI;
	this.flag=flag;
	this.arraylist1=arraylist1; 
	this.arraylist2=arraylist2;
	this.thebutton=thebutton;
	this.caculate=caculate;
	this.m=m;
	this.text=text;
  }
public void run()
{
	while(true)
	{
	if(flag)
	{
		text.setText("白方用时:   "+time2);
		++time2;
		System.out.println("++2,time2 is"+time2);
		try
		{
		Thread.sleep(1000);
   	}
   	catch(Exception ex)
   	{}
   
	}
	else
	{
 	text.setText("黑方用时:   "+time1);
		++time1;
				System.out.println("++1,time1 is:"+time1);
		try
		{
		Thread.sleep(1000);
   	}
   	catch(Exception ex)
   	{}
 
	}
  }
}
public void begin()                                        
{
if(AI!=true)
{
if((arraylist1.size()+arraylist2.size()<m*m)&&arraylist1.size()!=0&&arraylist2.size()!=0) //计时器开始,计算可以下的位置
{
if(flag)
{
int temp=0;
for(HeiBaiButton abutton:arraylist2)                       //计算白方可以下的位置
caculate.findblack(thebutton,abutton);
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
{
if(thebutton[i][j].state==HeiBaiButton.blacking)
{
++temp;
}
}
flag=false;
if(temp==0) 
{
JOptionPane.showMessageDialog(null,"黑方无子可下","黑白棋",JOptionPane.PLAIN_MESSAGE);
begin();
}
}
else
{
int temp=0;
for(HeiBaiButton abutton:arraylist1)                       //计算黑方可以下的位置
caculate.findwhite(thebutton,abutton);
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
{
if(thebutton[i][j].state==HeiBaiButton.whiting)
{
++temp;
}
}
flag=true;
if(temp==0) 
{
JOptionPane.showMessageDialog(null,"白方无子可下","黑白棋",JOptionPane.PLAIN_MESSAGE);
begin();
}

}	
}
else
end();
}
else
AIbegin();
}
public void AIbegin()
{
if((arraylist1.size()+arraylist2.size()<m*m)&&arraylist1.size()!=0&&arraylist2.size()!=0) //计时器开始,计算可以下的位置
{
if(flag)
{
if(sequence)
{
int max=0;
for(HeiBaiButton abutton:arraylist2)                       //计算白方可以下的位置
caculate.findblack(thebutton,abutton);
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
{
if(thebutton[i][j].state==HeiBaiButton.blacking)
{
ArrayList<HeiBaiButton> temp1=new ArrayList<HeiBaiButton>(arraylist1);
ArrayList<HeiBaiButton> temp2=new ArrayList<HeiBaiButton>(arraylist2);
caculate.changeToBlack(thebutton[i][j],temp1,temp2);
if(max<temp2.size())
{
max=temp2.size();
result=thebutton[i][j];
}
}
}
if(max==0) end();
caculate.changeToBlack(result,arraylist1,arraylist2);
	for(int i=1;i<=m;i++)                            //标记清空
  for(int j=1;j<=m;j++)
 {
	 thebutton[i][j].clearList();
	 thebutton[i][j].setState(HeiBaiButton.blank);
	}
 	for(HeiBaiButton abutton:arraylist1)                      //白方棋子重新显示
		  {
		  	abutton.setState(HeiBaiButton.white);
		  }
     for(HeiBaiButton abutton:arraylist2)                 //黑方棋子重新显示
		  {
		  	abutton.setState(HeiBaiButton.black);
		  }
		 flag=false;
		 sequence=false;
		 AIbegin();
}
else
{
int temp=0;
for(HeiBaiButton abutton:arraylist2)                       //计算白方可以下的位置
caculate.findblack(thebutton,abutton);
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
{
if(thebutton[i][j].state==HeiBaiButton.blacking)
++temp;
}
flag=false;
sequence=true;
if(temp==0)
{ 
JOptionPane.showMessageDialog(null,"你无子可下","黑白棋",JOptionPane.PLAIN_MESSAGE);
AIbegin();
}
}
}
else
{
if(sequence)
{
int max=0;
for(HeiBaiButton abutton:arraylist1)                       //计算白方可以下的位置
caculate.findwhite(thebutton,abutton);
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
{
if(thebutton[i][j].state==HeiBaiButton.whiting)
{
ArrayList<HeiBaiButton> temp1=new ArrayList<HeiBaiButton>(arraylist1);
ArrayList<HeiBaiButton> temp2=new ArrayList<HeiBaiButton>(arraylist2);
caculate.changeToWhite(thebutton[i][j],temp1,temp2);
if(max<temp1.size())
{
max=temp1.size();
result=thebutton[i][j];
}
}
}
caculate.changeToWhite(result,arraylist1,arraylist2);
	for(int i=1;i<=m;i++)                            //标记清空
  for(int j=1;j<=m;j++)
 {
	 thebutton[i][j].clearList();
	 thebutton[i][j].setState(HeiBaiButton.blank);
	}
 	for(HeiBaiButton abutton:arraylist1)                      //白方棋子重新显示
		  {
		  	abutton.setState(HeiBaiButton.white);
		  }
     for(HeiBaiButton abutton:arraylist2)                 //黑方棋子重新显示
		  {
		  	abutton.setState(HeiBaiButton.black);
		  }
		  flag=true;
		  sequence=false;   
		  AIbegin();               
}
else
{
int temp=0;
for(HeiBaiButton abutton:arraylist1)                       //计算黑方可以下的位置
caculate.findwhite(thebutton,abutton);
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
{
if(thebutton[i][j].state==HeiBaiButton.whiting)
++temp;
}
flag=true;
sequence=true;
if(temp==0)
{
JOptionPane.showMessageDialog(null,"你方无子可下","黑白棋",JOptionPane.PLAIN_MESSAGE); 
AIbegin();
}
}
}	
}
else
end();
}
public void end()
{
	int white=arraylist1.size();
	int black=arraylist2.size();
	String temp=null;
	if(white>black)
	temp=new String("白方胜黑方"+(white-black)+"个棋子");
	if(white<black)
  temp=new String("黑方胜白方"+(black-white)+"个棋子");
  if(white==black)
  temp=new String("双方战平");
	String message=new String("游戏结束:"+temp);
	JOptionPane.showMessageDialog(null,message,"黑白棋",JOptionPane.PLAIN_MESSAGE);
}
}

 class Caculate implements Serializable
{
	HeiBaiButton[][] thebutton;
	ArrayList<HeiBaiButton> blackarray,whitearray;
	int m;
	public Caculate(HeiBaiButton[][] thebutton,ArrayList<HeiBaiButton> blackarray,ArrayList<HeiBaiButton> whitearray,int m)
	{
		this.thebutton=thebutton;
		this.blackarray=blackarray;
		this.whitearray=whitearray;
		this.m=m;
	}

public void findblack(HeiBaiButton[][] thebutton,HeiBaiButton abutton)
	{
		int mode=HeiBaiButton.blacking;
		int p=HeiBaiButton.white;
		int q=HeiBaiButton.black;
		int i=abutton.i;
		int j=abutton.j;
	  findright(thebutton,i,j,mode,p,q);
	  findleft(thebutton,i,j,mode,p,q);
	  findup(thebutton,i,j,mode,p,q);
	  finddown(thebutton,i,j,mode,p,q);
	  findrightup(thebutton,i,j,mode,p,q);
	  findrightdown(thebutton,i,j,mode,p,q);
	  findleftup(thebutton,i,j,mode,p,q);
	  findleftdown(thebutton,i,j,mode,p,q);
	}
public void findwhite(HeiBaiButton[][] thebutton,HeiBaiButton abutton)
{

		int i=abutton.i;
		int j=abutton.j;
		int mode=HeiBaiButton.whiting;
		int p=HeiBaiButton.black;
		int q=HeiBaiButton.white;
	  findright(thebutton,i,j,mode,p,q);
	  findleft(thebutton,i,j,mode,p,q);
	  findup(thebutton,i,j,mode,p,q);
	  finddown(thebutton,i,j,mode,p,q);
	  findrightup(thebutton,i,j,mode,p,q);
	  findrightdown(thebutton,i,j,mode,p,q);
	  findleftup(thebutton,i,j,mode,p,q);
	  findleftdown(thebutton,i,j,mode,p,q);
}
public void findright(HeiBaiButton[][] thebutton,int i,int j,int mode,int p,int q)
{
	  
		int right=j+1;
		//System.out.println("right");
		if(right>m) return;
		int state=thebutton[i][right].state();
		if(state!=p) return;
		while(state==p)
		{
			++right;
			if(right>m) return;
			state=thebutton[i][right].state();
		}
		if(state==mode) 
		{
			thebutton[i][right].addtochange(thebutton[i][j]);
			//System.out.println("button:"+i+","+j+"add to click changelist of:"+i+","+right);
			return;
		}
		if(state==HeiBaiButton.blank) 
		{
		thebutton[i][right].setState(mode);
		//System.out.println("button:"+i+","+right+"add to click");
		thebutton[i][right].addtochange(thebutton[i][j]);
    //System.out.println("button:"+i+","+j+"add to click changelist of:"+i+","+right);
		return;
		}
		if(state==q) return;
}
public void findleft(HeiBaiButton[][] thebutton,int i,int j,int mode,int p,int q)
{
		int left=j-1;
		//System.out.println("left");
			if(left<=0) return;
		int	state=thebutton[i][left].state();
			if(state!=p) return;
			while(state==p)
			{
				--left;
			  if(left<=0) return;
				state=thebutton[i][left].state();
			}
			if(state==mode) 
			{
				thebutton[i][left].addtochange(thebutton[i][j]);
				//System.out.println("button:"+i+","+j+"add to click changelist of:"+i+","+left);
				return;
			}
				if(state==HeiBaiButton.blank) 
				{
				thebutton[i][left].setState(mode);
				//System.out.println("button:"+i+","+left+"add to click");
				thebutton[i][left].addtochange(thebutton[i][j]);
				//System.out.println("button:"+i+","+j+"add to click changelist of:"+i+","+left);
				return;
			  }
				if(state==q) return;
}
public void findup(HeiBaiButton[][] thebutton,int i,int j,int mode,int p,int q)
{
       int up=i-1;
    //System.out.println("up");
			if(up<=0) return;
		int	state=thebutton[up][j].state();
			if(state!=p) return;
			while(state==p)

⌨️ 快捷键说明

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