Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include "Game_Api.h"
  6. using json = nlohmann::json;
  7. using Player = Game_Api::Player;
  8. using Monster = Game_Api::Monster;
  9. using DeathEffects = Game_Api::DeathEffects;
  10.  
  11. #define RESPONSE_SECS 1
  12. #define RESPONSE_NSECS 0
  13.  
  14. #include <iostream>
  15. using namespace std;
  16.  
  17. //Globals
  18. Game_Api * api;
  19. int my_player_num = 0;
  20.  
  21. string get_beating_stance(string stance){
  22. if (stance == "Rock")
  23. return "Paper";
  24. if (stance == "Paper")
  25. return "Scissors";
  26. return "Rock";
  27. }
  28.  
  29. // sorts list such that it is highest to lowest Example {10,-1,8,0,6,1,4,2,3} -> {10,8,6,4,3,2,1,0,-1}
  30. bool compare_rock_value(Monster a, Monster b)
  31. {
  32. return a._death_effects._rock > b._death_effects._rock;
  33. }
  34.  
  35. // sorts list such that it is highest to lowest Example {10,-1,8,0,6,1,4,2,3} -> {10,8,6,4,3,2,1,0,-1}
  36. bool compare_paper_value(Monster a, Monster b)
  37. {
  38. return a._death_effects._paper > b._death_effects._paper;
  39. }
  40.  
  41. // sorts list such that it is highest to lowest Example {10,-1,8,0,6,1,4,2,3} -> {10,8,6,4,3,2,1,0,-1}
  42. bool compare_scissors_value(Monster a, Monster b)
  43. {
  44. return a._death_effects._scissors > b._death_effects._scissors;
  45. }
  46.  
  47. // sorts list such that it is highest to lowest Example {10,-1,8,0,6,1,4,2,3} -> {10,8,6,4,3,2,1,0,-1}
  48. bool compare_speed_value(Monster a, Monster b)
  49. {
  50. return a._death_effects._speed > b._death_effects._speed;
  51. }
  52.  
  53. class decision_maker {
  54. private:
  55. unsigned int amount_of_hardcoded_sequences = 5;
  56. unsigned int current_sequence = 0;
  57. unsigned int enemies_last_location = 0;
  58. std::vector<node_id_t> location_vector = {10, 11, 12, 22, 21, 20};
  59. std::vector<int > duration_vector = { 7, 7, 7, 7, 7, 8};
  60. std::vector<int > enemy_path_vector;
  61.  
  62. void log_enemy_path()
  63. {
  64. if(api->get_opponent()._location != enemies_last_location)
  65. {
  66. enemy_path_vector.push_back(api->get_opponent()._location);
  67. enemies_last_location = api->get_opponent()._location;
  68. }
  69. }
  70.  
  71. vector<Monster> get_rock_monsters()
  72. {
  73. vector<Monster> monster_vector = api->get_all_monsters();
  74. vector<Monster> rock_monsters;
  75.  
  76. std::sort (monster_vector.begin(), monster_vector.end(), compare_rock_value);
  77. for(Monster& monster : monster_vector)
  78. {
  79. if(monster._death_effects._rock > 0)
  80. {
  81. rock_monsters.push_back(monster);
  82. }
  83. }
  84.  
  85. return rock_monsters;
  86. }
  87.  
  88.  
  89. vector<Monster> get_paper_monsters()
  90. {
  91. vector<Monster> monster_vector = api->get_all_monsters();
  92. vector<Monster> paper_monsters;
  93.  
  94. std::sort (monster_vector.begin(), monster_vector.end(), compare_paper_value);
  95. for(Monster& monster : monster_vector)
  96. {
  97. if(monster._death_effects._paper > 0)
  98. {
  99. paper_monsters.push_back(monster);
  100. }
  101. }
  102.  
  103. return paper_monsters;
  104. }
  105.  
  106. vector<Monster> get_scissors_monsters()
  107. {
  108. vector<Monster> monster_vector = api->get_all_monsters();
  109. vector<Monster> scissors_monsters;
  110.  
  111. std::sort (monster_vector.begin(), monster_vector.end(), compare_scissors_value);
  112. for(Monster& monster : monster_vector)
  113. {
  114. if(monster._death_effects._scissors > 0)
  115. {
  116. scissors_monsters.push_back(monster);
  117. }
  118. }
  119.  
  120. return scissors_monsters;
  121. }
  122.  
  123.  
  124. vector<Monster> get_speed_monsters()
  125. {
  126. vector<Monster> monster_vector = api->get_all_monsters();
  127. vector<Monster> speed_monsters;
  128.  
  129. std::sort (monster_vector.begin(), monster_vector.end(), compare_speed_value);
  130. for(Monster& monster : monster_vector)
  131. {
  132. if(monster._death_effects._speed > 0)
  133. {
  134. speed_monsters.push_back(monster);
  135. }
  136. }
  137. return speed_monsters;
  138. }
  139.  
  140.  
  141. bool is_enemy_near_24()
  142. {
  143. return (enemy_path_vector.back() == 24) || (enemy_path_vector.back() == 23) || (enemy_path_vector.back() == 19);
  144. }
  145.  
  146. bool is_enemy_near_1_speed()
  147. {
  148. return (enemy_path_vector.back() == 0) || (enemy_path_vector.back() == 1) || (enemy_path_vector.back() == 4) || (enemy_path_vector.back() == 2) || (enemy_path_vector.back() == 3);
  149. }
  150.  
  151. bool is_speed_1_monster_alive()
  152. {
  153. vector<Monster> speed_monsters = get_speed_monsters();
  154. if((speed_monsters.front()._death_effects._speed == 1)||(speed_monsters.back()._death_effects._speed == 1))
  155. {
  156. return true;
  157. }else{
  158. return false;
  159. }
  160. }
  161.  
  162. bool early_midgame_decision_switch_flag = false;
  163. void early_midgame_decision_switch()
  164. {
  165. if(early_midgame_decision_switch_flag == true)
  166. {
  167. return;
  168. }
  169.  
  170. if(is_enemy_near_24() == true)
  171. {
  172. rush_1_speed_setup();
  173. }else if(!is_enemy_near_1_speed() && is_speed_1_monster_alive()){
  174. rush_1_speed_setup();
  175. }else{
  176. rush_24_setup();
  177. }
  178.  
  179. early_midgame_decision_switch_flag = true;
  180. }
  181.  
  182. void rush_24_setup()
  183. {
  184. location_vector.push_back(19);
  185. duration_vector.push_back(8);
  186. location_vector.push_back(23);
  187. duration_vector.push_back(5);
  188. location_vector.push_back(24);
  189. duration_vector.push_back(6);
  190. location_vector.push_back(23);
  191. duration_vector.push_back(6);
  192. location_vector.push_back(19);
  193. duration_vector.push_back(5);
  194. location_vector.push_back(22);
  195. duration_vector.push_back(5);
  196. location_vector.push_back(12);
  197. duration_vector.push_back(5);
  198. location_vector.push_back(13);
  199. duration_vector.push_back(5);
  200. location_vector.push_back(20);
  201. duration_vector.push_back(5);
  202. location_vector.push_back(21);
  203. duration_vector.push_back(3);
  204. api->log("24 lv size "+to_string(location_vector.size()));
  205. }
  206.  
  207. void rush_1_speed_setup()
  208. {
  209. location_vector.push_back(13);
  210. duration_vector.push_back(8);
  211. location_vector.push_back(4);
  212. duration_vector.push_back(6);
  213. location_vector.push_back(2);
  214. duration_vector.push_back(5);
  215. location_vector.push_back(3);
  216. duration_vector.push_back(4);
  217. location_vector.push_back(1);
  218. duration_vector.push_back(4);
  219. location_vector.push_back(0);
  220. duration_vector.push_back(4);
  221. location_vector.push_back(10);
  222. duration_vector.push_back(4);
  223. location_vector.push_back(16);
  224. duration_vector.push_back(4);
  225. location_vector.push_back(15);
  226. duration_vector.push_back(4);
  227. location_vector.push_back(18);
  228. duration_vector.push_back(4);
  229. location_vector.push_back(17);
  230. duration_vector.push_back(4);
  231. location_vector.push_back(16);
  232. duration_vector.push_back(4);
  233. location_vector.push_back(12);
  234. duration_vector.push_back(4);
  235. location_vector.push_back(22);
  236. duration_vector.push_back(4);
  237. location_vector.push_back(21);
  238. duration_vector.push_back(2);
  239. api->log("1s lv size "+to_string(location_vector.size()));
  240. }
  241.  
  242. public:
  243. node_id_t get_current_command_location() // -1 signifies there is no valid hardcoded path
  244. {
  245. node_id_t location = -1;
  246. log_enemy_path();
  247.  
  248. if(api->get_turn_num() <= 43)
  249. {
  250. location = run_decision_tree();
  251. }else if (api->get_turn_num() <= 150){ // TODO
  252. early_midgame_decision_switch();
  253. location = run_decision_tree();
  254. }
  255. return location;
  256. }
  257.  
  258. node_id_t run_decision_tree()
  259. {
  260. //if(current_sequence >= amount_of_hardcoded_sequences)
  261. //{
  262. // return -1;
  263. //}
  264.  
  265. if(duration_vector[current_sequence] == 0)
  266. {
  267. current_sequence++;
  268. }
  269.  
  270. //if(current_sequence >= amount_of_hardcoded_sequences)
  271. //{
  272. // return -1;
  273. //}
  274.  
  275. duration_vector[current_sequence]--;
  276.  
  277. if (duration_vector[current_sequence] < 7 - api->get_self()._speed)
  278. return location_vector[current_sequence];
  279. return api->get_self()._location;
  280. }
  281. };
  282.  
  283. decision_maker dm;
  284.  
  285. void strategy(){
  286. Player me = api->get_self();
  287. Player opponent = api->get_opponent();
  288. int turn = api->get_turn_num();
  289. node_id_t destination_decision = me._location;
  290. string stance = "Rock";
  291.  
  292. // determine destination
  293. destination_decision = dm.get_current_command_location();
  294.  
  295. // determine stance
  296. node_id_t stance_location = me._location;
  297. if (me._movement_counter-1 == me._speed){
  298. stance_location = destination_decision;
  299. }
  300. if (api->has_monster(stance_location)){
  301. Monster monster = api->get_monster(stance_location);
  302. stance = get_beating_stance(monster._stance);
  303. }
  304.  
  305. if (my_player_num == 1)
  306. api->log(to_string(turn)+") goto "+to_string(destination_decision)+" with "+stance);
  307. api->submit_decision(destination_decision, stance);
  308. }
  309.  
  310. int main() {
  311. while(1){
  312. char* buf = NULL;
  313. size_t size = 0;
  314. getline(&buf, &size, stdin);
  315. json data = json::parse(buf);
  316. if(data["type"] == "map"){
  317. my_player_num = data["player_id"];
  318. api = new Game_Api(my_player_num, data["map"]);
  319. } else {
  320. api->update(data["game_data"]);
  321. strategy();
  322. fflush(stdout);
  323. free(buf);
  324. }
  325. }
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement