Permalink
Browse files

version 3 - smart but buggy

  • Loading branch information...
0 parents commit f04740b961c5a2da2f50abf8f0f2e2bb8fa990e1 @apinkin committed Sep 14, 2010
Showing with 5,616 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +71 −0 MyBot.py
  3. +8 −0 README
  4. +108 −0 TCP.java
  5. +39 −0 batch.sh
  6. +1 −0 dual.bat
  7. BIN example_bots/BullyBot.jar
  8. +65 −0 example_bots/BullyBot.java
  9. BIN example_bots/DualBot.jar
  10. +86 −0 example_bots/DualBot.java
  11. +65 −0 example_bots/ExpandBot.java
  12. +92 −0 example_bots/Fleet.java
  13. +77 −0 example_bots/Planet.java
  14. +319 −0 example_bots/PlanetWars.java
  15. BIN example_bots/ProspectorBot.jar
  16. +65 −0 example_bots/ProspectorBot.java
  17. BIN example_bots/RageBot.jar
  18. +52 −0 example_bots/RageBot.java
  19. BIN example_bots/RandomBot.jar
  20. +58 −0 example_bots/RandomBot.java
  21. +26 −0 loggingbot.py
  22. +23 −0 maps/map1.txt
  23. +23 −0 maps/map10.txt
  24. +23 −0 maps/map100.txt
  25. +23 −0 maps/map11.txt
  26. +23 −0 maps/map12.txt
  27. +23 −0 maps/map13.txt
  28. +23 −0 maps/map14.txt
  29. +23 −0 maps/map15.txt
  30. +23 −0 maps/map16.txt
  31. +23 −0 maps/map17.txt
  32. +23 −0 maps/map18.txt
  33. +23 −0 maps/map19.txt
  34. +23 −0 maps/map2.txt
  35. +23 −0 maps/map20.txt
  36. +23 −0 maps/map21.txt
  37. +23 −0 maps/map22.txt
  38. +23 −0 maps/map23.txt
  39. +23 −0 maps/map24.txt
  40. +23 −0 maps/map25.txt
  41. +23 −0 maps/map26.txt
  42. +23 −0 maps/map27.txt
  43. +23 −0 maps/map28.txt
  44. +23 −0 maps/map29.txt
  45. +23 −0 maps/map3.txt
  46. +23 −0 maps/map30.txt
  47. +23 −0 maps/map31.txt
  48. +23 −0 maps/map32.txt
  49. +23 −0 maps/map33.txt
  50. +23 −0 maps/map34.txt
  51. +23 −0 maps/map35.txt
  52. +23 −0 maps/map36.txt
  53. +23 −0 maps/map37.txt
  54. +23 −0 maps/map38.txt
  55. +23 −0 maps/map39.txt
  56. +23 −0 maps/map4.txt
  57. +23 −0 maps/map40.txt
  58. +23 −0 maps/map41.txt
  59. +23 −0 maps/map42.txt
  60. +23 −0 maps/map43.txt
  61. +23 −0 maps/map44.txt
  62. +23 −0 maps/map45.txt
  63. +23 −0 maps/map46.txt
  64. +23 −0 maps/map47.txt
  65. +23 −0 maps/map48.txt
  66. +23 −0 maps/map49.txt
  67. +23 −0 maps/map5.txt
  68. +23 −0 maps/map50.txt
  69. +23 −0 maps/map51.txt
  70. +23 −0 maps/map52.txt
  71. +23 −0 maps/map53.txt
  72. +23 −0 maps/map54.txt
  73. +23 −0 maps/map55.txt
  74. +23 −0 maps/map56.txt
  75. +23 −0 maps/map57.txt
  76. +23 −0 maps/map58.txt
  77. +23 −0 maps/map59.txt
  78. +23 −0 maps/map6.txt
  79. +23 −0 maps/map60.txt
  80. +23 −0 maps/map61.txt
  81. +23 −0 maps/map62.txt
  82. +23 −0 maps/map63.txt
  83. +23 −0 maps/map64.txt
  84. +23 −0 maps/map65.txt
  85. +23 −0 maps/map66.txt
  86. +23 −0 maps/map67.txt
  87. +23 −0 maps/map68.txt
  88. +23 −0 maps/map69.txt
  89. +23 −0 maps/map7.txt
  90. +23 −0 maps/map70.txt
  91. +23 −0 maps/map71.txt
  92. +23 −0 maps/map72.txt
  93. +23 −0 maps/map73.txt
  94. +23 −0 maps/map74.txt
  95. +23 −0 maps/map75.txt
  96. +23 −0 maps/map76.txt
  97. +23 −0 maps/map77.txt
  98. +23 −0 maps/map78.txt
  99. +23 −0 maps/map79.txt
  100. +23 −0 maps/map8.txt
  101. +23 −0 maps/map80.txt
  102. +23 −0 maps/map81.txt
  103. +23 −0 maps/map82.txt
  104. +23 −0 maps/map83.txt
  105. +23 −0 maps/map84.txt
  106. +23 −0 maps/map85.txt
  107. +23 −0 maps/map86.txt
  108. +23 −0 maps/map87.txt
  109. +23 −0 maps/map88.txt
  110. +23 −0 maps/map89.txt
  111. +23 −0 maps/map9.txt
  112. +23 −0 maps/map90.txt
  113. +23 −0 maps/map91.txt
  114. +23 −0 maps/map92.txt
  115. +23 −0 maps/map93.txt
  116. +23 −0 maps/map94.txt
  117. +23 −0 maps/map95.txt
  118. +23 −0 maps/map96.txt
  119. +23 −0 maps/map97.txt
  120. +23 −0 maps/map98.txt
  121. +23 −0 maps/map99.txt
  122. +20 −0 myuniversebot.py
  123. +51 −0 planetwars/__init__.py
  124. +7 −0 planetwars/basebot.py
  125. +115 −0 planetwars/compat.py
  126. +19 −0 planetwars/fleet.py
  127. +125 −0 planetwars/game.py
  128. +69 −0 planetwars/planet.py
  129. +49 −0 planetwars/planet2.py
  130. +41 −0 planetwars/player.py
  131. +210 −0 planetwars/universe.py
  132. +66 −0 planetwars/universe2.py
  133. +110 −0 planetwars/util.py
  134. +1 −0 rage.bat
  135. +1 −0 random.bat
  136. +12 −0 stillstupidbot.py
  137. +11 −0 stupidbot.py
  138. BIN tools/PlayGame.jar
  139. BIN tools/ShowGame.jar
  140. +7 −0 visualizer/css/handheld.css
  141. +138 −0 visualizer/css/style.css
  142. +69 −0 visualizer/generated.htm
  143. +72 −0 visualizer/index.php
  144. +9 −0 visualizer/input
  145. +328 −0 visualizer/js/dd_belatedpng.js
  146. +154 −0 visualizer/js/jquery-1.4.2.min.js
  147. +28 −0 visualizer/js/modernizr-1.5.min.js
  148. +407 −0 visualizer/js/visualizer.js
  149. +22 −0 visualizer/readme.md
  150. +38 −0 visualizer/visualize_localy.py
@@ -0,0 +1,5 @@
+my_planet_wars.*
+planetwars/*.pyc
+*.class
+*.log
+log.txt
@@ -0,0 +1,71 @@
+from planetwars import BaseBot, Game
+from planetwars.universe2 import Universe2
+from planetwars.planet2 import Planet2
+from planetwars.universe import player
+from logging import getLogger
+
+log = getLogger(__name__)
+
+# DONE send as many ships as needed
+# DONE send only if my planet is not endangered
+# DONE defense first
+# DONE neutral planets close distance only
+class MyBot(BaseBot):
+
+ def endangered(self, p, threshold):
+ maxdist = -1
+ attackers = p.attacking_fleets
+ if len(attackers) == 0:
+ return (False,0)
+ for a in attackers:
+ maxdist = max(maxdist,a.turns_remaining)
+
+ reinforcements = p.reinforcement_fleets
+ for r in reinforcements:
+ maxdist = max(maxdist,r.turns_remaining)
+
+ fp = p.in_future(maxdist)
+
+ if fp.owner != player.ME or fp.ship_count <= threshold:
+ log.info("Endangered %s" % p)
+ return (True, fp.ship_count)
+ else:
+ return (False,0)
+
+ def do_turn(self):
+ log.info("I'm starting my turn")
+
+ log.info("Defense")
+ mp = self.universe.my_planets
+ if len(mp) > 0:
+ for dest in mp:
+ se = self.endangered(dest, 0)
+ if se[0]:
+ ships_needed = se[1]
+ msp = sorted(self.universe.my_planets, key=lambda p : p.distance(dest))
+
+ for source in msp:
+ if source.id != dest.id and ships_needed <= source.ship_count and (not self.endangered(source, source.ship_count - ships_needed)):
+ source.send_fleet(dest, ships_needed)
+ break
+
+ log.info("Offense")
+ ewp = self.universe.weakest_planets(player.NOT_ME,10)
+ if len(ewp) > 0:
+ for dest in ewp:
+ #msp = self.universe.my_strongest_planets(10)
+ msp = sorted(self.universe.my_planets, key=lambda p : p.distance(dest))
+ for source in msp:
+ dist = source.distance(dest)
+ if dest.owner == player.NOBODY and len(self.universe.enemy_planets) > 0 and dist > (dest.distance(self.universe.enemies_strongest_planet)*1.2):
+ continue
+
+ fdest = dest.in_future(dist)
+ ships_needed = fdest.ship_count + 1
+ if fdest.owner != player.ME and ships_needed <= source.ship_count and (not self.endangered(source, source.ship_count - ships_needed)[0]):
+ source.send_fleet(dest, ships_needed)
+ break
+
+
+
+Game(MyBot, universe_class=Universe2, planet_class=Planet2)
8 README
@@ -0,0 +1,8 @@
+BIG FAT WARNING: In the latest release self.universe.planets and universe.fleets are
+no longer dicts but also sets (to be consistent with .my_planets etc.).
+So if you are using those in your code, update it accordingly!
+
+
+This is an alternative python interface to the Google AI-Contest (http://ai-contest.com).
+
+See the example bots and planetwars/universe.py and planetwars/game.py for a bit of documentation.
108 TCP.java
@@ -0,0 +1,108 @@
+ import java.util.*;
+ import java.io.*;
+ import java.net.*;
+
+ public class TCP {
+
+ public static void main(String args[]) {
+ // Check the command-line arguments.
+ if (args.length < 5) {
+ System.err.println("ERROR: wrong number of command-line arguments.");
+ System.err.println("USAGE: TCP2 ip port username bot");
+ System.exit(1);
+ }
+
+ // Get the server info
+ int port = Integer.parseInt(args[1]);
+ String ip = args[0];
+
+ Socket sock = null;
+ BufferedReader sockReader = null;
+ PrintWriter sockWriter = null;
+
+ // Perform the connection and get the read/write streams
+ try {
+ sock = new Socket(ip, port);
+ sockReader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
+ sockWriter = new PrintWriter(sock.getOutputStream(), true);
+ }
+ catch(UnknownHostException e) {
+ System.err.println("ERROR: unable to determine the server IP address");
+ System.exit(1);
+ }
+ catch(Exception e) {
+ System.err.println("ERROR: error on server connection");
+ System.err.println(e.getMessage());
+ System.exit(1);
+ }
+
+ // Get the bot command line
+ String command = args[3];
+
+ // Start the bot
+ Process bot = null;
+ BufferedReader botReader = null;
+ PrintWriter botWriter = null;
+
+ try {
+ bot = Runtime.getRuntime().exec(command);
+ botReader = new BufferedReader(new InputStreamReader(bot.getInputStream()));
+ botWriter = new PrintWriter(bot.getOutputStream(), true);
+ } catch (Exception e) {
+ System.err.println("ERROR: failed to start bot");
+ System.err.println(e.getMessage());
+ System.exit(1);
+ }
+
+ // Tell the server we want play
+ sockWriter.println("USER " + args[2]);
+
+ // Basic stats data
+ int turns = 0;
+ boolean win = false;
+ String enemy = "unknown";
+
+ String line;
+ StringBuilder message = new StringBuilder();
+ try {
+ // While the server data is valid
+ while((line = sockReader.readLine()) != null) {
+ if(line.startsWith("INFO")) {
+ System.out.println(line);
+ }
+ else if(line.startsWith("go")) {
+ // Add the GO command to the message
+ message.append("go\n");
+
+ // Send the turn data to bot
+ botWriter.println(message.toString());
+ message = new StringBuilder();
+
+ // Get the bot response
+ while(!(line = botReader.readLine()).startsWith("go")) {
+ message.append(line).append("\n");
+ }
+
+ // Add the GO command to the message
+ message.append("go\n");
+
+ // Perform bot actions
+ sockWriter.println(message.toString());
+ message = new StringBuilder();
+
+ // Collect turn data [statistics]
+ turns++;
+ }
+ else {
+ message.append(line).append("\n");
+ }
+ }
+ } catch (Exception e) {
+ System.err.println("ERROR: unknown exception");
+ System.err.println(e.getMessage());
+ }
+
+ // TODO: collect the enemy name, win/lose stats
+ //System.out.println(enemy + "|" + win + "|" + turns);
+ }
+ }
@@ -0,0 +1,39 @@
+#!/usr/bin/bash
+ for file in example_bots/*.jar
+
+ do
+ player_1_counter=0
+ player_1_turn_counter=0
+
+ player_2_counter=0
+ player_2_turn_counter=0
+
+ maps_played=0
+
+ echo "Bot: $file"
+ for i in {1..100}
+ do
+ RES=`java -jar tools/PlayGame.jar maps/map$i.txt 200 200 log.txt "java -jar $file" "python MyBot.py" 2>&1 | tail -n 3 | grep "Turn\|Player"`
+
+ TURN=`echo $RES | grep -i turn | sed 's/.*urn \([0-9]*\).*/\1/'`
+
+ RES2=`echo $RES | grep -i player | sed 's/.*ayer \([0-9]*\).*/\1/'`
+
+ if [ "$RES2" = "1" ] ; then
+ player_1_counter=`expr $player_1_counter + 1`
+ player_1_turn_counter=`expr $player_1_turn_counter + $TURN`
+
+ else
+ player_2_counter=`expr $player_2_counter + 1`
+ player_2_turn_counter=`expr $player_2_turn_counter + $TURN`
+ fi
+
+ maps_played=`expr $maps_played + 1`
+
+ done
+ player_2_turn_counter=`expr $player_2_turn_counter / $maps_played`
+ player_1_turn_counter=`expr $player_1_turn_counter / $maps_played`
+
+ echo "won against $file : $player_2_counter/$maps_played, avg turns: $player_2_turn_counter"
+ echo "lost against $file lost : $player_1_counter/$maps_played, avg turns: $player_1_turn_counter"
+ done
@@ -0,0 +1 @@
+java -jar tools/PlayGame.jar maps/map%1.txt 200 200 log.txt "java -jar example_bots/DualBot.jar" "python MyBot.py --log 1.log" | python visualizer/visualize_localy.py
Binary file not shown.
@@ -0,0 +1,65 @@
+import java.util.*;
+
+public class BullyBot {
+ public static void DoTurn(PlanetWars pw) {
+ // (1) If we current have a fleet in flight, just do nothing.
+ if (pw.MyFleets().size() >= 1) {
+ return;
+ }
+ // (2) Find my strongest planet.
+ Planet source = null;
+ double sourceScore = Double.MIN_VALUE;
+ for (Planet p : pw.MyPlanets()) {
+ double score = (double)p.NumShips();
+ if (score > sourceScore) {
+ sourceScore = score;
+ source = p;
+ }
+ }
+ // (3) Find the weakest enemy or neutral planet.
+ Planet dest = null;
+ double destScore = Double.MIN_VALUE;
+ for (Planet p : pw.NotMyPlanets()) {
+ double score = 1.0 / (1 + p.NumShips());
+ if (score > destScore) {
+ destScore = score;
+ dest = p;
+ }
+ }
+ // (4) Send half the ships from my strongest planet to the weakest
+ // planet that I do not own.
+ if (source != null && dest != null) {
+ int numShips = source.NumShips() / 2;
+ pw.IssueOrder(source, dest, numShips);
+ }
+ }
+
+ public static void main(String[] args) {
+ String line = "";
+ String message = "";
+ int c;
+ try {
+ while ((c = System.in.read()) >= 0) {
+ switch (c) {
+ case '\n':
+ if (line.equals("go")) {
+ PlanetWars pw = new PlanetWars(message);
+ DoTurn(pw);
+ pw.FinishTurn();
+ message = "";
+ } else {
+ message += line + "\n";
+ }
+ line = "";
+ break;
+ default:
+ line += (char)c;
+ break;
+ }
+ }
+ } catch (Exception e) {
+ // Owned.
+ }
+ }
+}
+
Binary file not shown.
@@ -0,0 +1,86 @@
+import java.util.*;
+
+public class DualBot {
+ public static void DoTurn(PlanetWars pw) {
+ int numFleets = 1;
+ boolean attackMode = false;
+ if (pw.NumShips(1) > pw.NumShips(2)) {
+ if (pw.Production(1) > pw.Production(2)) {
+ numFleets = 1;
+ attackMode = true;
+ } else {
+ numFleets = 3;
+ }
+ } else {
+ if (pw.Production(1) > pw.Production(2)) {
+ numFleets = 1;
+ } else {
+ numFleets = 5;
+ }
+ }
+ // (1) If we current have more tha numFleets fleets in flight, just do
+ // nothing until at least one of the fleets arrives.
+ if (pw.MyFleets().size() >= numFleets) {
+ return;
+ }
+ // (2) Find my strongest planet.
+ Planet source = null;
+ double sourceScore = Double.MIN_VALUE;
+ for (Planet p : pw.MyPlanets()) {
+ double score = (double)p.NumShips() / (1 + p.GrowthRate());
+ if (score > sourceScore) {
+ sourceScore = score;
+ source = p;
+ }
+ }
+ // (3) Find the weakest enemy or neutral planet.
+ Planet dest = null;
+ double destScore = Double.MIN_VALUE;
+ List<Planet> candidates = pw.NotMyPlanets();
+ if (attackMode) {
+ candidates = pw.EnemyPlanets();
+ }
+ for (Planet p : candidates) {
+ double score = (double)(1 + p.GrowthRate()) / p.NumShips();
+ if (score > destScore) {
+ destScore = score;
+ dest = p;
+ }
+ }
+ // (4) Send half the ships from my strongest planet to the weakest
+ // planet that I do not own.
+ if (source != null && dest != null) {
+ int numShips = source.NumShips() / 2;
+ pw.IssueOrder(source, dest, numShips);
+ }
+ }
+
+ public static void main(String[] args) {
+ String line = "";
+ String message = "";
+ int c;
+ try {
+ while ((c = System.in.read()) >= 0) {
+ switch (c) {
+ case '\n':
+ if (line.equals("go")) {
+ PlanetWars pw = new PlanetWars(message);
+ DoTurn(pw);
+ pw.FinishTurn();
+ message = "";
+ } else {
+ message += line + "\n";
+ }
+ line = "";
+ break;
+ default:
+ line += (char)c;
+ break;
+ }
+ }
+ } catch (Exception e) {
+ // Owned.
+ }
+ }
+}
+
Oops, something went wrong.

0 comments on commit f04740b

Please sign in to comment.