Java小项目之:五子棋,你下棋下得过电脑吗?

原创
01/09 16:46
阅读数 23

Java小项目之:五子棋,你下棋下得过电脑吗?

Java五子棋功能要求:

1.创建窗口和设计一个棋盘界面

2.实现鼠标点击,棋子出现,黑白棋轮流下

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 = "所下位置黑方已经占了,请黑方重新下子";

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;

}

}

完整源码与素材可以私信我领取!

喜欢这样文章的可以关注我,我会持续更新,你们的关注是我更新的动力!

祝关注我的人都:身体健康,财源广进,福如东海,寿比南山,早生贵子,从不掉发!

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部