You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

player4.cpp 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include <future>
  2. #include "AI.h"
  3. #include "constants.h"
  4. // 为假则play()期间确保游戏状态不更新,为真则只保证游戏状态在调用相关方法时不更新
  5. extern const bool asynchronous = true;
  6. // 选手需要依次将player0到player4的职业在这里定义
  7. extern const std::array<THUAI6::StudentType, 4> studentType = {
  8. THUAI6::StudentType::Sunshine,
  9. THUAI6::StudentType::Sunshine,
  10. THUAI6::StudentType::StraightAStudent,
  11. THUAI6::StudentType::Sunshine };
  12. extern const THUAI6::TrickerType trickerType = THUAI6::TrickerType::Assassin;
  13. //可以在AI.cpp内部声明变量与函数THUAI6::
  14. struct node
  15. {
  16. int x; int y;
  17. };
  18. int step = 1 * 1000 * 1000 / Constants::Athlete::moveSpeed;
  19. void LeftTo(ITrickerAPI& api, int x)
  20. {
  21. auto self = api.GetSelfInfo();
  22. while (x < self->y)
  23. api.MoveLeft(50);
  24. }
  25. //右移至x处
  26. void RightTo(ITrickerAPI& api, int x)
  27. {
  28. auto self = api.GetSelfInfo();
  29. while (x > self->y)
  30. api.MoveRight(50);
  31. }
  32. //上移至x处
  33. void UpTo(ITrickerAPI& api, int x)
  34. {
  35. auto self = api.GetSelfInfo();
  36. while (x < self->x)
  37. api.MoveUp(50);
  38. }
  39. //下移至x处
  40. void DownTo(ITrickerAPI& api, int x)
  41. {
  42. auto self = api.GetSelfInfo();
  43. while (x > self->x)
  44. api.MoveDown(50);
  45. }
  46. void moveto(int a, int b, IStudentAPI& api)//移动到cell(a,b)
  47. {
  48. if ((a + 1) != 0)
  49. {
  50. while (api.GetSelfInfo()->x < 1000 * (a + 1) - 600)
  51. {
  52. api.MoveDown(50);
  53. }
  54. while (api.GetSelfInfo()->x > 1000 * (a + 1) - 400)
  55. {
  56. api.MoveUp(50);
  57. }
  58. }
  59. if ((b + 1) != 0)
  60. {
  61. while (api.GetSelfInfo()->y < 1000 * (b + 1) - 600)
  62. {
  63. api.MoveRight(50);
  64. }
  65. while (api.GetSelfInfo()->y > 1000 * (b + 1) - 400)
  66. {
  67. api.MoveLeft(50);
  68. }
  69. }
  70. api.EndAllAction();
  71. }
  72. void moveto3(int a, int b, IStudentAPI& api)
  73. {
  74. while ((api.GetSelfInfo()->y < 1000 * (b + 1) - 600) || (api.GetSelfInfo()->y > 1000 * (b + 1) - 400) || (api.GetSelfInfo()->x < 1000 * (a + 1) - 600) || (api.GetSelfInfo()->x > 1000 * (a + 1) - 400))
  75. {
  76. while (api.GetSelfInfo()->y < 1000 * (b + 1) - 600)
  77. {
  78. api.MoveRight(50);
  79. }
  80. while (api.GetSelfInfo()->y > 1000 * (b + 1) - 400)
  81. {
  82. api.MoveLeft(50);
  83. }
  84. api.EndAllAction();
  85. while (api.GetSelfInfo()->x < 1000 * (a + 1) - 600)
  86. {
  87. api.MoveDown(50);
  88. }
  89. while (api.GetSelfInfo()->x > 1000 * (a + 1) - 400)
  90. {
  91. api.MoveUp(50);
  92. }
  93. api.EndAllAction();
  94. }
  95. }
  96. void slide1(IStudentAPI& api)
  97. {
  98. auto p1 = api.GetSelfInfo();
  99. int x1 = api.GridToCell(p1->x);
  100. int y1 = api.GridToCell(p1->y);
  101. auto judge1 = api.GetPlaceType(x1 + 1, y1);
  102. auto judge2 = api.GetPlaceType(x1 - 1, y1);
  103. auto judge3 = api.GetPlaceType(x1, y1 + 1);
  104. auto judge4 = api.GetPlaceType(x1, y1 - 1);
  105. if (judge1 == THUAI6::PlaceType::Window || judge2 == THUAI6::PlaceType::Window || judge3 == THUAI6::PlaceType::Window || judge4 == THUAI6::PlaceType::Window)
  106. {
  107. api.SkipWindow();
  108. }
  109. api.EndAllAction();
  110. }
  111. void learn1(IStudentAPI& api)
  112. {
  113. auto p1 = api.GetSelfInfo();
  114. int x1 = api.GridToCell(p1->x);
  115. int y1 = api.GridToCell(p1->y);
  116. auto judge1 = api.GetPlaceType(x1 + 1, y1);
  117. auto judge2 = api.GetPlaceType(x1 - 1, y1);
  118. auto judge3 = api.GetPlaceType(x1, y1 + 1);
  119. auto judge4 = api.GetPlaceType(x1, y1 - 1);
  120. if (judge1 == THUAI6::PlaceType::ClassRoom || judge2 == THUAI6::PlaceType::ClassRoom || judge3 == THUAI6::PlaceType::ClassRoom || judge4 == THUAI6::PlaceType::ClassRoom)
  121. {
  122. api.StartLearning();
  123. }
  124. api.EndAllAction();
  125. }
  126. void moveto1(IStudentAPI& api, node b)
  127. {
  128. int x2, y2;
  129. int x0 = api.GetSelfInfo()->x;
  130. int y0 = api.GetSelfInfo()->y;
  131. x2 = api.CellToGrid(b.x);
  132. y2 = api.CellToGrid(b.y);
  133. api.Move(step, atan2(y2 - y0, x2 - x0));
  134. api.EndAllAction();
  135. }
  136. void moveto2(int a, int b, IStudentAPI& api)
  137. {
  138. if ((b + 1) != 0)
  139. {
  140. while (api.GetSelfInfo()->y < 1000 * (b + 1) - 600)
  141. {
  142. api.MoveRight(50);
  143. }
  144. while (api.GetSelfInfo()->y > 1000 * (b + 1) - 400)
  145. {
  146. api.MoveLeft(50);
  147. }
  148. }
  149. if ((a + 1) != 0)
  150. {
  151. while (api.GetSelfInfo()->x < 1000 * (a + 1) - 600)
  152. {
  153. api.MoveDown(50);
  154. }
  155. while (api.GetSelfInfo()->x > 1000 * (a + 1) - 400)
  156. {
  157. api.MoveUp(50);
  158. }
  159. }
  160. api.EndAllAction();
  161. }
  162. void moveto2(int a, int b, ITrickerAPI& api)
  163. {
  164. if ((b + 1) != 0)
  165. {
  166. while (api.GetSelfInfo()->y < 1000 * (b + 1) - 600)
  167. {
  168. api.MoveRight(50);
  169. }
  170. while (api.GetSelfInfo()->y > 1000 * (b + 1) - 400)
  171. {
  172. api.MoveLeft(50);
  173. }
  174. }
  175. if ((a + 1) != 0)
  176. {
  177. while (api.GetSelfInfo()->x < 1000 * (a + 1) - 600)
  178. {
  179. api.MoveDown(50);
  180. }
  181. while (api.GetSelfInfo()->x > 1000 * (a + 1) - 400)
  182. {
  183. api.MoveUp(50);
  184. }
  185. }
  186. api.EndAllAction();
  187. }
  188. void slide(IStudentAPI& api, node b)
  189. {
  190. int x2, y2;
  191. int x0 = api.GetSelfInfo()->x;
  192. int y0 = api.GetSelfInfo()->y;
  193. x2 = api.CellToGrid(b.x);
  194. y2 = api.CellToGrid(b.y);
  195. if (api.GetPlaceType(b.x, b.y) == THUAI6::PlaceType::Window)
  196. {
  197. api.Move(step, atan2(y2 - y0, x2 - x0));
  198. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  199. api.Wait();
  200. x0 = api.GetSelfInfo()->x;
  201. y0 = api.GetSelfInfo()->y;
  202. }
  203. }
  204. void learn(IStudentAPI& api, node b)
  205. {
  206. int x2, y2;
  207. int x0 = api.GetSelfInfo()->x;
  208. int y0 = api.GetSelfInfo()->y;
  209. x2 = api.CellToGrid(b.x);
  210. y2 = api.CellToGrid(b.y);
  211. if (api.GetPlaceType(b.x, b.y) == THUAI6::PlaceType::ClassRoom)
  212. {
  213. api.Move(step, atan2(y2 - y0, x2 - x0));
  214. x0 = api.GetSelfInfo()->x;
  215. y0 = api.GetSelfInfo()->y;
  216. }
  217. api.StartLearning();
  218. api.EndAllAction();
  219. }
  220. void AI::play(IStudentAPI& api)
  221. {
  222. // 公共操作
  223. if (this->playerID == 0)
  224. {
  225. // 玩家0执行操作
  226. }
  227. else if (this->playerID == 1)
  228. {
  229. // 玩家1执行操作
  230. }
  231. else if (this->playerID == 2)
  232. {
  233. // 玩家2执行操作
  234. }
  235. else if (this->playerID == 3)
  236. {
  237. if (api.GetClassroomProgress(40, 12) < 100)
  238. {
  239. moveto(40, 13, api);
  240. }
  241. while (api.GetClassroomProgress(40, 12) < 10000000)
  242. {
  243. learn1(api);
  244. }
  245. api.EndAllAction();
  246. if (api.GetClassroomProgress(28, 26) < 100)
  247. {
  248. moveto(40, 25, api);
  249. moveto(28, 25, api);
  250. }
  251. while (api.GetClassroomProgress(28, 26) < 10000000)
  252. {
  253. learn1(api);
  254. }
  255. api.EndAllAction();
  256. if (api.GetClassroomProgress(33, 40) < 100)
  257. {
  258. moveto(29, 34, api);
  259. moveto(33, 39, api);
  260. }
  261. while (api.GetClassroomProgress(33, 40) < 10000000)
  262. {
  263. learn1(api);
  264. }
  265. api.EndAllAction();
  266. if (api.GetClassroomProgress(44, 32) < 100)
  267. {
  268. moveto(33, 31, api);
  269. moveto(44, 31, api);
  270. }
  271. while (api.GetClassroomProgress(44, 32) < 10000000)
  272. {
  273. learn1(api);
  274. }
  275. moveto(43, 31, api);
  276. moveto2(41, 35, api);
  277. moveto2(41, 39, api);
  278. moveto(46, 44, api);
  279. /*api.EndAllAction();
  280. moveto(45, 37, api);
  281. std::this_thread::sleep_for(std::chrono::milliseconds(2000));
  282. slide1(api);
  283. moveto(46, 44, api);
  284. api.EndAllAction();*/
  285. while (api.GetGateProgress(46, 45) < 18000)
  286. {
  287. api.StartOpenGate();
  288. }
  289. api.EndAllAction();
  290. api.Graduate();
  291. api.EndAllAction();
  292. // 玩家3执行操作
  293. }
  294. //当然可以写成if (this->playerID == 2||this->playerID == 3)之类的操作
  295. // 公共操作
  296. }
  297. void AI::play(ITrickerAPI& api)
  298. {
  299. auto self = api.GetSelfInfo();
  300. api.PrintSelfInfo();
  301. }