Guest User

Untitled

a guest
Aug 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.17 KB | None | 0 0
  1. // Contestants do not need to worry about anything in this file. This is just
  2. // helper code that does the boring stuff for you, so you can focus on the
  3. // interesting stuff. That being said, you're welcome to change anything in
  4. // this file if you know what you're doing.
  5.  
  6. using System;
  7. using System.IO;
  8. using System.Collections.Generic;
  9.  
  10. public class PlanetWars {
  11. // Constructs a PlanetWars object instance, given a string containing a
  12. // description of a game state.
  13. public PlanetWars(string gameStatestring) {
  14. planets = new List<Planet>();
  15. fleets = new List<Fleet>();
  16. ParseGameState(gameStatestring);
  17. }
  18.  
  19. // Returns the number of planets. Planets are numbered starting with 0.
  20. public int NumPlanets() {
  21. return planets.Count;
  22. }
  23.  
  24. // Returns the planet with the given planet_id. There are NumPlanets()
  25. // planets. They are numbered starting at 0.
  26. public Planet GetPlanet(int planetID) {
  27. return planets[planetID];
  28. }
  29.  
  30. // Returns the number of fleets.
  31. public int NumFleets() {
  32. return fleets.Count;
  33. }
  34.  
  35. // Returns the fleet with the given fleet_id. Fleets are numbered starting
  36. // with 0. There are NumFleets() fleets. fleet_id's are not consistent from
  37. // one turn to the next.
  38. public Fleet GetFleet(int fleetID) {
  39. return fleets[fleetID];
  40. }
  41.  
  42. // Returns a list of all the planets.
  43. public List<Planet> Planets() {
  44. return planets;
  45. }
  46.  
  47. // Return a list of all the planets owned by the current player. By
  48. // convention, the current player is always player number 1.
  49. public List<Planet> MyPlanets() {
  50. List<Planet> r = new List<Planet>();
  51. foreach (Planet p in planets) {
  52. if (p.Owner() == 1) {
  53. r.Add(p);
  54. }
  55. }
  56. return r;
  57. }
  58.  
  59. // Return a list of all neutral planets.
  60. public List<Planet> NeutralPlanets() {
  61. List<Planet> r = new List<Planet>();
  62. foreach (Planet p in planets) {
  63. if (p.Owner() == 0) {
  64. r.Add(p);
  65. }
  66. }
  67. return r;
  68. }
  69.  
  70. // Return a list of all the planets owned by rival players. This excludes
  71. // planets owned by the current player, as well as neutral planets.
  72. public List<Planet> EnemyPlanets() {
  73. List<Planet> r = new List<Planet>();
  74. foreach (Planet p in planets) {
  75. if (p.Owner() >= 2) {
  76. r.Add(p);
  77. }
  78. }
  79. return r;
  80. }
  81.  
  82. // Return a list of all the planets that are not owned by the current
  83. // player. This includes all enemy planets and neutral planets.
  84. public List<Planet> NotMyPlanets() {
  85. List<Planet> r = new List<Planet>();
  86. foreach (Planet p in planets) {
  87. if (p.Owner() != 1) {
  88. r.Add(p);
  89. }
  90. }
  91. return r;
  92. }
  93.  
  94. // Return a list of all the fleets.
  95. public List<Fleet> Fleets() {
  96. List<Fleet> r = new List<Fleet>();
  97. foreach (Fleet f in fleets) {
  98. r.Add(f);
  99. }
  100. return r;
  101. }
  102.  
  103. // Return a list of all the fleets owned by the current player.
  104. public List<Fleet> MyFleets() {
  105. List<Fleet> r = new List<Fleet>();
  106. foreach (Fleet f in fleets) {
  107. if (f.Owner() == 1) {
  108. r.Add(f);
  109. }
  110. }
  111. return r;
  112. }
  113.  
  114. // Return a list of all the fleets owned by enemy players.
  115. public List<Fleet> EnemyFleets() {
  116. List<Fleet> r = new List<Fleet>();
  117. foreach (Fleet f in fleets) {
  118. if (f.Owner() != 1) {
  119. r.Add(f);
  120. }
  121. }
  122. return r;
  123. }
  124.  
  125. // Returns the distance between two planets, rounded up to the next highest
  126. // integer. This is the number of discrete time steps it takes to get
  127. // between the two planets.
  128. public int Distance(Planet sourcePlanet, Planet destinationPlanet) {
  129. //Planet source = planets[sourcePlanet];
  130. //Planet destination = planets[destinationPlanet];
  131. double dx = sourcePlanet.X() - destinationPlanet.X();
  132. double dy = sourcePlanet.Y() - destinationPlanet.Y();
  133. return (int)Math.Ceiling(Math.Sqrt(dx * dx + dy * dy));
  134. }
  135.  
  136. //Returns the number of ships on all friendly planets within specified
  137. //distance.
  138. public int MyShips(Planet source, int dist)
  139. {
  140. int numShips = 0;
  141. foreach (Planet p in MyPlanets())
  142. {
  143. if (Distance(source, p) <= dist)
  144. numShips += p.NumShips();
  145. }
  146. return numShips;
  147. }
  148.  
  149. //returns the nearest friendly planet
  150. public Planet FindNearestMyPlanet(Planet p)
  151. {
  152. int dist = int.MaxValue;
  153. Planet near = null;
  154. foreach (Planet np in MyPlanets())
  155. {
  156. if (dist < Distance(p, np))
  157. {
  158. dist = Distance(p, np);
  159. near = np;
  160. }
  161. }
  162. return near;
  163. }
  164.  
  165. //returns a list of 6 nearest friendly planets
  166. public List<Planet> FindNearFriendlyPlanets(Planet p, int dist)
  167. {
  168. List<Planet> planets = new List<Planet>();
  169. planets.Clear();
  170.  
  171. int d1 = int.MaxValue, d2 = int.MaxValue, d3 = int.MaxValue
  172. , d4 = int.MaxValue, d5 = int.MaxValue, d6 = int.MaxValue;
  173. Planet p1 = null, p2 = null, p3 = null, p4 = null, p5 = null,
  174. p6 = null;
  175. foreach (Planet l in MyPlanets())
  176. {
  177. int dista = Distance(p, l);
  178. if (dista < d1 && dista <= dist)
  179. {
  180. d1 = dista;
  181. p1 = l;
  182. }
  183. else if (dista < d2 && dista >= d1)
  184. {
  185. d2 = dista;
  186. p2 = l;
  187. }
  188. else if (dista < d3 && dista >= d2)
  189. {
  190. d3 = dista;
  191. p3 = l;
  192. }
  193. else if (dista < d4 && dista >= d3)
  194. {
  195. d4 = dista;
  196. p4 = l;
  197. }
  198. else if (dista < d5 && dista >= d4)
  199. {
  200. d5 = dista;
  201. p5 = l;
  202. }
  203. else if (dista < d6 && dista >= d5)
  204. {
  205. d6 = dista;
  206. p6 = l;
  207. }
  208. }
  209. planets.Add(p1);
  210. planets.Add(p2);
  211. planets.Add(p3);
  212. planets.Add(p4);
  213. planets.Add(p5);
  214. planets.Add(p6);
  215. return planets;
  216. }
  217.  
  218. //returns a list of 6 nearest neutral planets
  219. public List<Planet> FindNearNeutralPlanets(Planet p, int dist)
  220. {
  221. List<Planet> planets = new List<Planet>();
  222. planets.Clear();
  223.  
  224. int d1 = int.MaxValue, d2 = int.MaxValue, d3 = int.MaxValue
  225. , d4 = int.MaxValue, d5 = int.MaxValue, d6 = int.MaxValue;
  226. Planet p1 = null, p2 = null, p3 = null, p4 = null, p5 = null,
  227. p6 = null;
  228. foreach (Planet l in NeutralPlanets())
  229. {
  230. int dista = Distance(p, l);
  231. if (dista < d1 && dista <= dist)
  232. {
  233. d1 = dista;
  234. p1 = l;
  235. }
  236. else if (dista < d2 && dista >= d1)
  237. {
  238. d2 = dista;
  239. p2 = l;
  240. }
  241. else if (dista < d3 && dista >= d2)
  242. {
  243. d3 = dista;
  244. p3 = l;
  245. }
  246. else if (dista < d4 && dista >= d3)
  247. {
  248. d4 = dista;
  249. p4 = l;
  250. }
  251. else if (dista < d5 && dista >= d4)
  252. {
  253. d5 = dista;
  254. p5 = l;
  255. }
  256. else if (dista < d6 && dista >= d5)
  257. {
  258. d6 = dista;
  259. p6 = l;
  260. }
  261. }
  262. planets.Add(p1);
  263. planets.Add(p2);
  264. planets.Add(p3);
  265. planets.Add(p4);
  266. planets.Add(p5);
  267. planets.Add(p6);
  268. return planets;
  269. }
  270.  
  271. //returns a list of 6 nearest enemy planets
  272. public List<Planet> FindNearEnemyPlanets(Planet p, int dist)
  273. {
  274. List<Planet> planets = new List<Planet>();
  275. planets.Clear();
  276.  
  277. int d1 = int.MaxValue, d2 = int.MaxValue, d3 = int.MaxValue
  278. , d4 = int.MaxValue, d5 = int.MaxValue, d6 = int.MaxValue;
  279. Planet p1 = null, p2 = null, p3 = null, p4 = null, p5 = null,
  280. p6 = null;
  281. foreach (Planet l in EnemyPlanets())
  282. {
  283. int dista = Distance(p, l);
  284. if (dista < d1 && dista <= dist)
  285. {
  286. d1 = dista;
  287. p1 = l;
  288. }
  289. else if (dista < d2 && dista >= d1)
  290. {
  291. d2 = dista;
  292. p2 = l;
  293. }
  294. else if (dista < d3 && dista >= d2)
  295. {
  296. d3 = dista;
  297. p3 = l;
  298. }
  299. else if (dista < d4 && dista >= d3)
  300. {
  301. d4 = dista;
  302. p4 = l;
  303. }
  304. else if (dista < d5 && dista >= d4)
  305. {
  306. d5 = dista;
  307. p5 = l;
  308. }
  309. else if (dista < d6 && dista >= d5)
  310. {
  311. d6 = dista;
  312. p6 = l;
  313. }
  314. }
  315. planets.Add(p1);
  316. planets.Add(p2);
  317. planets.Add(p3);
  318. planets.Add(p4);
  319. planets.Add(p5);
  320. planets.Add(p6);
  321. return planets;
  322. }
  323.  
  324. //returns my starting planet
  325. public Planet LocateMyStart()
  326. {
  327. Planet start = MyPlanets()[0];
  328. return start;
  329. }
  330.  
  331. //pass it a list and it will return a list of the 3 best payoff planets
  332. public List<Planet> BestPayoff3(List<Planet> list, Planet source)
  333. {
  334. List<Planet> best = new List<Planet>();
  335. best.Clear();
  336. int po1 = int.MaxValue, po2 = int.MaxValue, po3 = int.MaxValue;
  337. Planet p1 = null, p2 = null, p3 = null;
  338. foreach (Planet p in list)
  339. {
  340. int po = Payoff(p, source);
  341. if (po < po1)
  342. {
  343. po1 = po;
  344. p1 = p;
  345. }
  346. else if (po < po2 && po > po1)
  347. {
  348. po2 = po;
  349. p2 = p;
  350. }
  351. else if (po < po3 && po > po2)
  352. {
  353. po3 = po;
  354. p3 = p;
  355. }
  356. }
  357. best.Add(p1);
  358. best.Add(p2);
  359. best.Add(p3);
  360. return best;
  361. }
  362.  
  363. //returns how many turns it would take to make up for the cost in ships
  364. public int Payoff(Planet p, Planet s)
  365. {
  366. int po = int.MaxValue;
  367. if (NeutralPlanets().Contains(p))
  368. po = (p.NumShips() / p.GrowthRate()) + Distance(p, s);
  369. else if (EnemyPlanets().Contains(p))
  370. po = ((p.NumShips() + (Distance(p, s) * p.GrowthRate())) / p.GrowthRate()) + Distance(p, s);
  371.  
  372. return po;
  373. }
  374.  
  375. // Sends an order to the game engine. An order is composed of a source
  376. // planet number, a destination planet number, and a number of ships. A
  377. // few things to keep in mind:
  378. // * you can issue many orders per turn if you like.
  379. // * the planets are numbered starting at zero, not one.
  380. // * you must own the source planet. If you break this rule, the game
  381. // engine kicks your bot out of the game instantly.
  382. // * you can't move more ships than are currently on the source planet.
  383. // * the ships will take a few turns to reach their destination. Travel
  384. // is not instant. See the Distance() function for more info.
  385. public void IssueOrder(int sourcePlanet,
  386. int destinationPlanet,
  387. int numShips) {
  388. Console.WriteLine("" + sourcePlanet + " " + destinationPlanet + " " +
  389. numShips);
  390. Console.Out.Flush();
  391. }
  392.  
  393. // Sends an order to the game engine. An order is composed of a source
  394. // planet number, a destination planet number, and a number of ships. A
  395. // few things to keep in mind:
  396. // * you can issue many orders per turn if you like.
  397. // * the planets are numbered starting at zero, not one.
  398. // * you must own the source planet. If you break this rule, the game
  399. // engine kicks your bot out of the game instantly.
  400. // * you can't move more ships than are currently on the source planet.
  401. // * the ships will take a few turns to reach their destination. Travel
  402. // is not instant. See the Distance() function for more info.
  403. public void IssueOrder(Planet source, Planet dest, int numShips) {
  404. Console.WriteLine("" + source.PlanetID() + " " + dest.PlanetID() +
  405. " " + numShips);
  406. Console.Out.Flush();
  407. }
  408.  
  409. // Sends the game engine a message to let it know that we're done sending
  410. // orders. This signifies the end of our turn.
  411. public void FinishTurn() {
  412. Console.WriteLine("go");
  413. Console.Out.Flush();
  414. }
  415.  
  416. // Returns true if the named player owns at least one planet or fleet.
  417. // Otherwise, the player is deemed to be dead and false is returned.
  418. public bool IsAlive(int playerID) {
  419. foreach (Planet p in planets) {
  420. if (p.Owner() == playerID) {
  421. return true;
  422. }
  423. }
  424. foreach (Fleet f in fleets) {
  425. if (f.Owner() == playerID) {
  426. return true;
  427. }
  428. }
  429. return false;
  430. }
  431.  
  432. // If the game is not yet over (ie: at least two players have planets or
  433. // fleets remaining), returns -1. If the game is over (ie: only one player
  434. // is left) then that player's number is returned. If there are no
  435. // remaining players, then the game is a draw and 0 is returned.
  436. public int Winner() {
  437. List<int> remainingPlayers = new List<int>();
  438. foreach (Planet p in planets) {
  439. if (!remainingPlayers.Contains(p.Owner())) {
  440. remainingPlayers.Add(p.Owner());
  441. }
  442. }
  443. foreach (Fleet f in fleets) {
  444. if (!remainingPlayers.Contains(f.Owner())) {
  445. remainingPlayers.Add(f.Owner());
  446. }
  447. }
  448. switch (remainingPlayers.Count) {
  449. case 0:
  450. return 0;
  451. case 1:
  452. return remainingPlayers[0];
  453. default:
  454. return -1;
  455. }
  456. }
  457.  
  458. // Returns the number of ships that the current player has, either located
  459. // on planets or in flight.
  460. public int NumShips(int playerID) {
  461. int numShips = 0;
  462. foreach (Planet p in planets) {
  463. if (p.Owner() == playerID) {
  464. numShips += p.NumShips();
  465. }
  466. }
  467. foreach (Fleet f in fleets) {
  468. if (f.Owner() == playerID) {
  469. numShips += f.NumShips();
  470. }
  471. }
  472. return numShips;
  473. }
  474.  
  475. // Parses a game state from a string. On success, returns 1. On failure,
  476. // returns 0.
  477. private int ParseGameState(string s) {
  478. planets.Clear();
  479. fleets.Clear();
  480. int planetID = 0;
  481. string[] lines = s.Split('\n');
  482. for (int i = 0; i < lines.Length; ++i) {
  483. string line = lines[i];
  484. int commentBegin = line.IndexOf('#');
  485. if (commentBegin >= 0) {
  486. line = line.Substring(0, commentBegin);
  487. }
  488. if (line.Trim().Length == 0) {
  489. continue;
  490. }
  491. string[] tokens = line.Split(' ');
  492. if (tokens.Length == 0) {
  493. continue;
  494. }
  495. if (tokens[0].Equals("P")) {
  496. if (tokens.Length != 6) {
  497. return 0;
  498. }
  499. double x = double.Parse(tokens[1]);
  500. double y = double.Parse(tokens[2]);
  501. int owner = int.Parse(tokens[3]);
  502. int numShips = int.Parse(tokens[4]);
  503. int growthRate = int.Parse(tokens[5]);
  504. Planet p = new Planet(planetID++,
  505. owner,
  506. numShips,
  507. growthRate,
  508. x, y);
  509. planets.Add(p);
  510. } else if (tokens[0].Equals("F")) {
  511. if (tokens.Length != 7) {
  512. return 0;
  513. }
  514. int owner = int.Parse(tokens[1]);
  515. int numShips = int.Parse(tokens[2]);
  516. int source = int.Parse(tokens[3]);
  517. int destination = int.Parse(tokens[4]);
  518. int totalTripLength = int.Parse(tokens[5]);
  519. int turnsRemaining = int.Parse(tokens[6]);
  520. Fleet f = new Fleet(owner,
  521. numShips,
  522. source,
  523. destination,
  524. totalTripLength,
  525. turnsRemaining);
  526. fleets.Add(f);
  527. } else {
  528. return 0;
  529. }
  530. }
  531. return 1;
  532. }
  533.  
  534. // Store all the planets and fleets. OMG we wouldn't wanna lose all the
  535. // planets and fleets, would we!?
  536. private List<Planet> planets;
  537. private List<Fleet> fleets;
  538. }
Add Comment
Please, Sign In to add comment