главная|main page

состояние|status

блог|blog

файлы|files

программы|software

summaryrefslogtreecommitdiff
path: root/clicker-ncurses.c
blob: 8decf8f250f948217eb4485d83b16dfba15b28b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include <ncurses.h>

#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 = 2;

	/* The main loop that also catches user input */
	int ch = 0;
	while (ch = getch())
	{
		switch (ch)
		{
			case ' ':
				set_score(cur_game, get_score(cur_game) + get_click(cur_game));
				break;
			case 'q':
				goto endwin;
			case 'u':
				if (get_score(cur_game) >= 2 * get_multiplifier(cur_game))
				{
					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));
				}
				break;
		}
	}

	/* Remove the main window and exit */
	endwin: endwin();

	printf("You have got %d points.\n", cur_game->score);
	exit(EXIT_SUCCESS);
}

int
get_score (struct game *game)
{
	return game->score;
}

void
set_score (struct game *game, int score)
{
	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;
}