0% found this document useful (0 votes)
23 views29 pages

Johhn

The document describes the methodology for developing a Hangman game using C programming language. It involves setting up data structures to store words and game state, randomly selecting a word, displaying the initial interface, handling user input for guesses, updating the display, and tracking game progress and outcomes.

Uploaded by

mpraveen22h
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views29 pages

Johhn

The document describes the methodology for developing a Hangman game using C programming language. It involves setting up data structures to store words and game state, randomly selecting a word, displaying the initial interface, handling user input for guesses, updating the display, and tracking game progress and outcomes.

Uploaded by

mpraveen22h
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Sri Ramakrishna Institute of Technology

Coimbatore - 641010
December-2023

HANG MAN GAME

PROGRAM USING C LANGUAGE

By

NAME:JESHO JOHN S
REGISTER NO:71382304021
First Year B.E (ECE)

i|Pa g e
APPROVAL AND DECLARATION

This project report titled HANG MAN GAME PROGRAM USING C LANGUAGE was

prepared andsubmitted by JESHO JOHN (71382304021) and has been found satisfactory in terms of scope,

quality and done the presentation as partial fulfillment of the requirement for the Bachelor of engineering

(electronics and communication engineering) in Sri Ramakrishna Institute of Technology, Coimbatore

(SRIT).

Checked and Approved by

Ms. A.Jayasmruthi M.E.,

Project Supervisor

Assistant Professor

Department of Science and Humanities Sri Ramakrishna Institute of Technology,

Coimbatore-10 december 2023

2|P ag e
Table of contents Pg.no

APPROVAL AND DECLARATION SHEET 2

TABLE OF CONTENTS 3

ABSTRACT 4

LIST OF FIGURES 5

CHAPTER 1 INTRODUCTION 6

8
CHAPTER 2 METHODOLOGY

2.1 EXISTING TECHNIQUE 10

2.2 SOFTWARE REQUIREMENTS 11

2.3 PROPOSED METHOD 13

24
CHAPTER 3 EXPECTED RESULTS AND OUTPUT
25
CHAPTER 4 CONCLUSION

3|P ag e
HANG MAN GAME USING C LANGUAGE

ABSTRACT

The Hangman game represents a classic word-guessing game that has entertained and challenged

players for generations. In this traditional pen-and-paper game, or its digital adaptations, players

attempt to guess a hidden word by suggesting letters within a specified number of attempts while

avoiding the drawing of acomplete hangman figure.

The digital version of Hangman is a popular and engaging application that brings this timeless game

to life in a digital format. Developed with user-friendly interfaces and interactive elements, the

Hangman game fosters entertainment and cognitive engagement among players of all ages.

The essence of the Hangman game lies in its simplicity and yet its ability to test and enhance

vocabulary, word recognition, and deductive reasoning skills. Players are presented with a series of

dashes representing letters of an unknown word and must strategically guess individual letters to

uncover the hidden word before exhausting the allowed attempts.

Developed through programming languages like Python, JavaScript, or others, the Hangman game

application involves fundamental functionalities such as generating random words from a

predefined list, managing user inputs for letter guesses, updating the display with correct guesses,

tracking incorrect attempts, and managing the game's progress.

4|P ag e
List of Figures

Figure No Page no

Figure 1 Flowchart of User Input Handling 7

Figure 2 Algorithm for Arithmetic Operations 17

Figure 3 Modular Structure of the Code 20

Figure 4 Sample Output Display 26

5|P ag e
CHAPTER -1

INTRODUCTION

The Hangman game stands as a timeless and engaging word puzzle that has entertained people of all ages for

decades. Its straightforward yet captivating gameplay revolves around word guessing, challenging players to

decipher a hidden word by guessing individual letters while avoiding the drawing of a complete hangman

figure.

Originating as a pen-and-paper pastime, the Hangman game has seamlessly transitioned into digital formats,

offering an interactive and immersive experience to players across various platforms. Through its simplicity

and yet intricate design, the game not only provides entertainment but also serves as a stimulating exercise for

vocabulary, word recognition, and deductive reasoning skills.

In the digital realm, the Hangman game is brought to life through programming languages such as Python,

