From 83ffee431513ff1957888e245e22acadb12542b1 Mon Sep 17 00:00:00 2001 From: structix Date: Tue, 2 May 2017 16:50:43 +0200 Subject: [PATCH] Added getopt; added help screen #4 --- hangman.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++--------- hangman.h | 5 ++- 2 files changed, 81 insertions(+), 15 deletions(-) diff --git a/hangman.c b/hangman.c index 78c88ea..570f9fe 100644 --- a/hangman.c +++ b/hangman.c @@ -3,8 +3,23 @@ #include //atexit #include #include //tolower +#include -int main(void) { + +int main(int argc, char **argv) { + + const char *short_options = "w:hf:c"; + struct option long_options[] = { + {"word", required_argument, NULL, 'w'}, + {"help", no_argument, NULL, 'h'}, + {"file", required_argument, NULL, 'f'}, + {"credits", no_argument, NULL, 'c'} + }; + int c, startscr = 1; /* Show startscreen by default */ + char *filename; + + //while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 ) + /* Initialization */ initscr(); atexit(quitProgram); @@ -14,23 +29,58 @@ int main(void) { gs->allowedMoves = DEFAULTTRIES; gs->moves = 0; gs->guesses = 0; - - /* Show the start screen */ - curs_set(0); - showStartScreen(gs); + + curs_set(0); + while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 ) { + switch (c) { + case 'w': + sprintf(gs->guessWord, "%s", optarg); + startscr = 0; + break; + case 'h': + showHelp(gs); + exit(0); + break; + case 'f': + /* Set filename */ + filename = optarg; + startscr = 0; + break; + case 'c': + + break; + default: + + break; + } + } + + if (startscr) { + showStartScreen(gs); + } + /* start game */ - initGuessWord(gs); + initGuessWord(gs, filename); + + startGame(gs); + + + getch(); + free(gs); + return 0; +} + +void startGame(game_state *gs) { noecho(); /* Don't show the player input */ while (checkWin(gs)) { updateScreen(gs); playerInput(gs); } - getch(); - free(gs); - return 0; + } + void quitProgram(void) { endwin(); } @@ -58,12 +108,14 @@ void initCoordinates(game_state *gs) { gs->centerx = gs->maxx / 2; } -void initGuessWord(game_state *gs) { +void initGuessWord(game_state *gs, char *filename) { int i; mvprintw(1, 1, "Please enter your word: "); - curs_set(1); - getstr(gs->guessWord); - curs_set(0); + if (strlen(gs->guessWord) == 0) { /* Word can be set by arguments */ + curs_set(1); + getstr(gs->guessWord); + curs_set(0); + } toLowerCase(gs->guessWord); gs->wordLength = strlen(gs->guessWord); for (i = 0; i < gs->wordLength; i++) { @@ -187,4 +239,15 @@ void printGameStats(game_state *gs) { refresh(); } - +void showHelp(game_state *gs) { + char *wordstring = "-w or --word: Enter the word or sentence as an argument"; + char *helpstring = "-h or --help: Show this page"; + mvprintw(gs->centery, centerDiff(gs->centerx, wordstring), wordstring); + mvprintw(gs->centery + 1, centerDiff(gs->centerx, helpstring), helpstring); + getch(); +} + +int centerDiff(int coordinate, char *str) { + int len = strlen(str); + return coordinate - (len / 2); /* Integer division */ +} diff --git a/hangman.h b/hangman.h index a03e66a..6dc236d 100644 --- a/hangman.h +++ b/hangman.h @@ -26,7 +26,7 @@ typedef struct word { void quitProgram(void); void updateScreen(game_state *gs); void initCoordinates(game_state *gs); -void initGuessWord(game_state *gs); +void initGuessWord(game_state *gs, char *filename); void drawGuessWord(game_state *gs); int playerInput(game_state *gs); int fillCurrentWord(game_state *gs, char validchar); @@ -35,3 +35,6 @@ int checkWin(game_state *gs); void printGameStats(game_state *gs); void toLowerCase(char *str); void showStartScreen(game_state *gs); +void startGame(game_state *gs); +void showHelp(game_state *gs); +int centerDiff(int coordinate, char *str);