Readd some logic details that are needed. Fix colors on some blocks being black
This commit is contained in:
parent
2dab267db2
commit
e9e6ce2715
|
@ -25,6 +25,7 @@ working directory. Any file with this name will be modified and replaced.
|
||||||
-b <rate> Set the block spawn rate
|
-b <rate> Set the block spawn rate
|
||||||
-r Resets hiscore. Will prompt user
|
-r Resets hiscore. Will prompt user
|
||||||
-c Enables color support (ncurses version only)
|
-c Enables color support (ncurses version only)
|
||||||
|
-C Disables color support (ncurses version only)
|
||||||
|
|
||||||
## License
|
## License
|
||||||
This code is licensed under the [MIT License](https://github.com/Tiehuis/2048-cli/blob/master/LICENSE).
|
This code is licensed under the [MIT License](https://github.com/Tiehuis/2048-cli/blob/master/LICENSE).
|
||||||
|
|
38
src/2048.c
38
src/2048.c
|
@ -28,7 +28,8 @@ int merge(dir_t d)
|
||||||
int moved = 0;
|
int moved = 0;
|
||||||
int i, j;
|
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 (i = 0; i < grid_size; i++) {
|
||||||
for (j = 0; j < grid_size - 1; j++) {
|
for (j = 0; j < grid_size - 1; j++) {
|
||||||
if (grid[i][j] == grid[i][j + 1]) {
|
if (grid[i][j] == grid[i][j + 1]) {
|
||||||
|
@ -41,7 +42,8 @@ int merge(dir_t d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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 (i = 0; i < grid_size - 1; i++) {
|
||||||
for (j = 0; j < grid_size; j++) {
|
for (j = 0; j < grid_size; j++) {
|
||||||
if (grid[i][j] == grid[i + 1][j]) {
|
if (grid[i][j] == grid[i + 1][j]) {
|
||||||
|
@ -54,6 +56,34 @@ int merge(dir_t d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return moved;
|
return moved;
|
||||||
}
|
}
|
||||||
|
@ -194,9 +224,9 @@ void draw_grid(WINDOW *gamewin)
|
||||||
mvwprintw(gamewin, yps, xps++, "|");
|
mvwprintw(gamewin, yps, xps++, "|");
|
||||||
for (j = 0; j < grid_size; j++) {
|
for (j = 0; j < grid_size; j++) {
|
||||||
if (grid[i][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]);
|
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, " |");
|
mvwprintw(gamewin, yps, xps + printwidth, " |");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -60,6 +60,6 @@ const char* get_highscore_file();
|
||||||
" -s <size> Set the grid border length\n"\
|
" -s <size> Set the grid border length\n"\
|
||||||
" -b <rate> Set the block spawn rate\n"\
|
" -b <rate> Set the block spawn rate\n"\
|
||||||
" -c Enables color support (ncurses version only)\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
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user