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

📄 robot.java

📁 java写的五子棋游戏
💻 JAVA
字号:
package like.wuziqi;

public class Robot 
{
	QiBan qb;
	
	int levelState = 1;
	int first = 0;
	//下落此子后的得分
	int [][] blackScore = new int[15][15];
	int [][] whiteScore = new int[15][15];
	
	int blackHighScore;
	int whiteHigeScore;
	
	int [] zu = new int[7];
	boolean blackWin,whiteWin,blackWin4,whiteWin4;
	
	public Robot(QiBan qb)
	{
		this.qb = qb;
	}
	
	public void start()
	{
		this.blackWin = false;
		this.whiteWin = false;
		this.first = 0;
	}
	public void setLevel(int level)
	{
		this.levelState = level;
	}
	public void next()
	{
		
		first++;
		System.out.println("ba"+this.qb.lastQiZi);
		
		if(this.qb.lastQiZi==-1)
		{
			System.out.println("c");
			qb.putDown(new ZuoBiao((int)(Math.random()*3)+6,(int)(Math.random()*3)+6),!qb.ifBlack);
		}
		else
			qb.putDown(this.getAttack(),!qb.ifBlack);
		qb.canPutBen = true;
	}
	
	public ZuoBiao getAttack()
	{
		ZuoBiao z1 =  this.getHighBlackScore();
		ZuoBiao z2 = this.getHighWhiteScore();
		if(z1==null||z2==null)
		{
			this.qb.setStr("棋盘满,游戏结束");
			this.qb.setGameOver(true);		
		}
		
		System.out.println("黑=="+this.blackHighScore+" 白=="+this.whiteHigeScore);
		
		
		if(qb.ifBlack)
		{
			if(this.whiteWin4)
			{
				System.out.println("必胜白点4");
				this.whiteWin4 = false;
				this.blackWin4 = false;
				this.whiteWin = false;
				this.blackWin = false;
				return z2;
			}
			else if(this.blackWin4)
			{
				System.out.println("必胜黑点4");
				this.whiteWin4 = false;
				this.blackWin4 = false;
				this.whiteWin = false;
				this.blackWin = false;
				return z1;
			}
		}
		else
		{
			if(this.blackWin4)
			{
				System.out.println("必胜黑点4");
				this.whiteWin4 = false;
				this.blackWin4 = false;
				return z1;
			}
			else if(this.whiteWin4)
			{
				System.out.println("必胜白点4");
				this.whiteWin4 = false;
				this.blackWin4 = false;
				return z2;
			}
		}
		
		if(qb.ifBlack)
		{
			if(this.whiteWin)
			{
				System.out.println("必胜白点");
				this.whiteWin = false;
				this.blackWin = false;
				return z2;
			}
			else if(this.blackWin)
			{
				System.out.println("必胜黑点");
				this.whiteWin = false;
				this.blackWin = false;
				return z1;
			}
		}
		else
		{
			if(this.blackWin)
			{
				System.out.println("必胜黑点");
				this.whiteWin = false;
				this.blackWin = false;
				return z1;
			}
			else if(this.whiteWin)
			{
				System.out.println("必胜白点");
				this.whiteWin = false;
				this.blackWin = false;
				return z2;
			}
		}
		
		if(qb.ifBlack)
		{
			if(this.blackHighScore>this.whiteHigeScore)
			{				
				System.out.println("防守");
				return z1;
			}
			else
			{			
				System.out.println("进攻");
				return z2;
			} 
				
		}
		else
		{
			if(this.blackHighScore<this.whiteHigeScore)
			{
				System.out.println("防守");
				return z2;
			}
			else 
			{
				System.out.println("进攻");
				return z1;	
			}	
		}			
	}
	
