start code cleaning
authorLaurent Mazet <mazet@softndesign.org>
Wed, 10 Jul 2024 22:25:08 +0000 (00:25 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Wed, 10 Jul 2024 22:25:08 +0000 (00:25 +0200)
function.c
function.h

index af0c3e7a95cc94a187108ec8ba047ea8ccbfdd6b..6b6d70fd0d3fed7d63b0c004153245f670409a34 100644 (file)
@@ -356,42 +356,82 @@ void pettoqueen (board_t *board)
     }
 }
 
-int testjump (board_t *board, int x, int y, int mode)
+int _testcell (board_t *board, int xcursor, int ycursor, char value, int delta, int mode)
 {
     int ret = 0;
+    switch (mode) {
+    case 0:
+        ret = (getvalue (board, xcursor + delta, ycursor - delta) == value);
+        break;
+    case 1:
+        ret = (getvalue (board, xcursor - delta, ycursor - delta) == value);
+        break;
+    case 2:
+        ret = (getvalue (board, xcursor + delta, ycursor + delta) == value);
+        break;
+    case 3:
+        ret = (getvalue (board, xcursor + delta, ycursor + delta) == value);
+        break;
+    }
+    return ret;
+}
+
+int isempty (board_t *board, int xcursor, int ycursor, int delta, int mode)
+{
+    return _testcell (board, xcursor, ycursor, '.', delta, mode);
+}
+
+int ispet (board_t *board, int xcursor, int ycursor, int id, int delta, int mode)
+{
+    return (id == -1) ?
+        (_testcell (board, xcursor, ycursor, '0', delta, mode) &&
+        _testcell (board, xcursor, ycursor, '1', delta, mode)) :
+        _testcell (board, xcursor, ycursor, '0' + id, delta, mode);
+}
+
+int isqueen (board_t *board, int xcursor, int ycursor, int id, int delta, int mode)
+{
+    return (id == -1) ?
+        (_testcell (board, xcursor, ycursor, '6', delta, mode) &&
+        _testcell (board, xcursor, ycursor, '7', delta, mode)) :
+        _testcell (board, xcursor, ycursor, '6' + id, delta, mode);
+}
 
-    char id = getvalue (board, x, y);
+int _testjump (board_t *board, int x, int y, int delta, int mode)
+{
+    int ret = 0;
     switch (mode) {
     case 0:
-        if ((x + 2 < board->width) && (y - 2 >= 0)) {
-            ret = (getvalue (board, x + 1, y - 1) == '0' + '1' - id) && (getvalue (board, x + 2, y - 2) == '.');
-        }
+        ret = ((x + 1 + delta < board->width) && (y - 1 - delta >= 0));
         break;
     case 1:
-        if ((x - 2 >= 0) && (y - 2 >= 0)) {
-            ret = (getvalue (board, x - 1, y - 1) == '0' + '1' - id) && (getvalue (board, x - 2, y - 2) == '.');
-        }
+        ret = ((x - 1 - delta >= 0) && (y - 1 - delta >= 0));
         break;
     case 2:
-        if ((x - 2 >= 0) && (y + 2 < board->height)) {
-            ret = (getvalue (board, x - 1, y + 1) == '0' + '1' - id) && (getvalue (board, x - 2, y + 2) == '.');
-        }
+        ret = ((x - 1 - delta >= 0) && (y + 1 + delta < board->height));
         break;
     case 3:
-        if ((x + 2 < board->width) && (y + 2 < board->height)) {
-            ret = (getvalue (board, x + 1, y + 1) == '0' + '1' - id) && (getvalue (board, x + 2, y + 2) == '.');
-        }
+        ret = ((x + 1 + delta < board->width) && (y + 1 + delta < board->height));
         break;
     }
+    if (ret) {
+        int id = getvalue (board, x, y) - '0';
+        ret = ispet (board, x, y, 1 - id, 1 + delta, mode) && isempty (board, x, y, 2 + delta, mode);
+    }
     return ret;
 }
 
+int testjump (board_t *board, int x, int y, int mode)
+{
+    return _testjump (board, x, y, 0, mode);
+}
+
 int testalljumps (board_t *board, int x, int y)
 {
     int ret = 0;
     int m;
     for (m = 0; m < 4; m++) {
-        if (testjump (board, x, y, m)) {
+        if (_testjump (board, x, y, 0, m)) {
             ret = 1;
             break;
         }
@@ -438,20 +478,7 @@ int testqueenmove (board_t *board, int xcursor, int ycursor, int queen, int mode
 {
     int ret = 0;
     if ((mode == queen) || (queen == -1)) {
-        switch (mode) {
-        case 0:
-            ret = (getvalue (board, xcursor + 1, ycursor - 1) == '.');
-            break;
-        case 1:
-            ret = (getvalue (board, xcursor - 1, ycursor - 1) == '.');
-            break;
-        case 2:
-            ret = (getvalue (board, xcursor + 1, ycursor + 1) == '.');
-            break;
-        case 3:
-            ret = (getvalue (board, xcursor + 1, ycursor + 1) == '.');
-            break;
-        }
+        ret = isempty (board, xcursor, ycursor, 1, mode);
     }
     return ret;
 }
@@ -463,39 +490,11 @@ int testqueenjump (board_t *board, int xcursor, int ycursor, int mode)
     }
     int ret = 0;
     int delta = 0;
-    switch (mode) {
-    case 0:
-        while (!(ret = testjump (board, xcursor + delta, ycursor - delta, 0))) {
-            delta++;
-            if (getvalue (board, xcursor + delta, ycursor - delta) != '.') {
-                break;
-            }
-        }
-        break;
-    case 1:
-        while (!(ret = testjump (board, xcursor - delta, ycursor - delta, 1))) {
-            delta++;
-            if (getvalue (board, xcursor - delta, ycursor - delta) != '.') {
-                break;
-            }
-        }
-        break;
-    case 2:
-        while (!(ret = testjump (board, xcursor - delta, ycursor + delta, 2))) {
-            delta++;
-            if (getvalue (board, xcursor - delta, ycursor + delta) != '.') {
-                break;
-            }
-        }
-        break;
-    case 3:
-        while (!(ret = testjump (board, xcursor + delta, ycursor + delta, 3))) {
-            delta++;
-            if (getvalue (board, xcursor + delta, ycursor + delta) != '.') {
-                break;
-            }
+    while (!(ret = _testjump (board, xcursor, ycursor, delta, mode))) {
+        delta++;
+        if (!isempty (board, xcursor, ycursor, delta, mode)) {
+            break;
         }
-        break;
     }
     return (ret) ? delta + 1 : ret;
 }
index 0ae101e7efea1c8602ddfa371ab3609d6a6c29e9..7c832606adaacc917c7f5a42c5b279bffdb64b71 100644 (file)
@@ -49,6 +49,12 @@ void cleanafterjump (board_t *board);
 
 void pettoqueen (board_t *board);
 
+int isempty (board_t *board, int xcursor, int ycursor, int delta, int mode);
+
+int ispet (board_t *board, int xcursor, int ycursor, int id, int delta, int mode);
+
+int isqueen (board_t *board, int xcursor, int ycursor, int id, int delta, int mode);
+
 int testjump (board_t *board, int x, int y, int mode);
 
 int testalljumps (board_t *board, int x, int y);