Java小項目之:五子棋,你下棋下得過電腦嗎?

Java小項目之:五子棋,你下棋下得過電腦嗎?
Java五子棋功能要求:
1.建立窗口和設計一個棋盤界面
2.實現鼠標點擊,棋子出現,黑白棋輪流下
3.可以判斷五子相連輸贏
4.添加劇新開始,悔棋,退出按鈕功能
這個java五子棋玩法豐富,可是界面簡陋,十分適合java小白上手。界面是這樣子的:Java小項目之:五子棋,你下棋下得過電腦嗎?java

代碼:
package fivechess1;ide

import java.awt.Color;
import java.awt.Font;this

import java.awt.Graphics;設計

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;code

import javax.swing.JOptionPane;
import javax.swing.JPanel;blog

public class GamePanel extends JPanel {遊戲

/**
 * 
 */
private static final long serialVersionUID = 5372121135068238487L;
private final int w = 20, h = 20, s = 20;
private int X, Y;
private int[] x = {}, y = {};
private Color[] c = {};
private String[][] str = new String[w][h];
private String white = "w", black = "b";
private String display = "開始遊戲,黑棋先下";
private Controller controller;

public void setController(Controller controller) {
    this.controller = controller;
}

public void init() {
    display = "開始遊戲,黑棋先下";
    x = Arrays.copyOf(x, 0);
    y = Arrays.copyOf(y, 0);
    c = Arrays.copyOf(c, 0);
    getStr();
}

public int getW() {
    return w;
}

public int getH() {
    return h;
}

public int getS() {
    return s;
}

public GamePanel() {
    this.setSize(w * s, h * s + 50);
    getStr();
    repaint();
    this.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
            X = e.getX();
            Y = e.getY();
            startGame();

        }

        @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

        }
    });
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.RED);
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            g.fill3DRect(x * s, y * s, s, s, true);
        }
    }

    if (x.length > 0 && y.length > 0) {
        for (int i = 0; i < x.length; i++) {
            g.setColor(c[i]);
            g.fillOval(x[i] / s * s, y[i] / s * s, s, s);
        }

    }
    Font f = new Font("楷書", Font.PLAIN, 20);
    g.setColor(Color.BLUE);
    g.setFont(f);
    g.drawString(display, 10, h * s + 25);
}

public void startGame() {

    if (c.length % 2 == 0) {

        if (str[X / s][Y / s].equals("*")) {
            display = "黑方已下子,請白方下子";
            str[X / s][Y / s] = black;
            x = Arrays.copyOf(x, x.length + 1);
            x[x.length - 1] = X;
            y = Arrays.copyOf(y, y.length + 1);
            y[y.length - 1] = Y;
            c = Arrays.copyOf(c, c.length + 1);
            c[c.length - 1] = Color.BLACK;
            repaint();
            if (isSuccess(black)) {
                int options = JOptionPane.showConfirmDialog(GamePanel.this,
                        "黑方勝,是否新開一局");
                if (options == JOptionPane.YES_OPTION) {
                    controller.returnButton();
                } else if (options == JOptionPane.NO_OPTION) {
                    System.exit(0);
                }

            }
        }

        else if (str[X / s][Y / s].equals(white)) {
            display = "所下位置被白方所佔,請黑方從新下子";
            repaint();
        } else {
            display = "所下位置黑方已經佔了,請黑方從新下子";
            repaint();
        }

    } else {

        if (str[X / s][Y / s].equals("*")) {
            display = "白方已下子,請黑方下子";
            str[X / s][Y / s] = white;
            x = Arrays.copyOf(x, x.length + 1);
            x[x.length - 1] = X;
            y = Arrays.copyOf(y, y.length + 1);
            y[y.length - 1] = Y;
            c = Arrays.copyOf(c, c.length + 1);
            c[c.length - 1] = Color.WHITE;
            repaint();
            if (isSuccess(white)) {
                int option = JOptionPane.showConfirmDialog(GamePanel.this,
                        "白方勝,是否新開一局");
                if (option == JOptionPane.YES_OPTION) {
                    controller.returnButton();
                } else if (option == JOptionPane.NO_OPTION) {
                    System.exit(0);
                }
            }
        }

        else if (str[X / s][Y / s].equals(black)) {
            display = "所下位置被黑方所佔,請白方從新下子";
            repaint();
        } else {
            display = "所下位置白方已經佔了,請白方從新下子";
            repaint();
        }

    }

}

public void getStr() {
    for (int i = 0; i < str.length; i++) {
        for (int j = 0; j < str[i].length; j++) {
            str[i][j] = "*";
        }
    }
}

public boolean isSuccess(String player) {
    int left, right, up, down, lu, rd, ld, ru;
    left = getLeft(player);
    right = getRight(player);
    up = getUp(player);
    down = getDown(player);
    lu = getLU(player);
    rd = getRD(player);
    ld = getLD(player);
    ru = getRU(player);
    return left + right >= 4 || up + down >= 4 || lu + rd >= 4
            || ld + ru >= 4;
}

public int getLeft(String player) {
    int left = 0;
    int i = X / s, j = Y / s;
    while (true) {
        if (i == 0) {
            break;
        }
        i--;
        if (!str[i][j].equals(player)) {
            break;
        }
        left++;
    }
    return left;
}

public int getRight(String player) {
    int right = 0;
    int i = X / s, j = Y / s;
    while (true) {
        if (i == w - 1) {
            break;
        }
        i++;
        if (!str[i][j].equals(player)) {
            break;
        }
        right++;
    }
    return right;
}

public int getUp(String player) {
    int up = 0;
    int i = X / s, j = Y / s;
    while (true) {
        if (j == 0) {
            break;
        }
        j--;
        if (!str[i][j].equals(player)) {
            break;
        }
        up++;
    }
    return up;
}

public int getDown(String player) {
    int down = 0;
    int i = X / s, j = Y / s;
    while (true) {
        if (j == h - 1) {
            break;
        }
        j++;
        if (!str[i][j].equals(player)) {
            break;
        }
        down++;
    }
    return down;
}

public int getLU(String player) {
    int lu = 0;
    int i = X / s, j = Y / s;
    while (true) {
        if (i == 0 || j == 0) {
            break;
        }
        i--;
        j--;
        if (!str[i][j].equals(player)) {
            break;
        }
        lu++;
    }
    return lu;
}

public int getRD(String player) {
    int rd = 0;
    int i = X / s, j = Y / s;
    while (true) {
        if (i == w - 1 || j == h - 1) {
            break;
        }
        i++;
        j++;
        if (!str[i][j].equals(player)) {
            break;
        }
        rd++;
    }
    return rd;
}

public int getRU(String player) {
    int ru = 0;
    int i = X / s, j = Y / s;
    while (true) {
        if (j == 0 || i == w - 1) {
            break;
        }
        i++;
        j--;
        if (!str[i][j].equals(player)) {
            break;
        }
        ru++;
    }
    return ru;
}

public int getLD(String player) {
    int ld = 0;
    int i = X / s, j = Y / s;
    while (true) {
        if (i == 0 || j == h - 1) {
            break;
        }
        i--;
        j++;
        if (!str[i][j].equals(player)) {
            break;
        }
        ld++;
    }
    return ld;
}

}
完整源碼與素材能夠私信我領取!
喜歡這樣文章的能夠關注我,我會持續更新,大家的關注是我更新的動力!
祝關注個人人都:身體健康,財源廣進,福如東海,壽比南山,早生貴子,從不掉髮!get

相關文章
相關標籤/搜索