Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int inf = 1000000007;
- static int sense_depth = 3;
- static int winTile = 4;
- void winCheck () {
- line = 0;
- for (int i = -(winLine - 1); i < winLine; i++) { // вертикаль
- if (field[x][y] == field[x + i][y]) {
- addCords(x + i, y);
- line++;
- if (line == winLine) {
- endGame();
- break;
- }
- } else {
- line = 0;
- clearCords();
- }
- }
- for (int i = -(winLine - 1); i < winLine; i++) { // горизонталь
- if (field[x][y] == field[x][y + i]) {
- addCords(x , y + i);
- line++;
- if (line == winLine) {
- endGame();
- break;
- }
- } else {
- line = 0;
- clearCords();
- }
- }
- for (int i = -(winLine - 1); i < winLine; i++) { // левая диагональ [\]
- if (field[x][y] == field[x + i][y + i]) {
- addCords(x + i , y + i);
- line++;
- if (line == winLine) {
- endGame();
- break;
- }
- } else {
- line = 0;
- clearCords();
- }
- }
- for (int i = -(winLine - 1); i < winLine; i++) { // правая диагональ [/]
- if (field[x][y] == field[x + i][y - i]) {
- addCords(x + i, y - i);
- line++;
- if (line == winLine) {
- endGame();
- break;
- }
- } else {
- line = 0;
- clearCords();
- }
- }
- if (!turn.getText().equals("X wins!") && !turn.getText().equals("O wins!")) {
- for (int i = 0; i < tiles.size(); i++) {
- if (tiles.get(i).getText().equals("S")) {
- break;
- }
- if (i == tiles.size() - 1) {
- turn.setText("DRAW");
- turn.setTextColor(getResources().getColor(R.color.gray));
- restart.setVisibility(View.VISIBLE);
- for (int j = 0; j < tiles.size(); j++) {
- tiles.get(j).setTextColor(getResources().getColor(R.color.gray));
- }
- change = !change;
- }
- }
- }
- }
- int s(int side) {
- if (side == 1) {
- return 1;
- } else {
- return -1;
- }
- }
- public class node {
- int[][] field = new int[15][15];
- int n, side;
- public int[][] getField() {
- return field;
- }
- int get(int x, int y) {
- return field[3 + x][3 + y];
- }
- void add(int x, int y, int val) {
- field[3 + x][3 + y] = val;
- return;
- }
- int cost(int tp) {
- int cnt = 0;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- if (j > 0 && get(i, j - 1) == tp) {
- continue;
- }
- if (get(i, j) != tp) {
- continue;
- }
- int len = 1;
- while (j + len < n && get(i, j + len) == tp) {
- ++len;
- }
- if (len >= winTile) {
- return inf;
- }
- int a = 0;
- while (j - a - 1 >= 0 && get(i, j - a - 1) == 0) {
- ++a;
- }
- int b = 0;
- while (j + len + b < n && get(i, j + len + b) == 0) {
- ++b;
- }
- if (a + len + b < winTile) {
- continue;
- }
- int x = (1 << (len + 3));
- int bad = x / 5;
- if (a + len < winTile) {
- x -= bad;
- }
- if (b + len < winTile) {
- x -= bad;
- }
- cnt += x;
- }
- }
- //^^^^here horizontal, after diagonal>>>>>>>>>>>
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- if (i > 0 && j > 0 && get(i - 1, j - 1) == tp) {
- continue;
- }
- if (get(i, j) != tp) {
- continue;
- }
- int len = 1;
- while (i + len < n && j + len < n && get(i + len, j + len) == tp) {
- ++len;
- }
- if (len >= winTile) {
- return inf;
- }
- int a = 0;
- while (i - a - 1 >= 0 && j - a - 1 >= 0 && get(i - a - 1, j - a - 1) == 0) {
- ++a;
- }
- int b = 0;
- while (i + len + b < n && j + len + b < n && get(i + len + b, j + len + b) == 0) {
- ++b;
- }
- if (a + len + b < winTile) {
- continue;
- }
- int x = (1 << (len + 3));
- int bad = x / 5;
- if (a + len < winTile) {
- x -= bad;
- }
- if (b + len < winTile) {
- x -= bad;
- }
- cnt += x;
- }
- }
- return cnt;
- }
- int cost() {
- return (int)(Math.random()*1000);
- //return s(side) * (cost(side) - cost(((side - 1) ^ 1) + 1));
- }
- }
- public node copy(node ex) {
- node n = ex;
- n.field = new int[15][15];
- for (int i = 0; i < 15; i++) {
- for (int j = 0; j < 15; j++) {
- n.field[i][j] = ex.field[i][j];
- }
- }
- //n.getField();
- return n;
- }
- public int alphabeta(node f, int dep, int alpha, int beta, int side) {
- if (dep == 0 || Math.abs(f.cost()) > inf / 2) {
- return f.cost();
- }
- if (s(side) == 1) {
- int res = -inf;
- int did = 0;
- for (int i = 0; i < f.n; ++i) {
- for (int j = 0; j < f.n; ++j) {
- if (did == 1) {
- break;
- }
- if (f.get(i, j) == 0) {
- node to = copy(f);
- to.add(i, j, side);
- to.side = ((side - 1) ^ 1) + 1;
- res = Math.max(res, alphabeta(to, dep - 1, alpha, beta, ((side - 1) ^ 1) + 1));
- alpha = Math.max(alpha, res);
- if (alpha >= beta) {
- did = 1;
- break;
- }
- }
- }
- }
- return res;
- } else {
- int res = inf;
- int did = 0;
- for (int i = 0; i < f.n; ++i) {
- for (int j = 0; j < f.n; ++j) {
- if (did == 1) {
- break;
- }
- if (f.get(i, j) == 0) {
- node to = copy(f);
- to.add(i, j, side);
- to.side = ((side - 1) ^ 1) + 1;
- res = Math.min(res, alphabeta(to, dep - 1, alpha, beta, ((side - 1) ^ 1) + 1));
- beta = Math.min(alpha, res);
- if (alpha >= beta) {
- did = 1;
- break;
- }
- }
- }
- }
- return res;
- }
- }
- int get_type() {
- if (flexx) {
- return 1;
- } else {
- return 2;
- }
- }
- int[] solve() {
- int side = get_type();
- side = ((side - 1) ^ 1) + 1;
- node st = new node();
- st.side = get_type();
- st.field = new int[15][15];
- for (int i = 0; i < 15; i++) {
- for (int j = 0; j < 15; j++) {
- st.field[i][j] = field[i][j];
- }
- }
- st.n = w;
- P1winCount.setText(Integer.toString(st.cost()));
- int[] res = new int[2];
- res[0] = -1; res[1] = -1;
- int ans = -inf;
- for (int i = 0; i < st.n; ++i) {
- for (int j = 0; j < st.n; ++j) {
- if (st.get(i, j) != 0) {
- continue;
- }
- node to = copy(st);
- to.add(i, j, side);
- to.side = side;
- int x = s(side) * alphabeta(to, sense_depth, -inf, inf, ((side - 1) ^ 1) + 1);
- if (ans < x) {
- ans = x;
- res[0] = i; res[1] = j;
- }
- }
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment