放开那三国战斗规则出手顺序实现
package com.wcc.game.fight;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
class TestPlayer {
private int side;
private int index;
private String name;
/**
* 排
*/
private int row;
/**
* 列
*/
private int col;
public TestPlayer() {
}
public TestPlayer(int index, String name, int side) {
this.index = index;
this.name = name;
this.side = side;
this.col = getCol(index);
this.row = getRow(index);
}
@Override
public String toString() {
return "TestPlayer [index=" + index + ", name=" + name + ", row=" + row + ", col=" + col + "]";
}
// 1 4
// 2 5
// 3 6
private static List<List<Integer>> rowConstants = Arrays.asList(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6));
private static List<List<Integer>> colConstants = Arrays.asList(Arrays.asList(1, 4), Arrays.asList(2, 5),
Arrays.asList(3, 6));
public static int getRow(int pos) {
for (int i = 0; i < rowConstants.size(); i++) {
List<Integer> r = rowConstants.get(i);
if (r.contains(pos)) {
return i + 1;
}
}
return -1;
}
public static int getCol(int pos) {
for (int i = 0; i < colConstants.size(); i++) {
List<Integer> r = colConstants.get(i);
if (r.contains(pos)) {
return i + 1;
}
}
return -1;
}
public void setSide(int side) {
this.side = side;
}
public int getSide() {
return side;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public int getIndex() {
return index;
}
}
public class MainTest {
public static void printArray(List<TestPlayer> players) {
for (TestPlayer testPlayer : players) {
System.out.println(testPlayer.toString());
}
}
public static TestPlayer selectMinDisTarget(List<TestPlayer> lstTarget, final TestPlayer roundFighter) {
if (CollectionUtils.isEmpty(lstTarget)) {
return null;
}
TestPlayer ret = null;
int distanceToTarget = 0;
for (int i = 0; i < lstTarget.size(); i++) {
int curDistance = Math.abs(roundFighter.getCol() - lstTarget.get(i).getCol());
if (i == 0) {
ret = lstTarget.get(i);
distanceToTarget = Math.abs(roundFighter.getCol() - ret.getCol());
} else {
if (curDistance < distanceToTarget) {
ret = lstTarget.get(i);
distanceToTarget = Math.abs(roundFighter.getCol() - ret.getCol());
}
}
}
return ret;
}
public static TestPlayer selectTarget(final TestPlayer curFighter, List<TestPlayer> playersA,
List<TestPlayer> playersB) {
List<TestPlayer> targetList = playersB;
if (curFighter.getSide() == 2) {
targetList = playersA;
}
// 首先判断前排有没有人
@SuppressWarnings("unchecked")
List<TestPlayer> frontTargetList = (List<TestPlayer>) CollectionUtils.select(targetList, new Predicate() {
@Override
public boolean evaluate(Object object) {
return ((TestPlayer) object).getRow() == 1;
}
});
// 如果前排有人
if (CollectionUtils.isNotEmpty(frontTargetList)) {
// 默认选择第0个
TestPlayer ret = selectMinDisTarget(frontTargetList, curFighter);
if (ret != null) {
return ret;
}
} else {
TestPlayer ret = selectMinDisTarget(targetList, curFighter);
if (ret != null) {
return ret;
}
}
return null;
}
public static void doFight(List<TestPlayer> endPlayers, List<TestPlayer> playersA, List<TestPlayer> playersB) {
for (int i = 0; i < endPlayers.size(); i++) {
TestPlayer curFighter = endPlayers.get(i);
TestPlayer targetFighter = selectTarget(curFighter, playersA, playersB);
System.out.println("cur: " + curFighter.toString());
System.out.println("target:" + targetFighter.toString());
}
}
public static void main(String[] args) {
TestPlayer testPlayerA1 = new TestPlayer(1, "A1", 1);
TestPlayer testPlayerA2 = new TestPlayer(2, "A2", 1);
TestPlayer testPlayerA3 = new TestPlayer(3, "A3", 1);
TestPlayer testPlayerA4 = new TestPlayer(4, "A4", 1);
TestPlayer testPlayerA5 = new TestPlayer(5, "A5", 1);
TestPlayer testPlayerA6 = new TestPlayer(6, "A6", 1);
TestPlayer testPlayerB1 = new TestPlayer(1, "B1", 2);
TestPlayer testPlayerB2 = new TestPlayer(2, "B2", 2);
TestPlayer testPlayerB3 = new TestPlayer(3, "B3", 2);
TestPlayer testPlayerB4 = new TestPlayer(4, "B4", 2);
TestPlayer testPlayerB5 = new TestPlayer(5, "B5", 2);
TestPlayer testPlayerB6 = new TestPlayer(6, "B6", 2);
List<TestPlayer> playersA = new ArrayList<TestPlayer>();
// playersA.add(testPlayerA1);
playersA.add(testPlayerA2);
playersA.add(testPlayerA3);
playersA.add(testPlayerA4);
playersA.add(testPlayerA5);
playersA.add(testPlayerA6);
List<TestPlayer> playersB = new ArrayList<TestPlayer>();
playersB.add(testPlayerB1);
playersB.add(testPlayerB2);
playersB.add(testPlayerB3);
playersB.add(testPlayerB4);
playersB.add(testPlayerB5);
playersB.add(testPlayerB6);
// printArray(playersA);
List<TestPlayer> endPlayers = new ArrayList<TestPlayer>();
// A1 A2 A3 A4 A5 A6
// B1 B2 B3 B4 B5 B6
// 合并两个数组 实现方式 隔位插入就可以实现效果
int maxSideSize = playersA.size() >= playersB.size() ? playersA.size() : playersB.size();
boolean aFirst = true;
for (int i = 0; i < maxSideSize; i++) {
if (aFirst) {
if (i < playersA.size()) {
endPlayers.add(playersA.get(i));
}
if (i < playersB.size()) {
endPlayers.add(playersB.get(i));
}
} else {
if (i < playersB.size()) {
endPlayers.add(playersB.get(i));
}
if (i < playersA.size()) {
endPlayers.add(playersA.get(i));
}
}
}
doFight(endPlayers, playersA, playersB);
// printArray(endPlayers);
}
}