天天看點

五子棋Java簡單五子棋(源碼)

Java簡單五子棋(源碼)

Chess

package com.kiligzzz.gobang;

import java.awt.*;

/**
 * 五子棋-棋子類
 */
public class Chess {
    private int x;
    private int y;
    private Color color;

    public static final int DIAMETER=24;

    public Chess(int x, int y, Color color) {
        this.x = x;
        this.y = y;
        this.color = color;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}

           

DrawPanel

package com.kiligzzz.gobang;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;

/**
 * 五子棋-棋盤類
 */
public class DrawPanel extends JPanel implements MouseListener{
    //棋子坐标
    private int x;
    private int y;
    //是否是黑子
    private boolean isBlack = true;
    //棋子數組
    private Chess[] chessList = new Chess[15*15];
    //棋子個數
    private int chessCount = 0;
    //遊戲是否結束
    private boolean gameOver = false;

//    private boolean BlackVictory = false;
//
//    private boolean WhiteVictory = false;

    public DrawPanel() {
        this.setBackground(Color.LIGHT_GRAY);
        this.addMouseListener(this);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //畫棋盤
        for (int i = 1; i <16; i++) {
            g.drawLine(35,35*i,525,35*i);
        }
        for (int j = 1; j <16; j++) {
            g.drawLine(35*j,35,35*j,525);
        }
        g.fillOval(35+35*7-5,35+35*7-5,10,10);
        g.fillOval(35+35*2-5,35+35*2-5,10,10);
        g.fillOval(35+35*12-5,35+35*2-5,10,10);
        g.fillOval(35+35*2-5,35+35*12-5,10,10);
        g.fillOval(35+35*12-5,35+35*12-5,10,10);

        //畫棋子
        for (int i = 0; i < chessCount; i++) {
            int xPos = chessList[i].getX();
            int yPos = chessList[i].getY();
            g.setColor(chessList[i].getColor());
            g.fillOval(xPos-Chess.DIAMETER/2,yPos-Chess.DIAMETER/2,Chess.DIAMETER,Chess.DIAMETER);
            //最後落的一個棋子加上紅色框
            if (i==chessCount-1){
                g.setColor(Color.red);
                g.drawRect(xPos-Chess.DIAMETER/2,yPos-Chess.DIAMETER/2,Chess.DIAMETER,Chess.DIAMETER);
            }
        }
    }

    /**
     * 自動調用
     * 設定目前元件的大小是最佳的大小
     * @return
     */
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(35*2+35*14,35*2+35*14);
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        //将滑鼠點選的坐标位置轉換為網格索引
        x = (e.getX()-35+35/2)/35*35+35;
        y = (e.getY()-35+35/2)/35*35+35;

        //判斷棋子是否可以下到棋盤上
        //遊戲結束不能下棋子
        if (gameOver){
            return;
        }
        //棋盤外面不能下棋子
        if (x > 35+35*14 || y > 35+35*14){
            return;
        }
        //位置上已經有棋子不能下
        if (haveChess(x,y)){
            return;
        }

        //棋子對象
        Chess chess = new Chess(x,y,isBlack?Color.black:Color.white);
        chessList[chessCount++] = chess;
        this.repaint();

        //當某一方勝利時
        if (victory1()||victory2()||victory3()||victory4()){
//            if (chess.getColor() == Color.black){
//                BlackVictory = true;
//            }else {
//                WhiteVictory = true;
//            }

            String msg = String.format("恭喜%s棋勝利!",isBlack?"黑":"白");
            JOptionPane.showMessageDialog(this,msg);
            gameOver = true;
        }

//        //如果某一方勝利了
//        String msg;
//        if (BlackVictory){
//            msg="恭喜黑棋勝利!";
//            JOptionPane.showMessageDialog(this,msg);
//        }
//        if (WhiteVictory){
//            msg="恭喜白棋勝利!";
//            JOptionPane.showMessageDialog(this,msg);
//        }

        //改變畫筆顔色的判定值
        isBlack = !isBlack;
    }

    //位置上是否有棋子
    public boolean haveChess(int x,int y){
        for (Chess chess : chessList) {
            if (chess!=null&&chess.getX()==x&&chess.getY()==y){
                return true;
            }
        }
        return false;
    }

