我正在打井字游戏,并且试图使计算机始终选择最佳移动方式。我在了解minimax方面做了很多研究,但似乎行不通(AI无法做出明智的选择)。我从一篇很棒的文章-check it here修改了代码。谁能帮我找出我的代码出了什么问题?
private Move minimax(JButton[][] boardCopy, String player){
if (getResult(boardCopy) == 2) {
return new Move(10);
} else if (getResult(boardCopy) == 1){
return new Move(-10);
} else if (getResult(boardCopy) == 3) {
return new Move(0);
}
ArrayList<Move> possibleMoves = getListOfPossibleMoves(boardCopy);
for (Move move : possibleMoves){
int i = move.i;
int j = move.j;
boardCopy[i][j].setText(player);
if (player.equals(compPlayer)) {
Move resMove = minimax(boardCopy, humanPlayer);
move.moveScore = resMove.moveScore;
} else {
Move resMove = minimax(boardCopy, compPlayer);
move.moveScore = resMove.moveScore;
}
boardCopy[i][j].setText("");
}
Move bestMove = new Move();
if (player.equals(compPlayer)){
int bestScore = -10000;
for(Move move : possibleMoves){
if (move.moveScore > bestScore){
bestScore = move.moveScore;
bestMove = new Move(move);
}
}
} else {
int bestScore = 10000;
for(Move move : possibleMoves){
if (move.moveScore < bestScore){
bestScore = move.moveScore;
bestMove = new Move(move);
}
}
}
return bestMove;
}
private void optimalComputerMove(JButton[][] board, String player){
if (hasWinner) return;
JButton[][] boardCopy = copyBoard(board);
Move bestMove = minimax(boardCopy, player);
int i = bestMove.i;
int j = bestMove.j;
board[i][j].setText(player);
moveCount++;
showResult(getResult(board));
}
private ArrayList<Move> getListOfPossibleMoves(JButton[][] board){
ArrayList<Move> possibleMoves = new ArrayList<>();
for (int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if (board[i][j].getText().equals("")){
Move availMove = new Move(i, j);
possibleMoves.add(availMove);
}
}
}
return possibleMoves;
}
编辑:
GetResult方法:
private int getResult(JButton[][] board){
if((board[0][0].getText().equals(currPlayer) && board[1][0].getText().equals(currPlayer) && board[2][0].getText().equals(currPlayer)) ||
(board[0][1].getText().equals(currPlayer) && board[1][1].getText().equals(currPlayer) && board[2][1].getText().equals(currPlayer)) ||
(board[0][2].getText().equals(currPlayer) && board[1][2].getText().equals(currPlayer) && board[2][2].getText().equals(currPlayer)) ||
(board[0][0].getText().equals(currPlayer) && board[0][1].getText().equals(currPlayer) && board[0][2].getText().equals(currPlayer)) ||
(board[1][0].getText().equals(currPlayer) && board[1][1].getText().equals(currPlayer) && board[1][2].getText().equals(currPlayer)) ||
(board[2][0].getText().equals(currPlayer) && board[2][1].getText().equals(currPlayer) && board[2][2].getText().equals(currPlayer)) ||
(board[0][0].getText().equals(currPlayer) && board[1][1].getText().equals(currPlayer) && board[2][2].getText().equals(currPlayer)) ||
(board[2][0].getText().equals(currPlayer) && board[1][1].getText().equals(currPlayer) && board[0][2].getText().equals(currPlayer))){
if(board == this.board){
hasWinner = true;
}
if (currPlayer.equals(humanPlayer)) return 1; //X won
else return 2; //O won
}
boolean full = true;
for(int i = 0; i < 3 && full; i++){
for(int j = 0; j < 3 & full; j++){
if (board[i][j].getText().equals("")){
full = false;
}
}
}
if (full) {
if (board == this.board){
hasWinner = true;
}
return 3; //tie
}
else return 0; //incomplete
}
最佳答案
你的极小值什么都不算
如您所见,它只是从第一个ifs函数返回:
if (getResult(boardCopy) == 2) {
return new Move(10);
} else if (getResult(boardCopy) == 1){
return new Move(-10);
} else if (getResult(boardCopy) == 3) {
return new Move(0);
}
关于java - 尝试使用minimax制作无与伦比的井字游戏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52076376/