Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- const int MAX_GUESS = 6;
- const char ABORT_CH = '0';
- const int NO=30;
- const int GAME_OVER=11;
- /*Utilities*/
- void print_wrong(int count);
- void clear_stdin();
- int game_outcome_print(char *word,char *gomd,int count,const int MAX_GUESS);
- /* IO */
- int play_again();
- char fel1[]="\n ----------";
- char fel2[]=" |";
- char fel3[]=" O";
- char fel4[]=" /|\\" ;
- char fel5[]=" |";
- char fel6[]=" / \\";
- int main()
- {
- while(1)
- {
- printf( "\n\nWelcome to Hangman 2015\n");
- printf( "You have six guesses (cancel with 0)\n");
- int i=0;
- int c=0;
- int r=0;
- int end=0;
- int count=0;
- char ch[2]="";
- char word[20] = "hejsah"; // ersätts med open file
- char gomd[20] = "truban";
- printf("The word has %i letters\n", strlen(word)); //
- strcpy(gomd, word); //
- int lol=strlen(word); // TYP GET_WORD!!!!
- for(i=0;lol > i;i++)
- { //
- gomd[i] = '_'; //
- } //
- while(1){
- printf("\nMake a guess: ");
- scanf("%c", ch);
- if (*ch == ABORT_CH)
- {
- return 0;
- }
- while (c<=strlen(word))
- {
- if (word[c] == *ch)
- {
- gomd[c] = *ch;
- r++;
- }
- if (c==strlen(word)&&r==0)
- { //När hela ordet har gåtts igenom och fel hittas
- printf("\n[Unlucky, you guessed wrong] \n");
- c=0;
- count++;
- print_wrong(count);
- fflush(stdin);
- break;
- }
- else if (c==strlen(word)&&r>=0)
- { //När hela ordet har gått igenom och du gissat 'minst 1 rätt'
- print_wrong(count);
- c=0;
- r=0;
- fflush(stdin);
- break;
- }
- c++;
- }
- for (i=0; i<= strlen(word); i++)
- {
- printf("%c ", gomd[i]);
- }
- end=game_outcome_print(word,gomd,count,MAX_GUESS);
- if (end==GAME_OVER)
- {
- break;
- }
- }
- if (play_again()==NO)
- {
- return 0;
- }
- clear_stdin();
- }
- }
- int game_outcome_print(char *word,char *gomd,int count,const int MAX_GUESS)
- {
- if( strcmp(word,gomd) == 0 ) //Winning conditions!!!!
- {
- printf("\nCongratulations you won\n");
- return GAME_OVER;
- }
- if (count==MAX_GUESS)
- {
- printf("\nYou lost the game!\n");
- return GAME_OVER;
- }
- return 0;
- }
- int play_again()
- {
- char ch;
- printf ("Do you want to play again (y/n)?): ");
- scanf("%c", &ch);
- if( ch == 'n' || ch == 'N' )
- {
- return NO;
- }
- return 0;
- }
- void clear_stdin()
- {
- fflush (stdin);
- }
- void print_wrong(int count){
- switch (count){
- case 1:
- printf("%s\n", fel1);
- break;
- case 2:
- printf("%s\n%s\n", fel1, fel2);
- break;
- case 3:
- printf("%s\n%s\n%s\n", fel1, fel2, fel3);
- break;
- case 4:
- printf("%s\n%s\n%s\n%s\n", fel1, fel2, fel3, fel4);
- break;
- case 5:
- printf("%s\n%s\n%s\n%s\n%s\n", fel1, fel2, fel3, fel4, fel5);
- break;
- case 6:
- printf("%s\n%s\n%s\n%s\n%s\n%s\n", fel1, fel2, fel3, fel4, fel5, fel6);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement