Skip to content

Commit f989560

Browse files
mhdmohammadimhdmohammadi
mhdmohammadi
authored and
mhdmohammadi
committed
edit calc paths
1 parent a3cdccb commit f989560

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

Diff for: src/Client/Model/Game.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -807,10 +807,16 @@ private void calcPathsFromPlayer(Player player) {
807807
Cell friendKingCell = getPlayerKing(getFriendIdOfPlayer(player.getPlayerId())).getCenter();
808808

809809
List<Path> paths = new ArrayList<>();
810-
for (Path path : initMessage.getMap().getPaths())
811-
if (path.getCells().indexOf(playerKingCell) == 0 || path.getCells().lastIndexOf(playerKingCell) == path.getCells().size() - 1)
812-
if (!path.getCells().contains(friendKingCell))
813-
paths.add(path);
810+
for (Path path : initMessage.getMap().getPaths()) {
811+
if (path.getCells().indexOf(playerKingCell) == 0 || path.getCells().indexOf(playerKingCell) == path.getCells().size() - 1) {
812+
if (!path.getCells().contains(friendKingCell)){
813+
Path newPath = path.copy();
814+
if(newPath.getCells().indexOf(playerKingCell) != 0)
815+
Collections.reverse(newPath.getCells());
816+
paths.add(newPath);
817+
}
818+
}
819+
}
814820
player.setPathsFromPlayer(paths);
815821
}
816822

@@ -826,8 +832,12 @@ private void calcPathToFriend(Player player) {
826832
for (Path path : initMessage.getMap().getPaths()) {
827833
List<Cell> pathCells = path.getCells();
828834
if (pathCells.indexOf(playerKingCell) == 0 || pathCells.lastIndexOf(playerKingCell) == pathCells.size() - 1)
829-
if (pathCells.indexOf(friendKingCell) == 0 || pathCells.lastIndexOf(friendKingCell) == pathCells.size() - 1)
830-
player.setPathToFriend(path);
835+
if (pathCells.indexOf(friendKingCell) == 0 || pathCells.lastIndexOf(friendKingCell) == pathCells.size() - 1) {
836+
Path newPath = path.copy();
837+
if(newPath.getCells().indexOf(playerKingCell) != 0)
838+
Collections.reverse(newPath.getCells());
839+
player.setPathToFriend(newPath);
840+
}
831841
}
832842
}
833843

Diff for: src/Client/Model/Path.java

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package Client.Model;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
/**
@@ -20,6 +21,14 @@ public boolean equals(Object object) {
2021
return false;
2122
}
2223

24+
public Path copy(){
25+
Path newPath = new Path();
26+
newPath.setId(this.id);
27+
List<Cell> newList = new ArrayList<>(this.cells);
28+
newPath.setCells(newList);
29+
return newPath;
30+
}
31+
2332
public int getId() {
2433
return id;
2534
}

0 commit comments

Comments
 (0)