用java编写一个五子棋
时间: 2023-08-22 12:05:16 浏览: 54
好的,下面是一个简单的五子棋 Java 实现:
```java
import java.util.Scanner;
public class Gobang {
// 棋盘大小
private static final int BOARD_SIZE = 15;
// 棋盘
private String[][] board = new String[BOARD_SIZE][BOARD_SIZE];
// 当前落子的玩家
private String currentPlayer;
// 初始化棋盘
public void initBoard() {
// 将棋盘上的所有位置都初始化为空
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = "+";
}
}
}
// 打印棋盘
public void printBoard() {
// 打印列标号
System.out.print(" ");
for (int i = 1; i <= BOARD_SIZE; i++) {
System.out.print(i + " ");
}
System.out.println();
// 打印行标号和棋盘
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print((i+1) + " ");
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
// 判断棋局是否结束
public boolean isGameOver(int x, int y) {
// 横向是否有五个棋子
boolean horizonal = true;
for (int i = 0; i < 5; i++) {
if (y-i < 0 || y-i+4 >= BOARD_SIZE || !board[x][y-i].equals(board[x][y-i+1])) {
horizonal = false;
break;
}
}
if (horizonal) {
return true;
}
// 竖向是否有五个棋子
boolean vertical = true;
for (int i = 0; i < 5; i++) {
if (x-i < 0 || x-i+4 >= BOARD_SIZE || !board[x-i][y].equals(board[x-i+1][y])) {
vertical = false;
break;
}
}
if (vertical) {
return true;
}
// 左上到右下是否有五个棋子
boolean diagonal1 = true;
for (int i = 0; i < 5; i++) {
if (x-i < 0 || x-i+4 >= BOARD_SIZE || y-i < 0 || y-i+4 >= BOARD_SIZE || !board[x-i][y-i].equals(board[x-i+1][y-i+1])) {
diagonal1 = false;
break;
}
}
if (diagonal1) {
return true;
}
// 右上到左下是否有五个棋子
boolean diagonal2 = true;
for (int i = 0; i < 5; i++) {
if (x+i >= BOARD_SIZE || x+i-4 < 0 || y-i < 0 || y-i+4 >= BOARD_SIZE || !board[x+i][y-i].equals(board[x+i-1][y-i+1])) {
diagonal2 = false;
break;
}
}
if (diagonal2) {
return true;
}
return false;
}
// 人下棋
public void humanPlay() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入您要下棋的位置(例如:3,5):");
String inputStr = scanner.nextLine();
String[] posStrArr = inputStr.split(",");
int xPos = Integer.parseInt(posStrArr[0]) - 1;
int yPos = Integer.parseInt(posStrArr[1]) - 1;
if (board[xPos][yPos].equals("+")) {
board[xPos][yPos] = "●";
currentPlayer = "电脑";
} else {
System.out.println("该位置已经有棋子了,请重新选择位置!");
humanPlay();
}
}
// 电脑下棋
public void computerPlay() {
int xPos = 0, yPos = 0;
boolean played = false;
// 首先判断电脑是否能直接连成五个棋子
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j].equals("+")) {
board[i][j] = "○";
if (isGameOver(i, j)) {
xPos = i;
yPos = j;
played = true;
}
board[i][j] = "+";
if (played) {
break;
}
}
}
if (played) {
break;
}
}
// 如果不能直接连成五个棋子,则随机落子
if (!played) {
while (true) {
xPos = (int) (Math.random() * BOARD_SIZE);
yPos = (int) (Math.random() * BOARD_SIZE);
if (board[xPos][yPos].equals("+")) {
board[xPos][yPos] = "○";
break;
}
}
}
currentPlayer = "玩家";
}
// 开始游戏
public void start() {
initBoard();
currentPlayer = "玩家";
printBoard();
while (true) {
if (currentPlayer.equals("玩家")) {
humanPlay();
} else {
computerPlay();
}
printBoard();
if (isGameOver(Integer.parseInt(inputStrArr[0]) - 1, Integer.parseInt(inputStrArr[1]) - 1)) {
System.out.println(currentPlayer + "获胜!");
break;
}
}
}
public static void main(String[] args) {
Gobang game = new Gobang();
game.start();
}
}
```
这个实现比较简单,电脑只会随机落子或者判断能否连成五个棋子,没有使用更高级的算法。如果你想要实现更强的五子棋 AI,可以参考 AlphaGo 和 AlphaZero 的实现。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)