java五子棋小遊戲實現代碼

前言

之前學完java基礎課程,試著簡單做瞭一下java的一個五子棋小遊戲,記錄下來。

界面

由於直接用的java庫中的一些基本控件寫的一個GUI,並沒有做過多優化,感覺比較醜
下面是界面展示:

黑子先行,但是我這邊簡化規則,並沒有考慮黑子先行的一些禁手。

下面直接貼代碼

接口類

我把五子棋界面的一些常量都定義在瞭這個接口類中,包括棋盤的起始坐標,棋盤線的間距和棋子半徑

public interface constant {

    int[][] chessLocation = new int[15][15];
    static final int x = 50;   //左上角位置
    static final int y = 50;
    static final int LN = 15;  //棋盤一些常量
    static final int R = 45;
}

實現類

接口

這個類中繼承瞭 constant、MouseListener、ActionListener三個接口

其中:

  • constant為自己定義
  • MouseListener為鼠標監聽
  • ActionListener為事件監聽

函數

show()繪制窗口基本框架
paint()繪制棋盤網格線和棋子
IsWin()判斷輸贏的基本邏輯
mouseClicked()獲取鼠標位置,判斷棋子落點等
actionPerformed()判斷鼠標點擊哪個按鈕(開始遊戲or認輸or悔棋)執行相應操作

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class game_logic extends JPanel implements constant, MouseListener, ActionListener {
    int chess_x = 0, chess_y = 0;
    int X = 0, Y = 0;
    boolean IsBlack = true; //判斷黑白
    boolean flag = false;  //是否已經開始遊戲
 //生成三個響應按鈕
    JFrame frame = new JFrame();
    JButton start = new JButton("開始遊戲");
    JButton regret = new JButton("悔棋");
    JButton Lost = new JButton("認輸");
 
    public void ShowUI() {
        frame.setSize(740, 800);
        frame.setTitle("五子棋");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//點擊關閉結束程序
        frame.setLocationRelativeTo(null);//窗口居中
        frame.setVisible(true);//窗體可視化
        frame.setResizable(false);//窗體大小不可調整
        frame.add(this);

        this.setBackground(Color.LIGHT_GRAY);//設置背景顏色
        this.addMouseListener(this);//窗體中添加鼠標監聽器

        start.setSize(50, 80);//設置按鈕大小
        start.addActionListener(this);//按鈕添加事件監聽器
        Lost.setSize(50, 80);
        Lost.addActionListener(this);
        regret.setSize(50, 80);
        regret.addActionListener(this);

        this.add(start);//添加按鈕到棋盤上
        this.add(Lost);
        this.add(regret);

    }

    /**
     * 繪制方法
     * 繪制五子棋棋盤
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        for (int i = 0; i < LN; i++) {       //畫棋盤
            g.drawLine(x, y + i * R, x + (LN - 1) * R, y + i * R);//行*15
            g.drawLine(x + i * R, y, x + i * R, y + (LN - 1) * R);//列*15
        }

        for (int i = 0; i < LN; i++) {       //畫棋子
            for (int j = 0; j < LN; j++) {

                if (chessLocation[i][j] == 1) {
                    g.setColor(Color.BLACK);//黑棋先行
                    g.fillOval(50 + i * R - 23, 50 + j * R - 23, R, R);
                }
                if (chessLocation[i][j] == 2) {
                    g.setColor(Color.WHITE);
                    g.fillOval(50 + i * R - 23, 50 + j * R - 23, R, R);
                }
                repaint();
            }
        }
    }

    /**
    *判斷輸贏
    *
    */
    public int IsWin() {
        int k = 0;
        for (int f = 2; f < 12; f++) {
            for (int g = 2; g < 12; g++) {
                if (chessLocation[f][g] == 1) {
                    if (chessLocation[f][g] == chessLocation[f - 1][g] && chessLocation[f - 1][g] == chessLocation[f - 2][g] && chessLocation[f - 2][g] == chessLocation[f + 1][g] && chessLocation[f + 1][g] == chessLocation[f + 2][g]) {
                        k = 1;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f][g - 1] && chessLocation[f][g - 1] == chessLocation[f][g - 2] && chessLocation[f][g - 2] == chessLocation[f][g + 1] && chessLocation[f][g + 1] == chessLocation[f][g + 2]) {
                        k = 1;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f - 1][g - 1] && chessLocation[f - 1][g - 1] == chessLocation[f - 2][g - 2] && chessLocation[f - 2][g - 2] == chessLocation[f + 1][g + 1] && chessLocation[f + 1][g + 1] == chessLocation[f + 2][g + 2]) {
                        k = 1;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f - 1][g + 1] && chessLocation[f - 1][g + 1] == chessLocation[f - 2][g + 2] && chessLocation[f - 2][g + 2] == chessLocation[f + 1][g - 1] && chessLocation[f + 1][g - 1] == chessLocation[f + 2][g - 2]) {
                        k = 1;
                        break;
                    }
                }
                if (chessLocation[f][g] == 2) {
                    if (chessLocation[f][g] == chessLocation[f - 1][g] && chessLocation[f - 1][g] == chessLocation[f - 2][g] && chessLocation[f - 2][g] == chessLocation[f + 1][g] && chessLocation[f + 1][g] == chessLocation[f + 2][g]) {
                        k = 2;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f][g - 1] && chessLocation[f][g - 1] == chessLocation[f][g - 2] && chessLocation[f][g - 2] == chessLocation[f][g + 1] && chessLocation[f][g + 1] == chessLocation[f][g + 2]) {
                        k = 2;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f - 1][g - 1] && chessLocation[f - 1][g - 1] == chessLocation[f - 2][g - 2] && chessLocation[f - 2][g - 2] == chessLocation[f + 1][g + 1] && chessLocation[f + 1][g + 1] == chessLocation[f + 2][g + 2]) {
                        k = 2;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f - 1][g + 1] && chessLocation[f - 1][g + 1] == chessLocation[f - 2][g + 2] && chessLocation[f - 2][g + 2] == chessLocation[f + 1][g - 1] && chessLocation[f + 1][g - 1] == chessLocation[f + 2][g - 2]) {
                        k = 2;
                        break;
                    }
                }
            }
        }
        return k;

    }

    @Override
    public void mouseClicked(MouseEvent e) {

        X = e.getX();
        Y = e.getY();                          //獲取鼠標位置
        if (flag == true) {
            if (X >= 25 && X <= 705 && Y >= 25 && Y <= 705) {   //比棋盤稍微大一點的落子判定范圍,即棋盤邊緣位置
                //應該安放的棋子的位置
                chess_x = (X - 20) / R;
                chess_y = (Y - 20) / R;

                if (chessLocation[chess_x][chess_y] == 0) {   //存儲棋子狀態,轉換棋子顏色
                    if (IsBlack == true) {
                        chessLocation[chess_x][chess_y] = 1;
                        IsBlack = false;
                    } else {
                        chessLocation[chess_x][chess_y] = 2;
                        IsBlack = true;
                    }

                    if (IsWin() == 1) {
                        JOptionPane.showMessageDialog(this, "黑棋獲勝");
                        flag = false;

                    }
                    if (IsWin() == 2) {
                        JOptionPane.showMessageDialog(this, "白棋獲勝");
                        flag = false;
                    }
                    repaint();
                }
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String buttonName = e.getActionCommand();

        if (buttonName.equals("開始遊戲") && flag == false) {//開始遊戲,棋盤清空
            flag = true;
            for (int i = 0; i < LN; i++) {
                for (int j = 0; j < LN; j++) {
                    chessLocation[i][j] = 0;
                }
            }
            IsBlack = true;
            repaint();
        }

        if (buttonName.equals("認輸") && flag == true) {
            flag = false;
            if (IsBlack) {
                JOptionPane.showMessageDialog(this, ",白棋認輸,黑棋獲勝");
            } else {
                JOptionPane.showMessageDialog(this, ",黑棋認輸,白棋獲勝");
            }
        }

        if (buttonName.equals("悔棋") && flag == true) {
            if (chessLocation[chess_x][chess_y] == 1) {
                JOptionPane.showMessageDialog(this, "黑方悔棋");
            }
            if (chessLocation[chess_x][chess_y] == 2) {
                JOptionPane.showMessageDialog(this, "白方悔棋");
            }
            chessLocation[chess_x][chess_y] = 0;
            IsBlack = !IsBlack;
            repaint();
        }
    }
}

其中比較有趣的是五子棋判贏方式,假設棋盤大小15*15,則我隻需要判斷正中間的13*13d的格子,向兩邊擴展,判斷是否五子連珠。

具體說明代碼裡都有註釋,不多贅述。

主函數類

public class Main_game {
    public static void main(String[] args) {
        game_logic start=new game_logic();
        start.ShowUI();
    }
}

總結

實現瞭五子棋小遊戲的基本功能,但是略感粗糙,細節不足。對於基本控件調用一學就會,做一個小的遊戲demo這是對流程控制和操作邏輯的訓練很有效的一種方式。之前看瞭別人的代碼覺得簡單,但是自己寫的時候往往邏輯流程難以連續,思維混亂,有些過程隻有自己寫瞭才知道其中的坑。

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: