board is ready
authorLaurent Mazet <mazet@softndesign.org>
Tue, 27 Aug 2024 22:47:58 +0000 (00:47 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Tue, 27 Aug 2024 22:47:58 +0000 (00:47 +0200)
constant.c
constant.h
display.c
display.h
function.c
function.h
reversi.c
type.h

index 3629180dd244cc3c771771faa55511d477606f92..8b5229d4340c80f450055314e25fc6ae0ca470cd 100644 (file)
@@ -1,7 +1,6 @@
 #include <stdio.h>
 #include <string.h>
 
-#include "block.h"
 #include "function.h"
 #include "type.h"
 
@@ -9,69 +8,37 @@
 
 /* constants */
 
-int height = 20;
-int minwidth = 8;
-int width = 10;
-int maxwidth = 15;
-
 int nbdigit = 5;
 int maxscor = 100000;
-int nbholes = 2;
 
 int savelen = 12;
 int xoffset = 1;
 int yoffset = 1;
 
-/* blocks */
-
-#define _nb_blocks_std 12
-
-block_t _blocks_std[_nb_blocks_std] = {
-    {3, 3, yellow, " ....  . "}, // F
-    {1, 5, blue, "...."},      // I
-    {2, 4, lightmagenta, ". . . .."},  // L
-    {2, 4, lightblue, " .... . "},  // N
-    {2, 3, lightgreen, " ....."},    // P
-    {3, 3, lightcyan, "... .  . "}, // T
-    {3, 2, magenta, ". ...."},    // U
-    {3, 3, cyan, ".  .  ..."}, // V
-    {3, 3, lightergreen, ".  ..  .."}, // W
-    {3, 3, red, " . ... . "}, // X
-    {2, 4, darkblue, " ... . ."},  // Y
-    {3, 3, brown, "..  .  .."}, // Z
-};
-
-block_t *getblocks (char *name, int *nb)
-{
-    block_t *pt = NULL;
-
-    if (strcmp (name, "std") == 0) {
-        pt = _blocks_std;
-        *nb = _nb_blocks_std;
-    }
-
-    return pt;
-}
-
 /* board */
 
 board_t *getboard (char *name)
 {
     board_t *board = NULL;
 
-    if (strcmp (name, "6x10") == 0) {
-        board = initboard (6, 10);
-    } else if (strcmp (name, "5x12") == 0) {
-        board = initboard (5, 12);
-    } else if (strcmp (name, "4x15") == 0) {
-        board = initboard (4, 15);
-    } else if (strcmp (name, "3x20") == 0) {
-        board = initboard (3, 20);
+    if (strcmp (name, "6x6") == 0) {
+        board = initboard (6, 6);
+    } else if (strcmp (name, "8x8") == 0) {
+        board = initboard (8, 8);
+    } else if (strcmp (name, "10x10") == 0) {
+        board = initboard (10, 10);
+    } else if (strcmp (name, "12x12") == 0) {
+        board = initboard (12, 12);
     } else if (strcmp (name, "list") == 0) {
-        printf ("board: 6x10 5x12 4x15 3x20\n");
+        printf ("board: 6x6 8x8 10x10 12x12\n");
         board = (board_t *)(-1);
     }
 
+    *getcell (board, board->width / 2 - 1, board->height / 2 - 1) = '0';
+    *getcell (board, board->width / 2 - 0, board->height / 2 - 1) = '1';
+    *getcell (board, board->width / 2 - 1, board->height / 2 - 0) = '1';
+    *getcell (board, board->width / 2 - 0, board->height / 2 - 0) = '0';
+
     return board;
 }
 
index 00de5ac0db317741884765f6fd66605370d44c5e..640eea985272b91ffff27c49d84924ae23fbe978 100644 (file)
@@ -13,10 +13,6 @@ extern int yoffset;
 
 #endif /* __CONSTANT_C__ */
 
-/* block definitions */
-
-block_t *getblocks (char *name, int *nb);
-
 board_t *getboard (char *name);
 
 #endif /* __CONSTANT_H__ */
index f8d85a35bb1fe172ec6643c61b25c0004fd9f1ad..40888167ae5c93a150d509bb81b2ab08d2174d04 100644 (file)
--- a/display.c
+++ b/display.c
@@ -72,29 +72,17 @@ void _put_color_block (int y, int x, char symb)
     case ' ':
         mvaddcb (y, x, black);
         break;
-    case '1':
-        mvaddcb (y, x, cyan);
-        break;
-    case '2':
-        mvaddcb (y, x, yellow);
-        break;
-    case '3':
-        mvaddcb (y, x, magenta);
-        break;
-    case '4':
-        mvaddcb (y, x, brown);
-        break;
-    case '5':
+    case '0':
         mvaddcb (y, x, blue);
         break;
-    case '6':
+    case '1':
         mvaddcb (y, x, red);
         break;
-    case '7':
-        mvaddcb (y, x, green);
+    case '2':
+        mvaddcb (y, x, lighterblue);
         break;
-    case '8':
-        mvaddcb (y, x, white);
+    case '3':
+        mvaddcb (y, x, lighterred);
         break;
     }
 }
@@ -161,25 +149,9 @@ void boardwindow (board_t *board, int mode)
     }
 }
 
-void displayblock (board_t *board, block_t *block, int x, int y)
+void displaycursor (board_t *board, int id, int x, int y)
 {
-    int i, j;
-
-    if (x == -1) {
-        x = board->width / 2;
-    }
-
-    if (y == -1) {
-        y = (board->height - block->height) / 2;
-    }
-
-    for (i = 0; i < block->width; i++) {
-        for (j = 0; j < block->height; j++) {
-            if (*getcell (block, i, j) != ' ') {
-                _element (board, x - block->width / 2 + i, y + j, '0' + block->color);
-            }
-        }
-    }
+    _element (board, x, y, (id == 0) ? '2' : '3');
 }
 
 char *savewindow (int length, int xoffset, int yoffset)
index 4cd615a63111624b0dac89a63156d8cf9d894c30..bbed75736e2f592f1c050eb2980225f3c54d8738 100644 (file)
--- a/display.h
+++ b/display.h
@@ -28,7 +28,7 @@ void _element (board_t *board, int x, int y, int symb);
 
 void boardwindow (board_t *board, int mode);
 
-void displayblock (board_t *board, block_t *block, int x, int y);
+void displaycursor (board_t *board, int id, int x, int y);
 
 char *savewindow (int length, int xoffset, int yoffset);
 
index cfa5471f8d58fca0179c56de330a19ea1b6622d3..4b3c1ff05cd2b32112a3aace6e8e7b76385667ab 100644 (file)
@@ -34,8 +34,6 @@ board_t *initboard (int width, int height)
     board->ysize = board->height = height;
     board->xoffset = 0;
     board->yoffset = 0;
-    board->current = -1;
-    board->next = -1;
     return board;
 }
 
@@ -95,10 +93,6 @@ char *saveboard (board_t *board)
     int l = sprintf (buffer, "width: %d\n", board->width);
     l += sprintf (buffer + l, "height: %d\n", board->height);
     l += sprintf (buffer + l, "tab: \"%s\"\n", board->tab);
-    l += sprintf (buffer + l, "current: %d\n", board->current);
-    l += sprintf (buffer + l, "lines: %d\n", board->lines);
-    l += sprintf (buffer + l, "next: %d\n", board->next);
-    l += sprintf (buffer + l, "score: %d\n", board->score);
 
     VERBOSE (INFO, _makecomments (buffer + l, board));
 
@@ -165,10 +159,6 @@ board_t *loadboard (char *str)
     int width = 0;
     int height = 0;
     char *tab = NULL;
-    int current = -1;
-    int lines = 0;
-    int next = -1;
-    int score = 0;
 
     char *saveptr1, *saveptr2;
 
@@ -188,14 +178,6 @@ board_t *loadboard (char *str)
             height = atoi (value);
         } else if (strcmp (keyword,  "tab") == 0) {
             tab = atos (value);
-        } else if (strcmp (keyword,  "current") == 0) {
-            current = atoi (value);
-        } else if (strcmp (keyword,  "lines") == 0) {
-            lines = atoi (value);
-        } else if (strcmp (keyword,  "next") == 0) {
-            next = atoi (value);
-        } else if (strcmp (keyword,  "score") == 0) {
-            score = atoi (value);
         } else if (strcmp (keyword,  "rem") == 0) {
             /* nothing to do with remark */
         } else {
@@ -209,103 +191,9 @@ board_t *loadboard (char *str)
     if ((tab) && (strlen (tab) == (size_t)(width * height))) {
         board = initboard (width, height);
         memcpy (board->tab, tab, width * height);
-        board->current = current;
-        board->lines = lines;
-        board->next = next;
-        board->score = score;
     }
 
     return board;
 }
 
-block_t *initblock (int width, int height)
-{
-    block_t *block = (block_t *) malloc (sizeof (block_t));
-    CHECKALLOC (block);
-    block->tab = (char *) calloc (1, width * height + 1);
-    CHECKALLOC (block->tab);
-    memset (block->tab, ' ', width * height);
-    block->width = width;
-    block->height = height;
-    return block;
-}
-
-block_t *changeblock (block_t *dest, block_t *src)
-{
-    block_t *ret = NULL;
-    if (dest && src) {
-        free (dest->tab);
-        memcpy (dest, src, sizeof (block_t));
-        dest->tab = strdup (src->tab);
-        CHECKALLOC (dest->tab);
-        ret = dest;
-    }
-    return ret;
-}
-
-block_t *copyblock (block_t *block)
-{
-    block_t *newblock = initblock (block->width, block->height);
-    char *tab = newblock->tab;
-    memcpy (newblock, block, sizeof (block_t));
-    newblock->tab = tab;
-    memcpy (newblock->tab, block->tab, block->width * block->height + 1);
-    return newblock;
-}
-
-void freeblock (block_t *block)
-{
-    if (block) {
-        free (block->tab);
-    }
-    free (block);
-}
-
-block_t *rotateblock (block_t *block, int rot)
-{
-    int i, j;
-
-    rot = (rot > 0) ? rot % 4 : ((1 - rot / 4) * 4 + rot) % 4;
-
-    block_t *newblock = NULL;
-
-    switch (rot) {
-    case 0:
-        newblock = copyblock (block);
-        break;
-    case 1:
-        newblock = initblock (block->height, block->width);
-        newblock->color = block->color;
-        for (i = 0; i < block->width; i++) {
-            for (j = 0; j < block->height; j++) {
-                *getcell (newblock, block->height - 1 - j, i) = *getcell (block, i, j);
-            }
-        }
-        break;
-    case 2:
-        newblock = initblock (block->width, block->height);
-        newblock->color = block->color;
-        for (i = 0; i < block->width; i++) {
-            for (j = 0; j < block->height; j++) {
-                *getcell (newblock, block->width - 1 - i, block->height - 1 - j) = *getcell (block, i, j);
-            }
-        }
-        break;
-    case 3:
-        newblock = initblock (block->height, block->width);
-        newblock->color = block->color;
-        for (i = 0; i < block->width; i++) {
-            for (j = 0; j < block->height; j++) {
-                *getcell (newblock, j, block->width - 1 - i) = *getcell (block, i, j);
-            }
-        }
-        break;
-    }
-
-    changeblock (block, newblock);
-    freeblock (newblock);
-
-    return block;
-}
-
 /* vim: set ts=4 sw=4 et: */
index 335fca6aa106a302d7c10ceecc20acd81d4b4b7d..0483576b94d172714feb75465b45eacd3558671a 100644 (file)
@@ -39,16 +39,6 @@ char *atos (char *str);
 
 board_t *loadboard (char *str);
 
