首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java Swing 開發五子棋遊戲:完整原始碼與邏輯解析

     編輯:Java 相關知識

專案架構

  • GomokuFrame:主視窗,繼承 JFrame,負責視窗初始化、居中顯示、載入棋盤面板 OperatorPane。
  • OperatorPane:核心邏輯面板,繼承 JPanel 並實現 MouseListener, Runnable,處理繪圖、落子邏輯、勝負判定、計時器、UI 交互(按鈕區域)。

棋盤繪製(雙緩衝)

public void paint(Graphics g) {
    BufferedImage buf = new BufferedImage(495, 475, BufferedImage.TYPE_INT_ARGB);
    Graphics g2 = buf.createGraphics();
    // 1. 繪製背景圖
    g2.drawImage(imageBackground, 0, 0, 495, 475, null);
    // 2. 繪製網格線(18×18,格距 20px)
    g2.setColor(Color.BLACK);
    for (int i = 0; i <= 18; i++) {
        g2.drawLine(10, 50 + 20*i, 10+360, 50+20*i); // 橫線
        g2.drawLine(10+20*i, 50, 10+20*i, 50+360);   // 直線
    }
    // 3. 繪製星位(五個黑點)與天元
    g2.fillOval(67, 107, 6, 6); // 等 5 個位置
    g2.fillOval(187, 227, 6, 6); // 天元
    // 4. 繪製所有棋子
    for (int i = 0; i <= 18; i++)
        for (int j = 0; j <= 18; j++)
            if (allchess[i][j] == 1) drawBlack(g2, i, j);
            else if (allchess[i][j] == 2) drawWhite(g2, i, j);
    // 5. 繪製狀態文字(回合、計時)
    g2.drawString("遊戲信息:" + message, 130, 40);
    g2.drawString("黑方時間:" + blackMessage, 25, 445);
    g2.drawString("白方時間:" + whiteMessage, 245, 445);
    // 6. 輸出到屏幕
    g.drawImage(buf, 0, 0, this);
}

落子與勝負判定

座標轉換

// 滑鼠像素座標 -> 棋盤邏輯座標 (0~18)
x = (e.getX() - 10) / 20;
y = (e.getY() - 50 - 20) / 20;

四向連線檢測

private boolean checkWin() {
    int color = allchess[x][y];
    // 四個方向:橫(1,0)、豎(0,1)、斜/(1,1)、斜\(1,-1)
    return checkCount(1,0,color) >= 5 ||
           checkCount(0,1,color) >= 5 ||
           checkCount(1,1,color) >= 5 ||
           checkCount(1,-1,color) >= 5;
}

private int checkCount(int dx, int dy, int color) {
    int cnt = 1;
    // 正向
    for (int i = 1; color == allchess[x + i*dx][y + i*dy]; i++) cnt++;
    // 反向
    for (int i = 1; color == allchess[x - i*dx][y - i*dy]; i++) cnt++;
    return cnt;
}

計時器線程

public void run() {
    if (maxTime > 0) {
        while (true) {
            if (isBlack) { blackTime--; if (blackTime == 0) timeout("黑方"); }
            else { whiteTime--; if (whiteTime == 0) timeout("白方"); }
            updateTimeStrings();
            repaint();
            Thread.sleep(1000);
        }
    }
}

UI 交互區域(右側按鈕)

在 mouseClicked 中通過座標範圍判斷點擊了哪個功能按鈕:

  • 新遊戲:重置棋盤、計時器、回合。
  • 設置時間:輸入分鐘數(0 為無限制),確認後重新開始。
  • 遊戲說明:顯示規則對話框。
  • 認輸:確認後直接判負。
  • 關於:作者信息。
  • 退出:System.exit(0)。

完整核心代碼(OperatorPane 節選)

public class OperatorPane extends JPanel implements MouseListener, Runnable {
    Image imageBackground;
    int boardrows=18, boardcols=18, boardsize=20;
    int x=0, y=0;
    int allchess[][] = new int[19][19];
    boolean isBlack = true, canPlay = true;
    String message = "黑方先行";
    int maxTime = 0, blackTime = 0, whiteTime = 0;
    String blackMessage = "無限制", whiteMessage = "無限制";
    Thread t = new Thread(this);

    @SuppressWarnings("deprecation")
    public OperatorPane() {
        t.start(); t.suspend();
        imageBackground = new ImageIcon("image/background.jpg").getImage();
    }

    // paint(), checkWin(), checkCount(), mouseClicked(), run() 等方法見上文
    // 其餘 MouseListener 方法留空
}

資源需求

專案目錄需包含 image/background.jpg 作為棋盤背景。若無圖片,可在 paint 中改用純色填充:g2.setColor(new Color(220,179,92)); g2.fillRect(0,0,495,475);。

擴展建議

  • 加入 AI 對戰(Minimax + Alpha-Beta 剪枝 + 启發式評估函數)。
  • 支援悔棋功能(棧保存歷史棋盤狀態)。
  • 網絡雙人對戰(Socket + 線程同步)。
  • 棋譜保存/回放(SGF 格式)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved