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

📄 othellopanel.java

📁 黑白棋123 黑白棋456 大学毕业设计 做很好
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
					for (i=x+2;(j<=8)&&(i<=8);i++)
					{
						if(chess[i][j]==side)	{return true;}
						else if (chess[i][j]==0) break;
						j++;
					}
				}
			}

			if (y>=2)
			{      
				if(chess[x][y-1]==-side)  
				{
					for (j=y-2;j>=1;j--)
					if(chess[x ][j]==side)	{return true;}
					else if (chess[x ][j]==0) break;
				}

				if(chess[x-1 ][y-1]==-side)  
				{
					j = y-2;
					for (i=x-2;(j>=1)&&(i>=1);i--)
					{
						if(chess[i ][j]==side)	{return true; }
						else if (chess[i ][j]==0) break;
						j--;
					}
				}
				if(chess[x+1 ][y-1]==-side)  
				{
					j = y-2;
					for (i=x+2;(j>=1)&&(i<=8);i++)
					{
						if(chess[i ][j]==side)	{return true; }
						else if (chess[i ][j]==0) break;
						j--;
					}
				}
			}

			if (x>=2)
			if(chess[x-1][y]==-side)
			{
				for (i=x-2;i>=1;i--)
				if(chess[i ][y]==side)	{return true; }
				else if (chess[i ][y]==0) break;
			}
			if (x<=6)
			if(chess[x+1][y]==-side)
			{
				for (i=x+2;i<=8;i++)
				if(chess[i ][y]==side)	{return true; }
				else if (chess[i][y]==0) break;
			}
		}
		return false;
	}

	//搜索的递归算法
	void Search( int chess[][] ,int l,int side)
	{  
		int i,j;
		// 查找最佳走法
		// _chess[][] 保存棋盘布局
		// l 当前查找深度
		int _chess[][],x,y;
	    int z;
		z=0;
		_chess = new int[10][10];
		if (_chess == null ) TRACE("!!! _chess[][] return null pointer!!");
	    
		for (i=1;i<=8;i++)
	    for (j=1;j<=8;j++) _chess[i][j]=chess[i][j];

	    if (l<=	SEARCH_DEEPNESS)
		{
			//查找每一个位置
			for (x=1;x<=8;x++)
			for (y=1;y<=8;y++)
				if (Judge(chess,x,y,side))
				{
					//可以放棋
					Put(chess,x,y,side);
					Search(chess,l+1,-side);
					if (m_exit) 
					{
						return; //放弃此分支
					}
				}
		} 
	    else   
		{
			//已到最深层
			for (i=1;i<=8;i++)
			for (j=1;j<=8;j++) z+=chess[i][j]*m_value[i][j];
			z*=m_side;
			if (z<=m_TrunkMin) 
			{ 
				m_exit=true; 

				return ;//放弃此支
			}
			else m_BranchMin=((z<=m_BranchMin)?z:m_BranchMin);//置分支最小值

		 }
		//恢复棋盘
		for (i=1;i<=8;i++)
		for (j=1;j<=8;j++) 
			chess[i][j]=_chess[i][j];
	} 

	void ComputerSide(int chess[][],int side)
	{
		//由电脑走棋
		Graphics g = getGraphics();
		
		TRACE("由电脑走棋");
		int  _chess[][];
		int  x_ai,y_ai,i,j,x,y,l;
		boolean Com_Passable;

		DrawSide(m_side,g);
		
		_chess = new int[10][10];
		Com_Passable = true;
		m_TrunkMin=-300;
		x_ai=0;y_ai=0;

		for(i=1;i<=8;i++)
		for(j=1;j<=8;j++)
			_chess[i][j]=chess[i][j];
		
		for (x=1;x<=8;x++)
		for (y=1;y<=8;y++) 
		{
			if (Judge(_chess,x,y,side))
			{ 
				l=1;
				m_BranchMin=300;
				Com_Passable = false;
				m_exit = false;
				Put(_chess,x,y,side);
				Search(_chess,l,-side);
				if (! m_exit )
				{
					m_TrunkMin=m_BranchMin;
					x_ai=x;
					y_ai=y;
				}

				for (i=1;i<=8;i++)
				for (j=1;j<=8;j++) 
					_chess[i][j]=chess[i][j];
			}
		}
		
		if (!Com_Passable)
		{
			//可以走棋
			if (!((x_ai<=8) && (x_ai>=1) && (y_ai<=8) && (y_ai>=1))) 
			{  
				TRACE(" ERRO in final put"); 
				return; 
			}
			if (!Judge(chess,x_ai,y_ai,side)) 
			{
				TRACE("ERRO in final judge before put!!"); 
				return ;
			}
				Put(chess,x_ai,y_ai,side);
				m_BackupX[m_step]=x_ai;
				m_BackupY[m_step]=y_ai;
				m_CurrentX=x_ai;
				m_CurrentY=y_ai;		    
				m_step++;

				DrawChessBoard(g);     	
		}	  
		else 
		{	 
			//无棋可走
			TRACE("无棋可走");
			m_BackupX[m_step]=PASS; 
			m_BackupY[m_step]=PASS;
			m_step++;
		//	DrawSide(-side);
		}
		TRACE("电脑走棋完毕"); 
		
		if(JudgeEnd()) 
		{
			return;
		}
		m_side = -m_side;
		m_MePut = true;
		DrawSide(m_side,g);
	}

	void OtherSide()
	{	
		Graphics g= this.getGraphics();
		if(JudgeEnd())
		{
			m_MePut = false;
			return;
		}
		
		if(m_State == WITH_NET)
		{
			m_MePut = false;
			String buf = new String();
			Integer bufi = new Integer(m_TempX);
			buf += bufi.toString();
			buf += '\\';
			bufi = new Integer(m_TempY);
			buf += bufi.toString();
			buf += '\\';
			
			m_socket.Send(21,m_Rival,buf);
			this.DrawSide( - m_side,g);
			m_side = - m_side;
		}
		else
		if(m_State == WITH_COM)
		{
			m_MePut = false;
			m_side = - m_side;
			ComputerSide(m_chess,m_side);
		}
		else
		if(m_State == WITH_LOC)
		{
			m_side=-m_side;
			DrawSide(m_side,g);
		}
	}

	public boolean JudgeEnd()
	{
		int i ,j;
		int countb = 0;
		int countw = 0;
		
		for(i=1;i<=8;i++)
		for(j=1;j<=8;j++)
			if ( m_chess[i][j] == NONE ) return false;
		
		for(i=1;i<=8;i++)
		for(j=1;j<=8;j++)
			if ( m_chess[i][j] == WHITE ) 
				countw++;
			else
				countb++;
		
		if (countw<countb)
			MessageBox.createMessageBox(" 黑方胜 !! ","胜利");
		else
		if (countb<countw)
			MessageBox.createMessageBox("白方胜 !! ","胜利");
		else
			MessageBox.createMessageBox("平局 !!","平局");
		return true;
	}
	
	
	/* **************
	
		绘图函数  
	
	*****************/
	// 画代表当前方的标志
	void DrawSide(int side , Graphics g)
	{	
		
		if(side == BLACK )
		{
			g.drawImage(m_swordGif,
						SWORD_B_OFF_X , SWORD_B_OFF_Y , SWORD_B_OFF_X+SWORD_WIDTH , SWORD_B_OFF_Y+SWORD_HEIGHT,
						0,0,SWORD_WIDTH , SWORD_HEIGHT,this);
			g.drawImage(m_backgdGif,
						SWORD_W_OFF_X , SWORD_W_OFF_Y , SWORD_W_OFF_X+SWORD_WIDTH , SWORD_W_OFF_Y+SWORD_HEIGHT,
						SWORD_W_OFF_X , SWORD_W_OFF_Y , SWORD_W_OFF_X+SWORD_WIDTH , SWORD_W_OFF_Y+SWORD_HEIGHT,
						this);
		}
		else
		if(side == WHITE )
		{
			g.drawImage(m_swordGif,
						SWORD_W_OFF_X , SWORD_W_OFF_Y , SWORD_W_OFF_X+SWORD_WIDTH , SWORD_W_OFF_Y+SWORD_HEIGHT,
						0,0,SWORD_WIDTH , SWORD_HEIGHT,this);
			g.drawImage(m_backgdGif,
						SWORD_B_OFF_X , SWORD_B_OFF_Y , SWORD_B_OFF_X+SWORD_WIDTH , SWORD_B_OFF_Y+SWORD_HEIGHT,
						SWORD_B_OFF_X , SWORD_B_OFF_Y , SWORD_B_OFF_X+SWORD_WIDTH , SWORD_B_OFF_Y+SWORD_HEIGHT,
						this);
		}
		else
		{
			g.drawImage(m_backgdGif,
						SWORD_B_OFF_X , SWORD_B_OFF_Y , SWORD_B_OFF_X+SWORD_WIDTH , SWORD_B_OFF_Y+SWORD_HEIGHT,
						SWORD_B_OFF_X , SWORD_B_OFF_Y , SWORD_B_OFF_X+SWORD_WIDTH , SWORD_B_OFF_Y+SWORD_HEIGHT,
						this);
			g.drawImage(m_backgdGif,
						SWORD_W_OFF_X , SWORD_W_OFF_Y , SWORD_W_OFF_X+SWORD_WIDTH , SWORD_W_OFF_Y+SWORD_HEIGHT,
						SWORD_W_OFF_X , SWORD_W_OFF_Y , SWORD_W_OFF_X+SWORD_WIDTH , SWORD_W_OFF_Y+SWORD_HEIGHT,
						this);
		}
	}

	//画一个棋子
	void DrawChess(int x,int y,int type , Graphics g)
	{
		int off;
		if (m_chessGif == null ) return;
		
		if ( type == NONE )
		{
			g.drawImage(m_backgdGif,
							CHESS_OFF_X+ x*CHESS_GRILLE_X ,CHESS_OFF_Y+y*CHESS_GRILLE_Y, CHESS_OFF_X+(x+1)*CHESS_GRILLE_X , CHESS_OFF_Y+(y+1)*CHESS_GRILLE_Y,
							(CHESS_OFF_X-BACK_OFF_X)+ x*CHESS_GRILLE_X ,(CHESS_OFF_Y-BACK_OFF_Y)+y*CHESS_GRILLE_Y, (CHESS_OFF_X-BACK_OFF_X)+(x+1)*CHESS_GRILLE_X , (CHESS_OFF_Y-BACK_OFF_Y)+(y+1)*CHESS_GRILLE_Y,
							this);
					return;
		}
		else
		switch(type )
		{
		case WHITE:
			off = 0 ;
			break;
		case BLACK:
			off =1;
			break;
		case TEMP:
			off =2;
			break;
		case NONE:
			off =3;
			break;
		default:
			return;
		}
				
		g.drawImage(m_backgdGif,
					CHESS_OFF_X+ x*CHESS_GRILLE_X ,CHESS_OFF_Y+y*CHESS_GRILLE_Y, CHESS_OFF_X+(x+1)*CHESS_GRILLE_X , CHESS_OFF_Y+(y+1)*CHESS_GRILLE_Y,
					(CHESS_OFF_X-BACK_OFF_X)+ x*CHESS_GRILLE_X ,(CHESS_OFF_Y-BACK_OFF_Y)+y*CHESS_GRILLE_Y, (CHESS_OFF_X-BACK_OFF_X)+(x+1)*CHESS_GRILLE_X , (CHESS_OFF_Y-BACK_OFF_Y)+(y+1)*CHESS_GRILLE_Y,
					this);

		g.drawImage(m_chessGif,
					CHESS_OFF_X+x*CHESS_GRILLE_X ,CHESS_OFF_Y+y*CHESS_GRILLE_Y, CHESS_OFF_X+x*CHESS_GRILLE_X+CHESS_WIDTH , CHESS_OFF_Y+y*CHESS_GRILLE_Y+CHESS_HEIGHT,
					off*CHESS_WIDTH, 0 , (off+1)*CHESS_WIDTH , CHESS_HEIGHT,
					this);
	}

	//画背景
	void DrawBack( Graphics g)
	{
		g = this.getGraphics();
		if(m_backgdGif == null) return;
		g.drawImage(m_backgdGif,
					BACK_OFF_X ,BACK_OFF_Y, BACK_OFF_X+BACK_WIDTH ,BACK_OFF_Y+BACK_HEIGHT,
					0,0,BACK_WIDTH,BACK_HEIGHT,this);
	}
	
	//重画所有棋子
	void DrawChessBoard(Graphics g)
	{
		g = this.getGraphics();
		if(m_backgdGif == null ) return;
		
		for( int i = 1; i<= 8 ; i++)
		for( int j = 1; j<= 8 ; j++)
		{
			if (m_chess[i][j] != NONE)
			DrawChess(i,j,m_chess[i][j],g);
		}
	}
	
	public void paint(Graphics g)
	{
		if (g == null ) return;
		this.DrawBack(g);
		this.DrawChessBoard(g);
		this.DrawSide(m_side,g);					
	}

	public void update(Graphics g)
	{
		paint(g);
	}
	
	public void addNotify()
	{
		super.addNotify();
		paint(getGraphics());
	}
	
	/* ********************
		
		消息响应函数
	
	***********************/
	class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			Object object = event.getSource();
			if (object == m_bNew)
				OnButtonNew();
			if (object == m_bPass)
				OnButtonPass();
			if (object == m_bBack)
				OnButtonBack();
			if (object == m_bNet)
				OnButtonNet();
			if (object == m_bLoc)
				OnButtonLoc();
		}
	}

	class MListener extends MouseAdapter
	{
		public void mouseClicked(MouseEvent e)
		{
			mouseDown(e.getX(),e.getY());	
		}
	}
	
	class MMListener extends MouseMotionAdapter
	{
		public void mouseMoved(MouseEvent e)
		{
			mouseMove(e.getX(),e.getY());		
		}
	}
	
	public void mouseMove(int px, int py)
	{
		//得到在棋盘中的坐标
		Graphics g= this.getGraphics();
		int x = (px-CHESS_OFF_X)/ CHESS_GRILLE_X;
		int y = (py-CHESS_OFF_Y)/ CHESS_GRILLE_Y;
		
		if((x<=8)&&(x>=1)&&(y<=8)&&(y>=1))//鼠标是否在棋盘内
		{
			if( !m_MePut )
			{	
				//对方放子
				if(!( (m_TempX<=8)&&(m_TempX>=1)&&(m_TempY<=8)&&(m_TempY>=1) ))
					setCursor( new Cursor(Cursor.WAIT_CURSOR) );
			}
			else
			if (!((m_TempX==x)&&(m_TempY==y)))	//是否还在上一次的格内
			{ 
				if (m_TempPut==true) //如果画了棕色棋子
				{
					//抹去	
				//	MessageBox("抹去");
					DrawChess(m_TempX,m_TempY,NONE,g);
					m_TempPut=false;
				}

				if (Judge(m_chess,x,y,m_side)) //是否能放子
				{
					DrawChess(x,y,TEMP,g); //画棕色棋子
					m_TempPut=true;
				}
			}
			
			m_TempX=x;
			m_TempY=y;

⌨️ 快捷键说明

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