Advertisement
jonator

aijona

Nov 22nd, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1.  
  2. #include "Player.hh"
  3. #include <queue>
  4. #include <list>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. /**
  10. * Escriu el nom * del teu jugador i guarda
  11. * aquest fitxer amb el nom AI*.cc
  12. */
  13. #define PLAYER_NAME Jonator
  14.  
  15.  
  16. /**
  17. * Podeu declarar constants aquí
  18. */
  19.  
  20. const int CAOTIC = 1;
  21. const int CICLIC = 2;
  22.  
  23. const int X[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
  24. const int Y[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
  25.  
  26.  
  27.  
  28. struct PLAYER_NAME : public Player {
  29.  
  30.  
  31. /**
  32. * Factory: retorna una nova instància d'aquesta classe.
  33. * No toqueu aquesta funció.
  34. */
  35. static Player* factory () {
  36. return new PLAYER_NAME;
  37. }
  38.  
  39.  
  40. /**
  41. * Els atributs dels vostres jugadors es poden definir aquí.
  42. */
  43.  
  44. typedef vector<int> VE;
  45. typedef map<int, int> MEE;
  46. typedef MEE::iterator MI;
  47. typedef vector<vector<P> > G;
  48. typedef vector<VE> VVE;
  49. typedef vector<bool> VB;
  50. typedef vector<VB> VVB;
  51. typedef vector<vector<bool> > Graf;
  52. typedef vector<vector<Posicio> > Graf2;
  53.  
  54. //MEE personalitat; // Guarda la personalitat dels soldats entre torns.
  55.  
  56. Posicio bfs(int i, int j) {
  57. bool primer = true;
  58. queue< Posicio > q;
  59. q.push(Posicio(i, j));
  60. Graf g (60, vector<bool> (60, false));
  61. Graf2 gg (60, vector<Posicio> (60));
  62. while (not q.empty()) {
  63. Posicio pos = q.front(); q.pop();
  64. if (quin_soldat(pos.x,pos.y) > 0) {return gg[pos.x][pos.y];}
  65. if (valor_post(pos.x,pos.y) > 0) {return gg[pos.x][pos.y];}
  66. g[pos.x][pos.y] = true;
  67. if (primer) {
  68. for(int i=0; i<8; ++i)
  69. {
  70. int a = pos.x+X[i], b = pos.y+Y[i];
  71. if(valid(a, b) and not g[a][b] and (que(a,b) == GESPA or que(a,b) == BOSC)) {
  72. q.push(Posicio(a, b));
  73. gg[a][b].x = a;
  74. gg[a][b].y = b;
  75. }
  76. }
  77. primer = false;
  78. } else {
  79. for(int i=0; i<8; ++i)
  80. {
  81. int a = pos.x+X[i], b = pos.y+Y[i];
  82. if(valid(a, b) and not g[a][b] and (que(a,b) == GESPA or que(a,b) == BOSC)) {
  83. q.push(Posicio(a, b));
  84. gg[a][b].x = gg[pos.x][pos.y].x;
  85. gg[a][b].y = gg[pos.x][pos.y].y;
  86. }
  87. }
  88. }
  89. }
  90. return Posicio(i,j);
  91. }
  92.  
  93. void juga_soldat(int equip, int id) {
  94. Info in = dades(id);
  95. int x = in.pos.x;
  96. int y = in.pos.y;
  97. Posicio p = bfs(x,y);
  98. //if (xx >= 0 and xx < MAX and yy >= 0 and yy < MAX)
  99. ordena_soldat(id, p.x, p.y);
  100. }
  101.  
  102. /* void juga_soldat(int equip, int id) {
  103. MI p = personalitat.find(id);
  104. // Els soldats que encara no estan decidits, els fem cíclics
  105. // amb probabilitat 0.4, i caòtics altrament.
  106. if (p == personalitat.end())
  107. //personalitat[id] = (probabilitat(0.4) ? CICLIC : CAOTIC);
  108. personalitat[id] = CICLIC;
  109. int pers = personalitat[id];
  110.  
  111. Info in = dades(id);
  112. int x = in.pos.x;
  113. int y = in.pos.y;
  114. for (int i = 0; i < 8; ++i) {
  115. int xx = x + X[i];
  116. int yy = y + Y[i];
  117. if (xx >= 0 and xx < MAX and yy >= 0 and yy < MAX) {
  118. int id2 = quin_soldat(xx, yy);
  119. // Si tenim un enemic al costat, l'ataquem.
  120. if (id2 and dades(id2).equip != equip) {
  121. ordena_soldat(id, xx, yy);
  122. return;
  123. }
  124. }
  125. }
  126. //P pos = BFS();
  127. //if (xx >= 0 and xx < MAX and yy >= 0 and yy < MAX)
  128. //ordena_soldat(id, xx, yy);
  129. }
  130. */
  131.  
  132. void juga_heli(int equip, int id) {
  133. Info in = dades(id);
  134. int x = in.pos.x;
  135. int y = in.pos.y;
  136. int suma = 0;
  137. for (int i = -ABAST; i <= ABAST; ++i)
  138. for (int j = -ABAST; j <= ABAST; ++j) {
  139. int xx = x + i;
  140. int yy = y + j;
  141. if (xx >= 0 and xx < MAX and yy >= 0 and yy < MAX) {
  142. int id2 = quin_soldat(xx, yy);
  143. if (id2 and dades(id2).equip != equip) ++suma;
  144. }
  145. }
  146. // Si tenim 3 o més enemics a sota, intentem llençar napalm.
  147. if (suma >= 3 and dades(id).napalm == 0) {
  148. ordena_helicopter(id, NAPALM);
  149. return;
  150. }
  151.  
  152. // Amb probabilitat 20% girem en el sentit contrari de les agulles del rellotge,
  153. // altrament intentem avançar dos passos.
  154. int c = uniforme(1, 5);
  155. ordena_helicopter(id, c == 1 ? CONTRA_RELLOTGE : AVANCA2);
  156. }
  157.  
  158.  
  159. void llenca_paracaigudista(int heli_id) {
  160. // Obtenim les dades de l'helicòpter...
  161. Info in = dades(heli_id);
  162. // ... i intentem llencar un paracaigudista, sense ni mirar el terreny.
  163. /*int xx = in.pos.x + uniforme(-2, 2);
  164. int yy = in.pos.y + uniforme(-2, 2);
  165. if (xx >= 0 and xx < MAX and yy >= 0 and yy < MAX)
  166. ordena_paracaigudista(xx, yy);*/
  167. int xx = in.pos.x;
  168. int yy = in.pos.y;
  169. }
  170.  
  171. /**
  172. * Mètode play.
  173. *
  174. * Aquest mètode serà invocat una vegada cada torn.
  175. */
  176. virtual void play () {
  177.  
  178. int equip = qui_soc();
  179. VE H = helis(equip); // helicòpters del meu equip
  180. VE S = soldats(equip); // soldats del meu equip
  181.  
  182. // si en un helicòpter escollit a l'atzar tinc paracaigudistes, en llenço un.
  183. for (int i = 0; i < (int)H.size(); ++i) {
  184. int heli_id = H[i];
  185. if (not dades(heli_id).paraca.empty()) llenca_paracaigudista(heli_id);
  186. }
  187. //if (not dades(heli_id).paraca.empty()) llenca_paracaigudista(heli_id);
  188.  
  189. for (int i = 0; i < (int)H.size(); ++i) juga_heli(equip, H[i]);
  190. for (int i = 0; i < (int)S.size(); ++i) juga_soldat(equip, S[i]);
  191. }
  192.  
  193.  
  194. };
  195.  
  196.  
  197. /**
  198. * No toqueu aquesta línia.
  199. */
  200. RegisterPlayer(PLAYER_NAME);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement