From af3fa93557135c9d13e7db44090f26e757fecc37 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Wed, 28 Aug 2024 00:47:58 +0200 Subject: [PATCH] board is ready --- constant.c | 61 +++++++---------------------- constant.h | 4 -- display.c | 44 ++++----------------- display.h | 2 +- function.c | 112 ----------------------------------------------------- function.h | 10 ----- reversi.c | 73 ++++++++++++++++++++++++++++++++-- type.h | 11 ------ 8 files changed, 93 insertions(+), 224 deletions(-) diff --git a/constant.c b/constant.c index 3629180..8b5229d 100644 --- a/constant.c +++ b/constant.c @@ -1,7 +1,6 @@ #include #include -#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; } diff --git a/constant.h b/constant.h index 00de5ac..640eea9 100644 --- a/constant.h +++ b/constant.h @@ -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__ */ diff --git a/display.c b/display.c index f8d85a3..4088816 100644 --- 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) diff --git a/display.h b/display.h index 4cd615a..bbed757 100644 --- 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); diff --git a/function.c b/function.c index cfa5471..4b3c1ff 100644 --- a/function.c +++ b/function.c @@ -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: */ diff --git a/function.h b/function.h index 335fca6..0483576 100644 --- a/function.h +++ b/function.h @@ -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: */ diff --git a/reversi.c b/reversi.c index 26d193d..3afe321 100644 --- 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 = " 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 199c75f..34ea365 100644 --- 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: */ -- 2.30.2