gpt4 book ai didi

java - 尝试使用极小极大制作无与伦比的井字游戏

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:07 25 4
gpt4 key购买 nike

我正在玩井字游戏,我试图让计算机始终选择最佳的移动。我做了很多研究来理解极小极大,但它似乎不起作用(人工智能没有做出明智的选择)。我修改了一篇关于该主题的精彩文章中的代码 - 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;
}

编辑:

获取结果方法:

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

}

最佳答案

你的极小极大值没有任何意义正如您所看到的,它只是从第一个 if 的函数返回:

 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 - 尝试使用极小极大制作无与伦比的井字游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52076376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com