aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 57b8179f81385ceed2609b643954708a62dc0385 (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
#include "hanoi.h"

int main()
{
	initscr();
	noecho();
	cbreak();

	int width, height;
	getmaxyx(stdscr, height, width);
	(void)height;

	int win_width = width / 3;

	WINDOW	*wins[3];
	for (int i = 0; i < 3; i++)
	{
		refresh();
		if ((wins[i] = newwin(0, win_width, 0, i * win_width)) == NULL)
			abort();
		box(wins[i], 0, 0);
		/* wrefresh(wins[i]); */
	}

	t_tower	towers[3];
	towers_init(towers, 3);

	int from_selection = 0;
	int to_selection = 1;
	bool mode_from = true;
	bool running = true;
	while (running)
	{
		for (int i = 0; i < 3; i++)
		{
			int highlight_level = 0;
			if (i == from_selection)
				highlight_level = 1;
			tower_put(&towers[i], wins[i], highlight_level);
		}

		char c;
		c = getch();

		int selection = mode_from ? from_selection : to_selection;
		switch (c)
		{
			case 'q':
				running = false;
				break;
			case 'j':
				selection--;
				break;
			case 'k':
				selection++;
				break;
			case '\n':
				if (mode_from)
					from_selection = selection;
				else
					to_selection = selection;
				if (!mode_from)
					towers_move(towers, from_selection, to_selection);
				mode_from = !mode_from;
				break;
		}
		selection %= 3;
		mvprintw(0, 0, "%d", selection);
		refresh();
	}

	/* refresh(); */

	for (int i = 0; i < 3; i++)
		delwin(wins[3]);
	endwin();
	return 0;
}