#include #include #include #include /* TODO: check for compatibility with different implementations */ #include #include "clicker-ncurses.h" int main (void) { /* Initialize NCurses screen and get the default window, stdscr */ if ( ! initscr() ) { printf("Too little amount of memory\n"); return 1; } /* This fixes the problem of input that shows up */ noecho(); /* Place a border with default settings (0 means default) */ border(0, 0, 0, 0, 0, 0, 0, 0); /* Get window size */ int sy, sx; getmaxyx(stdscr, sy, sx); /* Place a string onto the window in specified location */ char *name = "NCurses clicker"; mvprintw(0, sx / 2 - strlen(name) / 2, name); char *help = "[ ] - click, [q]uit, [u]pgrade"; mvprintw(sy - 2, sx / 2 - strlen(help) / 2, help); /* Refresh our window to include settings above */ refresh(); /* Initialize the structure and set the values */ struct game *cur_game = malloc(sizeof(struct game)); cur_game->click = 1; cur_game->multiplifier = 128; /* The main loop that also catches user input */ int ch = 0; while (ch = getch()) { switch (ch) { case ' ': set_score(cur_game, cur_game->score + cur_game->click); break; case 'q': goto endwin; case 'u': if (cur_game->score >= cur_game->click * cur_game->multiplifier) { set_score(cur_game, 0); ++cur_game->click; ++cur_game->multiplifier; } break; } if (cur_game->score < 0) { set_score(cur_game, 0); } } /* Remove the main window and exit */ endwin: endwin(); printf("You have got %d points.\n", cur_game->score); /* TODO: deal with valgrind complaints */ free(cur_game); exit(EXIT_SUCCESS); } void set_score (struct game *game, int score) { int score_len = snprintf(NULL, 0, "%d", game->score); for (int i = 0; i <= score_len; ++i) { mvaddch(3, i + 1, ' '); refresh(); } game->score = score; mvprintw(3, 1, "%d", game->score); }