井字棋(Tic-Tac-Toe)是一款经典的纸笔游戏,我们将使用Java Swing实现一个简单的井字棋GUI应用。我们将重点展示各类的核心逻辑。
项目概述
我们的井字棋游戏将包含以下几个重要部分:
- 游戏主类: 管理主窗口和用户事件。
- 游戏板类: 维护游戏状态和判定输赢。
- 玩家类: 表示玩家的基本信息。
1. 游戏主类
游戏主类 TicTacToeGameGUI
包含了创建和管理主窗口、响应用户点击事件的核心逻辑。
java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TicTacToeGameGUI extends JFrame {
private TicTacToeBoard board;
private Player player1;
private Player player2;
private Player currentPlayer;
private JButton[][] buttons;
public TicTacToeGameGUI() {
board = new TicTacToeBoard();
player1 = new Player('X');
player2 = new Player('O');
currentPlayer = player1;
buttons = new JButton[3][3];
setTitle("Tic-Tac-Toe");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 3));
initializeBoard();
}
private void initializeBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j] = new JButton("-");
buttons[i][j].setFont(new Font("Arial", Font.PLAIN, 60));
buttons[i][j].addActionListener(new ButtonClickListener(i, j));
add(buttons[i][j]);
}
}
}
private class ButtonClickListener implements ActionListener {
private int row;
private int col;
public ButtonClickListener(int row, int col) {
this.row = row;
this.col = col;
}
@Override
public void actionPerformed(ActionEvent e) {
if (board.placeMove(row, col, currentPlayer.getSymbol())) {
buttons[row][col].setText(String.valueOf(currentPlayer.getSymbol()));
buttons[row][col].setEnabled(false);
if (board.checkWin(currentPlayer.getSymbol())) {
JOptionPane.showMessageDialog(null, "Player " + currentPlayer.getSymbol() + " wins!");
resetGame();
} else if (board.isBoardFull()) {
JOptionPane.showMessageDialog(null, "The game is a draw!");
resetGame();
} else {
currentPlayer = (currentPlayer == player1) ? player2 : player1;
}
}
}
}
private void resetGame() {
board.resetBoard();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setText("-");
buttons[i][j].setEnabled(true);
}
}
currentPlayer = player1;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
TicTacToeGameGUI game = new TicTacToeGameGUI();
game.setVisible(true);
});
}
}
2. 游戏板类
TicTacToeBoard
类包含管理游戏状态和判定胜负的核心逻辑。
java
public class TicTacToeBoard {
private char[][] board;
private final int size;
public TicTacToeBoard() {
size = 3;
board = new char[size][size];
initializeBoard();
}
private void initializeBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = '-';
}
}
}
public boolean placeMove(int row, int col, char symbol) {
if (row >= 0 && row < size && col >= 0 && col < size && board[row][col] == '-') {
board[row][col] = symbol;
return true;
}
return false;
}
public boolean checkWin(char symbol) {
for (int i = 0; i < size; i++) {
if (board[i][0] == symbol && board[i][1] == symbol && board[i][2] == symbol) {
return true;
}
}
for (int j = 0; j < size; j++) {
if (board[0][j] == symbol && board[1][j] == symbol && board[2][j] == symbol) {
return true;
}
}
if (board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol) {
return true;
}
if (board[0][2] == symbol && board[1][1] == symbol && board[2][0] == symbol) {
return true;
}
return false;
}
public boolean isBoardFull() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
public void resetBoard() {
initializeBoard();
}
}
3. 玩家类
Player
类表示玩家及其标记符号。
java
public class Player {
private char symbol;
public Player(char symbol) {
this.symbol = symbol;
}
public char getSymbol() {
return symbol;
}
}
游戏截图:
通过上述代码,我们用Java Swing实现了一个简易的井字棋游戏。核心代码展示了如何创建GUI、处理用户输入以及管理游戏状态。这为想要学习GUI编程或构建类似游戏的开发者提供了一个良好的起点。