|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +int** makeAMove(int** board, int x, int y, int player,int isCheck); |
| 6 | + |
| 7 | +vector<pair<int, int>*>* possibleMoves(int** board, int player) { |
| 8 | + vector<pair<int, int>*>* output = new vector<pair<int, int>*>(); |
| 9 | + for (int i = 0; i < 4; i++) { |
| 10 | + for (int j = 0; j < 4; j++) { |
| 11 | + if (board[i][j] == 0) { |
| 12 | + if (makeAMove(board, i, j, player, true)) { |
| 13 | + pair<int, int>* p = new pair<int, int>(i, j); |
| 14 | + output->push_back(p); |
| 15 | + } |
| 16 | + } |
| 17 | + } } |
| 18 | + return output; |
| 19 | +} |
| 20 | + |
| 21 | +int evaluateBoard(int** board, bool& completed) { |
| 22 | + if (possibleMoves(board, 1)->size() != 0 || possibleMoves(board, 2)->size() != 0) { |
| 23 | + completed = false; |
| 24 | + return 0; |
| 25 | + } |
| 26 | + // cout << "here" << endl; |
| 27 | + completed = true; |
| 28 | + int count[3] = {}; |
| 29 | + for (int i = 0; i < 4; i++) { |
| 30 | + for (int j = 0; j < 4; j++) { |
| 31 | + count[board[i][j]]++; |
| 32 | + } |
| 33 | + } |
| 34 | + return count[1] - count[2]; |
| 35 | +} |
| 36 | + |
| 37 | +int** makeAMove(int** board, int x, int y, int player,int isCheck) { |
| 38 | + |
| 39 | + int** newBoard; |
| 40 | + |
| 41 | + if(isCheck){ |
| 42 | + newBoard = board; |
| 43 | + }else{ |
| 44 | + newBoard = new int*[4]; |
| 45 | + for (int i = 0; i < 4; i++) { |
| 46 | + newBoard[i] = new int[4]; |
| 47 | + for (int j = 0; j < 4; j++) { |
| 48 | + newBoard[i][j] = board[i][j]; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + int xDir[] = {-1,-1,0,1,1,1,0,-1}; |
| 54 | + int yDir[] = {0,1,1,1,0,-1,-1,-1}; |
| 55 | + if(x < 0 || x >= 4 || y < 0 || y >= 4 || board[x][y] != 0){ |
| 56 | + return NULL; |
| 57 | + } |
| 58 | + bool movePossible = false; |
| 59 | + for(int i = 0; i < 8; i++){ |
| 60 | + int xStep = xDir[i]; |
| 61 | + int yStep = yDir[i]; |
| 62 | + int currentX = x + xStep; |
| 63 | + int currentY = y + yStep; |
| 64 | + int count = 0; |
| 65 | + while(currentX >= 0 && currentX < 4 && currentY >= 0 && currentY < 4){ |
| 66 | + |
| 67 | + if(newBoard[currentX][currentY] == 0){ |
| 68 | + break; |
| 69 | + }else if(newBoard[currentX][currentY] != player){ |
| 70 | + currentX += xStep; |
| 71 | + currentY += yStep; |
| 72 | + count++; |
| 73 | + }else{ |
| 74 | + if(count > 0){ |
| 75 | + movePossible = true; |
| 76 | + if(isCheck){ |
| 77 | + return newBoard; |
| 78 | + } |
| 79 | + int convertX = currentX - xStep; |
| 80 | + int convertY = currentY - yStep; |
| 81 | + while(convertX != x || convertY != y){ |
| 82 | + newBoard[convertX][convertY] = player; |
| 83 | + convertX = convertX - xStep; |
| 84 | + convertY = convertY - yStep; |
| 85 | + } |
| 86 | + newBoard[x][y] = player; |
| 87 | + } |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + if(movePossible){ |
| 93 | + return newBoard; |
| 94 | + }else{ |
| 95 | + return NULL; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +int kkk = 0; |
| 100 | + |
| 101 | +void printBoard(int** board) { |
| 102 | + for (int i = 0 ; i < 4; i++) { |
| 103 | + for(int j = 0; j < 4; j++) { |
| 104 | + cout << board[i][j] << " "; |
| 105 | + } |
| 106 | + cout << endl; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +int findNextStep(int** board, bool maximizerTurn, int& x, int& y) { |
| 111 | + // check if done with play if yes return score of evaluate function |
| 112 | + bool completed; |
| 113 | + int score = evaluateBoard(board, completed); |
| 114 | + printBoard(board); |
| 115 | + if (completed) { |
| 116 | + return score; |
| 117 | + } |
| 118 | + |
| 119 | + // find all possible moves |
| 120 | + int player = maximizerTurn? 1 : 2; |
| 121 | + vector<pair<int, int>*>* options = possibleMoves(board, player); |
| 122 | + if (options->size() == 0) { |
| 123 | + x = -1; |
| 124 | + y = -1; |
| 125 | + int dummyX, dummyY; |
| 126 | + return findNextStep(board, !maximizerTurn, dummyX, dummyY); |
| 127 | + } |
| 128 | + int best; |
| 129 | + if (maximizerTurn) { |
| 130 | + best = INT_MIN; |
| 131 | + } else { |
| 132 | + best = INT_MAX; |
| 133 | + } |
| 134 | + // for each move |
| 135 | + for (int i = 0; i < options->size(); i++) { |
| 136 | + // Make the move |
| 137 | + int currentMoveX = options->at(i)->first; |
| 138 | + int currentMoveY = options->at(i)->second; |
| 139 | + int** newBoard = makeAMove(board, currentMoveX, currentMoveY, player, false); |
| 140 | + // Make recursive call |
| 141 | + int dummyX, dummyY; |
| 142 | + |
| 143 | + int score = findNextStep(newBoard, !maximizerTurn, dummyX, dummyY); |
| 144 | + // update best (will be min/max depending on who's turn it is) |
| 145 | + if (maximizerTurn) { |
| 146 | + best = max(best, score); |
| 147 | + } else { |
| 148 | + best = min(best, score); |
| 149 | + } |
| 150 | + if (best == score) { |
| 151 | + x = currentMoveX; |
| 152 | + y = currentMoveY; |
| 153 | + } |
| 154 | + // Undo the move. We will have to maintain a copy of the board for this. |
| 155 | + for (int i = 0; i < 4; i++) { |
| 156 | + delete [] newBoard[i]; |
| 157 | + } |
| 158 | + delete [] newBoard; |
| 159 | + } |
| 160 | + // update x & y and return best score |
| 161 | + return best; |
| 162 | +} |
| 163 | + |
| 164 | +int main() { |
| 165 | + int **a = new int*[4]; |
| 166 | + for (int i = 0; i < 4; i++) { |
| 167 | + a[i] = new int[4]; |
| 168 | + for (int j = 0; j < 4; j++) { |
| 169 | + a[i][j] = 0; |
| 170 | + } |
| 171 | + } |
| 172 | + a[1][1] = 1; |
| 173 | + a[2][2] = 1; |
| 174 | + a[1][2] = 2; |
| 175 | + a[2][1] = 2; |
| 176 | + int x, y; |
| 177 | + cout << findNextStep(a, true, x, y) << endl; |
| 178 | + cout << x << " " << y << endl; |
| 179 | + } |
0 commit comments