eclipse五子棋小游戏代码
时间: 2023-07-02 09:18:30 浏览: 87
以下是一个简单的基于 Eclipse 的五子棋小游戏的代码,仅供参考:
```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 int[][] position = new int[15][15]; // 棋盘上的棋子 0-空 1-黑 2-白
private boolean isBlack = true; // 当前下棋的是黑子还是白子
private boolean canPlay = true; // 是否可以继续下棋
private JLabel label = new JLabel("黑方先行");
public static void main(String[] args) {
Gobang game = new Gobang();
game.initUI();
}
public void initUI() {
this.setTitle("五子棋");
this.setSize(450, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(false);
JPanel panel = new JPanel(new GridLayout(15, 15));
for (int i = 0; i < 225; i++) {
JButton button = new JButton();
button.setPreferredSize(new Dimension(30, 30));
button.setMargin(new Insets(0, 0, 0, 0));
button.setFocusPainted(false);
button.setName(i + "");
button.addMouseListener(this);
panel.add(button);
}
JPanel bottomPanel = new JPanel(new FlowLayout());
bottomPanel.add(label);
this.getContentPane().add(panel, BorderLayout.CENTER);
this.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// do nothing
}
public void mouseClicked(MouseEvent e) {
JButton button = (JButton) e.getSource();
int index = Integer.parseInt(button.getName());
int row = index / 15;
int col = index % 15;
if (position[row][col] == 0 && canPlay) {
if (isBlack) {
position[row][col] = 1;
button.setIcon(new ImageIcon("black.png"));
label.setText("轮到白方行动");
} else {
position[row][col] = 2;
button.setIcon(new ImageIcon("white.png"));
label.setText("轮到黑方行动");
}
if (isWin(row, col)) {
canPlay = false;
if (isBlack) {
label.setText("黑方胜利");
} else {
label.setText("白方胜利");
}
}
isBlack = !isBlack;
}
}
public void mouseEntered(MouseEvent e) {
// do nothing
}
public void mouseExited(MouseEvent e) {
// do nothing
}
public void mousePressed(MouseEvent e) {
// do nothing
}
public void mouseReleased(MouseEvent e) {
// do nothing
}
private boolean isWin(int row, int col) {
int color = position[row][col];
// 判断横向是否有五个相同颜色的棋子
int count = 1;
for (int i = col - 1; i >= 0; i--) {
if (position[row][i] == color) {
count++;
} else {
break;
}
}
for (int i = col + 1; i < 15; i++) {
if (position[row][i] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 判断纵向是否有五个相同颜色的棋子
count = 1;
for (int i = row - 1; i >= 0; i--) {
if (position[i][col] == color) {
count++;
} else {
break;
}
}
for (int i = row + 1; i < 15; i++) {
if (position[i][col] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 判断左上到右下斜线是否有五个相同颜色的棋子
count = 1;
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (position[i][j] == color) {
count++;
} else {
break;
}
}
for (int i = row + 1, j = col + 1; i < 15 && j < 15; i++, j++) {
if (position[i][j] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 判断右上到左下斜线是否有五个相同颜色的棋子
count = 1;
for (int i = row - 1, j = col + 1; i >= 0 && j < 15; i--, j++) {
if (position[i][j] == color) {
count++;
} else {
break;
}
}
for (int i = row + 1, j = col - 1; i < 15 && j >= 0; i++, j--) {
if (position[i][j] == color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
}
```
以上代码仅为一个简单的实现,可能存在一些问题和不足之处,需要根据实际情况进行修改和完善。