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.

player5.cpp 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #include <vector>
  2. #include <thread>
  3. #include <array>
  4. #include<math.h>
  5. #include "AI.h"
  6. #include "constants.h"
  7. int flag1 = 0, flag2 = 0, flag3 = 0;
  8. long long c = 0;
  9. int ii = -1, jj = 0, iii = -1;
  10. int count = 0;
  11. // 为假则play()期间确保游戏状态不更新,为真则只保证游戏状态在调用相关方法时不更新
  12. extern const bool asynchronous = false;
  13. // 选手需要依次将player0到player4的职业在这里定义
  14. extern const std::array<THUAI6::StudentType, 4> studentType = {
  15. THUAI6::StudentType::Athlete,
  16. THUAI6::StudentType::Athlete,
  17. THUAI6::StudentType::Athlete,
  18. THUAI6::StudentType::Athlete };
  19. extern const THUAI6::TrickerType trickerType = THUAI6::TrickerType::ANoisyPerson;
  20. // 可以在AI.cpp内部声明变量与函数
  21. long long UpLeft(int dx, int dy, int x, int y, ITrickerAPI& api) //会输出一串形如101000111的数字(数字应有dx+dy-1位,若不足,则在高位自动补足0)//
  22. { //从右往左每个数字代表一步,0代表向上一步,1代表向左一步//
  23. int a = 0, flag = 0;
  24. if (dx == 0)
  25. {
  26. for (int i = 1; i < dy; i++)
  27. {
  28. if (api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) - i) == THUAI6::PlaceType::Wall|| api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) - i) == THUAI6::PlaceType::Window)
  29. {
  30. flag = 1; break;
  31. }
  32. else a = a * 10 + 1;
  33. }
  34. if (flag == 1) return 2;
  35. else return a;
  36. }
  37. flag = 0;
  38. if (dy == 0)
  39. {
  40. for (int i = 0; i < dx; i++)
  41. {
  42. if (api.GetPlaceType(api.GridToCell(x) - i, api.GridToCell(y)) == THUAI6::PlaceType::Wall|| api.GetPlaceType(api.GridToCell(x) - i, api.GridToCell(y)) == THUAI6::PlaceType::Window)
  43. {
  44. flag = 1; break;
  45. }
  46. else a = a * 10 + 0;
  47. }
  48. if (flag == 1) return 2;
  49. else return a;
  50. }
  51. if (UpLeft(dx, dy - 1, x, y - 1000, api) != 2 && api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) - 1) != THUAI6::PlaceType::Wall&& api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) - 1) != THUAI6::PlaceType::Window)
  52. {
  53. return 10 * UpLeft(dx, dy-1, x , y-1000, api) + 1;
  54. }
  55. if (UpLeft(dx - 1, dy, x - 1000, y, api) != 2 && api.GetPlaceType(api.GridToCell(x)-1, api.GridToCell(y)) != THUAI6::PlaceType::Wall && api.GetPlaceType(api.GridToCell(x)-1, api.GridToCell(y)) != THUAI6::PlaceType::Window)
  56. {
  57. return 10 * UpLeft(dx - 1, dy, x - 1000, y, api);
  58. }
  59. else return 2;
  60. }
  61. long long UpRight(int dx, int dy, int x, int y, ITrickerAPI& api) //会输出一串形如101000111的数字(数字应有dx+dy-1位,若不足,则在高位自动补足0)//
  62. { //从右往左每个数字代表一步,0代表向上一步,1代表向右一步//
  63. int a = 0, flag = 0;
  64. if (dx == 0)
  65. {
  66. for (int i = 1; i < dy; i++)
  67. {
  68. if (api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) + i) == THUAI6::PlaceType::Wall || api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) + i) == THUAI6::PlaceType::Window)
  69. {
  70. flag = 1; break;
  71. }
  72. else a = a * 10 + 1;
  73. }
  74. if (flag == 1) return 2;
  75. else return a;
  76. }
  77. flag = 0;
  78. if (dy == 0)
  79. {
  80. for (int i = 0; i < dx; i++)
  81. {
  82. if (api.GetPlaceType(api.GridToCell(x) - i, api.GridToCell(y)) == THUAI6::PlaceType::Wall || api.GetPlaceType(api.GridToCell(x) - i, api.GridToCell(y)) == THUAI6::PlaceType::Window)
  83. {
  84. flag = 1; break;
  85. }
  86. else a = a * 10 + 0;
  87. }
  88. if (flag == 1) return 2;
  89. else return a;
  90. }
  91. if (UpLeft(dx, dy - 1, x, y + 1000, api) != 2 && api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) + 1) != THUAI6::PlaceType::Wall && api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) + 1) != THUAI6::PlaceType::Window)
  92. {
  93. return 10 * UpLeft(dx, dy - 1, x, y + 1000, api) + 1;
  94. }
  95. if (UpLeft(dx - 1, dy, x - 1000, y, api) != 2 && api.GetPlaceType(api.GridToCell(x) - 1, api.GridToCell(y)) != THUAI6::PlaceType::Wall && api.GetPlaceType(api.GridToCell(x) - 1, api.GridToCell(y)) != THUAI6::PlaceType::Window)
  96. {
  97. return 10 * UpLeft(dx - 1, dy, x - 1000, y, api);
  98. }
  99. else return 2;
  100. }
  101. long long DownLeft(int dx, int dy, int x, int y, ITrickerAPI& api) //会输出一串形如101000111的数字(数字应有dx+dy-1位,若不足,则在高位自动补足0)//
  102. { //从右往左每个数字代表一步,0代表向下一步,1代表向左一步//
  103. int a = 0, flag = 0;
  104. if (dx == 0)
  105. {
  106. for (int i = 1; i < dy; i++)
  107. {
  108. if (api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) - i) == THUAI6::PlaceType::Wall || api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) - i) == THUAI6::PlaceType::Window)
  109. {
  110. flag = 1; break;
  111. }
  112. else a = a * 10 + 1;
  113. }
  114. if (flag == 1) return 2;
  115. else return a;
  116. }
  117. flag = 0;
  118. if (dy == 0)
  119. {
  120. for (int i = 0; i < dx; i++)
  121. {
  122. if (api.GetPlaceType(api.GridToCell(x) + i, api.GridToCell(y)) == THUAI6::PlaceType::Wall || api.GetPlaceType(api.GridToCell(x) + i, api.GridToCell(y)) == THUAI6::PlaceType::Window)
  123. {
  124. flag = 1; break;
  125. }
  126. else a = a * 10 + 0;
  127. }
  128. if (flag == 1) return 2;
  129. else return a;
  130. }
  131. if (UpLeft(dx, dy - 1, x, y - 1000, api) != 2 && api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) - 1) != THUAI6::PlaceType::Wall && api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) - 1) != THUAI6::PlaceType::Window)
  132. {
  133. return 10 * UpLeft(dx, dy - 1, x, y - 1000, api) + 1;
  134. }
  135. if (UpLeft(dx - 1, dy, x + 1000, y, api) != 2 && api.GetPlaceType(api.GridToCell(x) + 1, api.GridToCell(y)) != THUAI6::PlaceType::Wall && api.GetPlaceType(api.GridToCell(x) + 1, api.GridToCell(y)) != THUAI6::PlaceType::Window)
  136. {
  137. return 10 * UpLeft(dx - 1, dy, x + 1000, y, api);
  138. }
  139. else return 2;
  140. }
  141. long long DownRight(int dx, int dy, int x, int y, ITrickerAPI& api) //会输出一串形如101000111的数字(数字应有dx+dy-1位,若不足,则在高位自动补足0)//
  142. { //从右往左每个数字代表一步,0代表向下一步,1代表向右一步//
  143. int a = 0, flag = 0;
  144. if (dx == 0)
  145. {
  146. for (int i = 1; i < dy; i++)
  147. {
  148. if (api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) + i) == THUAI6::PlaceType::Wall || api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) + i) == THUAI6::PlaceType::Window)
  149. {
  150. flag = 1; break;
  151. }
  152. else a = a * 10 + 1;
  153. }
  154. if (flag == 1) return 2;
  155. else return a;
  156. }
  157. flag = 0;
  158. if (dy == 0)
  159. {
  160. for (int i = 0; i < dx; i++)
  161. {
  162. if (api.GetPlaceType(api.GridToCell(x) + i, api.GridToCell(y)) == THUAI6::PlaceType::Wall || api.GetPlaceType(api.GridToCell(x) + i, api.GridToCell(y)) == THUAI6::PlaceType::Window)
  163. {
  164. flag = 1; break;
  165. }
  166. else a = a * 10 + 0;
  167. }
  168. if (flag == 1) return 2;
  169. else return a;
  170. }
  171. if (UpLeft(dx, dy - 1, x, y + 1000, api) != 2 && api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) + 1) != THUAI6::PlaceType::Wall && api.GetPlaceType(api.GridToCell(x), api.GridToCell(y) + 1) != THUAI6::PlaceType::Window)
  172. {
  173. return 10 * UpLeft(dx, dy - 1, x, y + 1000, api) + 1;
  174. }
  175. if (UpLeft(dx - 1, dy, x + 1000, y, api) != 2 && api.GetPlaceType(api.GridToCell(x) + 1, api.GridToCell(y)) != THUAI6::PlaceType::Wall && api.GetPlaceType(api.GridToCell(x) + 1, api.GridToCell(y)) != THUAI6::PlaceType::Window)
  176. {
  177. return 10 * UpLeft(dx - 1, dy, x + 1000, y, api);
  178. }
  179. else return 2;
  180. }
  181. void DownRight(long long c, ITrickerAPI& api)
  182. {
  183. if (c % 10 == 0)
  184. {
  185. api.MoveDown(259.6053998);
  186. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  187. }
  188. if (c % 10 == 1)
  189. {
  190. api.MoveRight(259.6053998);
  191. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  192. }
  193. return;
  194. }
  195. void DownLeft(long long c, ITrickerAPI& api)
  196. {
  197. if (c % 10 == 0)
  198. {
  199. api.MoveDown(259.6053998);
  200. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  201. }
  202. if (c % 10 == 1)
  203. {
  204. api.MoveLeft(259.6053998);
  205. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  206. }
  207. return;
  208. }
  209. void UpRight(long long c, ITrickerAPI& api)
  210. {
  211. if (c % 10 == 0)
  212. {
  213. api.MoveUp(259.6053998);
  214. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  215. }
  216. if (c % 10 == 1)
  217. {
  218. api.MoveRight(259.6053998);
  219. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  220. }
  221. return;
  222. }
  223. void UpLeft(long long c, ITrickerAPI& api)
  224. {
  225. if (c % 10 == 0)
  226. {
  227. api.MoveUp(259.6053998);
  228. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  229. }
  230. if (c % 10 == 1)
  231. {
  232. api.MoveLeft(259.6053998);
  233. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  234. }
  235. return;
  236. }
  237. void AI::play(IStudentAPI& api)
  238. {
  239. // 公共操作
  240. if (this->playerID == 2)
  241. {
  242. }
  243. // 当然可以写成if (this->playerID == 2||this->playerID == 3)之类的操作
  244. // 公共操作
  245. }
  246. void AI::play(ITrickerAPI& api)
  247. {
  248. double dx, dy, aa=0;
  249. int ddx, ddy;
  250. auto self = api.GetSelfInfo();
  251. auto st = api.GetStudents();
  252. if (!st.empty())
  253. {
  254. api.UseSkill(0);
  255. std::this_thread::sleep_for(std::chrono::milliseconds(810));
  256. if (flag1 == 0)
  257. {
  258. dx = st[0]->x - self->x;
  259. dy = st[0]->y - self->y;
  260. ddx = abs(api.GridToCell(dx));
  261. ddy = abs(api.GridToCell(dy));
  262. if (dx >= 0&&dy >= 0) { c = DownRight(ddx, ddy, self->x, self->y, api); jj = 1; }
  263. if (dx >= 0&&dy < 0) { c = DownLeft(ddx, ddy, self->x, self->y, api); jj = 2; }
  264. if (dx < 0&&dy >= 0) { c = UpRight(ddx, ddy, self->x, self->y, api); jj = 3; }
  265. if (dx < 0&&dy < 0) { c = UpLeft(ddx, ddy, self->x, self->y, api); jj = 4; }
  266. ii = ddx + ddy - 1;
  267. flag1 = 1;
  268. }
  269. if (ii > 0 && c != 2 && jj == 1)
  270. {
  271. DownRight(c, api);
  272. c = c / 10;
  273. ii--;
  274. }
  275. if (ii > 0 && c != 2 && jj == 2)
  276. {
  277. DownLeft(c, api);
  278. c = c / 10;
  279. ii--;
  280. }
  281. if (ii > 0 && c != 2 && jj == 3)
  282. {
  283. UpRight(c, api);
  284. c = c / 10;
  285. ii--;
  286. }
  287. if (ii > 0 && c != 2 && jj == 4)
  288. {
  289. UpLeft(c, api);
  290. c = c / 10;
  291. ii--;
  292. }
  293. if (ii == 0)
  294. {
  295. auto self = api.GetSelfInfo();
  296. auto st = api.GetStudents();
  297. if (!st.empty())
  298. {
  299. if (api.GridToCell(st[0]->x) == api.GridToCell(self->x) && st[0]->y > self->y) aa = 1.5707963;
  300. if (api.GridToCell(st[0]->x) == api.GridToCell(self->x) && st[0]->y < self->y) aa = 4.7123889;
  301. if (api.GridToCell(st[0]->y) == api.GridToCell(self->y) && st[0]->x > self->x) aa = 0;
  302. if (api.GridToCell(st[0]->y) == api.GridToCell(self->y) && st[0]->x < self->x) aa = 3.1415926;
  303. api.Attack(aa);
  304. }
  305. }
  306. }
  307. if(st.empty())
  308. {
  309. if (count==0)
  310. {
  311. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  312. api.MoveUp(2 * 259.6053998);
  313. std::this_thread::sleep_for(std::chrono::milliseconds(540));
  314. }
  315. if (count < 13)
  316. {
  317. api.MoveLeft(259.6053998);
  318. std::this_thread::sleep_for(std::chrono::milliseconds(270));
  319. count++;
  320. }
  321. if (count == 13)
  322. {
  323. api.MoveDown(2 * 259.6053998);
  324. std::this_thread::sleep_for(std::chrono::milliseconds(530));
  325. count++;
  326. }
  327. }
  328. if (count == 14)
  329. {
  330. auto self = api.GetSelfInfo();
  331. c = DownRight(9, 5, self->x, self->y, api);
  332. iii = 13; count++; ii = -1;
  333. }
  334. if (count == 15)
  335. {
  336. if (iii > 0 && c != 2)
  337. {
  338. DownRight(c, api);
  339. c = c / 10;
  340. iii--;
  341. }
  342. if (iii == 1)
  343. {
  344. api.MoveDown(259.6053998);
  345. std::this_thread::sleep_for(std::chrono::milliseconds(280));
  346. count++; flag1 = 0;
  347. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  348. }
  349. }
  350. if (st.empty() && api.GridToCell(self->x) == 23 && api.GridToCell(self->y) == 12 && count == 16)
  351. {
  352. count++;
  353. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  354. api.MoveRight(259.6053998 * 2);
  355. std::this_thread::sleep_for(std::chrono::milliseconds(550));
  356. api.MoveDown(259.6053998 * 13);
  357. std::this_thread::sleep_for(std::chrono::milliseconds(3500));
  358. ii = -1;
  359. }
  360. if (count < 26 && count >= 17)
  361. {
  362. api.MoveRight(259.6053998);
  363. std::this_thread::sleep_for(std::chrono::milliseconds(280));
  364. count++;
  365. }
  366. if (count == 26)
  367. {
  368. ii = 0;
  369. }
  370. }