JavaScript,and others. Its digital adaptation involves creating a dynamic interface that allows players to

interactively guess letters, uncover parts of the hidden word, and strategically navigate the challenge of

revealing the complete word within alimited number of attempts.

The allure of the Hangman game lies in its simplicity – players are presented with a series of dashes

representing theletters of an unknown word and must guess individual letters to fill in the blanks, progressively

revealing the concealed word. However, incorrect guesses result in the gradual drawing of a hangman figure,

adding an element of suspenseand challenge to the game.

6|P ag e
FLOW CHART:

7|P ag e
CHAPTER – 2

METHODOLOGY

Developing a Hangman game using the C programming language involves a structured

methodology to createan interactive word-guessing game. Here's a step-by-step methodology:

Methodology for Hangman Game Development in C:

Define Data Structures:

Set up data structures to store the words to be guessed and track game progress:Use arrays or linked

lists to hold a collection of words for the game

Create structures or arrays to manage the state of the game, such as the hidden word, guessed

letters, andnumber of attempts.

Word Selection:

Implement a function to randomly select a word from the predefined list. Retrieve and store the

chosen word for the current game session.

Display Initial Interface:

Develop a function to display the initial interface of the Hangman game.

Show placeholders (underscores or dashes) for each letter in the word to be guessed.

Display relevant information such as the number of attempts left and any initial hangman figure
representation.

User Input and Guessing Mechanism:

Implement a function to handle user input for letter guesses. Validate the input to ensure it is a valid

letter.

Check the guessed letter against the hidden word and update the display accordingly (revealing
correct letters,
8|P ag e
updating the hangman figure for incorrect guesses).

Game Logic and Progression:

Develop the game logic to track progress and determine game outcomes.

Implement mechanisms to track correct and incorrect guesses, manage attempts left, and decide when

the gameis won or lost.

Display appropriate messages for winning, losing, or continuing the game.

User Interface and Display Updates:

Design functions to update the display after each guess, reflecting the current state of the game. Show

the updated hangman figure and reveal correctly guessed letters in the hidden word.

Continuously update the interface to provide feedback to the player on their progress.

Error Handling and Input Validation:

Implement robust error handling to manage unexpected inputs or invalid game states.

Validate user inputs to ensure they conform to the game's requirements (e.g., single letter guesses,

avoidingrepetition of guesses).

Optimization and Refinement:

Optimize the code for efficiency and readability.

Refine the user interface to enhance user experience and provide clear instructions and feedback.

Testing and Debugging:

Conduct thorough testing of the game to identify and rectify bugs or issues.

Test different scenarios, including various word lengths, correct and incorrect guesses, and game-

endingconditions.

9|P ag e
2.1 EXISTING TECHNIQUE

Creating a Hangman game in the C programming language involves a meticulous approach to

design an interactive and engaging word-guessing experience. The process encompasses several

key steps that bring together data handling, user interface development, game logic, and error

handling.

Fundamentally, the game begins by establishing data structures to manage essential components

such as the word list and game state. Employing arrays or structures helps store words for the

game and track the evolving game progress, including the hidden word and the user's guessed

letters.

An integral aspect is designing an intuitive user interface that renders the Hangman ASCII art

and presents an interactive display for guessing letters. This interface displays placeholders for

the unknown letters of the word and visually represents the hangman's progression based on

incorrect guesses, enhancing the player's immersion in the game.

The game's core functionality revolves around user input for guessing letters and the subsequent

processing of these guesses. It involves validating the input for correctness, comparing the

guessed letters against the hidden word, and dynamically updating the display to reveal correct

guesses while incrementally showcasing the hangman figure for incorrect attempts.

Additionally, robust game logic determines the win/loss conditions based on the accuracy of the

guesses and the remaining attempts. Implementing error-handling mechanisms ensures a smooth
12 | P a g e
gameplay experience by managing unexpected inputs and invalid states.

The modularity of functions and a structured code layout aid in maintaining an organized and

comprehensible codebase. Dividing tasks into smaller functions facilitates easier debugging,

optimization,and future enhancements, ensuring code readability and maintainability.

