package lx;
import java.util.Scanner;
public class DotsAndBoxes {
private static final int SIZE = 3;
private static String[][] board = new String[SIZE2+1][SIZE2+1];
private static boolean isPlayer1Turn = true;
private static int player1Score = 0, player2Score = 0;
public static void main(String[] args) {
initializeBoard();
printBoard();
while (!isGameOver()) {
int row, col;
Scanner sc = new Scanner(System.in);
if (isPlayer1Turn) {
System.out.println("Player 1's turn. Enter row and column: ");
} else {
System.out.println("Player 2's turn. Enter row and column: ");
}
row = sc.nextInt();
col = sc.nextInt();
if (isValidMove(row, col)) {
makeMove(row, col);
printBoard();
isPlayer1Turn = !isPlayer1Turn;
} else {
System.out.println("Invalid move. Try again.");
}
}
if (player1Score > player2Score) {
System.out.println("Player 1 wins!");
} else if (player2Score > player1Score) {
System.out.println("Player 2 wins!");
} else {
System.out.println("It's a draw!");
}
}
private static void initializeBoard() {
for (int i = 0; i < SIZE*2+1; i++) {
for (int j = 0; j < SIZE*2+1; j++) {
if (i % 2 == 0 && j % 2 == 0) {
board[i][j] = "+";
} else if (i % 2 == 0 || j % 2 == 0) {
board[i][j] = " ";
} else {
board[i][j] = " ";
}
}
}
}
private static void printBoard() {
for (int i = 0; i < SIZE*2+1; i++) {
for (int j = 0; j < SIZE*2+1; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
private static boolean isGameOver() {
for (int i = 0; i < SIZE*2+1; i++) {
for (int j = 0; j < SIZE*2+1; j++) {
if (board[i][j] == " ") {
return false;
}
}
}
return true;
}
private static boolean isValidMove(int row, int col) {
return row >= 0 && row < SIZE*2+1 && col >= 0 && col < SIZE*2+1 && board[row][col] == " ";
}
private static void makeMove(int row, int col) {
if (isPlayer1Turn) {
board[row][col] = "-";
} else {
board[row][col] = "|";
}
// Check for completed boxes
if (row % 2 == 1 && col % 2 == 0) { // Vertical line
if (row > 1 && board[row-1][col] == " " && board[row-2][col] == "-" && board[row-1][col-1] == "|" && board[row-1][col+1] == "|") {
board[row-1][col] = "1";
isPlayer1Turn = true;
player1Score++;
}
if (row < SIZE*2-1 && board[row+1][col] == " " && board[row+2][col] == "-" && board[row+1][col-1] == "|" && board[row+1][col+1] == "|") {
board[row+1][col] = "1";
isPlayer1Turn = true;
player1Score++;
}
} else if (row % 2 == 0 && col % 2 == 1) { // Horizontal line
if (col > 1 && board[row][col-1] == " " && board[row][col-2] == "|" && board[row-1][col-1] == "-" && board[row+1][col-1] == "-") {
board[row][col-1] = "2";
isPlayer1Turn = false;
player2Score++;
}
if (col < SIZE*2-1 && board[row][col+1] == " " && board[row][col+2] == "|" && board[row-1][col+1] == "-" && board[row+1][col+1] == "-") {
board[row][col+1] = "2";
isPlayer1Turn = false;
player2Score++;
}
}
}
}
没有SIZE2这个常量,你定义的是SIZE
参考GPT和自己的思路:您好,第五行代码中出现了一个拼写错误,应该将"SIZE2"改成"SIZE*2"。正确代码如下:
private static String[][] board = new String[SIZE*2+1][SIZE*2+1];
这里报错?两个变量不一致
好后缀规则与坏字符规则非常类似,当模式串滑动到如下图所示的位置时,模式串与主串有两个字符是匹配的,倒数第三个字符不匹配。
先来看看好后缀规则怎么工作的。
我们把已经匹配的"bc"称为好后缀,记作{u}.我们用他在模式串中进行查找,如果找到另一个与好后缀{u}匹配的子串{u*},那么我们就将模式串滑动到子串{u*}与好后缀{u}上下对齐的位置。如下图。
如果在模式串中找不到好后缀{u}匹配的另外的子串,就直接将模式串滑动到好后缀{u}的后面。
不过大家想没想过这个问题,当模式串中不存在与好后缀{u}匹配的子串时,我们直接将模式串滑动到好后缀{u}后面,是不是有点过头,看下图一个例子,其中"bc"是好后缀,尽管在模式串中没有另外一个与好后缀匹配的子串,但是我们如果模式串移动到好后缀的后面,就会错过模式串和主串可以匹配的情况。