用java编写一个简单可游玩的五子棋界面
时间: 2024-02-02 15:03:43 浏览: 79
好的,以下是一个简单的五子棋界面的Java代码示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gobang extends JFrame implements ActionListener, MouseListener {
private static final long serialVersionUID = 1L;
private JButton button_begin = new JButton("开始游戏");
private JButton button_restart = new JButton("重新开始");
private JButton button_undo = new JButton("悔棋");
private JPanel panel_board = new JPanel();
private int board[][] = new int[15][15];
private int player = 1;
private int steps = 0;
private int x = -1, y = -1;
public Gobang() {
super("五子棋");
button_begin.addActionListener(this);
button_restart.addActionListener(this);
button_undo.addActionListener(this);
panel_board.addMouseListener(this);
panel_board.setPreferredSize(new Dimension(450, 450));
JPanel panel_button = new JPanel(new FlowLayout());
panel_button.add(button_begin);
panel_button.add(button_restart);
panel_button.add(button_undo);
JPanel panel_main = new JPanel(new BorderLayout());
panel_main.add(panel_button, BorderLayout.NORTH);
panel_main.add(panel_board, BorderLayout.CENTER);
setContentPane(panel_main);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_begin) {
player = 1;
steps = 0;
x = -1;
y = -1;
board = new int[15][15];
repaint();
} else if (e.getSource() == button_restart) {
player = 1;
steps = 0;
x = -1;
y = -1;
board = new int[15][15];
repaint();
} else if (e.getSource() == button_undo) {
if (steps > 0) {
board[x][y] = 0;
steps--;
player = 3 - player;
x = -1;
y = -1;
repaint();
}
}
}
public void mousePressed(MouseEvent e) {
if (player == 1) {
int i = e.getX() / 30;
int j = e.getY() / 30;
if (i >= 0 && i < 15 && j >= 0 && j < 15 && board[i][j] == 0) {
board[i][j] = 1;
steps++;
player = 2;
x = i;
y = j;
repaint();
check();
}
}
}
private void check() {
int count = 0;
for (int i = x - 4; i <= x + 4; i++) {
if (i >= 0 && i < 15 && board[i][y] == player) {
count++;
if (count == 5) {
JOptionPane.showMessageDialog(null, "玩家 " + player + " 获胜!");
button_begin.setEnabled(true);
button_restart.setEnabled(false);
button_undo.setEnabled(false);
break;
}
} else {
count = 0;
}
}
count = 0;
for (int j = y - 4; j <= y + 4; j++) {
if (j >= 0 && j < 15 && board[x][j] == player) {
count++;
if (count == 5) {
JOptionPane.showMessageDialog(null, "玩家 " + player + " 获胜!");
button_begin.setEnabled(true);
button_restart.setEnabled(false);
button_undo.setEnabled(false);
break;
}
} else {
count = 0;
}
}
count = 0;
for (int i = x - 4, j = y - 4; i <= x + 4; i++, j++) {
if (i >= 0 && i < 15 && j >= 0 && j < 15 && board[i][j] == player) {
count++;
if (count == 5) {
JOptionPane.showMessageDialog(null, "玩家 " + player + " 获胜!");
button_begin.setEnabled(true);
button_restart.setEnabled(false);
button_undo.setEnabled(false);
break;
}
} else {
count = 0;
}
}
count = 0;
for (int i = x - 4, j = y + 4; i <= x + 4; i++, j--) {
if (i >= 0 && i < 15 && j >= 0 && j < 15 && board[i][j] == player) {
count++;
if (count == 5) {
JOptionPane.showMessageDialog(null, "玩家 " + player + " 获胜!");
button_begin.setEnabled(true);
button_restart.setEnabled(false);
button_undo.setEnabled(false);
break;
}
} else {
count = 0;
}
}
if (steps == 225) {
JOptionPane.showMessageDialog(null, "和局!");
button_begin.setEnabled(true);
button_restart.setEnabled(false);
button_undo.setEnabled(false);
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < 15; i++) {
g.drawLine(15 + i * 30, 15, 15 + i * 30, 435);
g.drawLine(15, 15 + i * 30, 435, 15 + i * 30);
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (board[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillOval(10 + i * 30, 10 + j * 30, 20, 20);
} else if (board[i][j] == 2) {
g.setColor(Color.WHITE);
g.fillOval(10 + i * 30, 10 + j * 30, 20, 20);
}
}
}
}
public static void main(String[] args) {
new Gobang();
}
}
```
这段代码实现了一个简单的五子棋界面,包含开始游戏、重新开始、悔棋等功能。玩家可以用鼠标在棋盘上下棋,程序会自动判断胜负并提示。
阅读全文