?? chessmodel.java
字號:
package chess;/** * <p>Title:5chess </p> * <p>Description: </p> * <p>Copyright: seaboy.ping@263.net Copyright (c) 2002</p> * <p>Company: </p> * @author chenxiaoping * @version 1.0 */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; //this.isExist=false; 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; //arrMap = new int[width+1][height+1]; 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; //arrMap[i][j] = 0; } } } 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 flash(int x,int y){ if(badxy(x,y)) return; if(chessExist(x,y)) return; this.arrMapShow[x][y]=-1; }*/ 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(getisOdd()){ setisOdd(false); this.arrMapShow[x][y]=1; } else{ setisOdd(true); this.arrMapShow[x][y]=2; } } //計算機走棋 /* 說明:用窮舉法判斷每一個坐標點的四個方向的的最大棋子數,最后 得出棋子數最大值的坐標,下子 */ public void computerdo(int width,int height){ int max_black,max_red,max_temp,max=0; //int x=0,y=0; setisOdd(true); System.out.println("computer is doing ..."); 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=max_red>=max_black?max_red:max_black; 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; //judge right for(int i=1;i<5;i++){ x_temp1+=1; if(x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_red) num++; else break; } //judge left 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; //judge up 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; } //judge down 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; } if(num>max_temp&&num<5) max_temp=num; //judge left_up x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i<5;i++){ x_temp1-=1; y_temp1-=1; if(y_temp1<0 || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_red) num++; else break; } //judge right_down x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<5;i++){ x_temp1+=1; y_temp1+=1; if(y_temp1>this.height || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_red) num++; else break; } if(num>max_temp&&num<5) max_temp=num; //judge right_up x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i<5;i++){ x_temp1+=1; y_temp1-=1; if(y_temp1<0 || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_red) num++; else break; } //judge left_down x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<5;i++){ x_temp1-=1; y_temp1+=1; if(y_temp1>this.height || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==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_temp1=x_temp,y_temp1=y_temp; //judge right for(int i=1;i<6;i++){ x_temp1+=1; if(x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } //judge left x_temp1=x_temp; for(int i=1;i<6;i++){ x_temp1-=1; if(x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; //judge up x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ y_temp1-=1; if(y_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } //judge down y_temp1=y_temp; for(int i=1;i<6;i++){ y_temp1+=1; if(y_temp1>this.height) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; //judge left_up x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ x_temp1-=1; y_temp1-=1; if(y_temp1<0 || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } //judge right_down x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<6;i++){ x_temp1+=1; y_temp1+=1; if(y_temp1>this.height || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; //judge right_up x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ x_temp1+=1; y_temp1-=1; if(y_temp1<0 || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } //judge left_down x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<6;i++){ x_temp1-=1; y_temp1+=1; if(y_temp1>this.height || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; return false; } public void showSuccess(JPanel jp){ JOptionPane.showMessageDialog(jp,"Congratulation!You win the game!","win",JOptionPane.INFORMATION_MESSAGE); } public void showDefeat(JPanel jp){ JOptionPane.showMessageDialog(jp,"You lost,try again!","lost",JOptionPane.INFORMATION_MESSAGE); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -