Guest User

Untitled

a guest
Jan 30th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.39 KB | None | 0 0
  1. static int inf = 1000000007;
  2. static int sense_depth = 3;
  3. static int winTile = 4;
  4.  
  5. void winCheck () {
  6. line = 0;
  7. for (int i = -(winLine - 1); i < winLine; i++) { // вертикаль
  8. if (field[x][y] == field[x + i][y]) {
  9. addCords(x + i, y);
  10. line++;
  11. if (line == winLine) {
  12. endGame();
  13. break;
  14. }
  15. } else {
  16. line = 0;
  17. clearCords();
  18. }
  19. }
  20. for (int i = -(winLine - 1); i < winLine; i++) { // горизонталь
  21. if (field[x][y] == field[x][y + i]) {
  22. addCords(x , y + i);
  23. line++;
  24. if (line == winLine) {
  25. endGame();
  26. break;
  27. }
  28. } else {
  29. line = 0;
  30. clearCords();
  31. }
  32. }
  33. for (int i = -(winLine - 1); i < winLine; i++) { // левая диагональ [\]
  34. if (field[x][y] == field[x + i][y + i]) {
  35. addCords(x + i , y + i);
  36. line++;
  37. if (line == winLine) {
  38. endGame();
  39. break;
  40. }
  41. } else {
  42. line = 0;
  43. clearCords();
  44. }
  45. }
  46. for (int i = -(winLine - 1); i < winLine; i++) { // правая диагональ [/]
  47. if (field[x][y] == field[x + i][y - i]) {
  48. addCords(x + i, y - i);
  49. line++;
  50. if (line == winLine) {
  51. endGame();
  52. break;
  53. }
  54. } else {
  55. line = 0;
  56. clearCords();
  57. }
  58. }
  59. if (!turn.getText().equals("X wins!") && !turn.getText().equals("O wins!")) {
  60. for (int i = 0; i < tiles.size(); i++) {
  61. if (tiles.get(i).getText().equals("S")) {
  62. break;
  63. }
  64. if (i == tiles.size() - 1) {
  65. turn.setText("DRAW");
  66. turn.setTextColor(getResources().getColor(R.color.gray));
  67. restart.setVisibility(View.VISIBLE);
  68. for (int j = 0; j < tiles.size(); j++) {
  69. tiles.get(j).setTextColor(getResources().getColor(R.color.gray));
  70. }
  71. change = !change;
  72. }
  73. }
  74. }
  75. }
  76.  
  77. int s(int side) {
  78. if (side == 1) {
  79. return 1;
  80. } else {
  81. return -1;
  82. }
  83. }
  84.  
  85. public class node {
  86. int[][] field = new int[15][15];
  87. int n, side;
  88.  
  89. public int[][] getField() {
  90. return field;
  91. }
  92.  
  93. int get(int x, int y) {
  94. return field[3 + x][3 + y];
  95. }
  96.  
  97. void add(int x, int y, int val) {
  98. field[3 + x][3 + y] = val;
  99. return;
  100. }
  101.  
  102. int cost(int tp) {
  103. int cnt = 0;
  104. for (int i = 0; i < n; ++i) {
  105. for (int j = 0; j < n; ++j) {
  106. if (j > 0 && get(i, j - 1) == tp) {
  107. continue;
  108. }
  109. if (get(i, j) != tp) {
  110. continue;
  111. }
  112. int len = 1;
  113. while (j + len < n && get(i, j + len) == tp) {
  114. ++len;
  115. }
  116. if (len >= winTile) {
  117. return inf;
  118. }
  119. int a = 0;
  120. while (j - a - 1 >= 0 && get(i, j - a - 1) == 0) {
  121. ++a;
  122. }
  123. int b = 0;
  124. while (j + len + b < n && get(i, j + len + b) == 0) {
  125. ++b;
  126. }
  127. if (a + len + b < winTile) {
  128. continue;
  129. }
  130. int x = (1 << (len + 3));
  131. int bad = x / 5;
  132. if (a + len < winTile) {
  133. x -= bad;
  134. }
  135. if (b + len < winTile) {
  136. x -= bad;
  137. }
  138. cnt += x;
  139. }
  140. }
  141. //^^^^here horizontal, after diagonal>>>>>>>>>>>
  142. for (int i = 0; i < n; ++i) {
  143. for (int j = 0; j < n; ++j) {
  144. if (i > 0 && j > 0 && get(i - 1, j - 1) == tp) {
  145. continue;
  146. }
  147. if (get(i, j) != tp) {
  148. continue;
  149. }
  150. int len = 1;
  151. while (i + len < n && j + len < n && get(i + len, j + len) == tp) {
  152. ++len;
  153. }
  154. if (len >= winTile) {
  155. return inf;
  156. }
  157. int a = 0;
  158. while (i - a - 1 >= 0 && j - a - 1 >= 0 && get(i - a - 1, j - a - 1) == 0) {
  159. ++a;
  160. }
  161. int b = 0;
  162. while (i + len + b < n && j + len + b < n && get(i + len + b, j + len + b) == 0) {
  163. ++b;
  164. }
  165. if (a + len + b < winTile) {
  166. continue;
  167. }
  168. int x = (1 << (len + 3));
  169. int bad = x / 5;
  170. if (a + len < winTile) {
  171. x -= bad;
  172. }
  173. if (b + len < winTile) {
  174. x -= bad;
  175. }
  176. cnt += x;
  177. }
  178. }
  179. return cnt;
  180. }
  181.  
  182. int cost() {
  183. return (int)(Math.random()*1000);
  184. //return s(side) * (cost(side) - cost(((side - 1) ^ 1) + 1));
  185. }
  186. }
  187.  
  188.  
  189. public node copy(node ex) {
  190. node n = ex;
  191. n.field = new int[15][15];
  192. for (int i = 0; i < 15; i++) {
  193. for (int j = 0; j < 15; j++) {
  194. n.field[i][j] = ex.field[i][j];
  195. }
  196. }
  197. //n.getField();
  198. return n;
  199. }
  200.  
  201. public int alphabeta(node f, int dep, int alpha, int beta, int side) {
  202. if (dep == 0 || Math.abs(f.cost()) > inf / 2) {
  203. return f.cost();
  204. }
  205. if (s(side) == 1) {
  206. int res = -inf;
  207. int did = 0;
  208. for (int i = 0; i < f.n; ++i) {
  209. for (int j = 0; j < f.n; ++j) {
  210. if (did == 1) {
  211. break;
  212. }
  213. if (f.get(i, j) == 0) {
  214. node to = copy(f);
  215. to.add(i, j, side);
  216. to.side = ((side - 1) ^ 1) + 1;
  217. res = Math.max(res, alphabeta(to, dep - 1, alpha, beta, ((side - 1) ^ 1) + 1));
  218. alpha = Math.max(alpha, res);
  219. if (alpha >= beta) {
  220. did = 1;
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. return res;
  227. } else {
  228. int res = inf;
  229. int did = 0;
  230. for (int i = 0; i < f.n; ++i) {
  231. for (int j = 0; j < f.n; ++j) {
  232. if (did == 1) {
  233. break;
  234. }
  235. if (f.get(i, j) == 0) {
  236. node to = copy(f);
  237. to.add(i, j, side);
  238. to.side = ((side - 1) ^ 1) + 1;
  239. res = Math.min(res, alphabeta(to, dep - 1, alpha, beta, ((side - 1) ^ 1) + 1));
  240. beta = Math.min(alpha, res);
  241. if (alpha >= beta) {
  242. did = 1;
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. return res;
  249. }
  250. }
  251.  
  252. int get_type() {
  253. if (flexx) {
  254. return 1;
  255. } else {
  256. return 2;
  257. }
  258. }
  259.  
  260. int[] solve() {
  261. int side = get_type();
  262. side = ((side - 1) ^ 1) + 1;
  263. node st = new node();
  264. st.side = get_type();
  265. st.field = new int[15][15];
  266. for (int i = 0; i < 15; i++) {
  267. for (int j = 0; j < 15; j++) {
  268. st.field[i][j] = field[i][j];
  269. }
  270. }
  271. st.n = w;
  272. P1winCount.setText(Integer.toString(st.cost()));
  273. int[] res = new int[2];
  274. res[0] = -1; res[1] = -1;
  275. int ans = -inf;
  276. for (int i = 0; i < st.n; ++i) {
  277. for (int j = 0; j < st.n; ++j) {
  278. if (st.get(i, j) != 0) {
  279. continue;
  280. }
  281. node to = copy(st);
  282. to.add(i, j, side);
  283. to.side = side;
  284. int x = s(side) * alphabeta(to, sense_depth, -inf, inf, ((side - 1) ^ 1) + 1);
  285. if (ans < x) {
  286. ans = x;
  287. res[0] = i; res[1] = j;
  288. }
  289. }
  290. }
  291. return res;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment