главная|main page

состояние|status

блог|blog

файлы|files

программы|software

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Davydov <lotigara@lotigara.ru>2025-04-07 00:05:46 +0300
committerIvan Davydov <lotigara@lotigara.ru>2025-04-07 00:13:53 +0300
commit0e42930b25a69fd794fe625f63e3bc10d3ca76d4 (patch)
treee87e94adb7d54a21ec5d8d6de4c171bb50b1d654
parent25c4c8427f6c4ff1ad99141c6321b1395a8fb3fb (diff)
Major changes to code readability and balance
Use curses instead of ncurses Make game harder to play, see source code for changes Print score normally (not like in implementations of classic vi) Run free() right before the end the program (valgrind still complains) Remove unnecessary get_* and set_* functions and refer to structure members directly
-rw-r--r--Makefile2
-rw-r--r--clicker-ncurses.c52
-rw-r--r--clicker-ncurses.h6
3 files changed, 23 insertions, 37 deletions
diff --git a/Makefile b/Makefile
index 4945fc2..b049c03 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@
.SUFFIXES:
CC = cc
CFLAGS = -Wall
-LDLIBS = -lncurses
+LDLIBS = -lcurses
all: clicker-ncurses
clicker-ncurses: clicker-ncurses.o
diff --git a/clicker-ncurses.c b/clicker-ncurses.c
index 8decf8f..23d3a4d 100644
--- a/clicker-ncurses.c
+++ b/clicker-ncurses.c
@@ -3,7 +3,8 @@
#include <stdlib.h>
#include <unistd.h>
-#include <ncurses.h>
+/* TODO: check for compatibility with different implementations */
+#include <curses.h>
#include "clicker-ncurses.h"
@@ -40,7 +41,7 @@ main (void)
/* Initialize the structure and set the values */
struct game *cur_game = malloc(sizeof(struct game));
cur_game->click = 1;
- cur_game->multiplifier = 2;
+ cur_game->multiplifier = 128;
/* The main loop that also catches user input */
int ch = 0;
@@ -49,54 +50,45 @@ main (void)
switch (ch)
{
case ' ':
- set_score(cur_game, get_score(cur_game) + get_click(cur_game));
+ set_score(cur_game, cur_game->score + cur_game->click);
break;
case 'q':
goto endwin;
case 'u':
- if (get_score(cur_game) >= 2 * get_multiplifier(cur_game))
+ if (cur_game->score >= cur_game->click * cur_game->multiplifier)
{
- set_score(cur_game, get_score(cur_game) - get_click(cur_game) * 2);
- set_click(cur_game, get_click(cur_game) * get_multiplifier(cur_game));
+ 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);
}
-int
-get_score (struct game *game)
-{
- return game->score;
-}
-
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);
}
-
-int
-get_click (struct game *game)
-{
- return game->click;
-}
-
-void
-set_click (struct game *game, int click)
-{
- game->click = click;
-}
-
-int
-get_multiplifier (struct game *game)
-{
- return game->multiplifier;
-}
diff --git a/clicker-ncurses.h b/clicker-ncurses.h
index b7c97c2..cbaf969 100644
--- a/clicker-ncurses.h
+++ b/clicker-ncurses.h
@@ -5,10 +5,4 @@ struct game
int score;
};
-int get_score (struct game *game);
void set_score (struct game *game, int score);
-
-int get_click (struct game *game);
-void set_click (struct game *game, int click);
-
-int get_multiplifier (struct game *game);