用java做一个五子棋小程序,棋盘16乘以16,第一行为空格和1到15的数字,第一列为空格和A到O的大写字母,剩下15乘以15为棋盘,程序可以自动判断输赢,当一方获胜后,提示获胜方,并标识出获胜的五颗棋子,可以悔棋,黑棋用大写字母X表示,白棋用大写字母Y表示,并且有一个菜单,菜单有“新游戏”和“悔棋”两个选项
时间: 2023-06-20 11:07:55 浏览: 87
这是一个用java开发的五子棋小游戏,可供大家学习参考.zip
好的,我可以为你提供一些思路和代码实现。首先,我们需要设计一个棋盘的数据结构,可以使用一个二维数组来表示,数组的大小为16*16。棋子可以用一个枚举类型表示,黑棋为X,白棋为Y,空位为NONE。
```java
public enum ChessType {
X, Y, NONE
}
public class ChessBoard {
private ChessType[][] board;
private int lastX;
private int lastY;
//构造函数
public ChessBoard() {
board = new ChessType[16][16];
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
board[i][j] = ChessType.NONE;
}
}
}
//下棋方法
public boolean putChess(int x, int y, ChessType type) {
if (board[x][y] != ChessType.NONE) {
return false;
}
board[x][y] = type;
lastX = x;
lastY = y;
return true;
}
//悔棋方法
public boolean undo() {
if (lastX == -1 || lastY == -1) {
return false;
}
board[lastX][lastY] = ChessType.NONE;
lastX = -1;
lastY = -1;
return true;
}
//判断获胜方法
public boolean isWin(ChessType type) {
int count = 0;
//横向判断
for (int i = 0; i < 16; i++) {
count = 0;
for (int j = 0; j < 16; j++) {
if (board[i][j] == type) {
count++;
} else {
count = 0;
}
if (count == 5) {
return true;
}
}
}
//纵向判断
for (int i = 0; i < 16; i++) {
count = 0;
for (int j = 0; j < 16; j++) {
if (board[j][i] == type) {
count++;
} else {
count = 0;
}
if (count == 5) {
return true;
}
}
}
//左上到右下斜向判断
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
count = 0;
for (int k = 0; k < 5; k++) {
if (board[i+k][j+k] == type) {
count++;
} else {
count = 0;
}
if (count == 5) {
return true;
}
}
}
}
//右上到左下斜向判断
for (int i = 0; i < 11; i++) {
for (int j = 4; j < 16; j++) {
count = 0;
for (int k = 0; k < 5; k++) {
if (board[i+k][j-k] == type) {
count++;
} else {
count = 0;
}
if (count == 5) {
return true;
}
}
}
}
return false;
}
//打印棋盘方法
public void printBoard() {
System.out.print(" ");
for (int i = 0; i < 15; i++) {
System.out.print((char)('A'+i) + " ");
}
System.out.println();
for (int i = 0; i < 16; i++) {
if (i < 10) {
System.out.print(" " + i + " ");
} else {
System.out.print(i + " ");
}
for (int j = 0; j < 15; j++) {
if (board[i][j] == ChessType.X) {
System.out.print("X ");
} else if (board[i][j] == ChessType.Y) {
System.out.print("Y ");
} else {
System.out.print("+ ");
}
}
System.out.println();
}
}
}
```
然后我们需要实现一个用户界面,用于接收用户的输入和展示棋盘。用户界面可以使用Java Swing实现,具体可以参考以下代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChessFrame extends JFrame implements ActionListener {
private ChessBoard board;
private JButton[][] buttons;
private JButton newGameButton;
private JButton undoButton;
private JLabel messageLabel;
private boolean isBlackTurn;
public ChessFrame() {
this.board = new ChessBoard();
this.buttons = new JButton[15][15];
this.isBlackTurn = true;
initUI();
}
private void initUI() {
JPanel boardPanel = new JPanel(new GridLayout(15, 15));
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
JButton button = new JButton();
button.setPreferredSize(new Dimension(30, 30));
button.addActionListener(this);
button.setActionCommand(i + "," + j);
buttons[i][j] = button;
boardPanel.add(button);
}
}
JPanel controlPanel = new JPanel();
newGameButton = new JButton("New Game");
newGameButton.addActionListener(this);
undoButton = new JButton("Undo");
undoButton.addActionListener(this);
messageLabel = new JLabel("Black's turn");
controlPanel.add(newGameButton);
controlPanel.add(undoButton);
controlPanel.add(messageLabel);
add(boardPanel, BorderLayout.CENTER);
add(controlPanel, BorderLayout.SOUTH);
setTitle("Five in a Row");
setSize(600, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("New Game")) {
board = new ChessBoard();
isBlackTurn = true;
messageLabel.setText("Black's turn");
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
buttons[i][j].setText("");
}
}
return;
}
if (command.equals("Undo")) {
boolean result = board.undo();
if (result) {
isBlackTurn = !isBlackTurn;
messageLabel.setText(isBlackTurn ? "Black's turn" : "White's turn");
int lastX = board.getLastX();
int lastY = board.getLastY();
buttons[lastX][lastY].setText("");
}
return;
}
String[] parts = command.split(",");
int x = Integer.parseInt(parts[0]);
int y = Integer.parseInt(parts[1]);
ChessType type = isBlackTurn ? ChessType.X : ChessType.Y;
boolean result = board.putChess(x, y, type);
if (result) {
buttons[x][y].setText(isBlackTurn ? "X" : "Y");
if (board.isWin(type)) {
String message = isBlackTurn ? "Black wins" : "White wins";
messageLabel.setText(message);
JOptionPane.showMessageDialog(this, message);
board = new ChessBoard();
isBlackTurn = true;
messageLabel.setText("Black's turn");
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
buttons[i][j].setText("");
}
}
} else {
isBlackTurn = !isBlackTurn;
messageLabel.setText(isBlackTurn ? "Black's turn" : "White's turn");
}
}
}
public static void main(String[] args) {
ChessFrame frame = new ChessFrame();
frame.setVisible(true);
}
}
```
以上就是一个简单的五子棋小程序的实现。希望对你有帮助。
阅读全文