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

📄 chessmodel.java

📁 我的课程设计的源代码
💻 JAVA
字号:
/**
 * @(#)ChessModel.java
 *
 *
 * @author 
 * @version 1.00 2008/4/23
 */

//package fivechess;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ChessModel{
	private int width,height,degree;
	private int x=0,y=0;
	private int[][] arrMap,arrMapShow;
	private boolean isOdd,isExist;
	
	public ChessModel(){
		
	}
	public ChessModel(int degree){
		this.isOdd=true;
		if(degree==1){
			PanelInit(20,15,degree);
		}
		if(degree==2){
			PanelInit(30,20,degree);
		}
		if(degree==3){
			PanelInit(40,30,degree);
		}
	}
	private void PanelInit(int width,int height,int degree){
		this.width=width;
		this.height=height;
		this.degree=degree;
		arrMapShow=new int[width+1][height+1];
		for(int i=0;i<=width;i++){
			for(int j=0;j<=height;j++){
			   arrMapShow[i][j]=-5;
		}
	  }
	}
	public boolean getisOdd(){
		return this.isOdd;
	}
	public void setisOdd(boolean isodd){
		if(isodd)
			this.isOdd=true;
		else
			this.isOdd=false;
	}
	public boolean getisExist(){
		return this.isExist;
	}
	public int getWidth(){
		return this.width;
	}
	public int getHeight(){
		return this.height;
	}
	public int getDegree(){
		return this.degree;
	}
	public int[][] getarrMapShow(){
		return arrMapShow;
	}
	private boolean badxy(int x,int y){
		if(x>=width+20||x<0)
			return true;
		return y>=height+20||y<0;
	}
	public boolean chessExist(int i,int j){
		if(this.arrMapShow[i][j]==1||this.arrMapShow[i][j]==2)
			return true;
			return false;
			
	}
	public void readyplay(int x,int y){
		if(badxy(x,y))
			return;
		if(chessExist(x,y))
			return;
	    this.arrMapShow[x][y]=3;
	}
	public void play(int x,int y){
		if(badxy(x,y))
			return;
		if(chessExist(x,y))
		{
			this.isExist=true;
			return;
		}
		else
			this.isExist=false;
		if(!ChessFrame.isnet){
			 if(getisOdd()){
			 	setisOdd(false);
			 	this.arrMapShow[x][y]=1;
			 }
			 else{
			 	setisOdd(true);
			 	this.arrMapShow[x][y]=2;
			 }
		     }
		     else{
		     	if(ChessFrame.istoken)
		     		this.arrMapShow[x][y]=1;
		        else 
		        	this.arrMapShow[x][y]=2;
		     }
      }
      public void computerdo(int width,int height){
      	int max_black,max_red,max_temp,max=0;
      	setisOdd(true);
      	System.out.println("计算机正在走棋>>>>>>>>");
      	for(int i=0;i<=width;i++){
      		for(int j=0;j<height;j++){
      			if(!chessExist(i,j)){
      				max_red=checkMax(i,j,2);
      				max_black=checkMax(i,j,1);
      				max_temp=Math.max(max_red,max_black);
      				if(max_temp>max){
      					max=max_temp;
      					this.x=i;
      					this.y=j;
      				}
      			}
      		}
      	}
        setX(this.x);
        setY(this.y);
        this.arrMapShow[this.x][this.y]=2;
      }
      public void setX(int x){
      	this.x=x;
      }
      public void setY(int y){
      	this.y=y;
      }
      public int getX(){
      	return this.x;
      }
      public int getY(){
      	return this.y;
      	
      }
      public int checkMax(int x,int y,int black_or_red){
      	int num=0,max_num,max_temp=0;
      	int x_temp=x,y_temp=y;
      	int x_templ=x_temp,y_templ=y_temp;
      	for(int i=1;i<5;i++){
      		x_templ+=1;
      		if(x_templ>this.width)
      			break;
      			if(this.arrMapShow[x_templ][y_templ]==black_or_red)
      				num++;
      		    else
      		    	break;
      	}
      	x_templ=x_temp;
      	for(int i=1;i<5;i++){
      		x_templ-=1;
      		if(x_templ<0)
      			break;
      		if(this.arrMapShow[x_templ][y_templ]==black_or_red)
      			num++;
      		else 
      			break;
      	}
      	if(num<5)
      		max_temp=num;
      	x_templ=x_temp;
      	y_templ=y_temp;
      	num=0;
      	for(int i=1;i<5;i++){
      		y_templ-=1;
      		if(y_templ<0)
      			break;
      	    if(this.arrMapShow[x_templ][y_templ]==black_or_red)
      	    	num++;
      	    else
      	    	break;
      	    	
      	}
      	y_templ=y_temp;
      	for(int i=1;i<5;i++){
      		y_templ+=1;
      		if(y_templ>this.height)
      			break;
      		if(this.arrMapShow[x_templ][y_templ]==black_or_red)
      			num++;
      		else
      			break;
      	}
        if(num>max_temp&&num<5)
            max_temp=num;
      
      x_templ=x_temp;
      y_templ=y_temp;
      num=0;
      for(int i=1;i<5;i++){
             x_templ-=1;
             y_templ-=1;
             if(y_templ<0||x_templ<0)
             	break;
             if(this.arrMapShow[x_templ][y_templ]==black_or_red)
             	num++;
             else
             	break;
             		
      }
      x_templ=x_temp;
      y_templ=y_temp;
      for(int i=1;i<5;i++){
      	x_templ+=1;
      	y_templ+=1;
      	if(y_templ>this.height||x_templ>this.width)
      		break;
        if(this.arrMapShow[x_templ][y_templ]==black_or_red)
        	num++;
        else 
        	break;
      }
      if(num>max_temp&&num<5)
      	max_temp=num;
      	x_templ=x_temp;
        y_templ=y_temp;
        num=0;
        for(int i=1;i<5;i++){
      	x_templ+=1;
      	y_templ+=1;
      	if(y_templ<0||x_templ>this.width)
      		break;
      	if(this.arrMapShow[x_templ][y_templ]==black_or_red)
      		num++;
      	else 
      		break;
        }
      	x_templ=x_temp;
      	y_templ=y_temp;
        for(int i=1;i<5;i++){
        	x_templ-=1;
        	y_templ+=1;
        	if(y_templ>this.height||x_templ<0)
        		break;
        	if(this.arrMapShow[x_templ][y_templ]==black_or_red)
        		num++;
        	else
        		break;
        	}
         if(num>max_temp&&num<5)
         	max_temp=num;
         max_num=max_temp;
         return max_num;     		
      }
      public boolean judgeSuccess(int x,int y,boolean isodd){
      	int num=1;
      	int arrvalue;
      	int x_temp=x,y_temp=y;
      	if(isodd)
      		arrvalue=2;
      	else
      		arrvalue=1;
      	int x_templ=x_temp,y_templ=y_temp;
      	for(int i=1;i<6;i++){
      		x_templ+=1;
      		if(x_templ>this.width)
      			break;
      		if(this.arrMapShow[x_templ][y_templ]==arrvalue)
      			num++;
      		else
      			break;
      	}
      	x_templ=x_temp;
      	for(int i=1;i<6;i++){
      		x_templ-=1;
      		if(x_templ<0)
      			break;
      	    if(this.arrMapShow[x_templ][y_templ]==arrvalue)
      	    	num++;
      	    else
      	    	break;   		
      	}
      	if(num==5)
      	   return true;
      	
      	x_templ=x_temp;
      	y_templ=y_temp;
      	num=1;
      	for(int i=1;i<6;i++){
      		y_templ-=1;
      		if(y_templ<0)
      			break;
      		if(this.arrMapShow[x_templ][y_templ]==arrvalue)
      			num++;
      	    else
      	    	break;
      	}   
        y_templ=y_temp;
        for(int i=1;i<6;i++){
        	y_templ+=1;
        	if(y_templ>this.height)
        		break;
        	if(this.arrMapShow[x_templ][y_templ]==arrvalue)
        		num++;
        	else
        		break;
        }
        if(num==5)
        	return true;
        x_templ=x_temp;
        y_templ=y_temp;
        for(int i=1;i<6;i++){
         x_templ-=1;
          y_templ-=1;
          if(y_templ<0||x_templ<0)
          	break;
          if(this.arrMapShow[x_templ][y_templ]==arrvalue)
          	num++;
          else
          	break;
          }	
          x_templ=x_temp;
          y_templ=y_temp;
          for(int i=1;i<6;i++){
          	x_templ+=1;
          	y_templ+=1;
          	if(y_templ>this.height||x_templ>this.width)
          		break;
          	if(this.arrMapShow[x_templ][y_templ]==arrvalue)
          		num++;
          	else
          		break;
          }	
          if(num==5)
          	return true;
          x_templ=x_temp;
          y_templ=y_temp;
          num=1;
          for(int i=1;i<6;i++){
          	x_templ+=1;
          	y_templ-=1;
            if(y_templ<0|x_templ>this.width)
            	break;
            if(this.arrMapShow[x_templ][y_templ]==arrvalue)
            	num++;
            else
            	break;
          }
          x_templ=x_temp;
          y_templ=y_temp;
          for(int i=1;i<6;i++){
          	x_templ-=1;
          	y_templ+=1;
          	if(y_templ>this.height||x_templ<0)
          		break;
          	if(this.arrMapShow[x_templ][y_templ]==arrvalue)
            	num++;
            else
            	break;
          }
          if(num==5)
          	return true;
       return false;
      }
      public void showSuccess(JPanel jp){
      	JOptionPane.showMessageDialog(jp,"恭喜!你赢了游戏!","胜利",JOptionPane.INFORMATION_MESSAGE);
      }
      public void showDefeet(JPanel jp){
      	JOptionPane.showMessageDialog(jp,"你输了游戏,请继续努力!","失败",JOptionPane.INFORMATION_MESSAGE);
      }
        	
}       
        	
        		
      		
      		
      	   
      		
      

⌨️ 快捷键说明

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