Skip to content

Commit 2592fbd

Browse files
Merge pull request #34 from SharifAIChallenge/randomresource
Randomresource
2 parents 9552d7f + e4bbb07 commit 2592fbd

File tree

5 files changed

+65
-18
lines changed

5 files changed

+65
-18
lines changed

map.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@
3737
{ "row": 0, "col": 30, "cell_type": 3, "rec1": 0, "rec2": 0 },
3838
{ "row": 0, "col": 31, "cell_type": 3, "rec1": 0, "rec2": 0 },
3939
{ "row": 1, "col": 0, "cell_type": 3, "rec1": 0, "rec2": 0 },
40-
{ "row": 1, "col": 1, "cell_type": 2, "rec1": 0, "rec2": 0 },
40+
{ "row": 1, "col": 1, "cell_type": 10, "rec1": 44, "rec2": 0 },
4141
{ "row": 1, "col": 2, "cell_type": 2, "rec1": 40, "rec2": 0 },
42-
{ "row": 1, "col": 3, "cell_type": 2, "rec1": 0, "rec2": 0 },
42+
{ "row": 1, "col": 3, "cell_type": 10, "rec1": 0, "rec2": 60 },
4343
{ "row": 1, "col": 4, "cell_type": 2, "rec1": 0, "rec2": 0 },
4444
{ "row": 1, "col": 5, "cell_type": 2, "rec1": 0, "rec2": 0 },
4545
{ "row": 1, "col": 6, "cell_type": 2, "rec1": 0, "rec2": 0 },
4646
{ "row": 1, "col": 7, "cell_type": 2, "rec1": 0, "rec2": 0 },
4747
{ "row": 1, "col": 8, "cell_type": 2, "rec1": 0, "rec2": 0 },
48-
{ "row": 1, "col": 9, "cell_type": 2, "rec1": 0, "rec2": 0 },
49-
{ "row": 1, "col": 10, "cell_type": 2, "rec1": 0, "rec2": 0 },
48+
{ "row": 1, "col": 9, "cell_type": 12, "rec1": 33, "rec2": 0 },
49+
{ "row": 1, "col": 10, "cell_type": 12, "rec1": 33, "rec2": 0 },
5050
{ "row": 1, "col": 11, "cell_type": 2, "rec1": 0, "rec2": 0 },
5151
{ "row": 1, "col": 12, "cell_type": 2, "rec1": 0, "rec2": 0 },
5252
{ "row": 1, "col": 13, "cell_type": 2, "rec1": 0, "rec2": 0 },
@@ -811,7 +811,7 @@
811811
{ "row": 25, "col": 4, "cell_type": 2, "rec1": 0, "rec2": 0 },
812812
{ "row": 25, "col": 5, "cell_type": 2, "rec1": 0, "rec2": 0 },
813813
{ "row": 25, "col": 6, "cell_type": 2, "rec1": 0, "rec2": 0 },
814-
{ "row": 25, "col": 7, "cell_type": 2, "rec1": 0, "rec2": 0 },
814+
{ "row": 25, "col": 7, "cell_type": 4, "rec1": 0, "rec2": 0 },
815815
{ "row": 25, "col": 8, "cell_type": 2, "rec1": 0, "rec2": 0 },
816816
{ "row": 25, "col": 9, "cell_type": 2, "rec1": 0, "rec2": 0 },
817817
{ "row": 25, "col": 10, "cell_type": 3, "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": 10, "rec2": 0 },
833+
{ "row": 25, "col": 26, "cell_type": 4, "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 },
@@ -856,7 +856,7 @@
856856
{ "row": 26, "col": 17, "cell_type": 2, "rec1": 0, "rec2": 0 },
857857
{ "row": 26, "col": 18, "cell_type": 2, "rec1": 0, "rec2": 0 },
858858
{ "row": 26, "col": 19, "cell_type": 2, "rec1": 0, "rec2": 0 },
859-
{ "row": 26, "col": 20, "cell_type": 2, "rec1": 0, "rec2": 0 },
859+
{ "row": 26, "col": 20, "cell_type": 5, "rec1": 0, "rec2": 0 },
860860
{ "row": 26, "col": 21, "cell_type": 2, "rec1": 0, "rec2": 0 },
861861
{ "row": 26, "col": 22, "cell_type": 2, "rec1": 0, "rec2": 0 },
862862
{ "row": 26, "col": 23, "cell_type": 3, "rec1": 0, "rec2": 0 },
@@ -870,11 +870,11 @@
870870
{ "row": 26, "col": 31, "cell_type": 3, "rec1": 0, "rec2": 0 },
871871
{ "row": 27, "col": 0, "cell_type": 3, "rec1": 0, "rec2": 0 },
872872
{ "row": 27, "col": 1, "cell_type": 2, "rec1": 0, "rec2": 0 },
873-
{ "row": 27, "col": 2, "cell_type": 2, "rec1": 0, "rec2": 0 },
873+
{ "row": 27, "col": 2, "cell_type": 5, "rec1": 0, "rec2": 0 },
874874
{ "row": 27, "col": 3, "cell_type": 2, "rec1": 0, "rec2": 0 },
875875
{ "row": 27, "col": 4, "cell_type": 2, "rec1": 0, "rec2": 0 },
876876
{ "row": 27, "col": 5, "cell_type": 2, "rec1": 0, "rec2": 0 },
877-
{ "row": 27, "col": 6, "cell_type": 2, "rec1": 0, "rec2": 0 },
877+
{ "row": 27, "col": 6, "cell_type": 5, "rec1": 0, "rec2": 0 },
878878
{ "row": 27, "col": 7, "cell_type": 2, "rec1": 0, "rec2": 0 },
879879
{ "row": 27, "col": 8, "cell_type": 2, "rec1": 0, "rec2": 0 },
880880
{ "row": 27, "col": 9, "cell_type": 2, "rec1": 0, "rec2": 0 },

src/main/java/ir/sharif/aichallenge/server/logic/handlers/AttackHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ private void runAttack(int fromXPosition, int fromYPosition, int damage, int att
102102
}
103103
}
104104
Ant defender = ants.get(index);
105+
if (attackerId < 0) {
106+
// base attack to all ants of a cell
107+
for (Ant defAnt : map.getCell(defender.getXPosition(), defender.getYPosition()).getAnts()) {
108+
defAnt.decreaseHealth(damage);
109+
AttackSummary attackSummary = new AttackSummary(attackerId, defAnt.getId(), fromYPosition,
110+
fromXPosition, defAnt.getYPosition(), defAnt.getXPosition());
111+
attackSummaries.add(attackSummary);
112+
}
113+
return;
114+
}
105115
defender.decreaseHealth(damage);
106116
AttackSummary attackSummary = new AttackSummary(attackerId, defender.getId(), fromYPosition, fromXPosition,
107117
defender.getYPosition(), defender.getXPosition());

src/main/java/ir/sharif/aichallenge/server/logic/model/Game.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void passTurn(Map<String, List<ClientMessageInfo>> messages) {
6767
removeDeadAntsNewMessages(messages);
6868
handleChatMessages(messages);
6969
handleAntsMove(messages);
70+
map.getAllCells().forEach(cell -> cell.renew(map, currentTurn)); //for future resources
7071
map.getAllCells().forEach(Cell::manageResources);
7172
/*
7273
* if (isFinished()) { Colony winnerColony = gameJudge.getWinner();

src/main/java/ir/sharif/aichallenge/server/logic/model/cell/Cell.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package ir.sharif.aichallenge.server.logic.model.cell;
22

3+
import ir.sharif.aichallenge.server.logic.config.ConstConfigs;
4+
import ir.sharif.aichallenge.server.logic.model.ant.Ant;
5+
import ir.sharif.aichallenge.server.logic.model.ant.AntType;
6+
import ir.sharif.aichallenge.server.logic.model.map.GameMap;
7+
38
import java.util.ArrayList;
49
import java.util.Collections;
510
import java.util.List;
6-
import java.util.Random;
711
import java.util.stream.Collectors;
812

9-
import ir.sharif.aichallenge.server.logic.config.ConstConfigs;
10-
import ir.sharif.aichallenge.server.logic.model.ant.Ant;
11-
import ir.sharif.aichallenge.server.logic.model.ant.AntType;
12-
1313
public class Cell {
1414
private int xPosition;
1515
private int yPosition;
1616
public CellType cellType;
1717
private ResourceType resourceType;
1818
private int resourceAmount;
1919
private List<Ant> ants;
20+
private int toBeAddedTurn = -1;
21+
private ResourceType toBeAddedResourceType;
22+
private int toBeAddedResourceAmount;
2023

2124
public Cell(int xPosition, int yPosition, CellType cellType, ResourceType resourceType, int resourceAmount) {
2225
this.xPosition = xPosition;
@@ -27,6 +30,23 @@ public Cell(int xPosition, int yPosition, CellType cellType, ResourceType resour
2730
ants = new ArrayList<>();
2831
}
2932

33+
// for future resource
34+
public Cell(int xPosition, int yPosition, ResourceType resourceType, int resourceAmount, int toBeAddedTurn) {
35+
this(xPosition, yPosition, CellType.EMPTY, ResourceType.NONE, 0);
36+
this.toBeAddedResourceType = resourceType;
37+
this.resourceType = ResourceType.NONE;
38+
this.resourceAmount = 0;
39+
this.toBeAddedResourceAmount = resourceAmount;
40+
this.toBeAddedTurn = toBeAddedTurn;
41+
}
42+
43+
public void renew(GameMap map, int currentTurn) {
44+
if (currentTurn != this.toBeAddedTurn)
45+
return;
46+
map.addResource(toBeAddedResourceType, toBeAddedResourceAmount, xPosition, yPosition);
47+
this.toBeAddedTurn = -1;
48+
}
49+
3050
public void setResourceType(ResourceType resourceType) {
3151
this.resourceType = resourceType;
3252
}
@@ -93,14 +113,14 @@ public void manageResources() {
93113
if (cellType == CellType.WALL || resourceType == ResourceType.NONE)
94114
return;
95115
List<Ant> freeWorkerAnts = getWorkerAnts().stream()
96-
.filter(x -> x.getCarryingResourceType() == ResourceType.NONE ||
97-
(x.getCarryingResourceType() == this.getResourceType() &&
98-
x.getCarryingResourceAmount() < ConstConfigs.WORKER_MAX_CARRYING_RESOURCE_AMOUNT))
116+
.filter(x -> x.getCarryingResourceType() == ResourceType.NONE
117+
|| (x.getCarryingResourceType() == this.getResourceType()
118+
&& x.getCarryingResourceAmount() < ConstConfigs.WORKER_MAX_CARRYING_RESOURCE_AMOUNT))
99119
.collect(Collectors.toList());
100120

101121
Collections.shuffle(freeWorkerAnts);
102122
for (Ant ant : freeWorkerAnts) {
103-
if (this.getResourceAmount() <= 0){
123+
if (this.getResourceAmount() <= 0) {
104124
break;
105125
}
106126

src/main/java/ir/sharif/aichallenge/server/logic/utility/JsonUtility.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
import java.io.FileReader;
1616
import java.io.IOException;
17+
import java.util.HashMap;
18+
import java.util.Map;
19+
import java.util.concurrent.ThreadLocalRandom;
1720

1821
public class JsonUtility {
1922

@@ -58,6 +61,8 @@ public static ExternalMap readMapFromFile(String fileName) throws IOException, P
5861
}
5962
}
6063

64+
private static HashMap<Integer, Integer> cellTypeToTurn = new HashMap<Integer, Integer>();
65+
6166
private static Cell parseCellObject(JSONObject cell) {
6267
// Get cell first name
6368
int yPosition = ((Long) cell.get("row")).intValue();
@@ -95,6 +100,17 @@ private static Cell parseCellObject(JSONObject cell) {
95100
if (cellType == CellType.BASE) {
96101
return new BaseCell(xPosition, yPosition);
97102
}
103+
// future resource
104+
if (cell_type_value > 6) {
105+
int toBeAddedTurn = ThreadLocalRandom.current().nextInt(1, ConstConfigs.GAME_MAXIMUM_TURN_COUNT);
106+
if (cellTypeToTurn.keySet().contains(cell_type_value)) {
107+
toBeAddedTurn = cellTypeToTurn.get(cell_type_value);
108+
} else {
109+
cellTypeToTurn.put(cell_type_value, toBeAddedTurn);
110+
}
111+
// Log.i("JsonUtility", "cell will be added in " + toBeAddedTurn);
112+
return new Cell(xPosition, yPosition, resourceType, resourceAmount, toBeAddedTurn);
113+
}
98114
return new Cell(xPosition, yPosition, cellType, resourceType, resourceAmount);
99115
}
100116
}

0 commit comments

Comments
 (0)