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

📄 chessmodel.java

📁 Java五子棋编程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


          																			            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;
          																			    			private ChessFrame cf;
          																			    			
          																			    			  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_temp1=x_temp,y_temp1=y_temp;
          																			    			  	 	 	 	for(int i=1;i<5;i++){
          																			    			  	 	 	 		x_temp1+=1;
          																			    			  	 	 	 		if(x_temp>this.width)
          																			    			  	 	 	 		 break;
          																			    			  	 	 	 		 if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)
          																			    			  	 	 	 		  num++;
          																			    			  	 	 	 		else
          																			    			  	 	 	 		  break;
          																			    			  	 	 	 }
          																			    			  	 	 	 x_temp1=x_temp;
          																			    			  	 	 	 for(int i=1;i<5;i++){
          																			    			  	 	 	 	x_temp1-=1;
          																			    			  	 	 	 	if(x_temp1<0)
          																			    			  	 	 	 	   break;
          																			    			  	 	 	 	if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)
          																			    			  	 	 	 	   num++;
          																			    			  	 	 	 	else
          																			    			  	 	 	 	  break;
          																			    			  	 	 	 	 }
          																			    			  	 	 	 	 if(num<5)
          																			    			  	 	 	 	   max_temp=num;
          																			    			  	 	 	 	   x_temp1=x_temp;
          																			    			  	 	 	 	   y_temp1=y_temp;
          																			    			  	 	 	 	   num=0;
          																			    			  	 	 	 	   for(int i=1;i<5;i++){
          																			    			  	 	 	 	   	y_temp1-=1;
          																			    			  	 	 	 	   	if(y_temp1<0)
          																			    			  	 	 	 	   	  break;
          																			    			  	 	 	 	   	if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)
          																			    			  	 	 	 	   	     num++;
          																			    			  	 	 	 	   	else
          																			    			  	 	 	 	   	     break;
          																			    			  	 	 	 	   	   }
          																			    			  	 	 	 	   	   y_temp1=y_temp;
          																			    			  	 	 	 	   	   for(int i=1;i<5;i++){
          																			    			  	 	 	 	   	   	y_temp1+=1;
          																			    			  	 	 	 	   	   if(y_temp1>this.height)
          																			    			  	 	 	 	   	      break;
          																			    			  	 	 	 	   	   if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)
          																			    			  	 	 	 	   	      num++;
          																			    			  	 	 	 	   	   else
          																			    			  	 	 	 	   	     break;

⌨️ 快捷键说明

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