Skip to content

Commit 6242d08

Browse files
author
octavian.grosu
committed
modificari
1 parent fc3e950 commit 6242d08

File tree

9 files changed

+690
-107
lines changed

9 files changed

+690
-107
lines changed

MyBot.java

Lines changed: 76 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,106 @@
1+
12
import hlt.*;
3+
//import hlt.Victory;
24

35
import java.util.*;
6+
import java.util.stream.Collectors;
47

58
public class MyBot {
69

7-
/**
8-
* Returneaza cea mai buna directie in care sa mearga nava - adancime 1
9-
* @param ship nava pentru care se aplica greedy
10-
* @param gameMap harta jocului
11-
* @return pozitia pe care urmeaza sa mearga
12-
*/
13-
public static Position Greedy (final Ship ship, final GameMap gameMap) {
14-
Position initial = ship.position;
15-
16-
Position[] positions =
17-
{
18-
new Position(initial.x - 1, initial.y), // sus
19-
new Position(initial.x, initial.y + 1), // dreapta
20-
new Position(initial.x + 1, initial.y), // jos
21-
new Position(initial.x, initial.y - 1) // stanga
22-
};
23-
24-
Arrays.sort(positions, new Comparator<Position>() {
25-
@Override
26-
public int compare(Position o1, Position o2) {
27-
return gameMap.at(o1).halite - gameMap.at(o2).halite;
28-
}
29-
});
30-
return positions[0];
31-
}
32-
33-
/**
34-
* Functie care returneaza comenzi in legatura cu jocul in general
35-
* @param game referinta la joc
36-
* @return comanda
37-
*/
38-
public static Command DecisionGame (final Game game) {
39-
40-
// TODO daca playerul are mai mult de 3k halite (ales arbitrar) si runda e sub 200
41-
// TODO sa se faca o nava
42-
if (game.me.halite > 3000 && game.turnNumber < Constants.MAX_TURNS / 2) {
43-
return null;
44-
}
45-
46-
// TODO daca conditia e adevarata sa se creeze un drop-off + conditia daca o nava a ajuns
47-
// TODo la mai mult de ~8 pozitii departare
48-
if (game.me.halite > 8000 && game.turnNumber < Constants.MAX_TURNS * 3 / 4) {
49-
return null;
50-
}
51-
52-
return null;
53-
}
54-
55-
/**
56-
* Functie care returneaza comenzi in legatura cu o singura nava
57-
* @param ship nava
58-
* @param gameMap harta jocului
59-
* @return comanda
60-
*/
61-
public static Command DecisionShip (final Ship ship, final GameMap gameMap) {
62-
63-
// TODO daca are mai mult de 750 halite - sa mearga la dropoff
64-
if (ship.halite > (Constants.MAX_HALITE * 3 / 4)) {
65-
66-
return null;
67-
}
68-
69-
// TODO daca pozitia actuala nu mai are destule resurse sa mearga in alta pozitie
70-
// TODO altfel ramane pe pozitia actuala
71-
if (gameMap.cells[ship.position.x][ship.position.y].halite < (Constants.MAX_HALITE / 4)) {
72-
73-
// TODO daca pozitia actuala are mai putine resurse decat cea mai buna alta
74-
// TODO pozitie sa ramana aici (ca pe else)
75-
int result = 0;
76-
if (gameMap.cells[ship.position.x][ship.position.y].halite > result) {
77-
78-
return null;
79-
}
80-
81-
return null;
82-
} else {
83-
84-
return null;
85-
}
86-
}
87-
8810
public static void main(final String[] args) {
11+
Victory victory = new Victory();
8912
final long rngSeed;
9013
if (args.length > 1) {
9114
rngSeed = Integer.parseInt(args[1]);
9215
} else {
93-
rngSeed = System.nanoTime();
16+
// constant seed number for constant results and a deterministic solution over all
17+
rngSeed = 42;
9418
}
95-
final Random rng = new Random(rngSeed);
19+
victory.rnd.setSeed(rngSeed);
9620

97-
Game game = new Game();
9821
// At this point "game" variable is populated with initial map data.
9922
// This is a good place to do computationally expensive start-up pre-processing.
10023
// As soon as you call "ready" function below, the 2 second per turn timer will start.
24+
victory.game.ready("QuantumGreedy");
25+
// maximum number of ships to be created
26+
final int maxShips = (int) Math.sqrt(victory.game.gameMap.height * victory.game.gameMap.width) / 2;
10127

102-
game.ready("MyJavaBot");
28+
Log.log("Successfully created bot! My Player ID is " + victory.game.myId
29+
+ ". Bot rng seed is " + rngSeed + ".");
10330

104-
Log.log("Successfully created bot! My Player ID is " + game.myId +
105-
". Bot rng seed is " + rngSeed + ".");
10631
for (;;) {
107-
game.updateFrame();
108-
final Player me = game.me;
109-
final GameMap gameMap = game.gameMap;
32+
victory.game.updateFrame();
33+
final Player me = victory.game.me;
34+
final GameMap gameMap = victory.game.gameMap;
11035

11136
final ArrayList<Command> commandQueue = new ArrayList<>();
37+
// processes a vip list with the top% positions with halite on the
38+
// map
39+
victory.scanVipPositions();
40+
// variable for knowing if we have queued a ship spawn command in
41+
// this round
42+
boolean willSpawn = false;
43+
// checks whether we hit the maximum limit of ships to build
44+
if (me.ships.size() < maxShips
45+
// and we have enough halite to build a new ship
46+
&& me.halite >= Constants.SHIP_COST
47+
// and the position of the shipyard is not occupied
48+
&& !gameMap.at(me.shipyard).isOccupied()
49+
// and there is no reason to avoid building a ship (for
50+
// better scoring)
51+
&& !victory.shouldAvoidBuildingShips(maxShips)) {
52+
me.halite -= Constants.SHIP_COST;
53+
commandQueue.add(me.shipyard.spawn());
54+
willSpawn = true;
55+
}
11256

113-
Command command = DecisionGame(game); //
114-
115-
// TODO schimbat cu logica noua
57+
// attempt to compute the action of each ship
11658
for (final Ship ship : me.ships.values()) {
117-
if (gameMap.at(ship).halite < Constants.MAX_HALITE / 10 || ship.isFull()) {
118-
final Direction randomDirection = Direction.ALL_CARDINALS.get(rng.nextInt(4));
119-
commandQueue.add(ship.move(randomDirection));
120-
} else {
59+
// checks if the ship should be turned into a drop off
60+
if (victory.shouldTurnIntoDropOff(ship)
61+
// and whether we have enough halite for transformation
62+
&& me.halite > Constants.DROPOFF_COST) {
63+
me.halite -= Constants.DROPOFF_COST;
64+
commandQueue.add(ship.makeDropoff());
65+
continue;
66+
}
67+
68+
// the direction which does not lead to a crash, initially nil
69+
Direction goodDir = null;
70+
// findDirections returns a "personalized" list, for every ship,
71+
// of close positions from the vips list
72+
for (Direction dir : victory.findDirections(ship)) {
73+
// avoid going into a direction that could lead to a crash
74+
if (!victory.canCrash(ship, dir)) {
75+
goodDir = dir;
76+
break;
77+
}
78+
}
79+
80+
// if no good direction was found, stay still
81+
if (goodDir == null) {
12182
commandQueue.add(ship.stayStill());
83+
84+
continue;
12285
}
123-
}
12486

125-
if (
126-
game.turnNumber <= 200 &&
127-
me.halite >= Constants.SHIP_COST &&
128-
!gameMap.at(me.shipyard).isOccupied())
129-
{
130-
commandQueue.add(me.shipyard.spawn());
87+
final Position pos = ship.position.directionalOffset(goodDir);
88+
// the ship will stay still if there is halite to collect on
89+
// this position and it is not full
90+
if (gameMap.at(ship.position).halite > 64 && !ship.isFull()
91+
// or if there will be a new spawned ship and there
92+
// could be a collision
93+
|| me.shipyard.position.equals(pos) && willSpawn
94+
// or if it hasn't got enough halite to move
95+
|| ship.halite - gameMap.at(ship).halite / 10 < 0) {
96+
commandQueue.add(ship.stayStill());
97+
} else {
98+
// otherwise move in the `best` direction
99+
commandQueue.add(ship.move(gameMap.naiveNavigate(ship, pos)));
100+
}
131101
}
132102

133-
game.endTurn(commandQueue);
103+
victory.game.endTurn(commandQueue);
134104
}
135105
}
136-
}
106+
}

MyBotUtils.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import hlt.Direction;
2+
import hlt.GameMap;
3+
import hlt.Position;
4+
import hlt.Ship;
5+
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
9+
import static hlt.Direction.NORTH;
10+
11+
public final class MyBotUtils {
12+
/**
13+
* Returneaza cea mai buna directie in care sa mearga nava - adancime 1
14+
* @param ship nava pentru care se aplica greedy
15+
* @param gameMap harta jocului
16+
* @return pozitia pe care urmeaza sa mearga
17+
*/
18+
public static Position Greedy (final Ship ship, final GameMap gameMap) {
19+
Position initial = ship.position;
20+
21+
Position[] positions =
22+
{
23+
new Position(initial.x - 1, initial.y), // sus
24+
new Position(initial.x, initial.y + 1), // dreapta
25+
new Position(initial.x + 1, initial.y), // jos
26+
new Position(initial.x, initial.y - 1) // stanga
27+
};
28+
Arrays.sort(positions,
29+
(Position o1, Position o2) -> gameMap.at(o2).halite - gameMap.at(o1).halite);
30+
for (int i = 0; i < positions.length; ++i) {
31+
if (!gameMap.at(positions[i]).isOccupied()) {
32+
return positions[i];
33+
}
34+
}
35+
36+
return initial;
37+
}
38+
39+
/**
40+
* Primeste o sursa si o destinatie si intoarce directia in care ar trebui sa mearga ca sa
41+
* ajunga destinatie pentru harta jocului (y, x)
42+
* @param source sursa
43+
* @param destination destinatie
44+
* @return directia
45+
*/
46+
public static Direction PositionToDirection (final Position source,
47+
final Position destination) {
48+
if (source.x == destination.x && source.y == destination.y) {
49+
return Direction.STILL;
50+
}
51+
52+
if (source.x == destination.x) {
53+
if (source.y > destination.y) {
54+
return NORTH;
55+
} else {
56+
return Direction.SOUTH;
57+
}
58+
}
59+
60+
if (source.x < destination.x && source.y == destination.y) {
61+
return Direction.EAST;
62+
} else {
63+
return Direction.WEST;
64+
}
65+
}
66+
67+
/**
68+
* Intoarce pozitia pe harta (x, y) unde o sa fie destinatia
69+
* @param source locul de und se pleaca
70+
* @param direction directia in care se pleaca
71+
* @return pozitia pe care o sa fie
72+
*/
73+
public static Position DirectionToPosition (final Position source,
74+
final Direction direction) {
75+
switch(direction) {
76+
case NORTH:
77+
return new Position(source.x, source.y - 1);
78+
case SOUTH:
79+
return new Position(source.x, source.y + 1);
80+
case EAST:
81+
return new Position(source.x + 1, source.y);
82+
case WEST:
83+
return new Position(source.x - 1, source.y);
84+
case STILL:
85+
return source;
86+
default:
87+
return null;
88+
}
89+
}
90+
}

Pair.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
/**
8+
*
9+
* @author victoria
10+
*/
11+
public class Pair<K, V> {
12+
private K first;
13+
private V second;
14+
15+
public Pair(K first, V second) {
16+
this.first = first;
17+
this.second = second;
18+
}
19+
20+
public K getFirst() {
21+
return first;
22+
}
23+
24+
public V getSecond() {
25+
return second;
26+
}
27+
}

0 commit comments

Comments
 (0)