用java结合列表写一个五子棋游戏
时间: 2023-07-01 21:18:22 浏览: 47
好的,以下是一个简单的五子棋游戏的Java代码,使用了列表(List)来存储棋盘状态:
```
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FiveChess {
private List<List<Integer>> board; // 棋盘状态
private int currentPlayer; // 当前玩家,1表示黑子,2表示白子
private boolean gameOver; // 游戏是否结束
private int winner; // 获胜者,1表示黑子,2表示白子
public FiveChess() {
board = new ArrayList<>();
for (int i = 0; i < 15; i++) {
board.add(new ArrayList<>());
for (int j = 0; j < 15; j++) {
board.get(i).add(0);
}
}
currentPlayer = 1;
gameOver = false;
winner = 0;
}
// 打印棋盘状态
public void printBoard() {
System.out.print(" ");
for (int i = 0; i < 15; i++) {
System.out.printf("%2d ", i + 1);
}
System.out.println();
for (int i = 0; i < 15; i++) {
System.out.printf("%2d ", i + 1);
for (int j = 0; j < 15; j++) {
if (board.get(i).get(j) == 0) {
System.out.print(" . ");
} else if (board.get(i).get(j) == 1) {
System.out.print(" X ");
} else {
System.out.print(" O ");
}
}
System.out.println();
}
}
// 下一步棋
public void nextMove(int x, int y) {
if (gameOver) {
System.out.println("游戏已结束!");
return;
}
if (board.get(x).get(y) != 0) {
System.out.println("该位置已经有棋子了,请重新落子!");
return;
}
board.get(x).set(y, currentPlayer);
if (checkWin()) {
gameOver = true;
winner = currentPlayer;
System.out.printf("游戏结束,%s获胜!\n", currentPlayer == 1 ? "黑子" : "白子");
} else if (checkDraw()) {
gameOver = true;
System.out.println("游戏结束,平局!");
} else {
currentPlayer = 3 - currentPlayer;
}
}
// 检查是否有一方获胜
private boolean checkWin() {
int count = 0;
// 横向检查
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (board.get(i).get(j) == currentPlayer) {
count++;
if (count >= 5) {
return true;
}
} else {
count = 0;
}
}
count = 0;
}
// 纵向检查
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (board.get(j).get(i) == currentPlayer) {
count++;
if (count >= 5) {
return true;
}
} else {
count = 0;
}
}
count = 0;
}
// 斜向检查
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
for (int k = 0; k < 5; k++) {
if (board.get(i + k).get(j + k) == currentPlayer) {
count++;
if (count >= 5) {
return true;
}
} else {
count = 0;
break;
}
}
}
count = 0;
}
for (int i = 0; i < 11; i++) {
for (int j = 14; j >= 4; j--) {
for (int k = 0; k < 5; k++) {
if (board.get(i + k).get(j - k) == currentPlayer) {
count++;
if (count >= 5) {
return true;
}
} else {
count = 0;
break;
}
}
}
count = 0;
}
return false;
}
// 检查是否平局
private boolean checkDraw() {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (board.get(i).get(j) == 0) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
FiveChess game = new FiveChess();
Scanner scanner = new Scanner(System.in);
while (!game.gameOver) {
game.printBoard();
System.out.printf("请%s落子(x,y):", game.currentPlayer == 1 ? "黑子" : "白子");
int x = scanner.nextInt() - 1;
int y = scanner.nextInt() - 1;
game.nextMove(x, y);
}
scanner.close();
}
}
```
注意:以上代码仅为示例,可能还有一些未考虑到的细节问题。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)