    //判斷是否勝利
    /**
     * 得到棋盤上的棋子
     * @param x 本次将要落下的棋子的橫坐标
     * @param y 本次将要落下的棋子的縱坐标
     * @param color 本次将要落下的棋子的顔色
     * @return  傳回這個棋子
     */
    public Chess getChess(int x,int y,Color color){
        for (Chess chess : chessList) {
            if (chess!=null&&chess.getX()==x&&chess.getY()==y&&chess.getColor()==color){
                return chess;
            }
        }
        return null;
    }
    //上下
    public boolean victory1(){
        //連續棋子的個數
        int continueCount = 1;
        //向上尋找
        for (int xi=x,yi = y-35; yi>=35; yi-=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        //向下尋找
        for (int xi=x,yi = y+35; yi<=35+35*14; yi+=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        return continueCount >= 5;
    }

    //左右
    public boolean victory2(){
        //連續棋子的個數
        int continueCount = 1;
        //向左尋找
        for (int xi=x-35,yi = y; xi>=35; xi-=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        //向右尋找
        for (int xi=x+35,yi = y; xi<=35+35*14; xi+=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        return continueCount >= 5;
    }

    //左上-右下
    public boolean victory3(){
        //連續棋子的個數
        int continueCount = 1;
        //向左上尋找
        for (int xi=x-35,yi = y-35; xi>=35&&yi>=35; xi-=35,yi-=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        //向右下尋找
        for (int xi=x+35,yi = y+35; xi<=35+35*14&&yi<=35+35*14; xi+=35,yi+=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        return continueCount >= 5;
    }

    //右上-左下
    public boolean victory4(){
        //連續棋子的個數
        int continueCount = 1;
        //向左下尋找
        for (int xi=x-35,yi = y+35; xi>=35&&yi<=35+35*14; xi-=35,yi+=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        //向右上尋找
        for (int xi=x+35,yi = y-35; xi<=35+35*14&&yi>=35; xi+=35,yi-=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        return continueCount >= 5;
    }

    /**
     * 重新開始遊戲
     */
    public void restartGame(){
        //清除棋子
        Arrays.fill(chessList, null);
//        for (int i = 0; i < chessList.length; i++) {
//            chessList[i] = null;
//        }

        //恢複遊戲相關的變量
        isBlack = true;
        gameOver = false;
        chessCount = 0;
        //重畫棋盤
        this.repaint();
    }

    /**
     *
     */
    public void regretChess(){
        //沒有棋子不能悔棋
        if (chessCount == 0){
        }
        chessList[chessCount-1]=null;
        chessCount--;
        if (chessCount > 0) {
            x=chessList[chessCount-1].getX();
            y=chessList[chessCount-1].getY();
        }

        isBlack=!isBlack;
        this.repaint();


    }


    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

           

JFiveFrame

package com.kiligzzz.gobang;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 視窗類
 */
public class JFiveFrame extends JFrame {
    /**
     * 工具欄裡面的三個按鈕
     */
    private JPanel toolBar;
    private JButton startButton,backButton,exitButton;
    /**
     * 棋盤的面闆
     */
    private DrawPanel drawPanel;

    private JMenuBar menuBar;
    private JMenu sysMenu;
    private JMenuItem startMenuItem,backMenuItem,exitMenuItem;

    private MyListener listener;

    public JFiveFrame() {
    }

    public void init(){
        listener = new MyListener();

        this.setTitle("單機版五子棋遊戲");

        toolBar = new JPanel();
        startButton = new JButton("開始");
        backButton = new JButton("悔棋");
        exitButton = new JButton("退出");

        drawPanel = new DrawPanel();

        menuBar = new JMenuBar();
        sysMenu = new JMenu("系統");
        JMenu help = new JMenu("幫助");
        JMenuItem ang = new JMenuItem("愛死我的小昂了!!!");

        startMenuItem = new JMenuItem("開始");
        backMenuItem = new JMenuItem("悔棋");
        exitMenuItem = new JMenuItem("退出");
        //給按鈕加監聽并變成小手
        listen(startMenuItem);
        listen(startButton);
        listen(backMenuItem);
        listen(backButton);
        listen(exitMenuItem);
        listen(exitButton);


        //設定視窗的菜單欄
        this.setJMenuBar(menuBar);
        menuBar.add(sysMenu);
        menuBar.add(help);
        help.add(ang);
        sysMenu.add(startMenuItem);
        sysMenu.add(backMenuItem);
        sysMenu.add(exitMenuItem);

        toolBar.add(startButton);
        toolBar.add(backButton);
        toolBar.add(exitButton);


        this.setLayout(new BorderLayout());
        this.add(toolBar,BorderLayout.NORTH);
        this.add(drawPanel,BorderLayout.CENTER);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);

        this.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/3,Toolkit.getDefaultToolkit().getScreenSize().height/3-200);

        //包裹元件
        pack();
        this.setVisible(true);
    }

    /**
     * 給按鈕加監聽并變成小手
     */
    public void listen(JButton jButton){
        jButton.addActionListener(listener);
        jButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }
    public void listen(JMenuItem jMenuItem){
        jMenuItem.addActionListener(listener);
        jMenuItem.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    /**
     * 監聽按鈕
     */
    public class MyListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            /**
             * 判斷事件源來源
             */
            if (e.getSource() == startButton || e.getSource() == startMenuItem){
                //調用DrawPanel的重新開始遊戲方法
                drawPanel.restartGame();
            }

            if (e.getSource() == backButton || e.getSource() == backMenuItem){
                //調用DrawPanel的悔棋方法
                drawPanel.regretChess();
            }

            if (e.getSource() == exitButton || e.getSource() == exitMenuItem){
                //正常退出
                System.exit(0);
            }
        }
    }
}

           

TestMain

package com.kiligzzz.gobang;

public class TestMain {
    public static void main(String[] args) {
        JFiveFrame jfiveFrame = new JFiveFrame();
        jfiveFrame.init();
    }
}

           

效果圖

五子棋Java簡單五子棋(源碼)

繼續閱讀