Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "GameUtils.h"
- int alphaBeta(board_t gameBoard, int a, int b, int depth, int maxDepth, int player, game* currGame){
- int movPos, i, curr, isLeaf = 1, currScore = (*currGame).scoreFunction(gameBoard);
- list* changePos;
- if (depth == maxDepth || currScore == 1000000 || currScore == -1000000){
- return currScore / depth;
- }
- for (i = 0; i < (*currGame).bound; i++){
- if (currGame->isValidLocation(gameBoard, i, player)){
- isLeaf = 0;
- if (player == PLAYER_1){
- changePos = (*currGame).addNewDisc(gameBoard, i + 1, X);
- curr = alphaBeta(gameBoard, a, b, depth + 1, maxDepth, 1 - player, currGame);
- (*currGame).delNewDisc(gameBoard, i + 1, changePos);
- if (b > curr){
- b = curr;
- movPos = i + 1;
- }
- if (b <= a){
- break;
- }
- }
- else {
- changePos = (*currGame).addNewDisc(gameBoard, i + 1, O);
- curr = alphaBeta(gameBoard, a, b, depth + 1, maxDepth, 1 - player, currGame);
- (*currGame).delNewDisc(gameBoard, i + 1, changePos);
- if (a < curr){
- a = curr;
- movPos = i + 1;
- }
- if (b <= a){
- break;
- }
- }
- }
- }
- if ((isLeaf) && (depth == 0))return -1;
- if (isLeaf) return currScore / depth;
- if (depth == 0)return movPos;
- if (player) return a;
- return b;
- }
- int loadGame(int slot, int *type, int* player, board_t * gameBoard){
- int width, height, newPlayer, i, j, token;
- char path[15] = "save_game/ .in";
- char * gameName = (char*)malloc(20 * sizeof(char));
- if (gameName == NULL){
- perror("ERROR faild to allocate memory for: gameName , in function \"loadGame\" - GameUtills.c ");
- exit(0);
- }
- path[10] = '0' + slot;
- FILE *saved_game;
- saved_game = fopen(path, "r");
- if (saved_game == NULL){
- perror("ERROR faild to open file : %s , in function \"loadGame\" - GameUtills.c ", path);
- return 0;
- }
- fscanf(saved_game, "%s\n", gameName);
- if (!strcmp(gameName, "Reversi")){
- height = 8;
- width = 8;
- *type = 2;
- }
- else if (!strcmp(gameName, "Connect4")){
- height = 6;
- width = 7;
- *type = 0;
- }
- else if (!strcmp(gameName, "Tic-tac-toe")){
- height = 3;
- width = 3;
- *type = 1;
- }
- else{
- }
- fscanf(saved_game, "%d\n", &newPlayer);
- if (newPlayer == -1)*player = 0;
- else *player = 1;
- *gameBoard = calloc(height, sizeof(int*));
- if (gameBoard == NULL){
- perror("ERROR faild to allocate memory for: gameBoard , in function \"loadGame\" - GameUtills.c ");
- exit(0);
- }
- for (i = 0; i < height; i++){
- (*gameBoard)[i] = calloc(width, sizeof(int));
- if (gameBoard[i] == NULL){
- perror("ERROR faild to allocate memory for: gameBoard , in function \"loadGame\" - GameUtills.c ");
- exit(0);
- }
- }
- for (i = 0; i < height; i++){
- for (j = 0; j < width; j++){
- fscanf(saved_game, "%d", &token);
- (*gameBoard)[i][j] = token;
- }
- }
- return 1;
- }
- void computerPlay(board_t gameBoard, int numOfSteps, game* currGame, int player){
- int enterCol;
- if (currGame->canMove(gameBoard, player) == 0){
- return;
- }
- enterCol = alphaBeta(gameBoard, INT_MIN, INT_MAX, 0, numOfSteps, player, currGame);
- if (player == PLAYER_1)(*currGame).addNewDisc(gameBoard, enterCol, X);
- else (*currGame).addNewDisc(gameBoard, enterCol, O);
- }
- void restart(game * currGame){
- currGame->initBoard(currGame->gameBoard);
- currGame->endGame = 0;
- }
- void saveGame(int slot, game* curGame, int player){
- char path[15] = "save_game/ .in";
- char* gameName = (char*)malloc(20, sizeof(char));
- if (gameName == NULL){
- perror("ERROR faild to allocate memory for: gameName , in function \"saveGame\" - GameUtills.c ");
- exit(0);
- }
- path[10] = '0' + slot;
- FILE *save_game;
- save_game = fopen(path, "w");
- if (save_game == NULL){
- perror("ERROR faild to open file : %s , in function \"saveGame\" - GameUtills.c ", path);
- exit(0);
- }
- int i, j, turn, heigth, width;
- turn = -1;
- if (player)turn = 1;
- if (curGame->type == 0){
- strcpy(gameName, "Connect4\n");
- heigth = 6;
- width = 7;
- }
- else if (curGame->type == 1){
- strcpy(gameName, "Tic-tac-toe\n");
- heigth = 3;
- width = 3;
- }
- else if (curGame->type == 2){
- strcpy(gameName, "Reversi\n");
- heigth = 8;
- width = 8;
- }
- fprintf(save_game, "%s", gameName);
- fprintf(save_game, "%d\n", turn);
- for (i = 0; i < heigth; i++){
- for (j = 0; j < width; j++){
- fprintf(save_game, "%d ", (curGame->gameBoard)[i][j]);
- }
- fprintf(save_game, "\n");
- }
- fflush(save_game);
- free(gameName);
- free(path);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement