Advertisement
Guest User

for kuba

a guest
Dec 12th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. //alpha-beta search algorithm variables and functions
  2. int maxDepth;
  3. int nodeCount;
  4. int maxPrune;
  5. int minPrune;
  6. int tempMaxDepth;
  7. int tempNodeCount;
  8. int tempMaxPrune;
  9. int tempMinPrune;
  10. double duration;
  11. clock_t start;
  12.  
  13. int boardEval(int wCount, int bCount, vector<Space>& bvec){ //evaluates the score of a game that is not over yet
  14. int score = 0;
  15. if (bvec[0].color == 'w' && bvec[1].color != 'w') { score -= 10; }
  16. if (bvec[1].color == 'w' && bvec[0].color != 'w') { score -= 10; }
  17. if (bvec[86].color == 'b' && bvec[87].color != 'b') { score += 10; }
  18. if (bvec[87].color == 'b' && bvec[86].color != 'b') { score += 10; }
  19. score += (bCount - wCount) * 4;
  20. return score;
  21. }
  22.  
  23. treeInfo alphaBetaScore(Space*& spo, vector<Space>& bveco, int nodeDepth, int depthLimit, bool isMaxTurn, int alpha, int beta){ //alpha-beta search function
  24. vector<Space> bvec = bveco;
  25. Space* sp = &bvec[spo->vectorNum];
  26. tempNodeCount += 1;
  27. treeInfo ti;
  28. ti.depth = nodeDepth;
  29.  
  30. auto whitepc = 0;
  31. auto blackpc = 0;
  32.  
  33. for (Space i : bvec){
  34. if (i.color == 'w') { whitepc += 1; }
  35. if (i.color == 'b') { blackpc += 1; }
  36. }
  37. if (whitepc == 1 && blackpc == 1) { //check for draw
  38. ti.score = 0;
  39. duration = (clock() - start) / (double)CLOCKS_PER_SEC;
  40. return ti;
  41. }
  42. else if ((whitepc > 1 && blackpc == 0) || (bvec[0].color == 'w' && bvec[1].color == 'w')) { //check for white win
  43. ti.score = -1000;
  44. duration = (clock() - start) / (double)CLOCKS_PER_SEC;
  45. return ti;
  46. }
  47. else if ((blackpc > 1 && whitepc == 0) || (bvec[86].color == 'b' && bvec[87].color == 'b')) { //check for black win
  48. ti.score = 1000;
  49. duration = (clock() - start) / (double)CLOCKS_PER_SEC;
  50. return ti;
  51. }
  52. else {
  53. if (isMaxTurn){ //max-value function
  54. if (nodeDepth == depthLimit){
  55. if (checkIfBlackCanCapture(bvec)){ blackCapToSpace(sp, bvec); }
  56. else { blackMoveToSpace(sp, bvec); }
  57. ti.score = boardEval(whitepc, blackpc, bvec); //evaluation function
  58. duration = (clock() - start) / (double)CLOCKS_PER_SEC;
  59. return ti;
  60. }
  61. else{
  62. vector<Space*> spVec;
  63. ti.score = alpha;
  64. if (checkIfBlackCanCapture(bvec)){
  65. blackCapToSpace(sp, bvec);
  66. spVec = getBlackCaptureArray(bvec);
  67. for (int i = 0; i < spVec.size(); i++){
  68. treeInfo cti = alphaBetaScore(spVec[i], bvec, nodeDepth + 1, depthLimit, false, alpha, beta);
  69. ti.score = max(ti.score, cti.score);
  70. ti.depth = max(ti.depth, cti.depth);
  71. alpha = max(alpha, ti.score);
  72. if (beta <= alpha) {
  73. tempMaxPrune += 1;
  74. break;
  75. }
  76. }
  77. duration = (clock() - start) / (double)CLOCKS_PER_SEC;
  78. return ti;
  79.  
  80. }
  81. else {
  82. blackMoveToSpace(sp, bvec);
  83. spVec = getBlackMoveArray(bvec);
  84. for (int i = 0; i < spVec.size(); i++){
  85. treeInfo cti = alphaBetaScore(spVec[i], bvec, nodeDepth + 1, depthLimit, false, alpha, beta);
  86. nodeCount += 1;
  87. ti.score = max(ti.score, cti.score);
  88. ti.depth = max(ti.depth, cti.depth);
  89. alpha = max(alpha, ti.score);
  90. if (beta <= alpha) {
  91. tempMaxPrune += 1;
  92. break;
  93. }
  94. }
  95. duration = (clock() - start) / (double)CLOCKS_PER_SEC;
  96. return ti;
  97. }
  98. }
  99. }
  100. else { //min-value function
  101. if (nodeDepth == depthLimit){
  102. if (aiCheckIfWhiteCanCapture(bvec)){ aiWhiteCapToSpace(sp, bvec); }
  103. else { aiWhiteMoveToSpace(sp, bvec); }
  104. ti.score = boardEval(whitepc, blackpc, bvec); //evaluation function
  105. return ti;
  106. }
  107. else{
  108. vector<Space*> spVec;
  109. ti.score = beta;
  110. if (aiCheckIfWhiteCanCapture(bvec)){
  111. aiWhiteCapToSpace(sp, bvec);
  112. spVec = getWhiteCaptureArray(bvec);
  113. for (int i = 0; i < spVec.size(); i++){
  114. treeInfo cti = alphaBetaScore(spVec[i], bvec, nodeDepth + 1, depthLimit, true, alpha, beta);
  115. nodeCount += 1;
  116. ti.score = min(ti.score, cti.score);
  117. ti.depth = max(ti.depth, cti.depth);
  118. beta = min(alpha, ti.score);
  119. if (beta <= alpha) {
  120. tempMinPrune += 1;
  121. break;
  122. }
  123. }
  124. duration = (clock() - start) / (double)CLOCKS_PER_SEC;
  125. return ti;
  126.  
  127. }
  128. else {
  129. aiWhiteMoveToSpace(sp, bvec);
  130. spVec = getWhiteMoveArray(bvec);
  131. for (int i = 0; i < spVec.size(); i++){
  132. treeInfo cti = alphaBetaScore(spVec[i], bvec, nodeDepth + 1, depthLimit, true, alpha, beta);
  133. nodeCount += 1;
  134. ti.score = min(ti.score, cti.score);
  135. ti.depth = max(ti.depth, cti.depth);
  136. beta = min(alpha, ti.score);
  137. if (beta <= alpha) {
  138. tempMinPrune += 1;
  139. break;
  140. }
  141. }
  142. duration = (clock() - start) / (double)CLOCKS_PER_SEC;
  143. return ti;
  144. }
  145. }
  146. }
  147. }
  148. }
  149.  
  150. Space* alphaBetaBestChoice(vector<Space*>& spvec){ //displays information regarding the alpha-beta algorithm and returns the chosen move
  151. int bestScore = -1000;
  152. int bestIndex = 0;
  153. vector<int> scoreVec;
  154. start = clock(); //timer starts here
  155. for (int i = 0;; i++){ //performs iterative deepening on algorithm and returns early results in case the algorithm does not finish in time
  156. vector<int> depthVec;
  157. tempMaxDepth = 0;
  158. tempNodeCount = 0;
  159. tempMaxPrune = 0;
  160. tempMinPrune = 0;
  161. for (Space* j : spvec) {
  162. treeInfo ti = alphaBetaScore(j, boardVec, 0, i, true, -1000, 1000);
  163. depthVec.push_back(ti.score);
  164. if (tempMaxDepth < ti.depth) { tempMaxDepth = ti.depth; }
  165. if (duration > 10.0) { break; } //timer ends search here
  166. }
  167. scoreVec = depthVec;
  168. maxDepth = tempMaxDepth;
  169. nodeCount = tempNodeCount;
  170. maxPrune = tempMaxPrune;
  171. minPrune = tempMinPrune;
  172. if (i > tempMaxDepth || duration > 10.0) { break; } //ends iterative deepening loop if depth exceeds max algorithm tree depth
  173. }
  174. for (int i = 0; i < scoreVec.size(); i++){
  175. if (bestScore < scoreVec[i]){
  176. bestScore = scoreVec[i];
  177. bestIndex = i;
  178. }
  179. }
  180. cout << "\nMaximum depth of game tree reached: " << maxDepth
  181. << "\nTotal number of nodes generated: " << nodeCount
  182. << "\nNumber of times pruning occurred within the MAX-VALUE function: " << maxPrune
  183. << "\nNumber of times pruning occurred within the MIN-VALUE function: " << minPrune
  184. << "\nTime elapsed: " << duration << " seconds\n"
  185. << "Computer's move: " << spvec[bestIndex]->yCoord << spvec[bestIndex]->xCoord << "\n\n";
  186. return spvec[bestIndex];
  187. }
  188.  
  189. void aiTurn(){
  190. cout << "\nComputer's turn.\n";
  191. if (checkIfBlackCanCapture(boardVec)){ //checks if AI can capture this turn
  192. cout << "Computer is obligated to capture an enemy piece.\n\n";
  193. vector<Space*> spVec = getBlackCaptureArray(boardVec); //gets vector of potential capture moves
  194. Space* spp = alphaBetaBestChoice(spVec); //runs alpha-beta search
  195. blackCapToSpace(spp, boardVec);
  196. }
  197. else{
  198. vector<Space*> spVec = getBlackMoveArray(boardVec); //gets vector of potential non-capture moves
  199. Space* spp = alphaBetaBestChoice(spVec); //runs alpha-beta search
  200. blackMoveToSpace(spp, boardVec);
  201. }
  202. humanTurn = true;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement