EditBoard: simplify loop logic by ending iterations early.
authorYann Dirson <ydirson@free.fr>
Sat, 15 Feb 2014 22:28:41 +0000 (23:28 +0100)
committerYann Dirson <ydirson@free.fr>
Sun, 16 Feb 2014 15:07:38 +0000 (16:07 +0100)
gnushogi/cursesdsp.c
gnushogi/rawdsp.c

index 213c008..127f6b4 100644 (file)
@@ -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++)
index 287e578..2c4d185 100644 (file)
@@ -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++)