	public ZuoBiao getHighBlackScore()
	{
		int high = -1;
		ZuoBiao [] hz = new ZuoBiao[8];
		for(int i=0;i<=14;i++)
			for(int j=0;j<=14;j++)
			{
				if(high<this.blackScore[i][j])
				{
					if(qb.qiGes[i][j]==qb.none_state)
					{
						high = this.blackScore[i][j];
						hz[0] = new ZuoBiao(0,0); 
						hz[0].x = i;
						hz[0].y = j;
					}	
				}			
			}
		int curr=0;
		stop :
		{
			for(int i=0;i<=14;i++)
				for(int j=0;j<=14;j++)
				{
					if(this.blackScore[i][j]==high)
					{
						if(qb.qiGes[i][j]==qb.none_state)
						{
							if(++curr>7)
								break stop;
							hz[curr] = new ZuoBiao(0,0);
							hz[curr].x = i;
							hz[curr].y = j;
						}	
					}
					
				}
		}
		this.blackHighScore = high;
		return hz[(int)(Math.random()*curr)];
	}
	
	public ZuoBiao getHighWhiteScore()
	{
		int high = -1;
		ZuoBiao [] hz = new ZuoBiao[8];
		for(int i=0;i<=14;i++)
			for(int j=0;j<=14;j++)
			{
				if(high<this.whiteScore[i][j])
				{
					if(qb.qiGes[i][j] == qb.none_state)
					{
						high = this.whiteScore[i][j];
						hz[0] = new ZuoBiao(0,0);
						hz[0].x = i;
						hz[0].y = j;
					}	
				}
				//System.out.print(whiteScore[i][j]+" ");
			}
		
		int curr = 0;	
		stop :
		{
			for(int i=0;i<=14;i++)
				for(int j=0;j<=14;j++)
				{
					if(this.whiteScore[i][j]==high)
					{
						if(qb.qiGes[i][j]==qb.none_state)
						{
							if(++curr>7)
								break stop;
							hz[curr] = new ZuoBiao(0,0);
							hz[curr].x = i;
							hz[curr].y = j;
						}	
					}
					
				}
		}
		this.whiteHigeScore = high;
		return hz[(int)(Math.random()*curr)];
	}

	public  void updateScore()
	{//System.out.print("updateScore(),");
		this.whiteWin4 = false;
		this.blackWin4 = false;
		this.whiteWin = false;
		this.blackWin = false;
		int hei = 0;
		int bai = 0;
		for(int i=0;i<=14;i++)
			for(int j=0;j<=14;j++)
			{
				this.blackScore[i][j] = 0;
				this.whiteScore[i][j] = 0;
				
				//----------------纵向检查-------------------
				int m = i-4;
				if(m<0)
					m = 0;
				for(;m<=i&&m+4<=14;m++)
				{
					hei = bai = 0;
					for(int n=m;n<=m+4;n++)
					{
						zu[n-m+1] = qb.qiGes[n][j];
						if(qb.qiGes[n][j]==qb.hei_state)
							hei++;
						else if(qb.qiGes[n][j]==qb.bai_state)
							bai++;
					}
					zu[i-m+1] = 3;
					if(m-1<0)
						zu[0] = 2;
					else
						zu[0] = qb.qiGes[m-1][j];
					if(m+5>14)
						zu[6] = 2;
					else
						zu[6] = qb.qiGes[m+5][j];
					addScore(hei,bai,i,j);	
				}
				
				//-------------------横向检查-----------------
				m = j -4;
				if(m<0)
					m = 0;
				for(;m<=j&&m+4<=14;m++)
				{	
					hei = bai = 0;
					for(int n=m;n<=m+4;n++)
					{
						zu[n-m+1] = qb.qiGes[i][n];
						if(qb.qiGes[i][n]==qb.hei_state)
							hei++;
						else if(qb.qiGes[i][n]==qb.bai_state)
							bai++;
					}
					zu[j-m+1] = 3;

					if(m-1<0)
						zu[0] = 2;
					else
						zu[0] = qb.qiGes[i][m-1];
					if(m+5>14)
						zu[6] = 2;
					else
						zu[6] = qb.qiGes[i][m+5];
					addScore(hei,bai,i,j);			
				}
				
				//-----------------左上到右下-----------------
				m = 4;
				if(i-m<0)
					m = i;
				if(j-m<0)
					m = j;		
				for(;m>=0;m--)
				{
					if(i-m+4>14)
						break;
					if(j-m+4>14)
						break;
					hei = bai = 0;
					for(int n=m;n>=m-4;n--)
					{
						zu[m-n+1] = qb.qiGes[i-n][j-n];
						if(qb.qiGes[i-n][j-n]==qb.hei_state)
							hei++;
						else if(qb.qiGes[i-n][j-n]==qb.bai_state)
							bai++;
					}
					zu[m+1] = 3;

					if(i-m-1<0||j-m-1<0)
						zu[0] = 2;
					else
						zu[0] = qb.qiGes[i-m-1][j-m-1];
					if(i-m+5>14||j-m+5>14)
						zu[6] = 2;
					else
						zu[6] = qb.qiGes[i-m+5][j-m+5];
					addScore(hei,bai,i,j);	
				}
				
				//-------------------左下到右上-------------------
				m = 4;
				if(i+m>14)
					m = 14 - i;
				if(j-m<0)
					m = j;
				for(;m>=0;m--)
				{
					hei = bai = 0;
					if(i+m-4<0)
						break;
					if(j-m+4>14)
						break;
					
					for(int n=m;n>=m-4;n--)
					{
						zu[m-n+1] = qb.qiGes[i+n][j-n];
						if(qb.qiGes[i+n][j-n]==qb.hei_state)
							hei++;
						else if(qb.qiGes[i+n][j-n]==qb.bai_state)
							bai++;
					}
					zu[m+1] = 3;
					if(j-m-1<0||i+m+1>14)
						zu[0] = 2;
					else
						zu[0] = qb.qiGes[i+m+1][j-m-1];
					if(j-m+5>14||i+m-5<0)
						zu[6] = 2;
					else
						zu[6] = qb.qiGes[i+m-5][j-m+5];
					addScore(hei,bai,i,j);	
				}
			}
	}//method updateScore()
	
