Last active
March 14, 2017 06:06
-
-
Save benjaminfspector/a2b8b88c940abe8f346df62a77e23441 to your computer and use it in GitHub Desktop.
Halite Random Improvement Sample Code (Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This Gist contains the sample code for improving the sample Halite random bot in Java. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the tips @Daniel-Wang, @Zupami. After you reported that bug, we fixed it.