Thread: simple hangman game

  1. #1
    Unregistered
    Guest

    Question simple hangman game

    i am trying to write a code for a sinple hangman game with just one word... mouse, without using strings. the pln is the word is a array then will have a second array to hold 5 -'s, then user will be asked to input characters and if they in the word will be placed in right place else they will be asked again and again unless the user inoputs * to stop. When the word is discovered the amount of tries will be displayed.
    I dont want to use any global data oir strings..just functions and arrays
    so far i have come up with the following code to recognise the correct characters..but there seems to be some faults with it so can anyone help me out with what that may be?
    also how would i go about making the correct letter go to the correct space? and count the amount of tries.
    Thank you for your help!

  2. #2
    Unregistered
    Guest
    opps forgot the code so far


    #include <stdlib.h>
    #define letters 5



    int main(void)

    {
    char word[] = {m, o, u, s, e};
    char letter;

    printf("choose a letter: ")
    scanf("%c", &letter);

    location= search(word, letters, letter);
    if(location >-1)
    printf("%c was found\n", word);
    else
    printf("input another character:");


    return 0;
    }

  3. #3
    Unregistered
    Guest

    Post

    Wow, your program needs help. I cleaned up what I could, but you still need to write the "int search()" function. If you need help with that, post again and somebody will probably help.

    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <stdlib.h> 
    
    
    #define letters 5 
    
    int search(char word, int letterNum, char letter)
    {
    	//YOUR SEARCH CODE HERE!
    	//example to return positive for letter #3 so the program will work:
    	return 3;
    }
    
    
    int main(void) 
    
    { 
    	//YOU NEEDED SINGLE QUOTES AROUND THE LETTERS
    char word[letters] = {'m', 'o', 'u', 's', 'e'}; 
    char letter; 
    int location; //YOU NEED TO INITIALIZE location AS AN INT
    int tries = 0; //THIS WILL COUNT YOUR TRIES
    
    
    printf("choose a letter: ");
    //USE A WHILE LOOP TO CONTINUTUE AND COUNT NUMBER OF TRIES:
    while( tries < 6)
    {
    // YOU WERE MISSING A ; HERE, ALSO ADDED A SPACE BEFORE %c
    	scanf(" %c", &letter); 
    
    //YOU NEED TO WRITE THE FUNCION int search() SOMEWHERE AS WELL
    	location = search(*word, letters, letter);
    	if(letter == '*')
    		break;
    	else if(location >-1) 
    		//YOU NEEDED TO CHANGE word TO letter AT THE END OF THIS printf()
    		printf("%c was found\ninput another character: ", letter); 
    	else if(letter == '*')
    		break;
    	else 
    	{
    		printf("inp98989898ut another character:"); 
    		++tries;//THIS WILL COUNT THE NUMBER OF WRONG TRIES
    		//MOVE THIS OUTSIDE THE ELSE-IF TO MAKE IT TOTAL TRIES
    	}
    }
    
    return 0; 
    }


    good luck!

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    This should work [badly]:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int lowerCase(char *word)
    {
     while(*word)
     {
      *word = tolower(*word);
      *word++;
     }
     return 0;
    }
    
    int search(char *word, char letter)
    {
     int l = tolower(letter);
     lowerCase(word);
     int i = 0;
    
     while(word[i])
     {
      if(l == word[i])
      {
       return i;
      }
      i++;
     }
     return -1;
    }
    
    
    int main(void)
    {
     char word[] = "Mouse";
     char letter;
     int location;
     int complete = 0;
     int lettersfound = 0;
     while(complete == 0)
     {
      printf("\nInput a letter or 5 to quit: ");
      scanf("%c", &letter);
      if(letter == 5)
      {
    	  return 0;
      }
      location= search(word, letter);
      if(location != -1)
      {
       printf("%c was found", word[location]);
       lettersfound++;
      }
      else
      {
      }
      if(lettersfound == strlen(word))
      {
       printf("You have found the word, %s!",word);
       complete = 1;
      }
     }
     return 0;
    }

  5. #5
    Unregistered
    Guest
    i have worked on the code a little more, but the code i come up with keeps getting a 'parse error' when i try to compile on line 11, i have tried over and over to figure out what it is but having no luck, so i am unable to see if the program works. Can anyone help?

    #include <stdio.h>
    #define letters 5

    int search(char word, int letternum, char letter);


    int main(void)

    {
    char word[letters] = {'m','o', 'u', 's', 'e'};
    char letter;
    int location;
    int tries= 0;
    {
    printf("choose a letter: ");
    while(tries<6)
    scanf("%c", &letter);
    }
    location= search(word, letters, letter);
    {
    if (location >-1)
    printf("%c was found\n input another letter" , letter);
    else
    printf("input another character:");
    }
    ++tries;
    return 0;
    }

    int search(const int list[], int letternum, char letter)
    { int i= 0, found;
    found = 0;
    while ( i<letternum && !found)
    { if (list[i] ==letter)
    found= 1;
    else i++;
    }
    if (found) return i ;
    else return -1;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman Game Troubles
    By emerica240 in forum C Programming
    Replies: 9
    Last Post: 11-26-2008, 01:39 AM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM
  4. Whats a very simple but good game i can try to make?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2001, 09:24 PM
  5. Replies: 1
    Last Post: 11-06-2001, 02:15 PM