1. 程式人生 > >Java五子棋的實現(附原始碼)

Java五子棋的實現(附原始碼)

學習目的:熟悉java中swing類與java基礎知識的鞏固.(文末有原始碼檔案和打包的jar檔案)
效果圖:這裡寫圖片描述
思路:**1.首先構建一個Frame框架,來設定選單選項與按鈕點選事件。
MyFrame.java檔案程式碼如下

package StartGame;

import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import
javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JRadioButtonMenuItem; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import
java.awt.event.MouseListener; //窗體框架類 public class MyFrame extends JFrame { // static boolean canPlay1 = false;判斷是否可以開始遊戲 final MyPanel panel = new MyPanel(); public MyFrame() { // 設定窗體的大小並居中 this.setSize(500, 600); // 設定窗體大小 this.setTitle("五子棋遊戲"); // 設定窗體標題 int width = Toolkit.getDefaultToolkit().getScreenSize().width;// 獲取螢幕的寬度
int height = Toolkit.getDefaultToolkit().getScreenSize().height;// 獲取螢幕的高度 this.setLocation((width - 500) / 2, (height - 500) / 2); // 設定窗體的位置(居中) this.setResizable(false); // 設定窗體不可以放大 // this.setLocationRelativeTo(null);//這句話也可以設定窗體居中 /* * 選單欄的目錄設定 */ // 設定選單欄 JMenuBar bar = new JMenuBar(); this.setJMenuBar(bar); // 新增選單欄目錄 JMenu menu1 = new JMenu("遊戲選單"); // 例項化選單欄目錄 JMenu menu2 = new JMenu("設定"); JMenu menu3 = new JMenu("幫助"); bar.add(menu1); // 將目錄新增到選單欄 bar.add(menu2); bar.add(menu3); JMenu menu4 = new JMenu("博弈模式"); // 將“模式”選單新增到“設定”裡面 menu2.add(menu4); // JMenuItem item1=new JMenuItem("人人博弈"); // JMenuItem item2=new JMenuItem("人機博弈"); // 設定“”目錄下面的子目錄 JRadioButtonMenuItem item1 = new JRadioButtonMenuItem("人人博弈"); JRadioButtonMenuItem item2 = new JRadioButtonMenuItem("人機博弈"); // item1按鈕新增時間並且為匿名類 item1.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "儲存並重新開始遊戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否儲存設定並重新開始", "人機博弈設定", 0, 1, icon, options, "儲存並重新開始遊戲"); if (n == 0) { panel.setIsManAgainst(true); panel.Start(); item1.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 設定item2按鈕的事件監聽事件,也就是設定人機博弈 item2.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "儲存並重新開始遊戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否儲存設定並重新開始", "人機博弈設定", 0, 1, icon, options, "儲存並重新開始遊戲"); if (n == 0) { panel.setIsManAgainst(false); panel.Start(); item2.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 設定按鈕組並把人機博弈與人人博弈新增到一個按鈕組裡面 ButtonGroup bg = new ButtonGroup(); bg.add(item1); bg.add(item2); // 將按鈕組新增到選單裡面 menu4.add(item1); menu4.add(item2); item2.setSelected(true); // 先行設定 JMenu menu5 = new JMenu("先行設定"); // 將“先行設定”選單新增到“設定”裡面 menu2.add(menu5); // 設定黑子先行還是白字先行的按鈕 JRadioButtonMenuItem item3 = new JRadioButtonMenuItem("黑方先行"); JRadioButtonMenuItem item4 = new JRadioButtonMenuItem("白字先行"); // 設定item3的滑鼠點選事件,設定黑方先行 item3.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "儲存並重新開始遊戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否儲存設定並重新開始", "人機博弈設定", 0, 1, icon, options, "儲存並重新開始遊戲"); if (n == 0) { panel.setIsBlack(true); panel.Start(); item3.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 設定item4的滑鼠點選事件 item4.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "儲存並重新開始遊戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否儲存設定並重新開始", "人機博弈設定", 0, 1, icon, options, "儲存並重新開始遊戲"); if (n == 0) { panel.setIsBlack(false); panel.Start(); item4.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 設定按鈕組並把人機博弈與人人博弈新增到一個按鈕組裡面 ButtonGroup bg1 = new ButtonGroup(); bg1.add(item3); bg1.add(item4); // 將按鈕組新增到選單裡面 menu5.add(item3); menu5.add(item4); item3.setSelected(true); // 設定“幫助”下面的子目錄 JMenuItem menu6 = new JMenuItem("幫助"); menu3.add(menu6); /* * 選單欄的目錄設定完畢 */ // 開始遊戲選單設定 JMenuItem menu7 = new JMenuItem("開始遊戲"); menu1.add(menu7); JMenuItem menu8 = new JMenuItem("重新開始"); menu1.add(menu8); menu7.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub panel.Start(); // panel.repaint(); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } }); menu8.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "重新開始遊戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否重新開始", "訊息", 0, 1, icon, options, "儲存並重新開始遊戲"); if (n == 0) { panel.Start(); } // panel.repaint(); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } }); // 退出遊戲選項設定 JMenuItem menu9 = new JMenuItem("退出遊戲"); menu1.add(menu9); menu9.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "退出遊戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否退出遊戲", "訊息", 0, 1, icon, options, "儲存並重新開始遊戲"); if (n == 0) { System.exit(0);// 退出程式 } // panel.repaint(); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } }); // 遊戲難度設定 JRadioButtonMenuItem item5 = new JRadioButtonMenuItem("傻瓜");// 新增按鈕 JRadioButtonMenuItem item6 = new JRadioButtonMenuItem("簡單"); JRadioButtonMenuItem item7 = new JRadioButtonMenuItem("普通"); // JRadioButtonMenuItem item8= new JRadioButtonMenuItem("困難"); ButtonGroup bg3 = new ButtonGroup();// 設定按鈕組 bg3.add(item5); bg3.add(item6); bg3.add(item7); // bg3.add(item8); JMenu menu10 = new JMenu("遊戲難度設定");// 新增選單到主選單 menu2.add(menu10); menu10.add(item5);// 新增選項到難度設定選單 menu10.add(item6); menu10.add(item7); // menu2.add(item8); item5.setSelected(true);// 預設選項按鈕 // 傻瓜難度設定的滑鼠點選事件 item5.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "儲存並重新開始遊戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否儲存設定並重新開始", "人機博弈設定", 0, 1, icon, options, "儲存並重新開始遊戲"); if (n == 0) { panel.setGameDifficulty(0); panel.Start(); item5.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 簡單難度設定模式 item6.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "儲存並重新開始遊戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否儲存設定並重新開始", "難度設定", 0, 1, icon, options, "儲存並重新開始遊戲"); if (n == 0) { panel.setGameDifficulty(1); panel.Start(); item6.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 普通難度設定 item7.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "儲存並重新開始遊戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否儲存設定並重新開始", "人機博弈設定", 0, 1, icon, options, "儲存並重新開始遊戲"); if (n == 0) { panel.setGameDifficulty(2); panel.Start(); item7.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); //遊戲幫助提示資訊 menu6.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; JOptionPane.showMessageDialog(null, "製作人員:韓紅劍"); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); /* * 窗口裡面的容器設定 */ Container con = this.getContentPane(); // 例項化一個容器父類 con.add(panel); // 將容器新增到父類 /* * 窗口裡面的容器設定完畢 */ } }

