From 90df5c62a458f5788ef4b08bbc709a72e844547b Mon Sep 17 00:00:00 2001 From: Tiehuis Date: Sat, 15 Mar 2014 09:45:55 +1300 Subject: [PATCH] Added simple color support for ncurses ver. Supposedly the game starts at 2 usually; lets change that --- 2048_curses.c | 58 +++++++++++++++++++++++++++++++++++------------- 2048_no_curses.c | 2 +- README.md | 1 + 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/2048_curses.c b/2048_curses.c index 844d975..8ab1170 100644 --- a/2048_curses.c +++ b/2048_curses.c @@ -1,6 +1,5 @@ #include #include /* for malloc */ -#include /* */ #include /* for time */ #include /* for getopts */ @@ -87,6 +86,7 @@ void merge(int d) /* move all values in the grid to the edge given by the direction pressed */ /* would be nice to generalize this code a little so didn't need four loops */ +/* if animations are wanted, then need to alter this so it moves each square one at a time */ void gravitate(int d) { if (d == DL) { @@ -159,13 +159,13 @@ int space_left() } /* places a random block onto the grid */ -/* do this in a smarter fashion */ +/* could do this in a much smarter fashion by finding which spaces are free */ void rand_block() { if (space_left()) { int x_p, y_p; while (g[x_p = rand() % SZ][y_p = rand() % SZ]); - g[x_p][y_p] = 1; + g[x_p][y_p] = 2; } else { endwin(); @@ -175,7 +175,17 @@ void rand_block() } } +/* quick floor log2(n) */ +int flog2(int n) +{ + int k = 0; + while (n) + k++, n >>= 1; + return k; +} + /* draws the grid and fills it with the current values */ +/* colors just rotate around, works for now, can be confusing when you have some fairly high values on the board */ void draw_grid(WINDOW *gamewin) { // mvwprintw will sometimes have a useless arg @@ -188,7 +198,10 @@ void draw_grid(WINDOW *gamewin) mvwprintw(gamewin, yps, xps++, "|"); for (j = 0; j < SZ; j++) { if (g[i][j]) { - mvwprintw(gamewin, yps, xps, "%*d |", MAXVAL, g[i][j]); + wattron(gamewin, COLOR_PAIR(flog2(g[i][j]) & 7)); + mvwprintw(gamewin, yps, xps, "%*d", MAXVAL, g[i][j]); + wattroff(gamewin, COLOR_PAIR(flog2(g[i][j]) & 7)); + mvwprintw(gamewin, yps, xps + MAXVAL, " |"); } else { ITER(MAXVAL + 1, waddch(gamewin, ' ')); @@ -205,19 +218,38 @@ void draw_grid(WINDOW *gamewin) /* parses options and stores the main game loop */ int main(int argc, char **argv) { + /* init ncurses environment */ + initscr(); + cbreak(); + noecho(); + curs_set(FALSE); + /* init variables */ s = 0; sl = 0; - MAXVAL = 4; SZ = 4; + MAXVAL = 4; CALLOC2D(g, SZ); int n_blocks = 1; /* parse options */ int c; - while ((c = getopt(argc, argv, "hs:b:")) != -1) { + while ((c = getopt(argc, argv, "chs:b:")) != -1) { switch (c) { + // color support - assumes your terminal can display colours + // should still work regardless + case 'c': + start_color(); + init_pair(1, 1, 0); + init_pair(2, 2, 0); + init_pair(3, 3, 0); + init_pair(4, 4, 0); + init_pair(5, 5, 0); + init_pair(6, 6, 0); + init_pair(7, 7, 0); + init_pair(8, 8, 0); + break; // different board sizes case 's': FREE2D(g, SZ); @@ -230,6 +262,7 @@ int main(int argc, char **argv) break; // help menu case 'h': + endwin(); printf("Controls:\n" " hjkl, wasd Movement\n" " q Quit\n" @@ -239,30 +272,23 @@ int main(int argc, char **argv) "\n" "Options:\n" " -b Set the grid border length\n" - " -s Set the block spawn rate\n"); + " -s Set the block spawn rate\n" + " -c Enables color support\n"); exit(EXIT_SUCCESS); } } - /* init ncurses environment */ - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - curs_set(FALSE); - int width = SZ * (MAXVAL + 2) + 1; int height = SZ * (MAXVAL + 2) + 2; // might center in middle of screen WINDOW *gamewin = newwin(height, width, 1, 1); + keypad(gamewin, TRUE); /* random seed */ srand((unsigned int)time(NULL)); ITER(2, rand_block()); draw_grid(gamewin); - - // SCREEN IS NOT DRAWING ON THE VERY FIRST ITERATION int key; while (1) { diff --git a/2048_no_curses.c b/2048_no_curses.c index 364dfbd..1666c5a 100644 --- a/2048_no_curses.c +++ b/2048_no_curses.c @@ -164,7 +164,7 @@ void rand_block() if (space_left()) { int x_p, y_p; while (g[x_p = rand() % SZ][y_p = rand() % SZ]); - g[x_p][y_p] = 1; + g[x_p][y_p] = 2; } else { printf("\n" diff --git a/README.md b/README.md index ca82afa..b888eee 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ If you want to use the ncurses version, make sure that you have the required ncu ## Options -b Set the grid border length -s Set the block spawn rate + -c Enables color support (ncurses version only) ## License This code is licensed under the [MIT License](https://github.com/Tiehuis/2048-cli/blob/master/LICENSE).