From 821bc5727f6e1457c8c8995c19df172c21ca5273 Mon Sep 17 00:00:00 2001 From: snsd0805 Date: Fri, 2 Jun 2023 15:47:02 +0800 Subject: [PATCH] feat: set up 'four in a row' enviroment --- constant.h | 3 ++ enviroment.c | 145 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 119 insertions(+), 29 deletions(-) diff --git a/constant.h b/constant.h index 3c0bd69..fdd0e00 100644 --- a/constant.h +++ b/constant.h @@ -9,3 +9,6 @@ #define ACTION_NUM 9 #define EPISODE_NUM 100000 #define FIRST true + +#define ROW_NUM 6 +#define COL_NUM 7 \ No newline at end of file diff --git a/enviroment.c b/enviroment.c index 6f5b721..83c984d 100644 --- a/enviroment.c +++ b/enviroment.c @@ -14,13 +14,13 @@ short PATHS[8][3] = { Reset the game, clear the chessboard. Args: - - short *board (array's address): chessboard's status + - 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<9; i++) + for (short i=0; i<(ROW_NUM*COL_NUM); i++) board[i] = 0; } @@ -35,22 +35,23 @@ void reset(short* board){ */ void show(short *board){ short loc; - printf("┼───┼───┼───┼\n"); - for (short i=0; i<3; i++){ - printf("│ "); - for (short j=0; j<3; j++){ - loc = 3*i+j; - if (board[loc] == 0) - printf(" │ "); - else if (board[loc] == BOT_SYMBOL) - printf("○ │ "); - else - printf("✕ │ "); - } - printf("\n"); - printf("┼───┼───┼───┼\n"); - } - printf("\n\n"); + 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"); } /* @@ -66,12 +67,33 @@ void show(short *board){ */ void get_available_actions(short *board, short *result, short *length){ short index = 0; - for (int i=0; i<9; i++) - if (board[i] == 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; @@ -80,14 +102,58 @@ void get_available_actions(short *board, short *result, short *length){ 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){ - int a, b, c; - for (int i=0; i<8; i++){ - a = PATHS[i][0]; b = PATHS[i][1]; c = PATHS[i][2]; - if ((board[a] == board[b]) && (board[b] == board[c]) && (board[a] != 0)){ - return board[a]; - } + 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. @@ -127,10 +213,11 @@ int state_hash(short *board){ */ 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[a->loc] == 0); - board[a->loc] = a->player; + assert(board[(ROW_NUM*COL_NUM-1)-(a->loc)] == 0); + + fall(board, a); *winner = get_winner(board); - *state = state_hash(board); + // *state = state_hash(board); if (*winner == a->player){ *reward = 1.0; *opponent_reward = -1.0;