基于java swing实现黑白翻转棋
时间: 2023-12-06 19:36:21 浏览: 34
以下是一个基于Java Swing实现黑白翻转棋的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Reversi extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton[][] board;
private int[][] status;
private int currentPlayer;
private JLabel statusLabel;
public Reversi() {
setTitle("黑白翻转棋");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setResizable(false);
JPanel boardPanel = new JPanel(); boardPanel.setLayout(new GridLayout(8, 8));
board = new JButton[8][8];
status = new int[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = new JButton();
board[i][j].setPreferredSize(new Dimension(40, 40));
board[i][j].addActionListener(this);
boardPanel.add(board[i][j]);
status[i][j] = 0;
}
}
board[3][3].setBackground(Color.WHITE);
board[3][4].setBackground(Color.BLACK);
board[4][3].setBackground(Color.BLACK);
board[4][4].setBackground(Color.WHITE);
status[3][3] = -1;
status[3][4] = 1;
status[4][3] = 1;
status[4][4] = -1;
JPanel statusPanel = new JPanel();
statusPanel.setLayout(new FlowLayout());
statusLabel = new JLabel("黑方先行");
statusPanel.add(statusLabel);
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(boardPanel, BorderLayout.CENTER);
container.add(statusPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
int row = -1, col = -1;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (button == board[i][j]) {
row = i;
col = j;
break;
}
}
}
if (row == -1 || col == -1) {
return;
}
if (status[row][col] != 0) {
return;
}
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) {
continue;
}
int r = row + i, c = col + j;
while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -currentPlayer) {
r += i;
c += j;
}
if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == currentPlayer) {
count += Math.max(Math.abs(row - r), Math.abs(col - c)) - 1;
}
}
}
if (count == 0) {
return;
}
status[row][col] = currentPlayer;
if (currentPlayer == 1) {
button.setBackground(Color.BLACK);
currentPlayer = -1;
statusLabel.setText("白方行动");
} else {
button.setBackground(Color.WHITE);
currentPlayer = 1;
statusLabel.setText("黑方行动");
}
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) {
continue;
}
int r = row + i, c = col + j;
while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -currentPlayer) {
r += i;
c += j;
}
if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == currentPlayer) {
int nr = row + i, nc = col + j;
while (nr != r || nc != c) {
status[nr][nc] = currentPlayer;
if (currentPlayer == 1) {
board[nr][nc].setBackground(Color.BLACK);
} else {
board[nr][nc].setBackground(Color.WHITE);
}
nr += i;
nc += j;
}
}
}
}
int blackCount = 0, whiteCount = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (status[i][j] == 1) {
blackCount++;
} else if (status[i][j] == -1) {
whiteCount++;
}
}
}
if (blackCount + whiteCount == 64) {
if (blackCount > whiteCount) {
JOptionPane.showMessageDialog(null, "黑方胜利!");
} else if (blackCount < whiteCount) {
JOptionPane.showMessageDialog(null, "白方胜利!");
} else {
JOptionPane.showMessageDialog(null, "平局!");
}
System.exit(0);
}
if (currentPlayer == 1) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (status[i][j] == 0) {
int cnt = 0;
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if (k == 0 && l == 0) {
continue;
}
int r = i + k, c = j + l;
while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -1) {
r += k;
c += l;
}
if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == 1) {
cnt += Math.max(Math.abs(i - r), Math.abs(j - c)) - 1;
}
}
}
if (cnt > 0) {
return;
}
}
}
}
JOptionPane.showMessageDialog(null, "白方无子可下,黑方继续行动");
currentPlayer = -1;
statusLabel.setText("黑方行动");
} else {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (status[i][j] == 0) {
int cnt = 0;
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if (k == 0 && l == 0) {
continue;
}
int r = i + k, c = j + l;
while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == 1) {
r += k;
c += l;
}
if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -1) {
cnt += Math.max(Math.abs(i - r), Math.abs(j - c)) - 1;
}
}
}
if (cnt > 0) {
return;
}
}
}
}
JOptionPane.showMessageDialog(null, "黑方无子可下,白方继续行动");
currentPlayer = 1;
statusLabel.setText("白方行动");
}
}
public static void main(String[] args) {
Reversi game = new Reversi();
game.setVisible(true);
game.currentPlayer = 1;
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)