Added getopt; added help screen #4

This commit is contained in:
2017-05-02 16:50:43 +02:00
parent 6df0af4c2f
commit 83ffee4315
2 changed files with 81 additions and 15 deletions

View File

@@ -3,8 +3,23 @@
#include <stdlib.h> //atexit
#include <string.h>
#include <ctype.h> //tolower
#include <getopt.h>
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 */
}