Skip to content

Commit 6ce5473

Browse files
TRAP & SWAMP cells added to map
1 parent 085b6f4 commit 6ce5473

File tree

7 files changed

+98
-54
lines changed

7 files changed

+98
-54
lines changed

map.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ WORKER_MAX_CARRYING_RESOURCE_AMOUNT = 10;
1919
# Number of agents for EACH TEAM
2020
# SCORPION = Soldier, ANT = Worker :)
2121
INIT_SCORPIONS = 0
22-
INIT_ANTS = 4
22+
INIT_ANTS = 2
23+
SWAMP_TURNS = 2
2324

2425
# server runtime configs
2526
READ_MAP_FROM_FILE = true

map.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@
798798
{ "row": 24, "col": 23, "cell_type": 3, "rec1": 0, "rec2": 0 },
799799
{ "row": 24, "col": 24, "cell_type": 2, "rec1": 0, "rec2": 0 },
800800
{ "row": 24, "col": 25, "cell_type": 2, "rec1": 0, "rec2": 0 },
801-
{ "row": 24, "col": 26, "cell_type": 2, "rec1": 0, "rec2": 0 },
801+
{ "row": 24, "col": 26, "cell_type": 4, "rec1": 0, "rec2": 0 },
802802
{ "row": 24, "col": 27, "cell_type": 2, "rec1": 0, "rec2": 0 },
803803
{ "row": 24, "col": 28, "cell_type": 2, "rec1": 0, "rec2": 0 },
804804
{ "row": 24, "col": 29, "cell_type": 2, "rec1": 0, "rec2": 0 },
@@ -830,7 +830,7 @@
830830
{ "row": 25, "col": 23, "cell_type": 3, "rec1": 0, "rec2": 0 },
831831
{ "row": 25, "col": 24, "cell_type": 2, "rec1": 0, "rec2": 0 },
832832
{ "row": 25, "col": 25, "cell_type": 2, "rec1": 0, "rec2": 0 },
833-
{ "row": 25, "col": 26, "cell_type": 2, "rec1": 0, "rec2": 0 },
833+
{ "row": 25, "col": 26, "cell_type": 2, "rec1": 10, "rec2": 0 },
834834
{ "row": 25, "col": 27, "cell_type": 2, "rec1": 0, "rec2": 0 },
835835
{ "row": 25, "col": 28, "cell_type": 2, "rec1": 0, "rec2": 0 },
836836
{ "row": 25, "col": 29, "cell_type": 2, "rec1": 0, "rec2": 0 },

