我制作了一个与两个人类玩家一起工作的 Connect4 java 程序。我也想将其更改为带有 AI 的程序。我不知道如何为此实现 minimax 算法。我看过并阅读了很多文章,但所有文章都在谈论棋盘和状态。我正在为此使用 2d jbutton 数组,所以我真的不知道如何实现它。有人可以告诉我该怎么做吗?我附上了代码。
import java.awt.*;
import javax.swing.*;
public class S08Connect4RunnerComputer extends JFrame{
private static final int rows = 6;
private static final int columns = 6;
private static final int cellsize = 100;
private static final int width = cellsize * rows;
private static final int height = cellsize * columns;
private JButton[][] board;
private int turn = 0;
private static int moveX;
private static int moveY;
public S08Connect4RunnerComputer() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName() );
} catch (Exception e) {
e.printStackTrace();
}
board = new JButton[rows][columns];
turn = 1;
setTitle("Connect4 with Computer");
setSize(width, height);
setLocationRelativeTo(null);
setResizable(false);
setLayout(new GridLayout(rows, columns));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
board[i][j] = new JButton();
board[i][j].setBackground(Color.WHITE);
add(board[i][j]);
}
}
//I need to add action listeners here with t
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
board[i][j].setVisible(true);
board[i][j].addActionListener(e -> {
JButton button = (JButton) e.getSource();
int row = -1;
int col = -1;
for (int k = 0; k < rows; k++) {
for (int l = 0; l < columns; l++) {
if (button == board[k][l]) {
row = k;
col = l;
break;
}
}
}
if (board[row][col].getBackground() == Color.WHITE && moveValid(row, col) && turn % 2 != 1) {
board[row][col].setBackground(Color.RED);
moveX = row;
moveY = col;
if (checkWin(row, col)) {
String winner = "";
if (board[row][col].getBackground() == Color.RED) {
winner = "Red";
} else if (board[row][col].getBackground() == Color.YELLOW) {
winner = "Yellow";
}
JOptionPane.showMessageDialog(this, "Game is over! " + winner + " wins!");
S08PlayAgain one = new S08PlayAgain();
setVisible(false);
dispose();
} else if (checkDraw()) {
JOptionPane.showMessageDialog(this, "It's a draw!");
S08PlayAgain one = new S08PlayAgain();
setVisible(false);
dispose();
} else {
turn++;
}
} else if (turn % 2 == 0) {
// I need to code a function that implements minimax algorithm. ill run it from here
}
});
}
}
setVisible(true);
}
private boolean moveValid(int row, int col) {
try {
if (row == 5)
return true;
else if (board[row+1][col].getBackground() == Color.WHITE)
return false;
else
return true;
} catch (Exception e) {
return true;
}
}
private boolean checkWin(int row, int col) {
Color color = board[row][col].getBackground();
int count = 0;
// Check for horizontal win
for (int j = 0; j < columns; j++) {
if (board[row][j].getBackground() == color) {
count++;
} else {
count = 0;
}
if (count == 4) {
return true;
}
}
count = 0;
// Check for vertical win
for (int i = 0; i < rows; i++) {
if (board[i][col].getBackground() == color) {
count++;
} else {
count = 0;
}
if (count == 4) {
return true;
}
}
count = 0;
// Check for diagonal win (top-left to bottom-right)
int i = row;
int j = col;
while (i > 0 && j > 0) {
i--;
j--;
}
while (i < rows && j < columns) {
if (board[i][j].getBackground() == color) {
count++;
} else {
count = 0;
}
if (count == 4) {
return true;
}
i++;
j++;
}
count = 0;
// Check for diagonal win (bottom-left to top-right)
i = row;
j = col;
while (i < rows - 1 && j > 0) {
i++;
j--;
}
while (i >= 0 && j < columns) {
if (board[i][j].getBackground() == color) {
count++;
} else {
count = 0;
}
if (count == 4) {
return true;
}
i--;
j++;
}
return false;
}
private boolean checkDraw() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (board[i][j].getBackground() == Color.WHITE) {
return false;
}
}
}
return true;
}
}
我试过开始,但我不知道从哪里开始。我不知道如何开始编写 minimax 方法。我知道我需要做的是什么玩家,以及棋盘的状态,但是很多网站都在谈论棋盘和状态,而我只是不知道从哪里开始。 编辑:有人可以帮我开始这个方法吗?我了解这个理论,但我不知道如何将其转化为代码。