黄慧颖五子棋游戏 - 在线玩经典五子棋
import javax.swing.; import java.awt.; import java.awt.event.*;
class 黄慧颖五子棋 extends JFrame { private final int ROWS = 15; private final int COLS = 15; private final int CELL_SIZE = 40; private final int CANVAS_SIZE = CELL_SIZE * (ROWS + 1); private final int OFFSET = CELL_SIZE / 2; private final int WINNING_COUNT = 5; private final int EMPTY = 0; private final int BLACK = 1; private final int WHITE = 2;
private int[][] board;
private int currentPlayer;
private boolean gameOver;
public 黄慧颖五子棋() {
board = new int[ROWS][COLS];
currentPlayer = BLACK;
gameOver = false;
BoardCanvas canvas = new BoardCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_SIZE, CANVAS_SIZE));
canvas.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (gameOver) {
return;
}
int row = (e.getY() - OFFSET) / CELL_SIZE;
int col = (e.getX() - OFFSET) / CELL_SIZE;
if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
board[row][col] = currentPlayer;
if (hasWon(currentPlayer, row, col)) {
gameOver = true;
String message = (currentPlayer == BLACK) ? '黑棋胜利' : '白棋胜利';
JOptionPane.showMessageDialog(黄慧颖五子棋.this, message);
} else {
currentPlayer = (currentPlayer == BLACK) ? WHITE : BLACK;
}
repaint();
}
}
});
add(canvas);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setTitle('黄慧颖的五子棋游戏');
setVisible(true);
}
private boolean hasWon(int player, int row, int col) {
int count = 1;
// Check horizontally
for (int i = col - 1; i >= 0 && board[row][i] == player; i--) {
count++;
}
for (int i = col + 1; i < COLS && board[row][i] == player; i++) {
count++;
}
if (count >= WINNING_COUNT) {
return true;
}
// Check vertically
count = 1;
for (int i = row - 1; i >= 0 && board[i][col] == player; i--) {
count++;
}
for (int i = row + 1; i < ROWS && board[i][col] == player; i++) {
count++;
}
if (count >= WINNING_COUNT) {
return true;
}
// Check diagonally (top-left to bottom-right)
count = 1;
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == player; i--, j--) {
count++;
}
for (int i = row + 1, j = col + 1; i < ROWS && j < COLS && board[i][j] == player; i++, j++) {
count++;
}
if (count >= WINNING_COUNT) {
return true;
}
// Check diagonally (top-right to bottom-left)
count = 1;
for (int i = row - 1, j = col + 1; i >= 0 && j < COLS && board[i][j] == player; i--, j++) {
count++;
}
for (int i = row + 1, j = col - 1; i < ROWS && j >= 0 && board[i][j] == player; i++, j--) {
count++;
}
return count >= WINNING_COUNT;
}
private class BoardCanvas extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.LIGHT_GRAY);
// Draw grid lines
g.setColor(Color.BLACK);
for (int i = 0; i <= ROWS; i++) {
g.drawLine(OFFSET, OFFSET + i * CELL_SIZE, OFFSET + COLS * CELL_SIZE, OFFSET + i * CELL_SIZE);
g.drawLine(OFFSET + i * CELL_SIZE, OFFSET, OFFSET + i * CELL_SIZE, OFFSET + ROWS * CELL_SIZE);
}
// Draw stones
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == BLACK) {
g.setColor(Color.BLACK);
g.fillOval(OFFSET + j * CELL_SIZE - CELL_SIZE / 2, OFFSET + i * CELL_SIZE - CELL_SIZE / 2, CELL_SIZE, CELL_SIZE);
} else if (board[i][j] == WHITE) {
g.setColor(Color.WHITE);
g.fillOval(OFFSET + j * CELL_SIZE - CELL_SIZE / 2, OFFSET + i * CELL_SIZE - CELL_SIZE / 2, CELL_SIZE, CELL_SIZE);
}
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(黄慧颖五子棋::new);
}}
原文地址: https://www.cveoy.top/t/topic/fOyR 著作权归作者所有。请勿转载和采集!