src/main/java/ir/sharif/aichallenge/server/logic/config/ConfigReader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public static void readConfigFile() {
5656
} catch (Exception ignored) {
5757
AntGenerator.PROCESS_TIMEOUT_SECONDS = 30;
5858
}
59+
try {
60+
ConstConfigs.SWAMP_TURNS = Integer.parseInt(props.getProperty("SWAMP_TURNS"));
61+
} catch (Exception ignore) {
62+
}
5963
} catch (Exception e) {
6064
e.printStackTrace();
6165
Log.e("ConfigReader", "error in config props");

src/main/java/ir/sharif/aichallenge/server/logic/config/ConstConfigs.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ public class ConstConfigs {
3535
public static boolean READ_MAP_FROM_FILE = false;
3636
public static int SHIFT_X = 0;
3737
public static int SHIFT_Y = 0;
38+
39+
public static int SWAMP_TURNS = 1;
3840
}

src/main/java/ir/sharif/aichallenge/server/logic/model/ant/Ant.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public class Ant {
88
private int colonyId;
99
private int health;
1010
private AntType antType;
11-
// TODO: use Cell instead of x, y
1211
private int xPosition;
1312
private int yPosition;
1413
// just used for worker ant
1514
private ResourceType carryingResourceType;
1615
private int carryingResourceAmount;
16+
private int inTrap = 0;
1717

1818
public Ant(int id, int colonyId, int xPosition, int yPosition, AntType antType) {
1919
this.id = id;
@@ -31,9 +31,8 @@ private void setInitialResourceSettings() {
3131
}
3232

3333
private void setInitialHealth(AntType antType) {
34-
health = antType == AntType.WORKER ?
35-
ConstConfigs.WORKER_ANT_INITIAL_HEALTH :
36-
ConstConfigs.SOLDIER_ANT_INITIAL_HEALTH;
34+
health = antType == AntType.WORKER ? ConstConfigs.WORKER_ANT_INITIAL_HEALTH
35+
: ConstConfigs.SOLDIER_ANT_INITIAL_HEALTH;
3736
}
3837

3938
public int getCarryingResourceAmount() {
@@ -90,7 +89,16 @@ public ResourceType getCarryingResourceType() {
9089
public void setCarryingResourceType(ResourceType carryingResourceType) {
9190
this.carryingResourceType = carryingResourceType;
9291
}
93-
}
9492

93+
public int getInTrap() {
94+
return this.inTrap;
95+
}
9596

97+
public void setIntrap(int trap) {
98+
this.inTrap = trap;
99+
}
96100

101+
public void callback() {
102+
// handle ant callbacks each turn
103+
}
104+
}
Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,57 @@
11
package ir.sharif.aichallenge.server.logic.model.cell;
22

33
public enum CellType {
4-
WALL,
5-
EMPTY,
6-
BASE;
4+
WALL, EMPTY, BASE, TRAP, SWAMP;
75

86
public int getValue() {
97
switch (this) {
10-
case WALL:
11-
return 2;
12-
case EMPTY:
13-
return 1;
14-
case BASE:
15-
return 0;
16-
default:
17-
return 1;
8+
case WALL:
9+
return 2;
10+
case EMPTY:
11+
return 1;
12+
case BASE:
13+
return 0;
14+
case TRAP: // 4 in map
15+
return 3;
16+
case SWAMP: // 5 in map
17+
return 4;
18+
default:
19+
return 1;
1820
}
1921
}
2022

2123
public static CellType getCellType(int value) {
2224
switch (value) {
23-
case 0:
24-
return BASE;
25-
case 1:
26-
return EMPTY;
27-
case 2:
28-
return WALL;
29-
default:
30-
return EMPTY;
25+
case 0:
26+
return BASE;
27+
case 1:
28+
return EMPTY;
29+
case 2:
30+
return WALL;
31+
case 4:
32+
return SWAMP;
33+
case 3:
34+
return TRAP;
35+
default:
36+
return EMPTY;
3137
}
3238
}
3339

3440
@Override
3541
public String toString() {
3642
switch (this) {
37-
case WALL:
38-
return "wall";
39-
case EMPTY:
40-
return "empty";
41-
case BASE:
42-
return "base";
43-
default:
44-
return "empty";
43+
case WALL:
44+
return "wall";
45+
case EMPTY:
46+
return "empty";
47+
case BASE:
48+
return "base";
49+
case SWAMP:
50+
return "swamp";
51+
case TRAP:
52+
return "trap";
53+
default:
54+
return "empty";
4555
}
4656
}
4757
}

src/main/java/ir/sharif/aichallenge/server/logic/model/map/GameMap.java

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ir.sharif.aichallenge.server.logic.model.Colony.Colony;
55
import ir.sharif.aichallenge.server.logic.model.Colony.ColonyBuilder;
66
import ir.sharif.aichallenge.server.logic.model.ant.Ant;
7+
import ir.sharif.aichallenge.server.logic.model.ant.AntType;
78
import ir.sharif.aichallenge.server.logic.model.ant.MoveType;
89
import ir.sharif.aichallenge.server.logic.model.cell.BaseCell;
910
import ir.sharif.aichallenge.server.logic.model.cell.Cell;
@@ -13,7 +14,7 @@
1314
import java.util.*;
1415

1516
public class GameMap {
16-
//cells[yAxisLength][xAxisLength]
17+
// cells[yAxisLength][xAxisLength]
1718
private Cell[][] cells;
1819
private int yAxisLength;
1920
private int xAxisLength;
@@ -34,12 +35,13 @@ void setCells(Cell[][] cells, int yAxisLength, int xAxisLength) {
3435
this.cells = cells;
3536
}
3637

37-
void setCell(int xPosition, int yPosition, Cell cell){
38+
void setCell(int xPosition, int yPosition, Cell cell) {
3839
cells[yPosition][xPosition] = cell;
3940
}
4041

4142
public Cell getCell(int xPosition, int yPosition) {
42-
return cells[((yPosition % yAxisLength) + yAxisLength) % yAxisLength][((xPosition % xAxisLength) + xAxisLength) % xAxisLength];
43+
return cells[((yPosition % yAxisLength) + yAxisLength) % yAxisLength][((xPosition % xAxisLength) + xAxisLength)
44+
% xAxisLength];
4345
}
4446

4547
public Cell[] getAntViewableCells(int xPosition, int yPosition) {
@@ -96,8 +98,8 @@ public void addResource(ResourceType resourceType, int resourceAmount, int xPos,
9698
}
9799

98100
public int get‌BorderlessManhattanDistance(int x1, int y1, int x2, int y2) {
99-
return Math.min(Math.abs(x1 - x2), xAxisLength - Math.abs(x1 - x2)) +
100-
Math.min(Math.abs(y1 - y2), yAxisLength - Math.abs(y1 - y2));
101+
return Math.min(Math.abs(x1 - x2), xAxisLength - Math.abs(x1 - x2))
102+
+ Math.min(Math.abs(y1 - y2), yAxisLength - Math.abs(y1 - y2));
101103
}
102104

103105
private int getManhattanDistance(int x1, int y1, int x2, int y2) {
@@ -113,27 +115,34 @@ public List<Cell> getAllCells() {
113115
}
114116

115117
public void changeAntCurrentCell(Ant ant, int moveType) {
118+
if (ant.getInTrap() > 0) {
119+
ant.setIntrap(ant.getInTrap() - 1);
120+
return;
121+
}
116122
int newX = ant.getXPosition();
117123
int newY = ant.getYPosition();
118124
switch (moveType) {
119-
case MoveType.UP:
120-
newY -= 1;
121-
break;
122-
case MoveType.DOWN:
123-
newY += 1;
124-
break;
125-
case MoveType.LEFT:
126-
newX -= 1;
127-
break;
128-
case MoveType.RIGHT:
129-
newX += 1;
130-
break;
131-
default:
132-
return;
125+
case MoveType.UP:
126+
newY -= 1;
127+
break;
128+
case MoveType.DOWN:
129+
newY += 1;
130+
break;
131+
case MoveType.LEFT:
132+
newX -= 1;
133+
break;
134+
case MoveType.RIGHT:
135+
newX += 1;
136+
break;
137+
default:
138+
return;
133139
}
134140
newX = newX % getXAxisLength();
135141
newY = newY % getYAxisLength();
136142
Cell targetCell = getCell(newX, newY);
143+
if (targetCell.cellType == CellType.SWAMP) {
144+
ant.setIntrap(ConstConfigs.SWAMP_TURNS);
145+
}
137146
if (targetCell.cellType == CellType.WALL)
138147
return;
139148

@@ -144,5 +153,15 @@ private void changeAntCurrentCell(Ant ant, Cell targetCell) {
144153
getCell(ant.getXPosition(), ant.getYPosition()).removeAnt(ant);
145154
ant.moveTo(targetCell.getX(), targetCell.getY());
146155
targetCell.addAnt(ant);
156+
if (targetCell.cellType == CellType.TRAP) {
157+
handleTrapCell(ant);
158+
}
159+
}
160+
161+
private void handleTrapCell(Ant ant) {
162+
if (ant.getAntType() == AntType.WORKER && ant.getCarryingResourceAmount() > 0) {
163+
ant.setCarryingResourceAmount(0);
164+
ant.setCarryingResourceType(ResourceType.NONE);
165+
}
147166
}
148167
}

0 commit comments

Comments
 (0)