자바 게임 : A * 알고리즘 (문자 앞의 셀만 검색)
게임 유형 : 기반이되는 그리드 타일 맵
허용되는 방향 : 왼쪽, 앞으로, 오른쪽 (방향을 바꾸려면 왼쪽 두 개 또는 오른쪽 두 개를 사용해야 함)-왼쪽과 오른쪽 모두 대각선으로 이동하지만 원래 얼굴에 따라 배의 얼굴을 변경합니다.
슬롯 : 배의 크기에 따라 사용자가 입력 할 수있는 특정 배의 슬롯이있어 배가 턴당 많은 지점을 이동할 수 있습니다 (예 : 3 개의 슬롯 == 턴당 3 개의 이동).
예:
시작 위치 : 2,2
시작면 : 북쪽
배치 된 이동 : 왼쪽
최종 결과 : 위치 : 1,3; 얼굴 : 서쪽
문제 : 알고리즘은 경로 찾기에 8 개의 타일을 모두 사용합니다. 하지만 앞에있는 타일 만 찾아야합니다 (배면에 따라 다름).
노드 클래스 :
public class AStarNode {
public Position position;
public VesselFace face;
public AStarNode parent;
public double fCost, gCost, hCost;
public AStarNode(Position position, VesselFace face, AStarNode parent, double gCost, double hCost) {
this.position = position;
this.face = face;
this.parent = parent;
this.gCost = gCost;
this.hCost = hCost;
this.fCost = this.gCost + this.hCost;
}
}
길 찾기 계산 :
private Comparator<AStarNode> nodeSorter = new Comparator<AStarNode>() {
@Override
public int compare(AStarNode n0, AStarNode n1) {
if(n1.fCost < n0.fCost) return 1;
if(n1.fCost > n0.fCost) return -1;
return 0;
}
};
public List<AStarNode> findPath(Position start, Position goal){
List<AStarNode> openList = new ArrayList<AStarNode>();
List<AStarNode> closedList = new ArrayList<AStarNode>();
AStarNode current = new AStarNode(start, null, 0, start.distance(goal));
openList.add(current);
while(openList.size() > 0) {
Collections.sort(openList, nodeSorter);
current = openList.get(0);
if(current.position.equals(goal)) {
List<AStarNode> path = new ArrayList<AStarNode>();
while(current.parent != null) {
path.add(current);
current = current.parent;
}
openList.clear();
closedList.clear();
return path;
}
openList.remove(current);
closedList.add(current);
for(int i = 0; i < 9; i++) {
if (i == 4)continue;
int x = current.position.getX();
int y = current.position.getY();
int xi = (i % 3) - 1;
int yi = (i / 3) - 1;
int at = context.getMap().getTile(x + xi, y + yi);
if(at == 1 || at == 2) continue; // ignore rocks
Position a = new Position(x + xi, y + yi);
double gCost = current.gCost + current.position.distance(a);
double hCost = a.distance(goal);
AStarNode node = new AStarNode(a, current, gCost, hCost);
if(positionInList(closedList, a) && gCost >= node.gCost) continue;
if(!positionInList(openList, a) || gCost < node.gCost) openList.add(node);
}
}
closedList.clear();
return null;
}
private boolean positionInList(List<AStarNode> list, Position position) {
for(AStarNode n : list) {
if(n.position.equals(position)) return true;
}
return false;
}
이행:
@Override
public void calculateRoute() {
Position destination = new Position(3,3); // replace with cluster
if(this.equals(destination)) {
return;
}based
path = context.getPlayerManager().findPath(this, destination);
VesselFace face = getFace();
if(path != null) {
if(path.size() > 0) {
int numberOfMoves = getVessel().has3Moves() ? 3 : 4;
Position currentPosition = this.copy();
for(int slot = 0; slot <= numberOfMoves; slot++) { //moves to enter
int positionIndex = (path.size() - 1) - (slot); //subtract slot to allow multiple moves
if(positionIndex < 0 || path.size() < slot) { // make sure it doesn't count too far
return;
}
Position pos = path.get(positionIndex).position;
Position left = MoveType.LEFT.getFinalPosition(currentPosition, face);
Position right = MoveType.RIGHT.getFinalPosition(currentPosition, face);
Position forward = MoveType.FORWARD.getFinalPosition(currentPosition, face);
if(left.equals(pos)) {
currentPosition.add(left.getX() - getX(), left.getY() - getY());
getMoves().setMove(slot, MoveType.LEFT);
switch(face) {
case NORTH:
face = VesselFace.WEST;
break;
case SOUTH:
face = VesselFace.EAST;
break;
case WEST:
face = VesselFace.SOUTH;
break;
case EAST:
face = VesselFace.NORTH;
break;
}
}else if(right.equals(pos)) {
currentPosition.add(right.getX() - getX(), right.getY() - getY());
getMoves().setMove(slot, MoveType.RIGHT);
switch(face) {
case NORTH:
face = VesselFace.EAST;
break;
case SOUTH:
face = VesselFace.WEST;
break;
case WEST:
face = VesselFace.NORTH;
break;
case EAST:
face = VesselFace.SOUTH;
break;
}
}else if(forward.equals(pos)){
currentPosition.add(forward.getX() - getX(), forward.getY() - getY());
getMoves().setMove(slot, MoveType.FORWARD);
switch(face) {
case NORTH:
face = VesselFace.NORTH;
break;
case SOUTH:
face = VesselFace.SOUTH;
break;
case WEST:
face = VesselFace.WEST;
break;
case EAST:
face = VesselFace.EAST;
break;
}
}
}
}
}
}
나는 switch 문과 currentPosition.add () 메서드를 사용하여 특정 턴에 3 개의 동작을 배치 할 수 있도록합니다. 어디로 끝나야하는지 안다. 아마도 모범 사례가 아닐 것입니다.
특정 슬롯에 이동을 추가하는 문
getMoves().setMove(slot, MoveType.FORWARD);
배면을 기준으로 매 턴마다 확인해야하는 타일 :

답변
이것은 내가 작성한 댓글에 대한 자세한 내용을 제공하는 부분적인 시도 일뿐입니다.
A *는 선박의 "상태"를 포함하는 노드의 그래프를 검색합니다. 대부분의 튜토리얼 (미안하지만 내 것을 포함)에서 상태는 위치 일뿐입니다. 그러나 귀하의 경우에는 상태가 위치와 직면 방향이라고 생각합니다. 앞의 세 위치를 계산하려면 마주 보는 방향을 알아야합니다. 그런 다음 이동 후 위치와 새로운 방향을 모두 갖게됩니다.
Node
현재 위치가 있습니다. 모두가로 변경 position
하고를 facing
. 다음 for(int i = 0; i < 9; i++)
은 이웃을 찾기 위한 대략적인 버전의 루프입니다. 9 개의 이웃을 통과하는 대신 4 개의 방향 각각에 정확히 3 개의 이웃이 있습니다. (예, 8 개가 아닌 12 개가 있습니다! 이전에 향한 방향에 따라 다르기 때문입니다)

int x = current.position.getX();
int y = current.position.getY();
List<Node> neighbors = new ArrayList<Node>();
switch (current.facing) {
case NORTH:
neighbors.add(new Node(new Position(x-1, y-1), WEST, …));
neighbors.add(new Node(new Position(x, y-1), NORTH, …));
neighbors.add(new Node(new Position(x+1, y-1), EAST, …));
break;
case EAST:
neighbors.add(new Node(new Position(x+1, y-1), NORTH, …));
neighbors.add(new Node(new Position(x+1, y), EAST, …));
neighbors.add(new Node(new Position(x+1, y+1), SOUTH, …));
break;
case SOUTH:
neighbors.add(new Node(new Position(x-1, y+1), WEST, …));
neighbors.add(new Node(new Position(x, y+1), SOUTH, …));
neighbors.add(new Node(new Position(x+1, y+1), EAST, …));
break;
case WEST:
neighbors.add(new Node(new Position(x-1, y-1), NORTH, …));
neighbors.add(new Node(new Position(x-1, y), WEST, …));
neighbors.add(new Node(new Position(x-1, y+1), SOUTH, …));
break;
}
/* for each of the nodes in the neighbors list, use the same
logic you already have:
1. check if it's a rock, and ignore if it is
2. calculate g cost, store it in the node
3. calculate h cost, store it in the node
4. consider adding the node to openList
*/