第一個文件:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/*
五子棋-主框架類, 程序啟動類
*/
public class StartChessJFrame extends JFrame {
private ChessBoard chessBoard;//對戰面板
private JPanel toolbar;//工具條面板
private JButton startButton, backButton, exitButton;
//重新開始按鈕,悔棋按鈕,和退出按鈕
private JMenuBar menuBar;//菜單欄
private JMenu sysMenu;//系統菜單
private JMenuItem startMenuItem, exitMenuItem, backMenuItem;
//重新開始,退出,和悔棋菜單項
public StartChessJFrame() {
setTitle("單機版五子棋");//設置標題
chessBoard = new ChessBoard();//初始化面板對象
// 創建和添加菜單
menuBar = new JMenuBar();//初始化菜單欄
sysMenu = new JMenu("系統");//初始化菜單
startMenuItem = new JMenuItem("重新開始");
exitMenuItem = new JMenuItem("退出");
backMenuItem = new JMenuItem("悔棋");//初始化菜單項
sysMenu.add(startMenuItem);//將三個菜單項添加到菜單上
sysMenu.add(backMenuItem);
sysMenu.add(exitMenuItem);
MyItemListener lis = new MyItemListener();//初始化按鈕事件監聽器內部類
this.startMenuItem.addActionListener(lis);//將三個菜單項註冊到事件監聽器上
backMenuItem.addActionListener(lis);
exitMenuItem.addActionListener(lis);
menuBar.add(sysMenu);//將系統菜單添加到菜單欄上
setJMenuBar(menuBar);// 將menuBar設置為菜單欄
toolbar = new JPanel();//工具面板欄實例化
startButton = new JButton("重新開始");//三個按鈕初始化
backButton = new JButton("悔棋");
exitButton = new JButton("退出");
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//將工具面板按鈕用FlowLayout佈局
toolbar.add(startButton);//將三個按鈕添加到工具面板上
toolbar.add(backButton);
toolbar.add(exitButton);
startButton.addActionListener(lis);//將三個按鈕註冊監聽事件
backButton.addActionListener(lis);
exitButton.addActionListener(lis);
add(toolbar, BorderLayout.SOUTH);//將工具面板佈局到界面"南"方也就是下面
add(chessBoard);//將面板對象添加到窗體上
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置界面關閉事件
//setSize(800,800);
pack(); // 自適應大小
}
private class MyItemListener implements ActionListener {//事件監聽器內部類
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource(); // 取得事件源
if (obj == StartChessJFrame.this.startMenuItem || obj == startButton) {
// 重新開始
// JFiveFrame.this內部類引用外部類
System.out.println("重新開始...");
chessBoard.restartGame();
} else if (obj == exitMenuItem || obj == exitButton) {
System.exit(0); // 結束應用程序
} else if (obj == backMenuItem || obj == backButton) { // 悔棋
System.out.println("悔棋...");
chessBoard.goback();
}
}
}
public static void main(String[] args) {
StartChessJFrame f = new StartChessJFrame(); // 創建主框架
f.setVisible(true); // 顯示主框架
}
}
第二個文件:
import java.awt.Color;
/*五子棋的棋子設計。
*/
public class Point {
private int x; // 棋盤中的x索引
private int y; // 棋盤中的y索引
private Color color;//顏色
public static final int DIAMETER = 30;//直徑
public Point(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public int getX() {// 拿到棋盤中的x索引
return x;
}
public int getY() {// 拿到棋盤中的Y索引
return y;
}
public Color getColor() {//得到顏色
return color;
}
}
第三個文件:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
/*
五子棋-棋盤類
*/
public class ChessBoard extends JPanel implements MouseListener {
public static final int MARGIN = 30; // 邊距
public static final int GRID_SPAN = 35; // 網格間距
public static final int ROWS = 15;//棋盤行數
public static final int COLS = 15;//棋盤列數
Point[] chessList = new Point[(ROWS + 1) * (COLS + 1)]; // 初始每個數組元素為null
boolean isBlack = true;//默然開始是黑棋先下
boolean gameOver = false;// 遊戲是否結束
int chessCount; // 當前棋盤的棋子個數
int xIndex, yIndex; // 當前剛下的棋子的索引
public ChessBoard() {
setBackground(Color.ORANGE);//設置背景顏色為橘黃色
addMouseListener(this);// 添加監聽器
addMouseMotionListener(new MouseMotionListener() { // 匿名內部類
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
int x1 = (e.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
// 將鼠標點擊的座標位置轉換成網格索引。
int y1 = (e.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
// 遊戲已經結束,不能下
// 落在棋盤外,不能下
// x,y位置已經有棋子存在,不能下
if (x1 < 0 || x1 > ROWS || y1 < 0 || y1 > COLS || gameOver
|| findChess(x1, y1))
setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); // 設置成默認形狀
else
setCursor(new Cursor(Cursor.HAND_CURSOR)); // 設置成手型
}
});
}
// 繪製
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 畫棋盤
for (int i = 0; i <= ROWS; i++) { // 畫橫線
g.drawLine(MARGIN, MARGIN + i * GRID_SPAN, MARGIN + COLS
* GRID_SPAN, MARGIN + i * GRID_SPAN);
}
for (int i = 0; i <= COLS; i++) {// 畫直線
g.drawLine(MARGIN + i * GRID_SPAN, MARGIN, MARGIN + i * GRID_SPAN,
MARGIN + ROWS * GRID_SPAN);
}
// 畫棋子
for (int i = 0; i < chessCount; i++) {
int xPos = chessList[i].getX() * GRID_SPAN + MARGIN; // 網格交叉點的x座標
int yPos = chessList[i].getY() * GRID_SPAN + MARGIN;// 網格交叉點的y座標
g.setColor(chessList[i].getColor()); // 設置顏色
g.fillOval(xPos - Point.DIAMETER / 2, yPos - Point.DIAMETER / 2,
Point.DIAMETER, Point.DIAMETER);
// 標記最後一個棋子的紅矩形框
if (i == chessCount - 1) { // 最後一個棋子
g.setColor(Color.red);
g.drawRect(xPos - Point.DIAMETER / 2,
yPos - Point.DIAMETER / 2, Point.DIAMETER,
Point.DIAMETER);
}
}
}
public void mousePressed(MouseEvent e) {// 鼠標按鍵在組件上按下時調用。
// 遊戲已經結束,不能下
if (gameOver)
return;
String colorName = isBlack ? "黑棋" : "白棋";
xIndex = (e.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN; // 將鼠標點擊的座標位置轉換成網格索引。
yIndex = (e.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
// 落在棋盤外,不能下
if (xIndex < 0 || xIndex > ROWS || yIndex < 0 || yIndex > COLS)
return;
// x,y位置已經有棋子存在,不能下
if (findChess(xIndex, yIndex))
return;
Point ch = new Point(xIndex, yIndex, isBlack ? Color.black: Color.white);
chessList[chessCount++] = ch;
repaint(); // 通知系統重新繪製
if (isWin()) {
// 給出勝利信息,不能再繼續下棋
String msg = String.format("恭喜,%s贏了!", colorName);
JOptionPane.showMessageDialog(this, msg);
gameOver = true;
}
isBlack = !isBlack;
}
// 覆蓋MouseListener的方法
public void mouseClicked(MouseEvent e) {
} // 鼠標按鍵在組件上單擊(按下並釋放)時調用。
public void mouseEntered(MouseEvent e) {
}// 鼠標進入到組件上時調用。
public void mouseExited(MouseEvent e) {
}// 鼠標離開組件時調用。
public void mouseReleased(MouseEvent e) {
} // 鼠標按鈕在組件上釋放時調用。
// 在棋子數組中查找是否有索引為x,y的棋子存在
private boolean findChess(int x, int y) {
for (Point c : chessList) {
if (c != null && c.getX() == x && c.getY() == y)
return true;
}
return false;
}
private boolean isWin() {//判斷那方贏
int continueCount = 1; // 連續棋子的個數
// 橫向向西尋找
for (int x = xIndex - 1; x >= 0; x--) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, yIndex, c) != null) {
continueCount++;
} else
break;
}
// 橫向向東尋找
for (int x = xIndex + 1; x <= ROWS; x++) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, yIndex, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else
continueCount = 1;
// 繼續另一種情況的搜索: 縱向
// 縱向向上尋找
for (int y = yIndex - 1; y >= 0; y--) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(xIndex, y, c) != null) {
continueCount++;
} else
break;
}
// 縱向向下尋找
for (int y = yIndex + 1; y <= ROWS; y++) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(xIndex, y, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else
continueCount = 1;
// 繼續另一種情況的搜索: 斜向
// 東北尋找
for (int x = xIndex + 1, y = yIndex - 1; y >= 0 && x <= COLS; x++, y--) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
// 西南尋找
for (int x = xIndex - 1, y = yIndex + 1; y <= ROWS && x >= 0; x--, y++) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else
continueCount = 1;
// 繼續另一種情況的搜索: 斜向
// 西北尋找
for (int x = xIndex - 1, y = yIndex - 1; y >= 0 && x >= 0; x--, y--) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
// 西南尋找
for (int x = xIndex + 1, y = yIndex + 1; y <= ROWS && x <= COLS; x++, y++) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else
continueCount = 1;
return false;
}
private Point getChess(int xIndex, int yIndex, Color color) {
for (Point c : chessList) {
if (c != null && c.getX() == xIndex && c.getY() == yIndex
&& c.getColor() == color)
return c;
}
return null;
}
public void restartGame() {
// 清除棋子
for (int i = 0; i < chessList.length; i++)
chessList[i] = null;
// 恢復遊戲相關的變量值
isBlack = true;
gameOver = false;// 遊戲是否結束
chessCount = 0; // 當前棋盤的棋子個數
repaint();
}
// 悔棋
public void goback() {
if (chessCount == 0)
return;
chessList[chessCount - 1] = null;
chessCount--;
if (chessCount > 0) {
xIndex = chessList[chessCount - 1].getX();
yIndex = chessList[chessCount - 1].getY();
}
isBlack = !isBlack;
repaint();
}
// Dimension: 矩形
public Dimension getPreferredSize() {
return new Dimension(MARGIN * 2 + GRID_SPAN * COLS, MARGIN * 2
+ GRID_SPAN * ROWS);
}
}