Optimization strategies focus on refining the code's efficiency, minimizing redundant

operations, and streamlining loops and conditional statements. Thorough testing across diverse

scenarios guarantees the game's functionality, identifying and resolving potential bugs or

inconsistencies.

Documenting the code with comprehensive comments and user guidelines enriches the

development process by enhancing readability and providing clarity for future reference,

ensuring players understand thegame's rules and controls.

This iterative methodology enables the creation of a fully functional Hangman game in C,

amalgamating programming intricacies and game design elements to deliver an entertaining and

immersive gaming experience.

13 | P a g e
.2.1 SOFTWARE REQUIREMENTS

Certainly! Let's delve into the details of the software requirements for developing and

running a Calculator Program using the C language:

C Compiler:

GCC (GNU Compiler Collection): This open-source compiler is widely used for C

programming. It translates your C source code into machine-readable code. On Windows,

you might use MinGW (Minimalist GNU for Windows) to get GCC.

Microsoft Visual C++: This compiler is provided by Microsoft and is often used for

Windows development. It integrates seamlessly with Visual Studio.

Integrated Development Environment (IDE) (Optional):

Code::Blocks: A free and open-source IDE that supports multiple compilers, including GCC.

Dev-C++: A lightweight and user-friendly IDE for C and C++ programming.

Visual Studio Code: While not a traditional IDE, Visual Studio Code is a highly

customizable and lightweight code editor that supports C/C++ development through

extensions.

Text Editor (Optional):

Notepad++: A free and feature-rich text editor that is popular among developers.

Sublime Text: A sophisticated text editor known for its speed and versatility.

Operating System:

C is a cross-platform language, so you can develop and run your calculator program on

various operating systems, including Windows, Linux, and macOS.

14 | P a g e
Terminal/Command Prompt (for Compilation and Execution):

On Windows, you might use the Command Prompt or PowerShell.

On Linux, you can use the terminal.

On macOS, the terminal is available for running commands.

Version Control System (Optional):

Git: A distributed version control system that allows you to track changes in your codebase.

It is beneficial for collaborative development and maintaining a history of your project.

Example Installation Steps (using GCC and Visual Studio Code):

Install GCC:

Windows: Download and install MinGW, and ensure that the GCC compiler is selected

during installation.

Linux: Use your distribution's package manager to install the build-essential package, which

includes GCC.

macOS: Install Xcode Command Line Tools, which includes GCC.

Install Visual Studio Code:

Windows/Linux/macOS: Download Visual Studio Code from the official website and

follow the installation instructions.

Install Extensions: Open Visual Studio Code, go to the Extensions view (Ctrl+Shift+X), and

search for "C/C++." Install the extension provided by Microsoft.

Write and Save Your C Code:

15 | P a g e
Open Visual Studio Code, create a new file, write your C code, and save it with a .c extension

(e.g., calculator.c).

Compile and Run:

Open a terminal within Visual Studio Code or use your system's terminal.

Navigate to the directory containing your C file.

Compile your code using the gcc command (e.g., gcc calculator.c -o calculator).

Run the executable (e.g., ./calculator).

By following these steps and meeting the specified software requirements, you can

successfully develop and execute a Calculator Program using the C language. Adjustments

may be necessary based on your specific development environment and preferences.

2.3 PROPOSED METHOD

The provided C program is an implementation of a hang man game that allows users to

perform various operations. Let's break down the key components and functionalities:

Header Files:

The program includes standard C library header files such as <stdio.h>, <conio.h>, and

<stdlib.h>. These headers provide functions for input/output, mathematical operations, and

program control.

Function Declarations:

The program declares several functions at the beginning, each corresponding to a specific

arithmetic operation .

16 | P a g e
Function declarations ensure that the compiler knows about these functions before they are

called in the main function.

Main Function:

The main function is the entry point of the program. It uses a do-while loop to repeatedly

display a menu of operations and perform the selected operation until the user chooses to exit

(option 7).

Switch Statement:

The switch statement is used to execute a block of code based on the value of the variable op,

representing the user's choice. Depending on the user's input, it calls the corresponding

function in the program.

