From bbbc5ac8c385165d27aeae2e976ee247accfb9cb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gon=C3=A7alo=20Ferreira?= Date: Wed, 16 Oct 2019 18:41:49 +0100 Subject: [PATCH] Part 4 of code style change --- src/move.c | 1 + src/pts_file.c | 50 +++++++++++------ src/scoring.c | 172 ++++++++++++++++++++++++++------------------------------- 3 files changed, 110 insertions(+), 113 deletions(-) diff --git a/src/move.c b/src/move.c index 1f3c1bd..217be6b 100644 --- a/src/move.c +++ b/src/move.c @@ -47,6 +47,7 @@ move coord_to_move( u8 y ) { assert(x < BOARD_SIZ && y < BOARD_SIZ); + return (move)(y * BOARD_SIZ + x); } diff --git a/src/pts_file.c b/src/pts_file.c index 9de0cfe..c07858d 100644 --- a/src/pts_file.c +++ b/src/pts_file.c @@ -46,23 +46,27 @@ Open and prepare a file to be interpreted line by line. void open_rule_file( const char * filename ) { - if (buffer != NULL) + if (buffer != NULL) { flog_crit("ptsf", "error: pts_file: file open"); + } char * fn = alloc(); - if (starts_with(filename, data_folder())) + if (starts_with(filename, data_folder())) { snprintf(fn, MAX_PAGE_SIZ, "%s", filename); - else + } else { snprintf(fn, MAX_PAGE_SIZ, "%s%s", data_folder(), filename); + } buffer = malloc(MAX_FILE_SIZ); - if (buffer == NULL) + if (buffer == NULL) { flog_crit("ptsf", "system out of memory"); + } d32 chars_read = read_ascii_file(buffer, MAX_FILE_SIZ, fn); - if (chars_read < 0) + if (chars_read < 0) { flog_crit("ptsf", "couldn't open file for reading"); + } release(fn); search_started = false; @@ -74,14 +78,14 @@ Read the next rule line. void read_next_rule( char * dst ) { - if (buffer == NULL) + if (buffer == NULL) { flog_crit("ptsf", "no file open"); + } char * line; - if (search_started) + if (search_started) { line = strtok_r(NULL, "\r\n", &save_ptr); - else - { + } else { line = strtok_r(buffer, "\r\n", &save_ptr); search_started = true; } @@ -129,12 +133,13 @@ void interpret_rule_as_pts_list( char * word; char * save_ptr2 = NULL; - if ((word = strtok_r(tmp, " ", &save_ptr2)) != NULL) + if ((word = strtok_r(tmp, " ", &save_ptr2)) != NULL) { strncpy(tokens[tokens_read++], word, 4); + } - while (tokens_read < TOTAL_BOARD_SIZ && (word = strtok_r(NULL, " ", - &save_ptr2)) != NULL) + while (tokens_read < TOTAL_BOARD_SIZ && (word = strtok_r(NULL, " ", &save_ptr2)) != NULL) { strncpy(tokens[tokens_read++], word, 4); + } if (tokens_read < 1 || tokens_read == TOTAL_BOARD_SIZ) { char * buf = alloc(); @@ -149,12 +154,14 @@ void interpret_rule_as_pts_list( for (u16 t = 0; t < tokens_read; ++t) { move m = coord_parse_alpha_num(tokens[t]); + if (!is_board_move(m)) { char * buf = alloc(); snprintf(buf, MAX_PAGE_SIZ, "malformed line: %s", src); flog_crit("ptsf", buf); release(buf); } + if (!attempt_play_slow(&b, true, m)) { char * buf = alloc(); snprintf(buf, MAX_PAGE_SIZ, "malformed line: %s", src); @@ -190,6 +197,7 @@ static void load_points( char * s = alloc(); read_next_rule(s); + if (s != NULL) { interpret_rule_as_pts_list(dst, s); @@ -207,14 +215,16 @@ static void load_points( Load handicap points. */ void load_handicap_points() { - if (handicap_points_attempted_load) + if (handicap_points_attempted_load) { return; + } load_points("handicap", &handicap); memset(is_handicap, false, TOTAL_BOARD_SIZ); - for (move i = 0; i < handicap.count; ++i) + for (move i = 0; i < handicap.count; ++i) { is_handicap[handicap.coord[i]] = true; + } handicap_points_attempted_load = true; } @@ -223,14 +233,16 @@ void load_handicap_points() { Load hoshi points. */ void load_hoshi_points() { - if (hoshi_points_attempted_load) + if (hoshi_points_attempted_load) { return; + } load_points("hoshi", &hoshi); memset(is_hoshi, false, TOTAL_BOARD_SIZ); - for (move i = 0; i < hoshi.count; ++i) + for (move i = 0; i < hoshi.count; ++i) { is_hoshi[hoshi.coord[i]] = true; + } hoshi_points_attempted_load = true; } @@ -239,14 +251,16 @@ void load_hoshi_points() { Load starting MCTS points. */ void load_starting_points() { - if (starting_points_attempted_load) + if (starting_points_attempted_load) { return; + } load_points("starting", &starting); memset(is_starting, false, TOTAL_BOARD_SIZ); - for (move i = 0; i < starting.count; ++i) + for (move i = 0; i < starting.count; ++i) { is_starting[starting.coord[i]] = true; + } starting_points_attempted_load = true; } diff --git a/src/scoring.c b/src/scoring.c index 45a0e09..02ce917 100644 --- a/src/scoring.c +++ b/src/scoring.c @@ -28,22 +28,21 @@ void score_to_string( char * dst, d16 score ) { - if (score == 0) + if (score == 0) { snprintf(dst, MAX_PAGE_SIZ, "0"); - else - if ((score & 1) == 1) { - if (score > 0) - snprintf(dst, MAX_PAGE_SIZ, "B+%d.5", score / 2); - else - snprintf(dst, MAX_PAGE_SIZ, "W+%d.5", (-score) / 2); + } else if ((score & 1) == 1) { + if (score > 0) { + snprintf(dst, MAX_PAGE_SIZ, "B+%d.5", score / 2); + } else { + snprintf(dst, MAX_PAGE_SIZ, "W+%d.5", (-score) / 2); } - else - { - if (score > 0) - snprintf(dst, MAX_PAGE_SIZ, "B+%d", score / 2); - else - snprintf(dst, MAX_PAGE_SIZ, "W+%d", (-score) / 2); + } else { + if (score > 0) { + snprintf(dst, MAX_PAGE_SIZ, "B+%d", score / 2); + } else { + snprintf(dst, MAX_PAGE_SIZ, "W+%d", (-score) / 2); } + } } /* @@ -53,22 +52,21 @@ void komi_to_string( char * dst, d16 komi ) { - if (komi == 0) + if (komi == 0) { snprintf(dst, MAX_PAGE_SIZ, "0"); - else - if ((komi & 1) == 1) { - if (komi > 0) - snprintf(dst, MAX_PAGE_SIZ, "%d.5", komi / 2); - else - snprintf(dst, MAX_PAGE_SIZ, "-%d.5", (-komi) / 2); + } else if ((komi & 1) == 1) { + if (komi > 0) { + snprintf(dst, MAX_PAGE_SIZ, "%d.5", komi / 2); + } else { + snprintf(dst, MAX_PAGE_SIZ, "-%d.5", (-komi) / 2); } - else - { - if (komi > 0) - snprintf(dst, MAX_PAGE_SIZ, "%d", komi / 2); - else - snprintf(dst, MAX_PAGE_SIZ, "-%d", (-komi) / 2); + } else { + if (komi > 0) { + snprintf(dst, MAX_PAGE_SIZ, "%d", komi / 2); + } else { + snprintf(dst, MAX_PAGE_SIZ, "-%d", (-komi) / 2); } + } } /* @@ -79,7 +77,8 @@ d16 score_stones_only( const u8 p[static TOTAL_BOARD_SIZ] ) { d16 r = 0; - for (move m = 0; m < TOTAL_BOARD_SIZ; ++m) + + for (move m = 0; m < TOTAL_BOARD_SIZ; ++m) { switch (p[m]) { case BLACK_STONE: r += 2; @@ -87,6 +86,7 @@ d16 score_stones_only( case WHITE_STONE: r -= 2; } + } return r - komi; } @@ -101,7 +101,8 @@ d16 score_stones_and_eyes2( ) { bool _ignored; d16 r = 0; - for (move m = 0; m < TOTAL_BOARD_SIZ; ++m) + + for (move m = 0; m < TOTAL_BOARD_SIZ; ++m) { switch (cb->p[m]) { case BLACK_STONE: r += 2; @@ -110,39 +111,34 @@ d16 score_stones_and_eyes2( r -= 2; break; case EMPTY: - if (is_4pt_eye(cb, true, m, &_ignored)) - { + if (is_4pt_eye(cb, true, m, &_ignored)) { r += 8; ++m; break; } - if (is_4pt_eye(cb, false, m, &_ignored)) - { + if (is_4pt_eye(cb, false, m, &_ignored)) { r -= 8; ++m; break; } - if (is_2pt_eye(cb, true, m, &_ignored)) - { + if (is_2pt_eye(cb, true, m, &_ignored)) { r += 4; break; } - if (is_2pt_eye(cb, false, m, &_ignored)) - { + if (is_2pt_eye(cb, false, m, &_ignored)) { r -= 4; break; } - if (is_eye(cb, true, m)) - { + if (is_eye(cb, true, m)) { r += 2; break; } - if (is_eye(cb, false, m)) - { + if (is_eye(cb, false, m)) { r -= 2; break; } } + } return r - komi; } @@ -174,59 +170,47 @@ static void _search( move_to_coord(m, &x, &y); if (x > 0) { - if (p[m + LEFT] == BLACK_STONE) + if (p[m + LEFT] == BLACK_STONE) { *black |= true; - else - if (p[m + LEFT] == WHITE_STONE) - *white |= true; - else - if (explored[m + LEFT] == false) - { - explored[m + LEFT] = true; - _search(p, m + LEFT, explored, black, white); - } + } else if (p[m + LEFT] == WHITE_STONE) { + *white |= true; + } else if (explored[m + LEFT] == false) { + explored[m + LEFT] = true; + _search(p, m + LEFT, explored, black, white); + } } if (x < BOARD_SIZ - 1) { - if (p[m + RIGHT] == BLACK_STONE) + if (p[m + RIGHT] == BLACK_STONE) { *black |= true; - else - if (p[m + RIGHT] == WHITE_STONE) - *white |= true; - else - if (explored[m + RIGHT] == false) - { - explored[m + RIGHT] = true; - _search(p, m + RIGHT, explored, black, white); - } + } else if (p[m + RIGHT] == WHITE_STONE) { + *white |= true; + } else if (explored[m + RIGHT] == false) { + explored[m + RIGHT] = true; + _search(p, m + RIGHT, explored, black, white); + } } if (y > 0) { - if (p[m + TOP] == BLACK_STONE) + if (p[m + TOP] == BLACK_STONE) { *black |= true; - else - if (p[m + TOP] == WHITE_STONE) - *white |= true; - else - if (explored[m + TOP] == false) - { - explored[m + TOP] = true; - _search(p, m + TOP, explored, black, white); - } + } else if (p[m + TOP] == WHITE_STONE) { + *white |= true; + } else if (explored[m + TOP] == false) { + explored[m + TOP] = true; + _search(p, m + TOP, explored, black, white); + } } if (y < BOARD_SIZ - 1) { - if (p[m + BOTTOM] == BLACK_STONE) + if (p[m + BOTTOM] == BLACK_STONE) { *black |= true; - else - if (p[m + BOTTOM] == WHITE_STONE) - *white |= true; - else - if (explored[m + BOTTOM] == false) - { - explored[m + BOTTOM] = true; - _search(p, m + BOTTOM, explored, black, white); - } + } else if (p[m + BOTTOM] == WHITE_STONE) { + *white |= true; + } else if (explored[m + BOTTOM] == false) { + explored[m + BOTTOM] = true; + _search(p, m + BOTTOM, explored, black, white); + } } } @@ -275,35 +259,33 @@ d16 score_stones_and_area( u8 bak[TOTAL_BOARD_SIZ]; memcpy(bak, p, TOTAL_BOARD_SIZ); - for (move m = 0; m < TOTAL_BOARD_SIZ; ++m) - if (p[m] == EMPTY && !explored[m]) /* Find owner of empty intersection */ - { + for (move m = 0; m < TOTAL_BOARD_SIZ; ++m) { + if (p[m] == EMPTY && !explored[m]) { /* Find owner of empty intersection */ bool found_black = false; bool found_white = false; explored[m] = true; _search(p, m, explored, &found_black, &found_white); - if (found_black != found_white) /* established intersection */ - { - if (found_black) - { + + if (found_black != found_white) { /* established intersection */ + if (found_black) { bak[m] = BLACK_STONE; _apply(bak, m, BLACK_STONE); - } - else - { + } else { bak[m] = WHITE_STONE; _apply(bak, m, WHITE_STONE); } } } + } d16 r = 0; - for (move m = 0; m < TOTAL_BOARD_SIZ; ++m) - if (bak[m] == BLACK_STONE) + for (move m = 0; m < TOTAL_BOARD_SIZ; ++m) { + if (bak[m] == BLACK_STONE) { r += 2; - else - if (bak[m] == WHITE_STONE) - r -= 2; + } else if (bak[m] == WHITE_STONE) { + r -= 2; + } + } return r - komi; } -- 2.11.4.GIT