- 已关闭。**此问题需要debugging details。当前不接受答案。
编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
19小时前关门了。
Improve this question
我正在学习Java类的入门课程,我们最近开始制作井字游戏机器人。我们的最终目标是只制作一个有一点点策略的机器人,但我已经非常接近完成一个完美的机器人了。我们还没有正式学习递归,所以我很困惑为什么我的极大极小算法不输出写值。它返回的最大值是0(平局),所以它只是把它的移动在下一个可用的位置。我会非常感激,如果有人可以只是推动我在正确的方向,当谈到让这个极大极小算法输出100(赢)。谢谢吨!
//12/15/2022
//Represents a player who looks to always win in tic tac toe
public class computerMoves {
private TTTBoard computerBoard;
private int moveRow;
private int moveColumn;
public computerMoves(TTTBoard board )
{
computerBoard = board;
moveRow = 0;
moveColumn = 0;
}
public int getRow()
{
return moveRow;
}
public int getColumn()
{
return moveColumn;
}
private int minmax(int depth, TTTBoard board, boolean isMax)
{
if(board.getWinner() == 'X')
{
return 100;
}
if(board.getWinner() == 'O')
{
return -100;
}
if(board.full())
{
return 0;
}
if(isMax)
{
int highestScore = -2000;
for(int rows = 1; rows <= 3; rows++)
{
for(int columns = 1; columns <= 3; columns++)
{
if(board.placeXorO('X', rows, columns))
{
highestScore = Math.max(highestScore, minmax(depth+1, board, !isMax));
board.placeXorO('-', rows, columns);
}
}
}
return highestScore;
}
else
{
int lowestScore = 2000;
for(int rows = 1; rows <= 3; rows++)
{
for(int columns = 1; columns <= 3; columns++)
{
if(board.placeXorO('O', rows, columns))
{
lowestScore = Math.min(lowestScore, minmax(depth+1, board, !isMax));
board.placeXorO('-', rows, columns);
}
}
}
return lowestScore;
}
}
public void bestMove()
{
int bestMove = -200;
for(int rows = 1; rows <= 3; rows++)
{
for(int columns = 1; columns <= 3; columns++)
{
if(computerBoard.getBoard()[rows-1][columns-1] == '-')
{
int currentMove = minmax(0, computerBoard, true);
System.out.println("Current move " + currentMove);
computerBoard.placeXorO('-', rows, columns);
if(currentMove > bestMove)
{
moveRow = rows;
moveColumn = columns;
bestMove = currentMove;
}
}
}
}
}
}
public boolean full() //checks if board is full
{
for(int row = 0; row < 3; row++)
{
for(int column = 0; column < 3; column++) //for each column in each row
{
if(board[row][column] == '-') //if there is a blank space
{
return false;
}
}
}
return true;
}
public boolean placeXorO(char player, int row, int column) //checks if a move is legal and does it if it is
{
if(board[row - 1][column - 1] == '-') //if the selected space is blank
{
board[row - 1][column - 1] = player; //replace blank space with current player
return true;
}
else if(player == '-')
{
board[row - 1][column - 1] = player; //replaces a move with a blank space
}
return false;
}
我知道我的getWinner函数可以工作,我已经在之前的任务中手动测试了它的每一个可能的组合。我几乎可以肯定我的board类中的所有函数都可以100%工作,因为它们都可以在一个人对人的井字游戏中工作。我相信只是我的极大极小算法不起作用,因为我承认我在递归的主题上有点挣扎。
1条答案
按热度按时间wztqucjr1#
从开始的位置游戏将永远是一个平局,因为极大极小假设最佳发挥双方的对手,井字游戏是解决了平局与最佳发挥。没有错。或者你的意思是它永远不会得到一个分数高于0,即使你明确地让它赢了?
我还发现你的代码有一个问题,那就是这些if语句:
这里你还需要考虑轮到谁了。如果是X玩,那么当X赢的时候返回100是正确的,但是如果是O玩,那么它需要相反。