Better end screen
This commit is contained in:
30
hangman.c
30
hangman.c
@@ -12,10 +12,12 @@ int main(void) {
|
||||
initCoordinates(gs);
|
||||
gs->allowedMoves = DEFAULTTRIES;
|
||||
gs->moves = 0;
|
||||
gs->guesses = 0;
|
||||
curs_set(0);
|
||||
|
||||
/* start game */
|
||||
initGuessWord(gs);
|
||||
noecho(); /* Don't show the player input */
|
||||
while (checkWin(gs)) {
|
||||
updateScreen(gs);
|
||||
playerInput(gs);
|
||||
@@ -31,7 +33,7 @@ void quitProgram(void) {
|
||||
|
||||
|
||||
void updateScreen(game_state *gs) {
|
||||
mvprintw(1, 1, "Remaining guesses: %i", (gs->allowedMoves - gs->moves));
|
||||
mvprintw(1, 1, "Remaining wrong guesses: %i", (gs->allowedMoves - gs->moves));
|
||||
mvprintw(gs->maxy - 1, 1, "Press ctrl + c to exit.");
|
||||
drawGuessWord(gs);
|
||||
mvprintw(2, 1, "Wrong characters: %s", gs->wrongCharacters);
|
||||
@@ -86,12 +88,14 @@ int playerInput(game_state *gs) {
|
||||
if (found) {
|
||||
/* found a valid character */
|
||||
if (fillCurrentWord(gs, inp)) {
|
||||
gs->moves++;
|
||||
//gs->moves++;
|
||||
gs->guesses++;
|
||||
}
|
||||
} else {
|
||||
/* no valid character found */
|
||||
if (stackWrongCharacter(gs, inp)) {
|
||||
gs->moves++;
|
||||
gs->guesses++;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -139,13 +143,21 @@ int checkWin(game_state *gs) {
|
||||
return 1;
|
||||
} else {
|
||||
/* game end: decide if game is won or lost */
|
||||
clear();
|
||||
if (gs->moves >= gs->allowedMoves) {
|
||||
mvprintw(gs->centery, gs->centerx, "Game lost.");
|
||||
} else {
|
||||
mvprintw(gs->centery, gs->centerx, "Game won!");
|
||||
}
|
||||
refresh();
|
||||
printGameStats(gs);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void printGameStats(game_state *gs) {
|
||||
clear();
|
||||
if (gs->moves >= gs->allowedMoves) {
|
||||
mvprintw(gs->centery, gs->centerx - 5, "Game lost.");
|
||||
} else {
|
||||
char message[100];
|
||||
sprintf(message, "Game won! Total guesses: %i", gs->guesses);
|
||||
mvprintw(gs->centery, gs->centerx - (strlen(message) / 2), message);
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
|
||||
|
@@ -3,8 +3,9 @@
|
||||
|
||||
/* Data structures */
|
||||
typedef struct {
|
||||
int moves;
|
||||
int moves; //Only wrong moves
|
||||
int allowedMoves;
|
||||
int guesses; //All guesses included valid moves
|
||||
int maxy;
|
||||
int maxx;
|
||||
int centery;
|
||||
@@ -31,3 +32,4 @@ int playerInput(game_state *gs);
|
||||
int fillCurrentWord(game_state *gs, char validchar);
|
||||
int stackWrongCharacter(game_state *gs, char wrongchar);
|
||||
int checkWin(game_state *gs);
|
||||
void printGameStats(game_state *gs);
|
||||
|
Reference in New Issue
Block a user