用Java写一个点格棋游戏
时间: 2023-07-01 08:06:18 浏览: 49
好的,以下是一个简单的点格棋游戏的 Java 代码:
```java
import java.util.Scanner;
public class DotsAndBoxes {
private static int n;
private static int[][] horizontalEdges;
private static int[][] verticalEdges;
private static int[][] boxes;
private static int currentPlayer;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入棋盘大小:");
n = scanner.nextInt();
horizontalEdges = new int[n][n + 1];
verticalEdges = new int[n + 1][n];
boxes = new int[n][n];
currentPlayer = 1;
while (true) {
printBoard();
System.out.printf("请 %d 号玩家输入要连接的边(例如 1,2):", currentPlayer);
String input = scanner.next();
String[] coordinates = input.split(",");
int row = Integer.parseInt(coordinates[0]) - 1;
int col = Integer.parseInt(coordinates[1]) - 1;
if (row >= 0 && row < n && col >= 0 && col < n) {
if (coordinates.length == 2) {
if (col < n - 1 && horizontalEdges[row][col] == 0) {
horizontalEdges[row][col] = currentPlayer;
if (checkForBox(row, col, true)) {
boxes[row][col] = currentPlayer;
} else {
currentPlayer = (currentPlayer == 1) ? 2 : 1;
}
} else if (row < n - 1 && verticalEdges[row][col] == 0) {
verticalEdges[row][col] = currentPlayer;
if (checkForBox(row, col, false)) {
boxes[row][col] = currentPlayer;
} else {
currentPlayer = (currentPlayer == 1) ? 2 : 1;
}
} else {
System.out.println("此边已经连接!");
}
} else {
System.out.println("输入有误,请重新输入!");
}
} else {
System.out.println("输入有误,请重新输入!");
}
if (isGameOver()) {
printBoard();
System.out.printf("游戏结束,%d 号玩家获胜!", getWinner());
break;
}
}
}
private static boolean checkForBox(int row, int col, boolean isHorizontal) {
if (isHorizontal) {
if (row > 0 && horizontalEdges[row - 1][col] != 0 && horizontalEdges[row - 1][col + 1] != 0 && verticalEdges[row - 1][col] != 0 && verticalEdges[row][col] != 0) {
return true;
}
if (row < n - 1 && horizontalEdges[row][col] != 0 && horizontalEdges[row][col + 1] != 0 && verticalEdges[row][col] != 0 && verticalEdges[row + 1][col] != 0) {
return true;
}
} else {
if (col > 0 && verticalEdges[row][col - 1] != 0 && verticalEdges[row + 1][col - 1] != 0 && horizontalEdges[row][col - 1] != 0 && horizontalEdges[row][col] != 0) {
return true;
}
if (col < n - 1 && verticalEdges[row][col] != 0 && verticalEdges[row + 1][col] != 0 && horizontalEdges[row][col] != 0 && horizontalEdges[row][col + 1] != 0) {
return true;
}
}
return false;
}
private static boolean isGameOver() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (boxes[i][j] == 0) {
return false;
}
}
}
return true;
}
private static int getWinner() {
int player1Score = 0;
int player2Score = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (boxes[i][j] == 1) {
player1Score++;
} else if (boxes[i][j] == 2) {
player2Score++;
}
}
}
return (player1Score > player2Score) ? 1 : 2;
}
private static void printBoard() {
System.out.println();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print("+");
if (horizontalEdges[i][j] != 0) {
System.out.print("-");
} else {
System.out.print(" ");
}
}
System.out.println("+");
for (int j = 0; j < n; j++) {
if (verticalEdges[i][j] != 0) {
System.out.print("|");
} else {
System.out.print(" ");
}
if (boxes[i][j] != 0) {
System.out.print(boxes[i][j]);
} else {
System.out.print(" ");
}
}
System.out.println("|");
}
for (int j = 0; j < n; j++) {
System.out.print("+");
if (horizontalEdges[n - 1][j] != 0) {
System.out.print("-");
} else {
System.out.print(" ");
}
}
System.out.println("+");
System.out.println();
}
}
```
这个代码实现了一个基本的点格棋游戏,玩家可以输入两个数字来连接棋盘上的边,并且程序会自动检查是否形成了一个方块。当游戏结束时,程序会输出获胜玩家的编号。