move log has now three columns and starts scrolling after 45 moves

Wed, 29 Aug 2018 13:45:13 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 29 Aug 2018 13:45:13 +0200
changeset 63
611332453da0
parent 62
564af8a16828
child 64
4eda5df55f86

move log has now three columns and starts scrolling after 45 moves

src/chess/rules.c file | annotate | diff | comparison | revisions
src/chess/rules.h file | annotate | diff | comparison | revisions
src/game.c file | annotate | diff | comparison | revisions
--- a/src/chess/rules.c	Tue Aug 28 15:56:33 2018 +0200
+++ b/src/chess/rules.c	Wed Aug 29 13:45:13 2018 +0200
@@ -171,10 +171,12 @@
         
         gamestate->lastmove->next = elem;
         gamestate->lastmove = elem;
+        gamestate->movecount++;
     } else {
         elem->move.movetime.tv_usec = 0;
         elem->move.movetime.tv_sec = 0;
         gamestate->movelist = gamestate->lastmove = elem;
+        gamestate->movecount = 1;
     }
 }
 
--- a/src/chess/rules.h	Tue Aug 28 15:56:33 2018 +0200
+++ b/src/chess/rules.h	Wed Aug 29 13:45:13 2018 +0200
@@ -112,6 +112,7 @@
     Board board;
     MoveList* movelist;
     MoveList* lastmove;
+    unsigned int movecount; /* number of (half-)moves (counting BOTH colors) */
     _Bool checkmate;
     _Bool stalemate;
     _Bool remis;
--- a/src/game.c	Tue Aug 28 15:56:33 2018 +0200
+++ b/src/game.c	Wed Aug 29 13:45:13 2018 +0200
@@ -38,7 +38,7 @@
 #include <stdio.h>
 #include <errno.h>
 
-static const uint8_t boardx = 10, boardy = 10;
+static const uint8_t boardx = 4, boardy = 10;
 static int inputy = 21; /* should be overridden on game startup */
 
 static int timecontrol(GameState *gamestate, GameInfo *gameinfo) {
@@ -112,29 +112,32 @@
     }
     
     /* move log */
-    // TODO: introduce window to avoid bugs with a long move log
-    uint8_t logy = 1;
-    const uint8_t logx = boardx + 30;
-    int logi = 1;
+    uint8_t logy = 2;
+    const uint8_t logx = boardx + 28;
+    move(logy, logx);
+    
+    unsigned int logi = 0;
     MoveList *logelem = gamestate->movelist;
     
+    /* wrap log after 45 moves */
+    while (gamestate->movecount/6-logi/3 >= 15) {
+        logelem = logelem->next->next;
+        logi++;
+    }
+    
     while (logelem) {
-        logi++;
-        if (logi % 2 == 0) {
-            if ((logi - 2) % 4 == 0) {
-                logy++;
-                move(logy, logx);
-            }
-            printw("%d. ", logi / 2);
+        _Bool iswhite = (logelem->move.piece & COLOR_MASK) == WHITE;
+        if (iswhite) {
+            logi++;
+            printw("%d. ", logi);
         }
 
         addstr(logelem->move.string);
-        if (!logelem->next) {
-            if (gamestate->stalemate) {
-                addstr(" stalemate");
-            }
+        if (!iswhite && logi%3 == 0) {
+            move(++logy, logx);
+        } else {
+            addch(' ');
         }
-        addch(' ');
 
         logelem = logelem->next;
     }

mercurial