	private  void addScore(int hei,int bai,int i,int j)
	{
		int k = 0;
		if(this.levelState == 1)
			k = 0;
		else 
		{
			if(first<=3)
				;
			else //(first>3)
			{
				if(this.qb.ifBlack)
					k = 1;
				else 
					k = -1;
			}
		}
		if(bai==0)
		{
			for(int m=1;m<=5;m++)
			{
				if(zu[m]==3)
					zu[m] = -1;
			} 
			if(hei==0)
				this.blackScore[i][j] += 1;
			else if(hei==1)
				this.blackScore[i][j] += 3;
			else if(hei==2)
			{
				if(this.levelState==3)
				{
					int v = 0;
					if(zu[0]==2||zu[0]==1)
						v++;
					if(zu[6]==2||zu[6]==1)
						v++;
					if(zu[1]==-1&&zu[3]==-1&&zu[5]==-1)
					{	
						if(v==2)
							this.blackScore[i][j] += 3;
						else if(v==1)
							this.blackScore[i][j] += 9;
						else if(v==0)
							this.blackScore[i][j] += 27;
					}
					else
					{
						if(v==2)
							this.blackScore[i][j] += 4;
						else if(v==0)
							this.blackScore[i][j] += 54;
						else
						{
							if(zu[1]==0&&zu[5]==0)
								this.blackScore[i][j] += 54;
							else if(zu[1]==-1)
							{
								if(zu[0]==0||zu[0]==-1)
									this.blackScore[i][j] += 54;
								else 
									this.blackScore[i][j] += 9;
							}
							else if(zu[5]==-1)
							{
								if(zu[6]==0||zu[6]==-1)
									this.blackScore[i][j] += 54;
								else
									this.blackScore[i][j] += 9;
							}
						}
					}
				}
				else
				{
					this.blackScore[i][j] += 9;
				}
			}
				
			else if(hei==3)
			{
				int m = this.blackScore[i][j];
				if(true)
				{
					int v = 0;
					if(zu[0]==2||zu[0]==1)
						v++;
					if(zu[6]==2||zu[6]==1)
						v++;
					if(v==2)
					{
						this.blackScore[i][j] += 40;
					}
					else if(v==1)
					{
						if(zu[0]==2||zu[0]==1)
						{
							if(zu[1]==0)
							{
								this.blackWin = true;
								this.blackScore[i][j] += 1000;
							}
							else
							{
								this.blackScore[i][j] += 40;
							}							
						}
						else if(zu[6]==2||zu[6]==1)
						{
							if(zu[5]==0)
							{
								this.blackWin = true;
								this.blackScore[i][j] += 1000;
							}
							else
							{
								this.blackScore[i][j] += 40;
							}	
						}
					}
					else if(v==0)
					{
						if(zu[1]==0||zu[5]==0)
						{
							this.blackWin = true;
							this.blackScore[i][j] += 1000;
						}
						else 
						{
							this.blackScore[i][j] += 63;
						}
					}
				
				}
				if(this.levelState==3)
					;
				else
				{
					this.blackScore[i][j] = m;
					this.blackScore[i][j] += 27;
				}
			}
				
			else if(hei==4)
			{
				this.blackWin4 = true;
				this.blackScore[i][j] += 10000;
			}
			else if(hei==5)
			{	
				qb.gameOver = true;
				qb.str = "五连珠,黑胜~!";
			}	 			
		}
		if(hei==0)
		{
			for(int m=1;m<=5;m++)
			{
				if(zu[m]==-1)
					zu[m] = 1;
			} 
			if(bai==0)
				this.whiteScore[i][j] += 1;
			else if(bai==1)
				this.whiteScore[i][j] += 3+k;
			else if(bai==2)
			{
				if(this.levelState==3)
				{
					int v = 0;
					if(zu[0]==2||zu[0]==-1)
						v++;
					if(zu[6]==2||zu[6]==-1)
						v++;
					if(zu[1]==1&&zu[3]==1&&zu[5]==1)
					{	
						if(v==2)
							this.whiteScore[i][j] += 3+k*2;
						else if(v==1)
							this.whiteScore[i][j] += 9+k*2;
						else if(v==0)
							this.whiteScore[i][j] += 27+k*2;
					}
					else
					{
						if(v==2)
							this.whiteScore[i][j] += 4+k*2;
						else if(v==0)
							this.whiteScore[i][j] += 54+k*2;
						else
						{
							if(zu[1]==0&&zu[5]==0)
								this.whiteScore[i][j] += 54+k*2;
							else if(zu[1]==1)
							{
								if(zu[0]==0||zu[0]==1)
									this.whiteScore[i][j] += 54+k*2;
								else 
									this.whiteScore[i][j] += 9+k*2;
							}
							else if(zu[5]==1)
							{
								if(zu[6]==0||zu[6]==1)
									this.whiteScore[i][j] += 54+k*2;
								else
									this.whiteScore[i][j] += 9+k*2;
							}
						}
					}
				}
				else
				{
					this.whiteScore[i][j] += 9+k*2;
				}
			}	
			else if(bai==3)
			{
				int m = this.whiteScore[i][j];
				
				if(true)
				{
					int v = 0;
					if(zu[0]==2||zu[0]==-1)
						v++;
					if(zu[6]==2||zu[6]==-1)
						v++;
					if(v==2)
					{
						this.whiteScore[i][j] += 40+k*3;
					}
					else if(v==1)
					{
						if(zu[0]==2||zu[0]==-1)
						{
							if(zu[1]==0)
							{
								this.whiteWin = true;
								this.whiteScore[i][j] += 1000+k*3;
							}
							else
							{
								this.whiteScore[i][j] += 40+k*3;
							}							
						}
						else if(zu[6]==2||zu[6]==-1)
						{
							if(zu[5]==0)
							{
								this.whiteWin = true;
								this.whiteScore[i][j] += 1000+k*3;
							}
							else
							{
								this.whiteScore[i][j] += 40+k*3;
							}	
						}
					}
					else if(v==0)
					{
						if(zu[1]==0||zu[5]==0)
						{
							this.whiteWin = true;
							this.whiteScore[i][j] += 1000+k*3;
						}
						else 
						{
							this.whiteScore[i][j] += 63+k*3;
						}
					}
				
				}
				if(this.levelState==3)
					;
				else
				{
					this.whiteScore[i][j] = m;
					this.whiteScore[i][j] += 27+k*3;
				}
			}	
			else if(bai==4)
			{
				this.whiteWin4 = true;
				this.whiteScore[i][j] += 10000+k*4;
			}
				
			else if(bai==5)
			{
				qb.gameOver = true;
				qb.str = "五连珠,白胜~!";
			}
					 				
		}		
	}
	
}

⌨️ 快捷键说明

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