17 | P a g e
ALGORITHM:

Initialize Game Data Structures:

Create arrays or structures to store the word list and manage game state. Define variables for the

hidden word, guessed letters, attempts left, etc. Load Word List:

Populate an array with a collection of words for the game.

Select Hidden Word:

Randomly select a word from the word list for the current game session
.
Store the chosen word as the hidden word to be guessed.

Display Initial Interface:

Show ASCII art representing the initial hangman figure.

Display placeholders (underscores or dashes) for each letter in the hidden word.

Game Loop:

Start a loop for the game to continue until a win or loss condition is met.Display the current state

of the hidden word with guessed letters.

Prompt the user to input a letter guess.

Validate User Input:

Check if the input is a valid single letter (not a digit, symbol, or multiple characters). Ensure the

letter hasn't been guessed before.

Check Guessed Letter:

Compare the guessed letter against the hidden word.If the letter matches a letter in the word:

Update the display to reveal correct guesses.

If the letter doesn't match:

Update the hangman figure display to show parts of the hangman.Decrease the attempts left.

Game Outcome Check:

Check if all letters of the hidden word have been guessed.If yes, display a winning message.

If attempts reach zero, display a losing message and reveal the hidden word.
18 | P a g e
End Game or Play Again:

Prompt the user to play again or exit the game.

Error Handling and Input Validation:

Implement error handling for invalid inputs or unexpected scenarios.

Validate user inputs rigorously to prevent errors and ensure smooth gameplay.

19 | P a g e
EXPLANATION:

The Hangman game stands as a popular and enjoyable word puzzle that challenges players to

guess a hiddenword by suggesting individual letters within a limited number of attempts. Its

straightforward yet captivating gameplay has made it a timeless pastime for people of all ages,

often serving as an engagingexercise for vocabulary and deductive reasoning.

In the digital realm, the Hangman game takes on various forms, including applications and

software developed using programming languages like C. The essence of the game remains

consistent: players aim to decipher an unknown word by guessing its letters while avoiding the

drawing of a complete hangman figure—a visual representation of incorrect guesses.

At its core, the game starts by selecting a word, which remains concealed from the player. The

player attempts to unravel this word by suggesting letters, one at a time. Correct guesses reveal

the positions of thecorrectly guessed letters within the word, while incorrect guesses result in the

gradual drawing of thehangman figure.

The visual representation of the hangman's progression, often depicted as a gallows and an

incomplete figure, adds an element of suspense and urgency. Players strive to guess the word

before the entire hangman figureis drawn, leading to a loss.

20 | P a g e
PROGRAM:
#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include <time.h>

#include <string.h>

#define WORDS 10

#define WORDLEN 40

#define CHANCE 6

bool srand_called = false;

int i_rnd(int i) {

if (!srand_called) {

srand(time(NULL) << 10);

srand_called = true;

return rand() % i;

char* decrypt(char* code) {

int hash = ((strlen(code) - 3) / 3) + 2;

char* decrypt = malloc(hash);

char* toFree = decrypt;

char* word = code;

for (int ch = *code; ch != '\0'; ch = *(++code))

20 | P a g e
if((code - word + 2) % 3 == 1){

*(decrypt++) = ch - (word - code + 1) - hash;

*decrypt = '\0';

return toFree;

void printBody(int mistakes, char* body) {

printf("\tMistakes :%d\n", mistakes);

switch(mistakes) {

case 6: body[6] = '\\'; break;

case 5: body[5] = '/'; break;

case 4: body[4] = '\\'; break;

case 3: body[3] = '|'; break;

case 2: body[2] = '/'; break;

case 1: body[1] = ')', body[0] = '('; break;

default: break;

printf("\t \n"

"\t| |\n"

"\t| %c %c\n"

"\t| %c%c%c\n"

"\t| %c %c\n"

"\t| \n"

21 | P a g e
"\t| ", body[0], body[1], body[2],

body[3], body[4], body[5], body[6]);

void printWord(char* guess, int len) {

printf("\t");

for (int i = 0; i < len; ++i)

printf("%c ", guess[i]);

printf("\n\n");

int main() {

printf("\n\t Be aware you can be hanged!!.");

printf("\n\n\t Rules : ");

printf("\n\t - Maximum 6 mistakes are allowed.");

printf("\n\t - All alphabet are in lower case.");

printf("\n\t - All words are name of very popular Websites. eg. Google");

printf("\n\t - If you enjoy continue, otherwise close it.");

printf("\n\t Syntax : Alphabet");

printf("\n\t Example : a \n\n");

char *body = malloc(CHANCE+1);

int id = i_rnd(WORDS);

22 | P a g e
char *word = decrypt(values[id]);

int len = strlen(word);

char *guessed = malloc(len);

char falseWord[CHANCE];

memset(body, ' ', CHANCE+1);

memset(guessed, '_', len);

char guess;

bool found;

char* win;

int mistakes = 0;

setvbuf(stdin, NULL, _IONBF, 0);

do {

found = false;

printf("\n\n");

printBody(mistakes, body);

printf("\n\n");

printf("\tFalse Letters : ");

if(mistakes == 0) printf("None\n");

for (int i = 0; i < mistakes; ++i)

printf("%c", falseWord[i]);

printf("\n\n");

printWord(guessed, len);

23 | P a g e
printf("\tGive me a alphabet in lower case : ");

do {scanf("%c",&guess);} while ( getchar() != '\n' );

for (int i = 0; i < len; ++i)

if(word[i] == guess) {

found = true;

guessed[i] = guess;

if(!found) {

falseWord[mistakes] = guess;

mistakes += 1;

win = strchr(guessed, '_');

}while(mistakes < CHANCE && win != NULL);

if(win == NULL) {

printf("\n");

printWord(guessed, len);

printf("\n\tCongrats! You have won : %s\n\n", word);

} else {

printf("\n");

printBody(mistakes, body);

printf("\n\n\tBetter try next time. Word was %s\n\n", word);

free(body);

24 | P a g e
free(guessed);

return 0;

25 | P a g e
CHAPTER – 3

EXPECTED RESULTS AND OUTPUT

Sample Output Display

24 | P a g e
25 | P a g e
26 | P a g e
CHAPTER – 4

CONCLUSION

In conclusion, the Hangman game epitomizes a timeless blend of simplicity, challenge, and

entertainment, captivating players with its intriguing word-guessing mechanics. From its origins as a

pen-and-paper pastime to its digital adaptations using programming languages like C, Hangman

continues to enchant players of all ages, offering an engaging blend offun and mental stimulation.

This classic game's allure lies in its straightforward yet immersive gameplay, challenging players to

unravel a hidden word through strategic letter guesses while avoiding the completion of the hangman

figure. Its interactive nature, accompanied by visual cues of the hangman's progression, fosters an

element of suspense, prompting players to exercise deductive reasoning and vocabulary skills in a

race against the hangman's completion.

Moreover, the Hangman game extends beyond mere entertainment, serving as an educational tool

that enhances language proficiency, word association, and problem- solving abilities. Its ability to

engage players in a fun yet educational manner makes it a valuable resource for vocabulary

enrichment and cognitive development.

The digital adaptation of Hangman, often developed using programming languages like C,

underscores the fusion of technology and gameplay, creating an interactive user interface while

preserving the game's fundamental mechanics. This amalgamation brings to life the essence of

Hangman, offering players an immersive experience while navigating the challenges of guessing the

hidden word.

28 | P a g e
SRI RAMAKRISHNA INSTITUTE OF TECHNOLOGY, COIMBATORE-10
(Approved by AICTE, New Delhi – Affiliated to Anna University, Chennai)
(Accredited by NAAC with ‘A’ Grade and All UG Engineering Programmes are
Accredited by NBA)

Department of Science and Humanities

2023 – 2024 Odd Semester

20ITG01 – PROGRAMMING FOR PROBLEM SOLVINGUSING C

I SEM ECE

Project based learning – Evaluation Rubrics

S.No Evaluation Mark Mark


Rubrics
1 Report 35

2 Presentation 35

3 Demo 30

4 Total 100

(A.Jayasmruthi)
Course Coordinator

27 | P a g e

You might also like