java图形化界面五子棋
时间: 2023-12-03 16:40:49 浏览: 24
以下是Java图形化界面五子棋的实现方法:
1. 创建一个棋盘类,用于绘制棋盘和处理子事件。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChessBoard extends JPanel implements MouseListener {
private static final int ROWS = 15;
private static final int COLS = 15;
private static final int CELL_SIZE = 30;
private static final int MARGIN = 30;
private static final int GRID_SIZE = CELL_SIZE * (ROWS - 1) + MARGIN * 2;
private static final int PIECE_SIZE = (int) (CELL_SIZE * 0.8);
private static final int PIECE_OFFSET = (CELL_SIZE - PIECE_SIZE) / 2;
private static final int[][] POSITIONS = new int[ROWS][COLS];
private static final int EMPTY = 0;
private static final int BLACK = 1;
private static final int WHITE = 2;
private int currentPlayer = BLACK;
private boolean gameOver = false;
public ChessBoard() {
setPreferredSize(new Dimension(GRID_SIZE, GRID_SIZE));
setBackground(Color.ORANGE);
addMouseListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < ROWS; i++) {
g.drawLine(MARGIN, MARGIN + i * CELL_SIZE, MARGIN + (COLS - 1) * CELL_SIZE, MARGIN + i * CELL_SIZE);
g.drawLine(MARGIN + i * CELL_SIZE, MARGIN, MARGIN + i * CELL_SIZE, MARGIN + (ROWS - 1) * CELL_SIZE);
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (POSITIONS[i][j] == BLACK) {
g.setColor(Color.BLACK);
g.fillOval(MARGIN + j * CELL_SIZE + PIECE_OFFSET, MARGIN + i * CELL_SIZE + PIECE_OFFSET, PIECE_SIZE, PIECE_SIZE);
} else if (POSITIONS[i][j] == WHITE) {
g.setColor(Color.WHITE);
g.fillOval(MARGIN + j * CELL_SIZE + PIECE_OFFSET, MARGIN + i * CELL_SIZE + PIECE_OFFSET, PIECE_SIZE, PIECE_SIZE);
}
}
}
}
public void mouseClicked(MouseEvent e) {
if (gameOver) {
return;
}
int x = e.getX();
int y = e.getY();
if (x < MARGIN - PIECE_OFFSET || x > MARGIN + (COLS - 1) * CELL_SIZE + PIECE_OFFSET ||
y < MARGIN - PIECE_OFFSET || y > MARGIN + (ROWS - 1) * CELL_SIZE + PIECE_OFFSET) {
return;
}
int row = Math.round((float) (y - MARGIN) / CELL_SIZE);
int col = Math.round((float) (x - MARGIN) / CELL_SIZE);
if (POSITIONS[row][col] != EMPTY) {
return;
}
POSITIONS[row][col] = currentPlayer;
repaint();
if (checkWin(row, col)) {
gameOver = true;
JOptionPane.showMessageDialog(this, (currentPlayer == BLACK ? "Black" : "White") + " wins!");
} else if (checkDraw()) {
gameOver = true;
JOptionPane.showMessageDialog(this, "Draw!");
} else {
currentPlayer = (currentPlayer == BLACK ? WHITE : BLACK);
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
private boolean checkWin(int row, int col) {
int count = 1;
for (int i = row - 1; i >= 0 && POSITIONS[i][col] == currentPlayer; i--) {
count++;
}
for (int i = row + 1; i < ROWS && POSITIONS[i][col] == currentPlayer; i++) {
count++;
}
if (count >= 5) {
return true;
}
count = 1;
for (int j = col - 1; j >= 0 && POSITIONS[row][j] == currentPlayer; j--) {
count++;
}
for (int j = col + 1; j < COLS && POSITIONS[row][j] == currentPlayer; j++) {
count++;
}
if (count >= 5) {
return true;
}
count = 1;
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && POSITIONS[i][j] == currentPlayer; i--, j--) {
count++;
}
for (int i = row + 1, j = col + 1; i < ROWS && j < COLS && POSITIONS[i][j] == currentPlayer; i++, j++) {
count++;
}
if (count >= 5) {
return true;
}
count = 1;
for (int i = row - 1, j = col + 1; i >= 0 && j < COLS && POSITIONS[i][j] == currentPlayer; i--, j++) {
count++;
}
for (int i = row + 1, j = col - 1; i < ROWS && j >= 0 && POSITIONS[i][j] == currentPlayer; i++, j--) {
count++;
}
if (count >= 5) {
return true;
}
return false;
}
private boolean checkDraw() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (POSITIONS[i][j] == EMPTY) {
return false;
}
}
}
return true;
}
}
```
2. 创建一个主类,用于启动程序。
```java
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Five in a Row");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ChessBoard());
frame.pack();
frame.setVisible(true);
}
}
```