2.第二步,設定顯示棋盤的容器,檔案原始碼為MyPanel.java

package StartGame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.*;
import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

//設定窗體裡面的容器操作
public class MyPanel extends JPanel implements MouseListener, Runnable {
    private static final Toolkit ResourceUtil = null;
    public Image boardImg; // 定義背景圖片
    static int[][] allChess = new int[15][15]; // 棋盤陣列
    static int[][] temporaryChess = new int[15][15];
    int x;// 儲存棋子的橫座標
    int y;// 儲存棋子的縱座標
    Boolean canPlay = false; // 遊戲是否繼續,預設為繼續
    Boolean isBlack = true;// 是否是黑子,預設為黑子
    Boolean isManAgainst = false; // 判斷是否是人人對戰
    String message = "使用者下棋";
    Thread t = new Thread(this);
    int maxTime = 120;
    int blackTime = 120;
    int whiteTime = 120;
    String blackMessage = "無限制";
    String whiteMessage = "無限制";
    static int gameDifficulty = 0;// 設定遊戲難度,0為傻瓜模式,1為簡單,2為普通,3為困難
    // 獲取isBlack的值
public boolean getIsBlack() {
        return this.isBlack;
    }

    // 設定isBlack的值
    public void setIsBlack(boolean isBlack) {
        this.isBlack = isBlack;
    }

