else
UIManager.setLookAndFeel(
\ SwingUtilities.updateComponentTreeUI(this); }catch(Exception ee){} if(arg.equals(\ this.width=20; this.height=15;
cm=new ChessModel(1);
MapSize(this.width,this.height);
SwingUtilities.updateComponentTreeUI(this); }
if(arg.equals(\ this.width=30; this.height=20;
cm=new ChessModel(2);
MapSize(this.width,this.height);
SwingUtilities.updateComponentTreeUI(this); }
if(arg.equals(\ this.width=40; this.height=30;
cm=new ChessModel(3);
MapSize(this.width,this.height);
SwingUtilities.updateComponentTreeUI(this); }
if(arg.equals(\人机对弈\ this.checkcomputer=true; this.iscomputer=true;
cm=new ChessModel(cm.getModeChess()); MapSize(cm.getWidth(),cm.getHeight());
SwingUtilities.updateComponentTreeUI(this); }
if(arg.equals(\人人对弈\ this.checkcomputer=false; this.iscomputer=false;
cm=new ChessModel(cm.getModeChess()); MapSize(cm.getWidth(),cm.getHeight());
SwingUtilities.updateComponentTreeUI(this); }
if(arg.equals(\开局\ restart(); }
if(arg.equals(\关于\
第 8 页 共 21 页
JOptionPane.showMessageDialog(this, \五子棋游戏\关于\ if(arg.equals(\退出\ System.exit(0); } }
class ChessModel {
private int width,height,modeChess; private int x=0,y=0;
private int[][] arrMapShow; private boolean isOdd,isExist; public ChessModel() {}
public ChessModel(int modeChess){ this.isOdd=true; if(modeChess == 1){
PanelInit(20, 15, modeChess); }
if(modeChess == 2){
PanelInit(30, 20, modeChess); }
if(modeChess == 3){
PanelInit(40, 30, modeChess); } }
private void PanelInit(int width, int height, int modeChess){ //按照棋盘模式构建棋盘大小 this.width = width; this.height = height;
this.modeChess = modeChess;
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;
第 9 页 共 21 页
}
public int getWidth(){//获取棋盘宽度
return this.width; }
public int getHeight(){//获取棋盘高度 return this.height; }
public int getModeChess(){//获取棋盘模式 return this.modeChess; }
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(getisOdd()){ setisOdd(false);
this.arrMapShow[x][y]=1; }else{
setisOdd(true);
第 10 页 共 21 页
this.arrMapShow[x][y]=2; } }
public void computerDo(int width,int height){ //计算机走棋
int max_black,max_white,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_white=checkMax(i,j,2);//判断白子的最大值 max_black=checkMax(i,j,1);//判断黑子的最大值 max_temp=Math.max(max_white,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_white){ int num=0,max_num,max_temp=0; int x_temp=x,y_temp=y;
int x_temp1=x_temp,y_temp1=y_temp; // right
for(int i=1;i<5;i++){ x_temp1+=1;
if(x_temp1>this.width) break;
第 11 页 共 21 页
相关推荐: