Skip to content

Instantly share code, notes, and snippets.

@benjaminfspector
Last active March 14, 2017 06:06
Show Gist options
  • Save benjaminfspector/a2b8b88c940abe8f346df62a77e23441 to your computer and use it in GitHub Desktop.
Save benjaminfspector/a2b8b88c940abe8f346df62a77e23441 to your computer and use it in GitHub Desktop.
Halite Random Improvement Sample Code (Java)
This Gist contains the sample code for improving the sample Halite random bot in Java.
// Goes in MyBot.java
import java.util.ArrayList;
public class MyBot {
public static void main(String[] args) throws java.io.IOException {
InitPackage iPackage = Networking.getInit();
int myID = iPackage.myID;
GameMap gameMap = iPackage.map;
Networking.sendInit("MyJavaBot");
while(true) {
ArrayList<Move> moves = new ArrayList<Move>();
gameMap = Networking.getFrame();
for(int y = 0; y < gameMap.height; y++) {
for(int x = 0; x < gameMap.width; x++) {
Site site = gameMap.getSite(new Location(x, y));
if(site.owner == myID) {
moves.add(new Move(new Location(x, y), Direction.randomDirection()));
}
}
}
Networking.sendFrame(moves);
}
}
}
// Goes in MyBot.java
import java.util.ArrayList;
public class MyBot {
public static void main(String[] args) throws java.io.IOException {
InitPackage iPackage = Networking.getInit();
int myID = iPackage.myID;
GameMap gameMap = iPackage.map;
Networking.sendInit("JavaBot");
while(true) {
ArrayList<Move> moves = new ArrayList<Move>();
gameMap = Networking.getFrame();
for(int y = 0; y < gameMap.height; y++) {
for(int x = 0; x < gameMap.width; x++) {
Site site = gameMap.getSite(new Location(x, y));
if(site.owner == myID) {
boolean movedPiece = false;
if(!movedPiece && gameMap.getSite(new Location(x, y)).strength == 0) {
moves.add(new Move(new Location(x, y), Direction.STILL));
movedPiece = true;
}
if(!movedPiece) {
moves.add(new Move(new Location(x, y), Direction.randomDirection()));
movedPiece = true;
}
}
}
}
Networking.sendFrame(moves);
}
}
}
// Goes in MyBot.java
import java.util.ArrayList;
public class MyBot {
public static void main(String[] args) throws java.io.IOException {
InitPackage iPackage = Networking.getInit();
int myID = iPackage.myID;
GameMap gameMap = iPackage.map;
Networking.sendInit("JavaBot");
while(true) {
ArrayList<Move> moves = new ArrayList<Move>();
gameMap = Networking.getFrame();
for(int y = 0; y < gameMap.height; y++) {
for(int x = 0; x < gameMap.width; x++) {
Site site = gameMap.getSite(new Location(x, y));
if(site.owner == myID) {
boolean movedPiece = false;
if(!movedPiece && gameMap.getSite(new Location(x, y)).strength < gameMap.getSite(new Location(x, y)).production * 5) {
moves.add(new Move(new Location(x, y), Direction.STILL));
movedPiece = true;
}
if(!movedPiece) {
moves.add(new Move(new Location(x, y), Direction.randomDirection()));
movedPiece = true;
}
}
}
}
Networking.sendFrame(moves);
}
}
}
// Goes in MyBot.java
import java.util.ArrayList;
import java.util.Random;
public class MyBot {
public static void main(String[] args) throws java.io.IOException {
InitPackage iPackage = Networking.getInit();
int myID = iPackage.myID;
GameMap gameMap = iPackage.map;
Networking.sendInit("JavaBot");
Random rand = new Random();
while(true) {
ArrayList<Move> moves = new ArrayList<Move>();
gameMap = Networking.getFrame();
for(int y = 0; y < gameMap.height; y++) {
for(int x = 0; x < gameMap.width; x++) {
Site site = gameMap.getSite(new Location(x, y));
if(site.owner == myID) {
boolean movedPiece = false;
if(!movedPiece && gameMap.getSite(new Location(x, y)).strength < gameMap.getSite(new Location(x, y)).production * 5) {
moves.add(new Move(new Location(x, y), Direction.STILL));
movedPiece = true;
}
if(!movedPiece) {
moves.add(new Move(new Location(x, y), rand.nextBoolean() ? Direction.NORTH : Direction.WEST));
movedPiece = true;
}
}
}
}
Networking.sendFrame(moves);
}
}
}
// Goes in MyBot.java
import java.util.ArrayList;
import java.util.Random;
public class MyBot {
public static void main(String[] args) throws java.io.IOException {
InitPackage iPackage = Networking.getInit();
int myID = iPackage.myID;
GameMap gameMap = iPackage.map;
Networking.sendInit("JavaBot");
Random rand = new Random();
while(true) {
ArrayList<Move> moves = new ArrayList<Move>();
gameMap = Networking.getFrame();
for(int y = 0; y < gameMap.height; y++) {
for(int x = 0; x < gameMap.width; x++) {
Site site = gameMap.getSite(new Location(x, y));
if(site.owner == myID) {
boolean movedPiece = false;
for(Direction d : Direction.CARDINALS) {
if(gameMap.getSite(new Location(x, y), d).owner != myID && gameMap.getSite(new Location(x, y), d).strength < gameMap.getSite(new Location(x, y)).strength) {
moves.add(new Move(new Location(x, y), d));
movedPiece = true;
break;
}
}
if(!movedPiece && gameMap.getSite(new Location(x, y)).strength < gameMap.getSite(new Location(x, y)).production * 5) {
moves.add(new Move(new Location(x, y), Direction.STILL));
movedPiece = true;
}
if(!movedPiece) {
moves.add(new Move(new Location(x, y), rand.nextBoolean() ? Direction.NORTH : Direction.WEST));
movedPiece = true;
}
}
}
}
Networking.sendFrame(moves);
}
}
}
@Daniel-Wang
Copy link

For me, moves.insert was unrecognised so I replaced it with 'add' in line 42

@truell20
Copy link

truell20 commented Nov 24, 2016

Thanks for the tips @Daniel-Wang, @Zupami. After you reported that bug, we fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment