Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Game Programming (http://www.go4expert.com/articles/game-programming/)
-   -   Hangman game source code (http://www.go4expert.com/articles/hangman-game-source-code-t1142/)

TEJUS 9Aug2006 13:52

Hangman game source code
 
Hangman game source code for you.
============
----HANGMAN----
============
Code: C

#include<conio.h>
#include<string.h>
main()
{
    char word[30],getit[30],*found;
    int score,loop,len,num,lup,om,luptimes,times,fitimes,ondtimes,antimes;
    luptimes=0;
    fitimes=0;
    ondtimes=0;
    clrscr();
    printf("\n ENTER THE WORD: ");
    gets(word);
    score=0;
    num=0;
    om=0;
   
    len=strlen(word);
    clrscr();
    char gname[70];
    printf("\n ENTER YOUR NAME: ");
    scanf("%s",gname);
    char h;
    printf("\n LET's START: ");
    h='_';
    printf("\n LOOK HERE: ");
    for(loop=0;loop!=len;loop++)
    {
        printf("\n %c ",h);
    }
    for(loop=0;loop!=len;loop++)
    {
        om=1;
        num=num+1;
        fitimes=0;
        ondtimes=0;
        printf("\n\n %d LETTER",num);
        scanf("%s",&getit[num]);
        found=strchr(word,getit[num]);
        if(found)
        {
            for(luptimes=0;antimes=1;luptimes!=len;luptimes++,antimes++)
            {
                if(getit[num]==word[luptimes])
                {
                    fitimes++;
                }
                else
                {
                    fitimes=fitimes;
                }
            }
            for(luptimes=num;luptimes!=0;luptimes--)
            {
                if(getit[num]==getit[luptimes])
                {
                    ondtimes++;
                }
                else
                {
                    ondtimes=ondtimes;
                }
            }
            if(fitimes>=ondtimes)
            {
                score=score+1;
                printf("\n OK\n");
                for(lup=0;getit[num]!=word[lup];lup++)
                {
                    om++;
                }
                printf("\n POSITION: %d\n",om);
                printf("\n ===================\n");
            }
            else
            {
                score=score;
                printf("\n NOT AGAIN!\n");
            }
        }
        else
        {
            score=score;
            printf("\n NO\n");
        }
    }
    printf("\n THE WORD IS '%s'\n"word);
    printf("\n YOUR SCORE IS %d OUT OF %d",score,len);
    getch();
    return 0;
}


TEJUS 3Nov2006 19:10

Re: Hangman game source code
 
Why No One Is Replying ?

shabbir 3Nov2006 19:22

Re: Hangman game source code
 
What you want in reply?

TEJUS 25Nov2006 11:53

Re: Hangman game source code
 
i want to know wheter my program is good?

shabbir 25Nov2006 14:39

Re: Hangman game source code
 
Quote:

Originally Posted by TEJUS
i want to know wheter my program is good?

Yup its a good one I must say.

Not more people are replying or commenting probably because of lack of some explanation / introductory para.

shailender0435 1Dec2006 23:21

Re: Hangman game source code
 
hey iam getting an error
undefined symbol_main in module c).ASM

rai_gandalf 4Dec2006 00:24

9 Errors and 5 Warnings!! Whats this?!
 
Quote:

Originally Posted by shabbir
Yup its a good one I must say.

Not more people are replying or commenting probably because of lack of some explanation / introductory para.


How come u n Shabbir r sayin that its workin .... in my Borland Compiler its showing 9 Errors & 5 Warnings :eek: .... some of the errors dont even require a compiler - they r glaring sytax errors .... or mebbe I am wrong - mebbe I am using the wrong compiler .... pls do clarify and elaborate in this regard. Thx.

Ciao,
Rajiv :)

shabbir 4Dec2006 10:42

Re: Hangman game source code
 
What I meant good is from code point of view and I never compiled it.

Yup it has some syntax errors and here is the rectification
Code:

#include<conio.h>
#include<string.h>
#include <stdio.h>

main()
{
    char word[30],getit[30],*found;
    int score,loop,len,num,lup,om,luptimes,fitimes,ondtimes,antimes;
    luptimes=0;
    fitimes=0;
    ondtimes=0;
    printf("\n ENTER THE WORD: ");
    gets(word);
    score=0;
    num=0;
    om=0;
   
    len=strlen(word);
    char gname[70];
    printf("\n ENTER YOUR NAME: ");
    scanf("%s",gname);
    char h;
    printf("\n LET's START: ");
    h='_';
    printf("\n LOOK HERE: ");
    for(loop=0;loop!=len;loop++)
    {
        printf("\n %c ",h);
    }
    for(loop=0;loop!=len;loop++)
    {
        om=1;
        num=num+1;
        fitimes=0;
        ondtimes=0;
        printf("\n\n %d LETTER",num);
        scanf("%s",&getit[num]);
        found=strchr(word,getit[num]);
        if(found)
        {
            for(luptimes=0,antimes=1;luptimes!=len;luptimes++,antimes++)
            {
                if(getit[num]==word[luptimes])
                {
                    fitimes++;
                }
                else
                {
                    fitimes=fitimes;
                }
            }
            for(luptimes=num;luptimes!=0;luptimes--)
            {
                if(getit[num]==getit[luptimes])
                {
                    ondtimes++;
                }
                else
                {
                    ondtimes=ondtimes;
                }
            }
            if(fitimes>=ondtimes)
            {
                score=score+1;
                printf("\n OK\n");
                for(lup=0;getit[num]!=word[lup];lup++)
                {
                    om++;
                }
                printf("\n POSITION: %d\n",om);
                printf("\n ===================\n");
            }
            else
            {
                score=score;
                printf("\n NOT AGAIN!\n");
            }
        }
        else
        {
            score=score;
            printf("\n NO\n");
        }
    }
    printf("\n THE WORD IS '%s'\n",word);
    printf("\n YOUR SCORE IS %d OUT OF %d",score,len);
    getch();
    return 0;
}


rai_gandalf 4Dec2006 10:58

3 Errors still remain!
 
There is still an error - the spacing between "ondtime" and "s". Rectify that and the code compiles without errors but with 4 warnings - all of "variable assigned a value but never used" .... But it does work in the way I think it should.

Ciao,
Rajiv

shabbir 4Dec2006 11:20

Re: Hangman game source code
 
The space problem is actually the problem in the algo of parsing the code and not with the actual code.

If you have a line greater than n it will insert a space in between.

Normally that thing does not happen and in normal circumstances you dont have chars after chars without a space.

rai_gandalf 4Dec2006 18:29

Re: Hangman game source code
 
oh! didnt know that .... alright then, that settles it.

Ciao,
Rajiv

Peter_APIIT 12Apr2007 12:57

Re: Hangman game source code
 
Hello everyone, what is hangman? Sorry for my sutpidness.

Thanks for oyur explanations.

h4rapture 27Nov2007 01:36

Re: Hangman game source code
 
Hi I am doing a research project on a hangman program for C++ and i stumbled across this, I was wondering if someone could explain the different variables to me. I did not fully understand this. I am still in the entry levels of programming and I need to learn what variable does what for my class.

int score,loop,len,num,lup,om,luptimes,fitimes,ondtime s,antimes;

thanks :)

wolverine 28Nov2007 19:22

Re: Hangman game source code
 
Hello i am Knew to the forum and I am trying to write a hangman game for 2 players. I want to begin with a menu of 3 choices. New game, high scores and exit. If the player press the first, the game will start and ask for the “Word” .After that will scan the word. It will ask for how many wrong letters can the other player write. As a default will have 7. Every time it will ask for given word or letter. With an algorithm will compute the high scores.It Will store the names and the scores to a file if the player choose to see the high scores with the second option . Can anyone help me how to do that.
Thanks!

oogabooga 14Jan2008 03:13

Re: Hangman game source code
 
Quote:

Originally Posted by TEJUS
i want to know wheter my program is good?

For a beginner, any program that runs correctly is great.
But to learn proper style you need to study how good programs are written.
Good style consists of such things as:
* dividing a program up into subroutines (functions in C)
* not using more variables than you absolutely have to
* giving your variables informative names so readers don't have to figure out what they're for.
* comments are also useful, especially for non-trivial things (don't just say what a simple line of code does in English, that's just for textbook examples).

Also, some ascii graphics couldn't hurt!
Code:

  ______
  |    |
  |    O
  |  /-\
  |    |
  |  /-\


skp819 7Jan2009 10:51

Re: Hangman game source code
 
you should explain some thing about your program.
you should use some comment to explain you program working.

Erol 23Jun2009 21:31

Re: Hangman game source code
 
Hi guys ,I have just done my C# hangman program.If somebody needs that send me an e-mail.Thanks Erolaktekin@hotmail.com

shabbir 24Jun2009 09:00

Re: Hangman game source code
 
Quote:

Originally Posted by Erol (Post 50344)
Hi guys ,I have just done my C# hangman program.If somebody needs that send me an e-mail.Thanks Erolaktekin@hotmail.com

If you really want to share the code why not post them here in the forum for others to see it.

akanglil 27Jun2009 02:25

Re: Hangman game source code
 
where's a code?

shabbir 27Jun2009 08:05

Re: Hangman game source code
 
Quote:

Originally Posted by akanglil (Post 50555)
where's a code?

Right at the top of this thread. - Hangman game source code

shipra123 26Oct2009 18:44

Re: Hangman game source code
 
I will try to play it and will surely share the feedback.

Erum Malik 16May2010 18:49

Re: Hangman game source code
 
i really think that this is the most valuable site i have ever had. itx very easy to find the stuff of my interest. gota gona be the best progrmmer in the world. Inshallah. :)

seangtz 18May2010 09:39

Re: Hangman game source code
 
Hey, I'm getting error in spacing.....plz help

unni krishnan.r 28May2010 17:08

Re: Hangman game source code
 
Still worries

patrick198 25Jan2011 18:22

Re: Hangman game source code
 
Quote:

Originally Posted by shabbir (Post 6262)
The space problem is actually the problem in the algo of parsing the code and not with the actual code free online movies

If you have a line greater than n it will insert a space in between.

Normally that thing does not happen and in normal circumstances you dont have chars after chars without a space.

Great post! It's very nice. Thank you so much for your post.

rvinnparker 4Sep2011 07:06

Re: Hangman game source code
 
hi im just a beginner and i tried the codes. the codes are working but i dont understand some part of it. can someone explain it to me i really need to understand the flow of this code. thank you very much

Syperus 4Sep2011 08:21

Re: Hangman game source code
 
Quote:

Originally Posted by rvinnparker (Post 86603)
hi im just a beginner and i tried the codes. the codes are working but i dont understand some part of it. can someone explain it to me i really need to understand the flow of this code. thank you very much

Which part are you not understanding? When you say flow are you talking about the flow of the program in general, or the loops. The general flow of the program is pretty self explanatory if you know the basics of programming. For loops, the statements within the loop are executed until the condition is met.

rvinnparker 5Sep2011 19:10

Re: Hangman game source code
 
Quote:

Originally Posted by Syperus (Post 86605)
Which part are you not understanding? When you say flow are you talking about the flow of the program in general, or the loops. The general flow of the program is pretty self explanatory if you know the basics of programming. For loops, the statements within the loop are executed until the condition is met.

thank you for the reply sir. the one that i dont undertsand is the declaration of the variables (int score,loop,len,num,lup,om,luptimes,times,fitimes,o ndtimes,antimes).

rvinnparker 5Sep2011 19:12

Re: Hangman game source code
 
Quote:

Originally Posted by Syperus (Post 86605)
Which part are you not understanding? When you say flow are you talking about the flow of the program in general, or the loops. The general flow of the program is pretty self explanatory if you know the basics of programming. For loops, the statements within the loop are executed until the condition is met.

thanks for the reply sir. im just a 4th year HS so its hard for me to understand it. can you pls explain to me whats the meaning of the int score,loop,len,num,lup,om,luptimes,times,fitimes,o ndtimes,antimes.

Syperus 5Sep2011 23:26

Re: Hangman game source code
 
Quote:

Originally Posted by rvinnparker (Post 86639)
thanks for the reply sir. im just a 4th year HS so its hard for me to understand it. can you pls explain to me whats the meaning of the int score,loop,len,num,lup,om,luptimes,times,fitimes,o ndtimes,antimes.

That line is defining several variables as integers. It's easier to declare variables like this when your going to have many variables with the same data type instead of having to do:
Code:

int score;
int loop;
int len;
int num;
int lup;
int om;
int luptimes;
int times;
int fitimes;
int ondtimes;
int antimes;


rswest 4Mar2013 20:07

Re: Hangman game source code
 
hello. this is my first post here. thank you for the code. now i understand how the game actually works. but i have a question. instead of typing the word to guess how can i use files r text files as source for the words to be guessed? im taking up programming as a minor subject in engineering and i really am finding it to be fun. i also have an assembly subject which is driving me nuts. so far i am finding C to be easier :D can anyone help me? thanks

Syperus 7Mar2013 10:05

Re: Hangman game source code
 
Quote:

Originally Posted by rswest (Post 99140)
hello. this is my first post here. thank you for the code. now i understand how the game actually works. but i have a question. instead of typing the word to guess how can i use files r text files as source for the words to be guessed? im taking up programming as a minor subject in engineering and i really am finding it to be fun. i also have an assembly subject which is driving me nuts. so far i am finding C to be easier :D can anyone help me? thanks

Use Regex. Have all the words in a .txt file and fstream to read them and use regex to split it up into chars.


All times are GMT +5.5. The time now is 01:41.