您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Java實現(xiàn)五子棋游戲的方法。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
Java五子棋功能要求:
1.創(chuàng)建窗口和設(shè)計一個棋盤界面
2.實現(xiàn)鼠標點擊,棋子出現(xiàn),黑白棋輪流下
3.能夠判斷五子相連輸贏
4.添加重新開始,悔棋,退出按鈕功能
這個java五子棋玩法豐富,但是界面簡陋,十分適合java小白上手。界面是這樣子的:
代碼:
package fivechess1;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
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 = "所下位置黑方已經(jīng)占了,請黑方重新下子";
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 = "所下位置白方已經(jīng)占了,請白方重新下子";
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;
}
}
看完上述內(nèi)容,你們掌握Java實現(xiàn)五子棋游戲的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。