feat: format function docs & act()

This commit is contained in:
snsd0805 2023-05-24 17:39:46 +08:00
parent d48a767223
commit f7c4132cd6
Signed by: snsd0805
GPG Key ID: 569349933C77A854

44
main.c
View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <math.h>
#define BOT_SYMBOL 1
#define OPPONENT_SYMBOL 2
@ -9,7 +10,11 @@ short PATHS[8][3] = {
{0, 4, 8}, {2, 4, 6}
};
void reset(short* board){
struct action{
short player;
short loc;
};
/*
Reset the game, clear the chessboard.
@ -19,11 +24,11 @@ void reset(short* board){
Results:
- None, set all blocks on the chessboard to zero.
*/
void reset(short* board){
for (short i=0; i<9; i++)
board[i] = 0;
}
void show(short *board){
/*
Print the chessboard on the console.
@ -33,6 +38,7 @@ void show(short *board){
Results:
- None. Only printing.
*/
void show(short *board){
short loc;
printf("┼───┼───┼───┼\n");
for (short i=0; i<3; i++){
@ -52,7 +58,6 @@ void show(short *board){
printf("\n\n");
}
void get_available_actions(short *board, short *result, short *length){
/*
Save all available actions into the "result" array.
@ -64,6 +69,7 @@ void get_available_actions(short *board, short *result, short *length){
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<9; i++)
if (board[i] == 0)
@ -71,7 +77,6 @@ void get_available_actions(short *board, short *result, short *length){
*length = index;
}
short get_winner(short *board){
/*
Return winner's number;
@ -81,6 +86,7 @@ short get_winner(short *board){
Results:
- short winner_number(integer): winner's number, 0 for no winner now, 1 for Bot, 2 for opponent
*/
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];
@ -91,7 +97,6 @@ short get_winner(short *board){
return 0;
}
int state_hash(short *board){
/*
Hash chesstable's status into hash.
@ -101,6 +106,7 @@ int state_hash(short *board){
Results:
- int hash (integer): chessboard's status in i-th block * pow(3, i)
*/
int state_hash(short *board){
int base, hash = 0;
for (int i=0; i<9; i++){
base = pow(3, i);
@ -112,15 +118,39 @@ int state_hash(short *board){
return hash;
}
// void action(short *board, short a, )
/*
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
- int *reward (pointer): for return. To save the number of rewards which the player gets after 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, int *reward, short *winner){
board[a->loc] = a->player;
*winner = get_winner(board);
*state = state_hash(board);
if (*winner == a->player)
*reward = 1;
else if(*winner != 0)
*reward = -1;
else
*reward = 0;
}
// int choose_action
int main(){
short board[9]= {0}; // tic tac toe's chessboard
short available_actions[9];
short available_actions_length;
short winner;
board[1] = 2;
board[3] = 1;
board[4] = 1;