From e9e6ce27155aa0570e05548ed339e97d8dd05f83 Mon Sep 17 00:00:00 2001 From: Tiehuis Date: Sat, 13 Sep 2014 09:52:55 +1200 Subject: [PATCH] Readd some logic details that are needed. Fix colors on some blocks being black --- README.md | 1 + src/2048.c | 68 +++++++++++++++++++++++++++++++++++++++--------------- src/2048.h | 2 +- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c9e763e..71e3392 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ working directory. Any file with this name will be modified and replaced. -b Set the block spawn rate -r Resets hiscore. Will prompt user -c Enables color support (ncurses version only) + -C Disables color support (ncurses version only) ## License This code is licensed under the [MIT License](https://github.com/Tiehuis/2048-cli/blob/master/LICENSE). diff --git a/src/2048.c b/src/2048.c index b784f59..4f18260 100644 --- a/src/2048.c +++ b/src/2048.c @@ -28,30 +28,60 @@ int merge(dir_t d) int moved = 0; int i, j; - if (d == DIR_LEFT || d == DIR_RIGHT) { + if (d == DIR_LEFT) { + /* Move from upper left, across rows until second to second-last elem each row */ for (i = 0; i < grid_size; i++) { - for (j = 0; j < grid_size - 1; j++) { - if (grid[i][j] == grid[i][j + 1]) { - grid[i][j] <<= 1; - grid[i][j + 1] = 0; - score_last += grid[i][j]; - score += grid[i][j]; - moved = 1; + for (j = 0; j < grid_size - 1; j++) { + if (grid[i][j] == grid[i][j + 1]) { + grid[i][j] <<= 1; + grid[i][j + 1] = 0; + score_last += grid[i][j]; + score += grid[i][j]; + moved = 1; + } } } - } } - else { + else if (d == DIR_UP) { + /* Move from upper left, across rows until final row which is skipped */ for (i = 0; i < grid_size - 1; i++) { - for (j = 0; j < grid_size; j++) { - if (grid[i][j] == grid[i + 1][j]) { - grid[i][j] <<= 1; - grid[i + 1][j] = 0; - score_last += grid[i][j]; - score += grid[i][j]; - moved = 1; + for (j = 0; j < grid_size; j++) { + if (grid[i][j] == grid[i + 1][j]) { + grid[i][j] <<= 1; + grid[i + 1][j] = 0; + score_last += grid[i][j]; + score += grid[i][j]; + moved = 1; + } } } + } + else if (d == DIR_RIGHT) { + /* Move from lower right, backwards across rows until first elem each row */ + for (i = grid_size - 1; i >= 0; i--) { + for (j = grid_size - 1; j > 0; j--) { + if (grid[i][j] == grid[i][j - 1]) { + grid[i][j] <<= 1; + grid[i][j - 1] = 0; + score_last += grid[i][j]; + score += grid[i][j]; + moved = 1; + } + } + } + } + else if (d == DIR_DOWN) { + /* Move from lower right, across rows until first row which is skipped */ + for (i = grid_size - 1; i > 0; i--) { + for (j = grid_size - 1; j >= 0; j--) { + if (grid[i][j] == grid[i - 1][j]) { + grid[i][j] <<= 1; + grid[i - 1][j] = 0; + score_last += grid[i][j]; + score += grid[i][j]; + moved = 1; + } + } } } @@ -194,9 +224,9 @@ void draw_grid(WINDOW *gamewin) mvwprintw(gamewin, yps, xps++, "|"); for (j = 0; j < grid_size; j++) { if (grid[i][j]) { - wattron(gamewin, COLOR_PAIR(flog2(grid[i][j]) & 7)); + wattron(gamewin, COLOR_PAIR(flog2(grid[i][j]) % 7)); mvwprintw(gamewin, yps, xps, "%*d", printwidth, grid[i][j]); - wattroff(gamewin, COLOR_PAIR(flog2(grid[i][j]) & 7)); + wattroff(gamewin, COLOR_PAIR(flog2(grid[i][j]) % 7)); mvwprintw(gamewin, yps, xps + printwidth, " |"); } else { diff --git a/src/2048.h b/src/2048.h index 82fc6be..49937c4 100644 --- a/src/2048.h +++ b/src/2048.h @@ -60,6 +60,6 @@ const char* get_highscore_file(); " -s Set the grid border length\n"\ " -b Set the block spawn rate\n"\ " -c Enables color support (ncurses version only)\n"\ - " -C Disabled color support (ncurses version only)\n" + " -C Disables color support (ncurses version only)\n" #endif