符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
这篇文章给大家分享的是有关Java实现五子棋游戏的方法。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。
创新互联专业为企业提供和龙网站建设、和龙做网站、和龙网站设计、和龙网站制作等企业网站建设、网页设计与制作、和龙企业网站模板建站服务,10多年和龙做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
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;
}
}
看完上述内容,你们掌握Java实现五子棋游戏的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!