Java 如何选择在国际象棋中获得最高价值棋子的随机移动?
因此,我有一个数组列表,其中列出了国际象棋中当前棋盘状态下的所有棋步Java 如何选择在国际象棋中获得最高价值棋子的随机移动?,java,Java,因此,我有一个数组列表,其中列出了国际象棋中当前棋盘状态下的所有棋步 ArrayList<Move> takePiece; 我希望能够从数组列表中选择一个随机移动,以获取最高值的片段 因此,如果国王有3个不同的招式,它会随机选择其中一个招式(检查和将死尚未实施) 我该怎么办 public Move(Piece p , int x, int y, int dx, int dy, boolean t){ this.p = p; this.x = x;
ArrayList<Move> takePiece;
我希望能够从数组列表中选择一个随机移动,以获取最高值的片段
因此,如果国王有3个不同的招式,它会随机选择其中一个招式(检查和将死尚未实施)
我该怎么办
public Move(Piece p , int x, int y, int dx, int dy, boolean t){
this.p = p;
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.t = t;
}
因此,这就是移动的意义。为了检查将要执行的工件值,我将在(dx,dy)处检查工件。您必须在数组中检查最有价值的
工件
并相应地选择移动,要执行此操作,您必须迭代两次:
// get the max value of possible pieces that could be taken
ArrayList<Move> takePiece // here you must have all possible takes
int maxPieceValue = 0;
for (Move m : takePiece) {
if(PieceCode.charToInt(m.getChar()) > maxPieceValue)
maxPieceValue = m.getPiece.getType();
}
// get a list with best moves available
ArrayList<Move> bestMoves new ArrayList<Move>();
for (Move m : takePiece) {
if(PieceCode.charToInt(m.getChar()) > maxPieceValue)
bestMoves.add(m);
}
// choose randomly one of best moves
Random random = new Random();
Move choosenMove = bestMoves.get(random.nextInt(bestMoves.lenght));
//获取可以获取的可能碎片的最大值
ArrayList takePiece//这里您必须有所有可能的takes
int maxPieceValue=0;
对于(移动m:takePiece){
if(PieceCode.ChartPoint(m.getChar())>maxPieceValue)
maxPieceValue=m.getPiece.getType();
}
//获取一份包含最佳可用移动的列表
ArrayList最佳移动新ArrayList();
对于(移动m:takePiece){
if(PieceCode.ChartPoint(m.getChar())>maxPieceValue)
加上(m);
}
//随机选择一个最好的动作
随机=新随机();
Move choosenMove=bestMoves.get(random.nextInt(bestMoves.lenght));
你如何在移动中引用你的作品?@JordiCastilla我已经为你更新了一个问题,这个问题类似于getType()
?@JordiCastillapiecode.chartPoint(test.getChar())
如果这个作品被称为test
。检查我的答案。。。如果您将片段常量设置为int,为什么要使用getChar??
// get the max value of possible pieces that could be taken
ArrayList<Move> takePiece // here you must have all possible takes
int maxPieceValue = 0;
for (Move m : takePiece) {
if(PieceCode.charToInt(m.getChar()) > maxPieceValue)
maxPieceValue = m.getPiece.getType();
}
// get a list with best moves available
ArrayList<Move> bestMoves new ArrayList<Move>();
for (Move m : takePiece) {
if(PieceCode.charToInt(m.getChar()) > maxPieceValue)
bestMoves.add(m);
}
// choose randomly one of best moves
Random random = new Random();
Move choosenMove = bestMoves.get(random.nextInt(bestMoves.lenght));