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