hiscores. persistent (saves to file).
This commit is contained in:
parent
90df5c62a4
commit
ee2bf9042c
|
@ -52,6 +52,12 @@ int SZ;
|
||||||
int s;
|
int s;
|
||||||
int sl;
|
int sl;
|
||||||
|
|
||||||
|
/* highscore */
|
||||||
|
int hs;
|
||||||
|
|
||||||
|
/* highscore file */
|
||||||
|
char *file;
|
||||||
|
|
||||||
/* Merges adjacent squares of the same value together in a certain direction */
|
/* Merges adjacent squares of the same value together in a certain direction */
|
||||||
void merge(int d)
|
void merge(int d)
|
||||||
{
|
{
|
||||||
|
@ -147,6 +153,24 @@ void gravitate(int d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* load hiscore */
|
||||||
|
void load_score() {
|
||||||
|
FILE *fd = fopen(file, "r");
|
||||||
|
if (fd == NULL) fd = fopen(file, "w+");
|
||||||
|
if (fscanf(fd, "%d", &hs) == EOF) hs = 0;
|
||||||
|
fclose(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* saves hiscore, but only if playing on standard size grid */
|
||||||
|
void save_score() {
|
||||||
|
if (s > hs && SZ == 4) {
|
||||||
|
hs = s;
|
||||||
|
FILE *fd = fopen(file, "w+");
|
||||||
|
fprintf(fd, "%d", hs);
|
||||||
|
fclose(fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* returns if there are any available spaces left on the grid */
|
/* returns if there are any available spaces left on the grid */
|
||||||
int space_left()
|
int space_left()
|
||||||
{
|
{
|
||||||
|
@ -171,6 +195,7 @@ void rand_block()
|
||||||
endwin();
|
endwin();
|
||||||
printf("\n"
|
printf("\n"
|
||||||
"YOU LOSE! - Your score was %d\n", s);
|
"YOU LOSE! - Your score was %d\n", s);
|
||||||
|
save_score();
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,12 +213,13 @@ int flog2(int n)
|
||||||
/* colors just rotate around, works for now, can be confusing when you have some fairly high values on the board */
|
/* 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)
|
void draw_grid(WINDOW *gamewin)
|
||||||
{
|
{
|
||||||
// mvwprintw will sometimes have a useless arg
|
// mvwprintw will sometimes have a useless arg, this is warned, but doesn't affect the program
|
||||||
char *scr = sl ? "SCORE: %d (+%d)\n" : "SCORE: %d\n";
|
char *scr = sl ? "SCORE: %d (+%d)\n" : "SCORE: %d\n";
|
||||||
mvwprintw(gamewin, 0, 0, scr, s, sl);
|
mvwprintw(gamewin, 0, 0, scr, s, sl);
|
||||||
|
mvwprintw(gamewin, 1, 0, "HISCR: %d\n", hs);
|
||||||
|
|
||||||
ITER(SZ*(MAXVAL + 2) + 1, waddch(gamewin, '-'));
|
ITER(SZ*(MAXVAL + 2) + 1, waddch(gamewin, '-'));
|
||||||
int i, j, xps = 0, yps = 2;
|
int i, j, xps = 0, yps = 3;
|
||||||
for (i = 0; i < SZ; i++, xps = 0, yps++) {
|
for (i = 0; i < SZ; i++, xps = 0, yps++) {
|
||||||
mvwprintw(gamewin, yps, xps++, "|");
|
mvwprintw(gamewin, yps, xps++, "|");
|
||||||
for (j = 0; j < SZ; j++) {
|
for (j = 0; j < SZ; j++) {
|
||||||
|
@ -225,17 +251,20 @@ int main(int argc, char **argv)
|
||||||
curs_set(FALSE);
|
curs_set(FALSE);
|
||||||
|
|
||||||
/* init variables */
|
/* init variables */
|
||||||
|
file = ".hs2048g";
|
||||||
|
hs = 0;
|
||||||
s = 0;
|
s = 0;
|
||||||
sl = 0;
|
sl = 0;
|
||||||
SZ = 4;
|
SZ = 4;
|
||||||
MAXVAL = 4;
|
MAXVAL = 4;
|
||||||
CALLOC2D(g, SZ);
|
CALLOC2D(g, SZ);
|
||||||
|
|
||||||
|
load_score();
|
||||||
int n_blocks = 1;
|
int n_blocks = 1;
|
||||||
|
|
||||||
/* parse options */
|
/* parse options */
|
||||||
int c;
|
int c;
|
||||||
while ((c = getopt(argc, argv, "chs:b:")) != -1) {
|
while ((c = getopt(argc, argv, "rchs:b:")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
// color support - assumes your terminal can display colours
|
// color support - assumes your terminal can display colours
|
||||||
// should still work regardless
|
// should still work regardless
|
||||||
|
@ -248,18 +277,28 @@ int main(int argc, char **argv)
|
||||||
init_pair(5, 5, 0);
|
init_pair(5, 5, 0);
|
||||||
init_pair(6, 6, 0);
|
init_pair(6, 6, 0);
|
||||||
init_pair(7, 7, 0);
|
init_pair(7, 7, 0);
|
||||||
init_pair(8, 8, 0);
|
|
||||||
break;
|
break;
|
||||||
// different board sizes
|
// different board sizes
|
||||||
case 's':
|
case 's':
|
||||||
FREE2D(g, SZ);
|
FREE2D(g, SZ);
|
||||||
SZ = atoi(optarg);
|
int optint = atoi(optarg);
|
||||||
|
SZ = optint > 4 ? optint : 4;
|
||||||
CALLOC2D(g, SZ);
|
CALLOC2D(g, SZ);
|
||||||
break;
|
break;
|
||||||
// different block spawn rate
|
// different block spawn rate
|
||||||
case 'b':
|
case 'b':
|
||||||
n_blocks = atoi(optarg);
|
n_blocks = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
// reset hiscores
|
||||||
|
case 'r':
|
||||||
|
endwin();
|
||||||
|
printf("Are you sure you want to reset your highscores? (Y)es or (N)o\n");
|
||||||
|
int response;
|
||||||
|
if ((response = getchar()) == 'y' || response == 'Y') {
|
||||||
|
FILE *fd = fopen(file, "w+");
|
||||||
|
fclose(fd);
|
||||||
|
}
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
// help menu
|
// help menu
|
||||||
case 'h':
|
case 'h':
|
||||||
endwin();
|
endwin();
|
||||||
|
@ -279,7 +318,7 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
int width = SZ * (MAXVAL + 2) + 1;
|
int width = SZ * (MAXVAL + 2) + 1;
|
||||||
int height = SZ * (MAXVAL + 2) + 2;
|
int height = SZ * (MAXVAL + 2) + 3;
|
||||||
|
|
||||||
// might center in middle of screen
|
// might center in middle of screen
|
||||||
WINDOW *gamewin = newwin(height, width, 1, 1);
|
WINDOW *gamewin = newwin(height, width, 1, 1);
|
||||||
|
@ -324,6 +363,7 @@ int main(int argc, char **argv)
|
||||||
erase();
|
erase();
|
||||||
refresh();
|
refresh();
|
||||||
endwin();
|
endwin();
|
||||||
|
save_score();
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
default:
|
default:
|
||||||
goto retry;
|
goto retry;
|
||||||
|
|
|
@ -52,6 +52,12 @@ int SZ;
|
||||||
int s;
|
int s;
|
||||||
int sl;
|
int sl;
|
||||||
|
|
||||||
|
/* highscore */
|
||||||
|
int hs;
|
||||||
|
|
||||||
|
/* highscore file */
|
||||||
|
char *file;
|
||||||
|
|
||||||
/* Merges adjacent squares of the same value together in a certain direction */
|
/* Merges adjacent squares of the same value together in a certain direction */
|
||||||
void merge(int d)
|
void merge(int d)
|
||||||
{
|
{
|
||||||
|
@ -146,6 +152,24 @@ void gravitate(int d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* loads hiscore */
|
||||||
|
void load_score() {
|
||||||
|
FILE *fd = fopen(file, "r");
|
||||||
|
if (fd == NULL) fd = fopen(file, "w+");
|
||||||
|
if (fscanf(fd, "%d", &hs) == EOF) hs = 0;
|
||||||
|
fclose(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* saves hiscore, but only if playing on standard size grid */
|
||||||
|
void save_score() {
|
||||||
|
if (s > hs && SZ == 4) {
|
||||||
|
hs = s;
|
||||||
|
FILE *fd = fopen(file, "w+");
|
||||||
|
fprintf(fd, "%d", hs);
|
||||||
|
fclose(fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* returns if there are any available spaces left on the grid */
|
/* returns if there are any available spaces left on the grid */
|
||||||
int space_left()
|
int space_left()
|
||||||
{
|
{
|
||||||
|
@ -170,6 +194,7 @@ void rand_block()
|
||||||
printf("\n"
|
printf("\n"
|
||||||
"YOU LOSE\n"
|
"YOU LOSE\n"
|
||||||
"Your score was %d\n", s);
|
"Your score was %d\n", s);
|
||||||
|
save_score();
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -177,7 +202,8 @@ void rand_block()
|
||||||
/* draws the grid and fills it with the current values */
|
/* draws the grid and fills it with the current values */
|
||||||
void draw_grid()
|
void draw_grid()
|
||||||
{
|
{
|
||||||
printf("SCORE: %d ", s);
|
printf("HISCORE: %d |", hs);
|
||||||
|
printf("| SCORE: %d ", s);
|
||||||
if (sl) printf("(+%d)", sl);
|
if (sl) printf("(+%d)", sl);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
@ -210,28 +236,42 @@ void reset_term()
|
||||||
/* parses options and stores the main game loop */
|
/* parses options and stores the main game loop */
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
|
||||||
/* init variables */
|
/* init variables */
|
||||||
|
file = ".hs2048g";
|
||||||
|
hs = 0;
|
||||||
s = 0;
|
s = 0;
|
||||||
sl = 0;
|
sl = 0;
|
||||||
SZ = 4;
|
SZ = 4;
|
||||||
CALLOC2D(g, SZ);
|
CALLOC2D(g, SZ);
|
||||||
|
|
||||||
|
load_score();
|
||||||
int n_blocks = 1;
|
int n_blocks = 1;
|
||||||
|
|
||||||
/* parse options */
|
/* parse options */
|
||||||
int c;
|
int c;
|
||||||
while ((c = getopt(argc, argv, "hs:b:")) != -1) {
|
while ((c = getopt(argc, argv, "rhs:b:")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
// different board sizes
|
// different board sizes
|
||||||
case 's':
|
case 's':
|
||||||
FREE2D(g, SZ);
|
FREE2D(g, SZ);
|
||||||
SZ = atoi(optarg);
|
int optint = atoi(optarg);
|
||||||
|
SZ = optint > 4 ? optint : 4;
|
||||||
CALLOC2D(g, SZ);
|
CALLOC2D(g, SZ);
|
||||||
break;
|
break;
|
||||||
// different block spawn rate
|
// different block spawn rate
|
||||||
case 'b':
|
case 'b':
|
||||||
n_blocks = atoi(optarg);
|
n_blocks = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
// reset hiscores
|
||||||
|
case 'r':
|
||||||
|
printf("Are you sure you want to reset your highscores? (Y)es or (N)o\n");
|
||||||
|
int response;
|
||||||
|
if ((response = getchar()) == 'y' || response == 'Y') {
|
||||||
|
FILE *fd = fopen(file, "w+");
|
||||||
|
fclose(fd);
|
||||||
|
}
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
case 'h':
|
case 'h':
|
||||||
printf("Controls:\n"
|
printf("Controls:\n"
|
||||||
" hjkl, wasd Movement\n"
|
" hjkl, wasd Movement\n"
|
||||||
|
@ -289,7 +329,7 @@ int main(int argc, char **argv)
|
||||||
TURN(DU);
|
TURN(DU);
|
||||||
break;
|
break;
|
||||||
case 'q':
|
case 'q':
|
||||||
FREE2D(g, SZ);
|
save_score();
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
default:
|
default:
|
||||||
goto retry;
|
goto retry;
|
||||||
|
|
|
@ -5,7 +5,7 @@ A cli version of the game [2048](https://github.com/gabrielecirulli/2048) for yo
|
||||||
![Screenshot](http://i.imgur.com/fwZEvdh.png)
|
![Screenshot](http://i.imgur.com/fwZEvdh.png)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
If you want to use the ncurses version, make sure that you have the required ncurses library and link against this during compilation with `-lcurses`
|
If you want to use the ncurses version, make sure that you have the required ncurses library and link against this during compilation with `-lcurses`. Uses the filename `.hs2048g` so ensure that you have no file of this name otherwise it will be overwritten.
|
||||||
### Get
|
### Get
|
||||||
cd ~/$INSTALL_DIR
|
cd ~/$INSTALL_DIR
|
||||||
wget https://raw.githubusercontent.com/Tiehuis/2048-cli/master/2048_no_curses.c
|
wget https://raw.githubusercontent.com/Tiehuis/2048-cli/master/2048_no_curses.c
|
||||||
|
@ -16,6 +16,7 @@ If you want to use the ncurses version, make sure that you have the required ncu
|
||||||
## Options
|
## Options
|
||||||
-b <size> Set the grid border length
|
-b <size> Set the grid border length
|
||||||
-s <rate> Set the block spawn rate
|
-s <rate> Set the block spawn rate
|
||||||
|
-r Resets hiscore. Will prompt user
|
||||||
-c Enables color support (ncurses version only)
|
-c Enables color support (ncurses version only)
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
Loading…
Reference in New Issue
Block a user