Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. #include "dseif.h"
  2. #include "c5utils.h"
  3. #include <math.h>
  4. #define NULL 0
  5. #define MAXSCORE 1000
  6. #include "Configuration.h"
  7. #include <iostream>
  8. using namespace std;
  9. int turn = 0;
  10. int total = 0;
  11. int lBound = 0;
  12. int rBound = 0;
  13. int bBound = 0;
  14. int tBound = 0;
  15. int alpha = 1000000;
  16. int beta = -1000000;
  17.  
  18.  
  19.  
  20. dseif::Node::Node(){
  21. for(int i=0;i<NUMSQUARES;i++){
  22. for(int j=0;j<NUMSQUARES;j++){
  23. child_[i][j]=NULL;
  24. }
  25. }
  26. }
  27.  
  28. /*board is the board right now. lvl is the lvl of the node we are creating,
  29. row and col is the position of the most recently added piece. row and col are
  30. not relevant when lvl is 0 and are invalid*/
  31. dseif::Node* dseif::GameTree::CreateTree(int board[][NUMSQUARES],int lvl,int row,int col, int &alpha, int &beta){
  32. Node* nn=new Node();
  33. int whowon;
  34. int whomoves=(lvl%2)?-player_:player_;
  35. if(lvl > 0)
  36. whowon=fastCheckWin(board,row,col);
  37. else
  38. whowon=checkWin(board);
  39.  
  40. //Base case 1: someone has won the game, game is over score is 100
  41. if(whowon) {
  42. nn->score_=whowon * player_* MAXSCORE; //if player_ is 1 and they won the whowon would be +ve so
  43. //we have score being +1, otherwise if player_ is -1 and
  44. //player 2 won, then whowon would be -1 so score will still
  45. //be +1.
  46. }
  47. else if(boardFull(board)){ //tie game
  48. nn->score_=0;
  49. }
  50. else if(lvl==height_-1){ //game is tied.
  51. nn->score_=Eval(board,player_, 0, 0); //always evaluate for player at root!
  52. }
  53. else{
  54. if (turn == 0)
  55. {
  56. bestrow_=7;
  57. bestcol_=7;
  58. turn += 2;
  59. lBound = 7;
  60. rBound = 7;
  61. bBound = 7;
  62. tBound = 7;
  63. }
  64. else
  65. {
  66.  
  67. int tmp = 0;
  68.  
  69. for(int i=tBound-1;i <= bBound+1;i++){
  70. for(int j=lBound-1;j <= rBound+1;j++){
  71. if(board[i][j]==0){
  72. board[i][j]=whomoves; //make the move onto the board//undo the move...this is important
  73. nn->child_[i][j]=CreateTree(board,lvl+1,i,j, alpha, beta); //recursively create the //subtree
  74. nn->child_[i][j]->threat_ = Eval(board, -whomoves, i, j);
  75. nn->child_[i][j]->score_ = Eval(board, whomoves, i, j);
  76. board[i][j]=0;
  77. if(nn->child_[i][j]->score_ > tmp)
  78. {
  79. if(i < tBound)
  80. tBound = i;
  81. if(i > bBound)
  82. bBound = i;
  83. if(j > rBound)
  84. rBound = j;
  85. if(j < lBound)
  86. lBound = j;
  87. }
  88. /*if(nn->child_[i][j]->score_ > beta)
  89. {
  90. beta = nn->score_;
  91. break;
  92. }
  93. if(nn->child_[i][j]->score_ < alpha && nn->child_[i][j]->score_ > beta)
  94. alpha = nn->score_;
  95. */
  96. }
  97. }
  98. }/*for j*/
  99. }/*for i*/
  100. }
  101. bool setgood=false;
  102.  
  103.  
  104. for(int i=tBound-1;i <= bBound+1;i++){
  105. for(int j=lBound-1;j <= rBound+1;j++){
  106. if(nn->child_[i][j]){
  107. if(!setgood || nn->child_[i][j]->score_ > nn->score_ && nn->child_[i][j]->score_ > nn->child_[i][j]->threat_){
  108. nn->score_=nn->child_[i][j]->score_;
  109. setgood=true;
  110. if(lvl==0){
  111. bestrow_= i;
  112. bestcol_= j;
  113. }
  114. }
  115. else if(!setgood || nn->child_[i][j]->threat_ > nn->score_ && nn->child_[i][j]->threat_ > nn->child_[i][j]->score_){
  116. nn->score_=nn->child_[i][j]->threat_;
  117. setgood=true;
  118. if(lvl==0){
  119. bestrow_= i;
  120. bestcol_= j;
  121. }
  122. }
  123. }
  124. }//for
  125. }//for
  126. return nn;
  127. }
  128.  
  129. /*PLEASE PLEASE PLEASE REMEMBER YOUR DESTRUCTORS! with the size of the trees
  130. we are creating, a lack of one will be a major issue*/
  131. dseif::GameTree::~GameTree(){
  132. DestroyTree(root_);
  133. }
  134.  
  135. void dseif::GameTree::DestroyTree(Node* root){
  136. if (root){
  137. for(int i=0;i<NUMSQUARES;i++){
  138. for(int j=0;j<NUMSQUARES;j++){
  139. DestroyTree(root->child_[i][j]);
  140. }
  141. }
  142. delete root;
  143. }/*if*/
  144. }
  145. /*this function returns a board score for the given board for the player
  146. indicated. well.. actually it just randomly generates a number
  147. and returns that right now. You will need to make it better.*/
  148. int dseif::Eval(int board[][NUMSQUARES],int player, int row, int col){
  149. int score = 0;
  150. double mult = 0;
  151. if(player == 1)
  152. mult = 0.5;
  153. board[row][col] = player;
  154. if(numUp(board,row,col) > 0)
  155. {
  156. if(board[row-numUp(board,row,col)-1][col] == 0)
  157. mult += 0.25;
  158. score += pow(10.0, numUp(board,row,col)+(mult+numDown(board,row,col)));
  159. mult = 0;
  160. }
  161. if(numDown(board,row,col) > 0)
  162. {
  163. if(board[row+numDown(board,row,col)+1][col] == 0)
  164. mult += 0.25;
  165. score += pow(10.0, numDown(board,row,col)+(mult+numUp(board,row,col)));
  166. mult = 0;
  167. }
  168. if(numLeft(board,row,col) > 0)
  169. {
  170. if(board[row][col-numLeft(board,row,col)-1] == 0)
  171. mult += 0.25;
  172. score += pow(10.0, numLeft(board,row,col)+ (mult+numRight(board,row,col)));
  173. mult = 0;
  174. }
  175. if(numRight(board,row,col) > 0)
  176. {
  177. if(board[row][col+numRight(board,row,col)+1] == 0)
  178. mult += 0.25;
  179. score += pow(10.0, numRight(board,row,col)+ (mult+numLeft(board,row,col)));
  180. mult = 0;
  181. }
  182. if(numUpLeft(board,row,col) > 0)
  183. {
  184. if(board[row-numUpLeft(board,row,col)-1][col-numUpLeft(board,row,col)-1] == 0)
  185. mult += 0.25;
  186. score += pow(10.0, numUpLeft(board,row,col)+ (mult+numDownRight(board,row,col)));
  187. mult = 0;
  188. }
  189. if(numUpRight(board,row,col) > 0)
  190. {
  191. if(board[row-numUpRight(board,row,col)-1][col+numUpRight(board,row,col)+1] == 0)
  192. mult += 0.25;
  193. score += pow(10.0, numUpRight(board,row,col)+ (mult+numDownLeft(board,row,col)));
  194. mult = 0;
  195. }
  196. if(numDownLeft(board,row,col) > 0)
  197. {
  198. if(board[row+numDownLeft(board,row,col)+1][col-numDownLeft(board,row,col)-1] == 0)
  199. mult += 0.25;
  200. score += pow(10.0, numDownLeft(board,row,col)+ (mult+numUpRight(board,row,col)));
  201. mult = 0;
  202. }
  203. if(numDownRight(board,row,col) > 0)
  204. {
  205. if(board[row+numDownRight(board,row,col)+1][col+numDownRight(board,row,col)+1] == 0)
  206. mult += 0.25;
  207. score += pow(10.0, numDownRight(board,row,col)+ (mult+numUpLeft(board,row,col)));
  208. mult = 0;
  209. }
  210. board[row][col] = 0;
  211.  
  212. return score;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement