/*----------------------------------------------------------------------------*\ * lm - http://www.incubus.co.uk * * Hangman - Curses Based (Linux). * * Updated 27/11/2007 - Improved code and style. * cc hangman.c -lncurses -Wall -o hangman ; ./hangman \*----------------------------------------------------------------------------*/ /****************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ******************************************************************************/ /*----------------------------------------------------------------------------*\ * System includes. \*----------------------------------------------------------------------------*/ #include #include #include #include #include /*----------------------------------------------------------------------------*\ * \*----------------------------------------------------------------------------*/ #define MAXLEN 20 #define MAX 10 #define YES 1 #define NO 0 #define STARS "********************" /*----------------------------------------------------------------------------*\ * \*----------------------------------------------------------------------------*/ int main (void) { char word [MAXLEN + 1] = ""; char solution [MAXLEN + 1] = ""; char ch = 0; char *w_ptr = NULL; char *s_ptr = NULL; int num_guess = 0; int len = 0; int found = 0; initscr(); cbreak(); echo(); clear(); printw ("\n**** Welcome to @incubus Hangman ****"); refresh (); do { num_guess = 0; printw ("\nEnter the word for the player to guess > "); refresh (); getnstr (word, MAXLEN); clear (); for ( w_ptr = word ; *w_ptr ; w_ptr++) *w_ptr = toupper(*w_ptr); len = strlen(word); strncpy (solution, STARS, len); solution[len] = '\0'; while ( num_guess < MAX ) { printw ("\n\nWord so far is %s, Guesses so far %d", solution ,num_guess++); printw ("\nEnter a letter > "); refresh (); ch = toupper(getch()); s_ptr = solution; w_ptr = word; found = NO; while ( *w_ptr ) { if ( ch == *w_ptr ) { *s_ptr = *w_ptr; found = YES; } w_ptr++; s_ptr++; } if ( strcmp(solution , word) == 0 ) break; if ( found ) { printw ("\nThe letter \"%c\" was in the answer",ch); refresh (); } } if ( num_guess < MAX ) printw("\nYou got it! The word was %s.\nIt took you %d tries \n",word , num_guess); else printw("\nYou had %d tries, the word was %s\n", MAX ,word); printw ("\nPlay again [Y/N] - "); refresh (); }while( (toupper(getch())) == 'Y'); printw ("\nGoodbye\n"); refresh(); endwin (); return EXIT_SUCCESS; }
© Copyright 1999-2008 @incubus. All Rights Reserved. All trademarks acknowledged. incubus.co.uk || zenithpaints.co.uk || rankinstine.co.uk