#include #include #include #include "constant.h" #include "enviroment.h" short PATHS[8][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6} }; /* Reset the game, clear the chessboard. Args: - short *board (array's start address): chessboard's status Results: - None, set all blocks on the chessboard to zero. */ void reset(short* board){ for (short i=0; i<(ROW_NUM*COL_NUM); i++) board[i] = 0; } /* Print the chessboard on the console. Args: - short *board (array's address): chessboard's status Results: - None. Only printing. */ void show(short *board){ short loc; for (short i=0; i=0; i--){ if (board[i] == BOT_SYMBOL) { printf("● "); } else if(board[i] == OPPONENT_SYMBOL) { printf("◴ "); } else { printf("◌ "); } if (i%COL_NUM == 0){ printf("\n"); } } printf("\n\n"); } /* Save all available actions into the "result" array. Args: - short *board (array's address): chessboard's status - short *result (array's address): To save all available actions. - short *length (integer's pointer): To save the number of available actions. Results: - None. All available actions are saved into "result" and the number of actions is saved in "length" */ void get_available_actions(short *board, short *result, short *length){ short index = 0; for (int i=0; i= ROW_NUM) || (row < 0)) { return -1; } if ((col >= COL_NUM) || (col < 0)) { return -1; } return board[row*COL_NUM+col]; } /* Return winner's number; Args: - short *board (array's address): chessboard's status Results: - short winner_number(integer): winner's number, 0 for no winner now, 1 for Bot, 2 for opponent board's coodinate diagram ^ | 5 | 4 | 3 | 2 | 1 | 0 <----------------------------- 6 5 4 3 2 1 0 | */ short get_winner(short *board){ short a, b, c, d; for (short i=0; iloc)); while (*ptr == 0) { // printf("%d ", *ptr); ptr -= COL_NUM; } *(ptr+COL_NUM) = a->player; } /* Act on the chessboard. Args: - short *board (array's address): chessboards' status - struct action *a (a action's pointer): include player & choose loc - int *state (pointer): for return. To save the chessboard's state hash which after doing this action - float *reward (pointer): for return. To save the number of rewards which the player gets after doing this action. - float *opponent_reward (pointer): for return. To save the number of rewards which the opponents gets after the player doing this action. - short *winner (pointer): for return. To save the winner in this action. If haven't finish, it will be zero. Results: - None. Save in state & reward & winner */ void act(short *board, struct action *a, int *state, float *reward, float *opponent_reward, short *winner){ // printf("Act( player=%d, action=%d )\n", a->player, a->loc); assert(board[(ROW_NUM*COL_NUM-1)-(a->loc)] == 0); fall(board, a); *winner = get_winner(board); // *state = state_hash(board); if (*winner == a->player){ *reward = 1.0; *opponent_reward = -1.0; } else if(*winner != 0){ *reward = -1.0; *opponent_reward = 1.0; } else{ *reward = 0; *opponent_reward = 0; } }