2011-06-06 03:42:23 +00:00
|
|
|
#include <stdlib.h>
|
2011-02-12 15:56:31 +00:00
|
|
|
#include <ncurses.h>
|
2011-06-03 06:09:14 +00:00
|
|
|
#include <locale.h>
|
2011-06-08 04:02:05 +00:00
|
|
|
#include <getopt.h>
|
2011-06-08 14:51:22 +00:00
|
|
|
#include <errno.h>
|
2011-06-03 05:48:26 +00:00
|
|
|
|
2011-06-09 01:55:44 +00:00
|
|
|
#include "gui.h"
|
2011-02-06 23:28:11 +00:00
|
|
|
#include "game.h"
|
|
|
|
#include "keyboard.h"
|
2011-06-08 14:51:22 +00:00
|
|
|
#include "common.h"
|
2010-03-31 05:21:17 +00:00
|
|
|
|
2011-02-06 05:44:45 +00:00
|
|
|
const char *program_name;
|
2011-06-08 05:36:46 +00:00
|
|
|
struct game game;
|
2011-02-06 05:44:45 +00:00
|
|
|
|
2011-06-06 03:42:23 +00:00
|
|
|
void draw_greeting() {
|
|
|
|
mvprintw(8, 26, "Welcome to tty-solitaire.");
|
2011-06-06 06:15:44 +00:00
|
|
|
mvprintw(10, 23, "Move with the arrow keys or hjkl.");
|
2011-06-06 03:42:23 +00:00
|
|
|
mvprintw(11, 19, "Use the space bar to mark and move cards.");
|
|
|
|
mvprintw(12, 16, "After marking a card you can use m to increase ");
|
|
|
|
mvprintw(13, 17, "and n to decrease the number of marked cards.");
|
|
|
|
mvprintw(15, 19, "Press the space bar to play or q to quit.");
|
|
|
|
}
|
|
|
|
|
2011-06-08 04:02:05 +00:00
|
|
|
void usage() {
|
2011-06-08 15:01:29 +00:00
|
|
|
printf("usage: %s [-v|--version] [-h|--help] [-p|--passes=NUMBER]\n", program_name);
|
2011-06-08 04:02:05 +00:00
|
|
|
printf(" -v, --version Show the version\n");
|
|
|
|
printf(" -h, --help Show this message\n");
|
2011-06-08 15:01:29 +00:00
|
|
|
printf(" -p, --passes Number of passes through the deck\n");
|
2011-06-08 04:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void version() {
|
2011-06-08 14:51:22 +00:00
|
|
|
FILE *version_file;
|
2011-06-08 04:02:05 +00:00
|
|
|
char version_string[6];
|
2011-06-08 14:51:22 +00:00
|
|
|
|
|
|
|
if (!(version_file = fopen("VERSION", "rb"))) {
|
|
|
|
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
|
|
|
exit(errno);
|
|
|
|
}
|
2011-06-08 04:02:05 +00:00
|
|
|
fread(version_string, 1, 5, version_file);
|
|
|
|
version_string[5] = '\0';
|
|
|
|
printf("%s\n", version_string);
|
|
|
|
fclose(version_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int option;
|
|
|
|
int option_index;
|
2011-06-08 05:36:46 +00:00
|
|
|
int passes_through_deck = 3;
|
2011-06-08 04:02:05 +00:00
|
|
|
static const struct option options[] = {
|
2011-06-08 05:36:46 +00:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
{"passes", required_argument, NULL, 'p'}
|
2011-06-08 04:02:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
program_name = argv[0];
|
|
|
|
|
2011-06-08 05:36:46 +00:00
|
|
|
while ((option = getopt_long(argc, argv, "hvp:", options, &option_index)) != -1) {
|
2011-06-08 04:02:05 +00:00
|
|
|
switch (option) {
|
|
|
|
case 'v':
|
|
|
|
version();
|
|
|
|
exit(0);
|
2011-06-08 05:36:46 +00:00
|
|
|
case 'p':
|
|
|
|
passes_through_deck = atoi(optarg);
|
|
|
|
break;
|
2011-06-08 04:02:05 +00:00
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2010-04-13 04:07:00 +00:00
|
|
|
|
2011-06-08 14:51:22 +00:00
|
|
|
setlocale(LC_ALL, "");
|
2011-06-03 06:09:14 +00:00
|
|
|
initscr();
|
2011-06-08 14:51:22 +00:00
|
|
|
raw();
|
2011-06-03 06:09:14 +00:00
|
|
|
noecho();
|
2011-06-08 14:51:22 +00:00
|
|
|
keypad(stdscr, TRUE);
|
2011-06-04 20:15:12 +00:00
|
|
|
start_color();
|
2011-06-08 14:51:22 +00:00
|
|
|
curs_set(FALSE);
|
2011-06-03 06:09:14 +00:00
|
|
|
set_escdelay(0);
|
|
|
|
assume_default_colors(COLOR_WHITE, COLOR_GREEN);
|
|
|
|
init_pair(1, COLOR_BLACK, COLOR_WHITE);
|
|
|
|
init_pair(2, COLOR_RED, COLOR_WHITE);
|
|
|
|
init_pair(3, COLOR_WHITE, COLOR_BLUE);
|
|
|
|
init_pair(4, COLOR_WHITE, COLOR_GREEN);
|
|
|
|
|
2011-06-03 06:22:48 +00:00
|
|
|
draw_greeting();
|
2010-04-13 04:07:00 +00:00
|
|
|
|
2011-06-04 20:15:12 +00:00
|
|
|
int key;
|
|
|
|
do {
|
2011-02-06 02:32:21 +00:00
|
|
|
switch (key = getch()) {
|
2011-02-06 06:14:17 +00:00
|
|
|
case KEY_SPACEBAR:
|
2011-06-04 20:15:12 +00:00
|
|
|
clear();
|
|
|
|
refresh();
|
2011-06-08 05:36:46 +00:00
|
|
|
game_init(&game, passes_through_deck);
|
2011-02-06 06:14:17 +00:00
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
2011-06-03 06:09:14 +00:00
|
|
|
endwin();
|
|
|
|
return(0);
|
2010-04-13 04:07:00 +00:00
|
|
|
}
|
2011-06-04 20:15:12 +00:00
|
|
|
} while (key != KEY_SPACEBAR);
|
2010-04-13 04:07:00 +00:00
|
|
|
|
2011-06-04 20:15:12 +00:00
|
|
|
do {
|
2011-02-06 02:32:21 +00:00
|
|
|
if ((key = getch()) == 'q' || key == 'Q') {
|
2011-06-03 06:09:14 +00:00
|
|
|
endwin();
|
2011-06-04 20:15:12 +00:00
|
|
|
game_end();
|
2011-06-06 03:42:23 +00:00
|
|
|
exit(0);
|
2011-02-06 02:32:21 +00:00
|
|
|
} else {
|
2011-06-07 03:27:43 +00:00
|
|
|
keyboard_event(key);
|
2010-04-20 04:10:42 +00:00
|
|
|
}
|
2011-06-06 03:42:23 +00:00
|
|
|
} while (!game_won());
|
|
|
|
|
|
|
|
endwin();
|
|
|
|
game_end();
|
|
|
|
printf("You won.\n");
|
2010-04-03 03:32:19 +00:00
|
|
|
|
2010-04-12 05:56:28 +00:00
|
|
|
return(0);
|
2010-03-31 05:21:17 +00:00
|
|
|
}
|