📄 analyser.java
字号:
package org.yushang.jumpchess.strategy;
// Download:http://www.codefans.net
import java.util.Arrays;
import org.yushang.jumpchess.pkg.Chess;
import org.yushang.jumpchess.pkg.ChessBoard;
import org.yushang.jumpchess.pkg.Player;
import org.yushang.jumpchess.pub.BoardArea;
import org.yushang.jumpchess.pub.Position;
public class Analyser {
private Player player = null;
private ChessBoard chessBoard = null;
public Analyser(Player player, ChessBoard chessBoard) {
this.player = player;
this.chessBoard = chessBoard;
}
private boolean IsNecessaryDouble(Step[] steps) {
Strategy strategy = new MainStrategy(chessBoard, player);
int CurrentScore = strategy.CalculateScore();
int MaxGap = Integer.MIN_VALUE;
for (int i = 0; i < steps.length; i++) {
if (MaxGap < CurrentScore - steps[i].Score) {
MaxGap = CurrentScore - steps[i].Score;
}
}
return (MaxGap < 5);
}
private boolean InOtherArea(Position position) {
boolean InArea = false;
for (int j = 0; j < BoardArea.Areas.length; j++) {
if ((BoardArea.Areas[j] != player.getArea()) &&
(BoardArea.Areas[j] != player.getArea().getOppsiteArea())) {
if (BoardArea.Areas[j].isInclude(position)) {
InArea = true;
break;
}
}
}
return InArea;
}
private Step RandomStep(Step[] steps) {
Arrays.sort(steps);
int Score0 = steps[0].Score;
int EndIndex = -1;
for (int i = 0; i < steps.length; i++) {
if (Score0 != steps[i].Score) {
break;
}
EndIndex ++;
}
int re = (int) (Math.random() * (EndIndex + 1));
//System.out.println("RE=" + re + " EndIndex=" + EndIndex);
return steps[re];
}
private boolean isLag(Chess chess, int distance) {
Position position = player.getArea().getOppsiteArea().getAreaPositions()[0];
int d = chessBoard.getPosition(chess).getDistance(position);
return (d > distance);
}
private int LagCount() {
int Count = 0;
for (int i = 0; i < player.getChesses().length; i++) {
if (isLag(player.getChesses()[i], 8)) {
Count ++;
}
}
return Count;
}
public Step GetBestStep() {
Step[] steps = chessBoard.getAllStep(player.getChesses());
for (int i = 0; i < steps.length; i++) {
Strategy strategy = new MainStrategy(chessBoard, player, steps[i]);
steps[i].Score = strategy.CalculateScore();
//System.out.println(steps[i].toString());
}
//如果没有什么好棋则进行两步运算 DoubleStrategy
if (IsNecessaryDouble(steps)) {
System.out.println("没什么好棋了");
for (int i = 0; i < steps.length; i++) {
Strategy strategy = new DoubleStrategy(chessBoard, player, steps[i]);
steps[i].Score = strategy.CalculateScore();
}
}
if (LagCount() <= 2) {
for (int i = 0; i < steps.length; i++) {
if (isLag(steps[i].chess, 8)) {
steps[i].Score -= 8;
}
}
}
//找出敌方最落后的子,如果可以跳很远,则不能走这步棋,(条件自己的损失不能太大)
if (player.getOppsite() != null) {
Strategy m = new MainStrategy(chessBoard, player);
int CurrentScore = m.CalculateScore();
for (int i = 0; i < steps.length; i++) {
Strategy s = new MainStrategy(chessBoard, player, steps[i]);
int p = 0;
if (CurrentScore - s.CalculateScore() < 5) {
p = 8;
} else {
p = 3;
}
Strategy strategy = new OppsiteScore(chessBoard, player.getOppsite(), steps[i]);
steps[i].Score -= strategy.CalculateScore() * 2 / p;
}
}
//下面进行修正
//1.落单了不行 (不搞也影响也不是很大)
//2.落后了,优先级提高 (不搞也影响也不是很大)
/* for (int i = 0; i < steps.length; i++) {
Position p1 = player.getArea().getOppsiteArea().getAreaPositions()[0];
Position p2 = chessBoard.getPosition(steps[i].chess);
if (p1.getDistance(p2) > 8) {
steps[i].Score -= 5;
}
}*/
//3.进入其它区域去了要加分 (分数越低优先级越高)
for (int i = 0; i < steps.length; i++) {
if ((InOtherArea(steps[i].position)) &&
(!InOtherArea(chessBoard.getPosition(steps[i].chess)))) {
steps[i].Score += 5;
}
}
//4.前
return RandomStep(steps);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -