Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. #include "GameUtils.h"
  2.  
  3.  
  4.  
  5.  
  6.  
  7. int alphaBeta(board_t gameBoard, int a, int b, int depth, int maxDepth, int player, game* currGame){
  8. int movPos, i, curr, isLeaf = 1, currScore = (*currGame).scoreFunction(gameBoard);
  9. list* changePos;
  10. if (depth == maxDepth || currScore == 1000000 || currScore == -1000000){
  11. return currScore / depth;
  12. }
  13. for (i = 0; i < (*currGame).bound; i++){
  14. if (currGame->isValidLocation(gameBoard, i, player)){
  15. isLeaf = 0;
  16. if (player == PLAYER_1){
  17. changePos = (*currGame).addNewDisc(gameBoard, i + 1, X);
  18. curr = alphaBeta(gameBoard, a, b, depth + 1, maxDepth, 1 - player, currGame);
  19. (*currGame).delNewDisc(gameBoard, i + 1, changePos);
  20. if (b > curr){
  21. b = curr;
  22. movPos = i + 1;
  23. }
  24. if (b <= a){
  25. break;
  26. }
  27. }
  28. else {
  29. changePos = (*currGame).addNewDisc(gameBoard, i + 1, O);
  30. curr = alphaBeta(gameBoard, a, b, depth + 1, maxDepth, 1 - player, currGame);
  31. (*currGame).delNewDisc(gameBoard, i + 1, changePos);
  32. if (a < curr){
  33. a = curr;
  34. movPos = i + 1;
  35. }
  36. if (b <= a){
  37. break;
  38. }
  39. }
  40.  
  41. }
  42. }
  43. if ((isLeaf) && (depth == 0))return -1;
  44. if (isLeaf) return currScore / depth;
  45. if (depth == 0)return movPos;
  46. if (player) return a;
  47. return b;
  48.  
  49. }
  50.  
  51. int loadGame(int slot, int *type, int* player, board_t * gameBoard){
  52.  
  53. int width, height, newPlayer, i, j, token;
  54. char path[15] = "save_game/ .in";
  55. char * gameName = (char*)malloc(20 * sizeof(char));
  56. if (gameName == NULL){
  57. perror("ERROR faild to allocate memory for: gameName , in function \"loadGame\" - GameUtills.c ");
  58. exit(0);
  59. }
  60. path[10] = '0' + slot;
  61. FILE *saved_game;
  62. saved_game = fopen(path, "r");
  63. if (saved_game == NULL){
  64. perror("ERROR faild to open file : %s , in function \"loadGame\" - GameUtills.c ", path);
  65. return 0;
  66. }
  67. fscanf(saved_game, "%s\n", gameName);
  68. if (!strcmp(gameName, "Reversi")){
  69. height = 8;
  70. width = 8;
  71. *type = 2;
  72. }
  73. else if (!strcmp(gameName, "Connect4")){
  74. height = 6;
  75. width = 7;
  76. *type = 0;
  77. }
  78. else if (!strcmp(gameName, "Tic-tac-toe")){
  79. height = 3;
  80. width = 3;
  81. *type = 1;
  82. }
  83. else{
  84.  
  85. }
  86. fscanf(saved_game, "%d\n", &newPlayer);
  87. if (newPlayer == -1)*player = 0;
  88. else *player = 1;
  89. *gameBoard = calloc(height, sizeof(int*));
  90. if (gameBoard == NULL){
  91. perror("ERROR faild to allocate memory for: gameBoard , in function \"loadGame\" - GameUtills.c ");
  92. exit(0);
  93. }
  94. for (i = 0; i < height; i++){
  95. (*gameBoard)[i] = calloc(width, sizeof(int));
  96. if (gameBoard[i] == NULL){
  97. perror("ERROR faild to allocate memory for: gameBoard , in function \"loadGame\" - GameUtills.c ");
  98. exit(0);
  99. }
  100. }
  101. for (i = 0; i < height; i++){
  102. for (j = 0; j < width; j++){
  103. fscanf(saved_game, "%d", &token);
  104. (*gameBoard)[i][j] = token;
  105. }
  106. }
  107. return 1;
  108.  
  109.  
  110. }
  111.  
  112. void computerPlay(board_t gameBoard, int numOfSteps, game* currGame, int player){
  113. int enterCol;
  114. if (currGame->canMove(gameBoard, player) == 0){
  115. return;
  116. }
  117. enterCol = alphaBeta(gameBoard, INT_MIN, INT_MAX, 0, numOfSteps, player, currGame);
  118. if (player == PLAYER_1)(*currGame).addNewDisc(gameBoard, enterCol, X);
  119. else (*currGame).addNewDisc(gameBoard, enterCol, O);
  120.  
  121. }
  122.  
  123. void restart(game * currGame){
  124. currGame->initBoard(currGame->gameBoard);
  125. currGame->endGame = 0;
  126. }
  127.  
  128. void saveGame(int slot, game* curGame, int player){
  129. char path[15] = "save_game/ .in";
  130. char* gameName = (char*)malloc(20, sizeof(char));
  131. if (gameName == NULL){
  132. perror("ERROR faild to allocate memory for: gameName , in function \"saveGame\" - GameUtills.c ");
  133. exit(0);
  134. }
  135. path[10] = '0' + slot;
  136. FILE *save_game;
  137. save_game = fopen(path, "w");
  138. if (save_game == NULL){
  139. perror("ERROR faild to open file : %s , in function \"saveGame\" - GameUtills.c ", path);
  140. exit(0);
  141. }
  142. int i, j, turn, heigth, width;
  143. turn = -1;
  144. if (player)turn = 1;
  145. if (curGame->type == 0){
  146. strcpy(gameName, "Connect4\n");
  147. heigth = 6;
  148. width = 7;
  149. }
  150. else if (curGame->type == 1){
  151. strcpy(gameName, "Tic-tac-toe\n");
  152. heigth = 3;
  153. width = 3;
  154. }
  155. else if (curGame->type == 2){
  156. strcpy(gameName, "Reversi\n");
  157. heigth = 8;
  158. width = 8;
  159. }
  160. fprintf(save_game, "%s", gameName);
  161. fprintf(save_game, "%d\n", turn);
  162. for (i = 0; i < heigth; i++){
  163. for (j = 0; j < width; j++){
  164. fprintf(save_game, "%d ", (curGame->gameBoard)[i][j]);
  165. }
  166. fprintf(save_game, "\n");
  167. }
  168. fflush(save_game);
  169.  
  170. free(gameName);
  171. free(path);
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement