From ff70bcfb13c2a38180a93d689baccd6828dfda90 Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Sat, 15 Feb 2014 23:28:41 +0100 Subject: [PATCH] EditBoard: simplify loop logic by ending iterations early. --- gnushogi/cursesdsp.c | 55 +++++++++++++++++++++++++------------------------ gnushogi/rawdsp.c | 52 +++++++++++++++++++++++----------------------- 2 files changed, 54 insertions(+), 53 deletions(-) diff --git a/gnushogi/cursesdsp.c b/gnushogi/cursesdsp.c index 213c008..127f6b4 100644 --- a/gnushogi/cursesdsp.c +++ b/gnushogi/cursesdsp.c @@ -499,10 +499,13 @@ Curses_EditBoard(void) ClearCaptured(); UpdateCatched(); + continue; } - if (s[0] == 'c') + if (s[0] == 'c') { a = otherside[a]; + continue; + } if (s[1] == '*') { @@ -517,40 +520,38 @@ Curses_EditBoard(void) } } - c = -1; - } - else - { - c = COL_NUM(s[1]); - r = ROW_NUM(s[2]); + continue; } - if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS)) + c = COL_NUM(s[1]); + r = ROW_NUM(s[2]); + + if ((c < 0) || (c >= NO_COLS) || (r < 0) || (r >= NO_ROWS)) + continue; + + sq = locn(r, c); + color[sq] = a; + board[sq] = no_piece; + + for (i = NO_PIECES; i > no_piece; i--) { - sq = locn(r, c); - color[sq] = a; - board[sq] = no_piece; - - for (i = NO_PIECES; i > no_piece; i--) + if ((s[0] == pxx[i]) || (s[0] == qxx[i])) { - if ((s[0] == pxx[i]) || (s[0] == qxx[i])) - { - if (s[3] == '+') - board[sq] = promoted[i]; - else - board[sq] = unpromoted[i]; + if (s[3] == '+') + board[sq] = promoted[i]; + else + board[sq] = unpromoted[i]; - found = 1; - break; - } + found = 1; + break; } - + } - if (found == 0) - color[sq] = neutral; - DrawPiece(sq); - } + if (found == 0) + color[sq] = neutral; + + DrawPiece(sq); } for (sq = 0; sq < NO_SQUARES; sq++) diff --git a/gnushogi/rawdsp.c b/gnushogi/rawdsp.c index 287e578..2c4d185 100644 --- a/gnushogi/rawdsp.c +++ b/gnushogi/rawdsp.c @@ -366,10 +366,13 @@ Raw_EditBoard(void) } ClearCaptured(); + continue; } - if (s[0] == 'c') + if (s[0] == 'c') { a = otherside[a]; + continue; + } if (s[1] == '*') { @@ -382,39 +385,36 @@ Raw_EditBoard(void) break; } } - - c = -1; - r = -1; + continue; } - else - { - c = COL_NUM(s[1]); - r = ROW_NUM(s[2]); + + c = COL_NUM(s[1]); + r = ROW_NUM(s[2]); + + if ((c < 0) || (c >= NO_COLS) || (r < 0) || (r >= NO_ROWS)) { + continue; } - if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS)) - { - sq = locn(r, c); - color[sq] = a; - board[sq] = no_piece; + sq = locn(r, c); + color[sq] = a; + board[sq] = no_piece; - for (i = no_piece; i <= king; i++) + for (i = no_piece; i <= king; i++) + { + if ((s[0] == pxx[i]) || (s[0] == qxx[i])) { - if ((s[0] == pxx[i]) || (s[0] == qxx[i])) - { - if (s[3] == '+') - board[sq] = promoted[i]; - else - board[sq] = i; + if (s[3] == '+') + board[sq] = promoted[i]; + else + board[sq] = i; - found = 1; - break; - } + found = 1; + break; } - - if (found == 0) - color[sq] = neutral; } + + if (found == 0) + color[sq] = neutral; } for (sq = 0; sq < NO_SQUARES; sq++) -- 1.7.0.4