编写以下代码,使用javaswing编写一款五子棋游戏,要求有规则说明
时间: 2024-03-08 07:50:57 浏览: 65
以下是使用Java Swing编写的五子棋游戏代码,包括规则说明:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gobang extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private ChessBoard chessBoard; // 棋盘
private JButton button1, button2; // 按钮
private JLabel label; // 标签
private boolean isBlack = true; // 是否是黑棋先手
private boolean isOver = false; // 游戏是否结束
public Gobang() {
super("五子棋游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 550);
setResizable(false);
chessBoard = new ChessBoard();
add(chessBoard, BorderLayout.CENTER);
JPanel panel = new JPanel(new FlowLayout());
button1 = new JButton("重新开始");
button1.addActionListener(this);
panel.add(button1);
button2 = new JButton("退出游戏");
button2.addActionListener(this);
panel.add(button2);
label = new JLabel("黑方先行");
panel.add(label);
add(panel, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new Gobang();
}
// 实现ActionListener接口的方法
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
chessBoard.restart();
isBlack = true;
isOver = false;
label.setText("黑方先行");
} else if (e.getSource() == button2) {
System.exit(0);
}
}
// 棋盘
private class ChessBoard extends JPanel implements MouseListener {
private static final long serialVersionUID = 1L;
private int[][] chess = new int[15][15]; // 棋盘数组
private int x, y; // 当前下棋子的坐标
public ChessBoard() {
setBackground(new Color(245, 255, 250));
addMouseListener(this);
}
// 重新开始游戏
public void restart() {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
chess[i][j] = 0;
}
}
repaint();
}
// 绘制棋盘
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 15; i++) {
g.drawLine(20, 20 + i * 30, 440, 20 + i * 30);
}
for (int i = 0; i < 15; i++) {
g.drawLine(20 + i * 30, 20, 20 + i * 30, 440);
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (chess[i][j] == 1) {
g.setColor(Color.black);
g.fillOval(7 + i * 30, 7 + j * 30, 26, 26);
} else if (chess[i][j] == 2) {
g.setColor(Color.white);
g.fillOval(7 + i * 30, 7 + j * 30, 26, 26);
}
}
}
}
// 下棋
public void mouseClicked(MouseEvent e) {
if (isOver) {
return;
}
x = e.getX();
y = e.getY();
if (x >= 20 && x <= 440 && y >= 20 && y <= 440) {
int i = (x - 20) / 30;
int j = (y - 20) / 30;
if (chess[i][j] == 0) {
chess[i][j] = isBlack ? 1 : 2;
repaint();
if (isWin(i, j)) {
JOptionPane.showMessageDialog(null, (isBlack ? "黑" : "白") + "方获胜!");
isOver = true;
return;
}
isBlack = !isBlack;
label.setText((isBlack ? "黑" : "白") + "方下棋");
}
}
}
// 判断是否获胜
public boolean isWin(int i, int j) {
int count = 1;
int color = chess[i][j];
// 横向
for (int k = i - 1; k >= 0; k--) {
if (chess[k][j] == color) {
count++;
} else {
break;
}
}
for (int k = i + 1; k < 15; k++) {
if (chess[k][j] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 纵向
count = 1;
for (int k = j - 1; k >= 0; k--) {
if (chess[i][k] == color) {
count++;
} else {
break;
}
}
for (int k = j + 1; k < 15; k++) {
if (chess[i][k] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 斜向
count = 1;
for (int k = i - 1, m = j - 1; k >= 0 && m >= 0; k--, m--) {
if (chess[k][m] == color) {
count++;
} else {
break;
}
}
for (int k = i + 1, m = j + 1; k < 15 && m < 15; k++, m++) {
if (chess[k][m] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 反斜向
count = 1;
for (int k = i + 1, m = j - 1; k < 15 && m >= 0; k++, m--) {
if (chess[k][m] == color) {
count++;
} else {
break;
}
}
for (int k = i - 1, m = j + 1; k >= 0 && m < 15; k--, m++) {
if (chess[k][m] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
}
```
规则说明:
1. 本游戏采用黑白对弈,黑方先行。
2. 棋盘为15x15的方格,黑子用黑色圆形表示,白子用白色圆形表示。
3. 玩家通过点击棋盘上的空白位置来下棋。
4. 每一方下完棋后,轮到对方下棋。
5. 当一方在横线、竖线、斜线上连续放置五个同色棋子时,即可获胜。
6. 若棋盘上已经没有空白位置,且没有一方达成获胜条件,游戏结束。
阅读全文