五子棋java

joe0010發表於2024-06-14

JAVA課程設計
利用所學習的JAVA知識設計一個五子棋小遊戲

1.團隊名稱、團隊成員介紹
王世俊 計科22-1
2.git地址
https://github.com/joeeojjoe/chess

  1. 專案git提交記錄截圖

    4.專案架構
    類ChessFrame主要功能是建立五子棋遊戲主窗體和選單
    類ChessModel實現了整個五子棋程式演算法的核心
    類MainPanel主要完成如下功能:
    1、構建一個皮膚,在該皮膚上畫上棋盤;
    2、處理在該棋盤上的滑鼠事件
    5.專案執行截圖
    介面以及執行效果

    結束

    選單以及功能展示

    走棋的記錄

6.專案關鍵程式碼
//計算機走棋
/*
用窮舉法判斷每一個座標點的四個方向的的最大棋子數,
最後得出棋子數最大值的座標,下子
*/
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;
}
//響應滑鼠的點選事件,根據滑鼠的點選來下棋,
@Override
public void mousePressed(MouseEvent evt){
int x = (evt.getX()-10) / 20;
int y = (evt.getY()-10) / 20;
System.out.println(x+" "+y);
if (evt.getModifiers()==MouseEvent.BUTTON1_MASK){
cm.play(x,y);
System.out.println(cm.getIsOdd()+" "+cm.getarrMapShow()[x][y]);
repaint();
if(cm.judgeSuccess(x,y,cm.getIsOdd())){
cm.showSuccess(this);
evt.consume();
ChessFrame.iscomputer=false;
}
//判斷是否為人機對弈
if(ChessFrame.iscomputer&&!cm.getisExist()){
cm.computerDo(cm.getWidth(),cm.getHeight());
repaint();
if(cm.judgeSuccess(cm.getX(),cm.getY(),cm.getIsOdd())){
cm.showDefeat(this);
evt.consume();
}
}
}
}
//構造五子棋遊戲的主窗體
public ChessFrame() {
this.setTitle("五子棋遊戲");
cm=new ChessModel(1);
mp=new MainPanel(cm);
Container con=this.getContentPane();
con.add(mp,"Center");
this.setResizable(false);
this.addWindowListener(new ChessWindowEvent());
MapSize(20,15);
JMenuBar mbar = new JMenuBar();
this.setJMenuBar(mbar);
JMenu gameMenu = new JMenu("遊戲");
mbar.add(makeMenu(gameMenu, new Object[] {
"開局", "棋盤","模式", null, "退出"
}, this));
JMenu lookMenu =new JMenu("檢視");
mbar.add(makeMenu(lookMenu,new Object[] {
"Metal","Motif","Windows"
},this));
JMenu helpMenu = new JMenu("幫助");
mbar.add(makeMenu(helpMenu, new Object[] {
"關於"
}, this));
}
7.尚待改進或者新的想法
1.計算機演算法效率較低,遇到複雜的居民計算機計算時間很長
2.無法實現電腦下黑棋,玩家下白棋。

相關文章