-block_t *initblock (int width, int height);
-
-block_t *changeblock (block_t *dest, block_t *src);
-
-block_t *copyblock (block_t *block);
-
-void freeblock (block_t *block);
-
-block_t *rotateblock (block_t *block, int rot);
-
 #endif /* __FUNCTION_H__ */
 
 /* vim: set ts=4 sw=4 et: */
index 26d193d03ca62390763c4349f116bf6637bf41de..3afe321159bcbb34d2dcae74b30b9f541bdd4432 100644 (file)
--- a/reversi.c
+++ b/reversi.c
@@ -20,7 +20,7 @@ char *version = "0.1";
 
 char *filename = NULL;
 int scale = 1;
-char *boardname = "6x10";
+char *boardname = "8x8";
 
 char *help =
     "<i> Move up cursor\n"
@@ -150,10 +150,12 @@ int main (int argc, char *argv[])
     /* window positions */
     int xboard = board->xoffset = xoffset + 1;
     int yboard = board->yoffset = xoffset + 1;
-    int xhelp = xboard + xoffset + 1 + board->xsize;
+    int xscore = xboard + xoffset + 1 + board->xsize;
+    int yscore = yboard;
+    int xhelp = xscore + xoffset;
+    int yhelp = yscore - 1;
     int xcursor = 0;
     int ycursor = 0;
-    int yhelp = yboard - 1;
     int xsave = max (xboard + (board->xsize - savelen) / 2, 1);
     int ysave = yboard + (board->ysize - 1) / 2;
     char *savename = NULL;
@@ -166,10 +168,75 @@ int main (int argc, char *argv[])
     int ymsg = max (yboard + xoffset + 1 + board->ysize, yhelp + lhelp + yoffset + 1);
     int lmsg = xhelp - xmsg + strmaxlen (help, '\n');
 
+    /* main loop */
+    int stop = 0;
+    int id = 0;
+    int mode = 0;
+    while (!stop) {
 
+        boardwindow (board, 0);
+        displaycursor (board, id, xcursor, ycursor);
 
+        int ch = getch ();
 
+        /* general controls */
+        switch (ch) {
+        case KEY_ESC:
+        case 'q':
+            stop = 1;
+            break;
+        case 's':
+            savename = savewindow (savelen, xsave, ysave);
+            if (savename != NULL) {
+                char *ptr = saveboard (board);
+                if (writedata (savename, ptr)) {
+                    VERBOSE (WARNING, printf ("issue writing Board\n"));
+                }
+                free (ptr);
+                free (savename);
+            }
+            break;
+        }
 
+        /* test end of game */
+        if (mode == 1) {
+            continue;
+        }
+
+        /* game controls */
+        switch (ch) {
+        case '\n':
+        case '\r':
+        case ' ':
+            id = (id == 0) ? 1 : 0;
+            break;
+        case KEY_UP:
+        case 'i':
+            if (ycursor > 0) {
+                ycursor--;
+            }
+            break;
+        case KEY_LEFT:
+        case 'j':
+            if (xcursor > 0) {
+                xcursor--;
+            }
+            break;
+        case KEY_DOWN:
+        case 'k':
+            if (ycursor < board->height - 1) {
+                ycursor++;
+            }
+            break;
+        case KEY_RIGHT:
+        case 'l':
+            if (xcursor < board->width - 1) {
+                xcursor++;
+            }
+            break;
+        }
+
+    }
 
     endwin ();
 
diff --git a/type.h b/type.h
index 199c75f7edc3501d218feccc8914e6c495e2450a..34ea36521c6194f96303fe65152f755cb4c37555 100644 (file)
--- a/type.h
+++ b/type.h
@@ -10,19 +10,8 @@ typedef struct {
     int ysize;
     int xoffset;
     int yoffset;
-    int current;
-    int lines;
-    int next;
-    int score;
 } board_t;
 
-typedef struct {
-    int width;
-    int height;
-    int color;
-    char *tab;
-} block_t;
-
 #endif /* __TYPE_H__ */
 
 /* vim: set ts=4 sw=4 et: */