    // 獲取isManAgainst的值
    public boolean getIsManAgainst() {
        return this.isManAgainst;
    }

    // 獲取isManAgainst的值
    public void setIsManAgainst(boolean isManAgainst) {
        this.isManAgainst = isManAgainst;
    }

    // 獲取isManAgainst的值
    public int getGameDifficulty() {
        return this.gameDifficulty;
    }

    // 設定setGameDifficulty的值
    public void setGameDifficulty(int gameDifficulty) {
        this.gameDifficulty = gameDifficulty;
    }

    // 建構函式
    public MyPanel() {

        boardImg = Toolkit.getDefaultToolkit().getImage("./src/StartGame/fiveCHessBourd.jpg");
        this.repaint();
        // 新增滑鼠監視器
        addMouseListener((MouseListener) this);
        // addMouseMotionListener((MouseMotionListener) this);
        // this.requestFocus();
        t.start();
        t.suspend();// 執行緒掛起

        // t.resume();

    }

    // 資料初始化

    @Override
    public void paint(Graphics g) {

        super.paint(g);
        int imgWidth = boardImg.getWidth(this); // 獲取圖片的寬度
        int imgHeight = boardImg.getHeight(this); // 獲取圖片的高度
        int FWidth = getWidth();
        int FHeight = getHeight();
        String message; // 標記誰下棋
        int x = (FWidth - imgWidth) / 2;
        int y = (FHeight - imgHeight) / 2;
        g.drawImage(boardImg, x, y, null); // 新增背景圖片到容器裡面
        g.setFont(new Font("宋體", 0, 14));
        g.drawString("黑方時間:" + blackTime, 30, 470);
        g.drawString("白方時間:" + whiteTime, 260, 470);
        // 繪製棋盤
        for (int i = 0; i < 15; i++) {
            g.drawLine(30, 30 + 30 * i, 450, 30 + 30 * i);
            g.drawLine(30 + 30 * i, 30, 30 + 30 * i, 450);
        }

        // 繪製五個中心點
        g.fillRect(240 - 5, 240 - 5, 10, 10); // 繪製最中心的正方形
        g.fillRect(360 - 5, 360 - 5, 10, 10); // 繪製右下的正方形
        g.fillRect(360 - 5, 120 - 5, 10, 10); // 繪製右上的正方形
        g.fillRect(120 - 5, 360 - 5, 10, 10);// 繪製左下的正方形
        g.fillRect(120 - 5, 120 - 5, 10, 10);// 繪製左上的正方形

        // 定義棋盤陣列

        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                // if (allChess[i][j] == 1) {
                // // 黑子
                // int tempX = i * 30 + 30;
                // int tempY = j * 30 + 30;
                // g.fillOval(tempX - 7, tempY - 7, 14, 14);
                // }
                // if (allChess[i][j] == 2) {
                // // 白子
                // int tempX = i * 30 + 30;
                // int tempY = j * 30 + 30;
                // g.setColor(Color.WHITE);
                // g.fillOval(tempX - 7, tempY - 7, 14, 14);
                // g.setColor(Color.BLACK);
                // g.drawOval(tempX - 7, tempY - 7, 14, 14);
                // }
                draw(g, i, j); // 呼叫下棋子函式
            }

        }
    }

    // 滑鼠點選時發生的函式
    @Override
    public void mousePressed(MouseEvent e) {
        // x = e.getX();// 獲取滑鼠點選座標的橫座標
        // y = e.getY();// 獲取滑鼠點選座標的縱座標
        // if (x >= 29 && x <= 451 && y >= 29 && y <= 451) { // 滑鼠點選在棋子框裡面才有效
        //
        // }

        if (canPlay == true) {// 判斷是否可以開始遊戲
            x = e.getX(); // 獲取滑鼠的焦點
            y = e.getY();
            if (isManAgainst == true) {// 判斷是否是人人對戰
                manToManChess();
            } else { // 否則是人機對戰,人機下棋
                manToMachine();
            }
        }
    }

    // 判斷是否輸贏的函式
    private boolean checkWin(int x, int y) {
        // TODO Auto-generated method stub

        boolean flag = false;
        // 儲存共有多少相同顏色棋子相連
        int count = 1;
        // 判斷橫向 特點:allChess[x][y]中y值相同
        int color = allChess[x][y];
        // 判斷橫向
        count = this.checkCount(x, y, 1,