package com.glp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class FiveInARow extends JFrame implements ActionListener, MouseListener {
private static final long serialVersionUID = 1L;
private JButton[][] btns = new JButton[15][15];
private JPanel panel = new JPanel();
private JLabel msgLabel = new JLabel("Black goes first");
private boolean blackTurn = true;
private boolean isGameOver = false;
public FiveInARow() {
super("Five in a Row");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
panel.setLayout(new GridLayout(15, 15));
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
JButton btn = new JButton();
btn.setPreferredSize(new Dimension(30, 30));
btn.addMouseListener(this);
panel.add(btn);
btns[i][j] = btn;
}
}
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(msgLabel, BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
public void mouseClicked(MouseEvent e) {
JButton btn = (JButton) e.getSource();
if (!btn.getText().equals("") || isGameOver) {
return;
}
if (blackTurn) {
btn.setText("●");
msgLabel.setText("White's turn");
} else {
btn.setText("○");
msgLabel.setText("Black's turn");
}
if (isWin()) {
if (blackTurn) {
msgLabel.setText("Black wins!");
} else {
msgLabel.setText("White wins!");
}
isGameOver = true;
}
blackTurn = !blackTurn;
}
private boolean isWin() {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 11; j++) {
if (btns[i][j].getText().equals(btns[i][j + 1].getText()) && btns[i][j + 1].getText().equals(btns[i][j + 2].getText()) && btns[i][j + 2].getText().equals(btns[i][j + 3].getText()) && btns[i][j + 3].getText().equals(btns[i][j + 4].getText()) && !btns[i][j].getText().equals("")) {
return true;
}
}
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 15; j++) {
if (btns[i][j].getText().equals(btns[i + 1][j].getText()) && btns[i + 1][j].getText().equals(btns[i + 2][j].getText()) && btns[i + 2][j].getText().equals(btns[i + 3][j].getText()) && btns[i + 3][j].getText().equals(btns[i + 4][j].getText()) && !btns[i][j].getText().equals("")) {
return true;
}
}
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (btns[i][j].getText().equals(btns[i + 1][j + 1].getText()) && btns[i + 1][j + 1].getText().equals(btns[i + 2][j + 2].getText()) && btns[i + 2][j + 2].getText().equals(btns[i + 3][j + 3].getText()) && btns[i + 3][j + 3].getText().equals(btns[i + 4][j + 4].getText()) && !btns[i][j].getText().equals("")) {
return true;
}
}
}
for (int i = 0; i < 11; i++) {
for (int j = 4; j < 15; j++) {
if (btns[i][j].getText().equals(btns[i + 1][j - 1].getText()) && btns[i + 1][j - 1].getText().equals(btns[i + 2][j - 2].getText()) && btns[i + 2][j - 2].getText().equals(btns[i + 3][j - 3].getText()) && btns[i + 3][j - 3].getText().equals(btns[i + 4][j - 4].getText()) && !btns[i][j].getText().equals("")) {
return true;
}
}
}
return false;
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public static void main(String[] args) {
new FiveInARow();
}
}
This implementation uses a JFrame as the main GUI window, with a 15x15 grid of JButtons representing the game board. Mouse clicks on the buttons are handled with a MouseListener, and the game logic is implemented in the mouseClicked
method. The isWin
method checks for five in a row horizontally, vertically, and diagonally.
此实现使用JFrame作为主GUI窗口,其中15x15的JButton网格表示游戏板。鼠标点击按钮是用MouseListener处理的,游戏逻辑是用“mouseClicked”方法实现的。“isWin”方法在水平、垂直和对角方向上检查一行中的五个。