0% found this document useful (0 votes)
72 views7 pages

Amrita School of Engineering, Bangalore Amrita Vishwa Vidyapeetham BENGALURU-560035

The document describes a C program to simulate the game of Hangman. It breaks down the program into modules using concepts like decomposition, abstraction and pattern recognition. Functions are used to define rules, choose a genre, display the hangman as lives are lost, and the core game logic. The program flow involves choosing a genre, displaying rules if needed, running the game function with a random word from the chosen genre list, and allowing the user to play again.

Uploaded by

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

Amrita School of Engineering, Bangalore Amrita Vishwa Vidyapeetham BENGALURU-560035

The document describes a C program to simulate the game of Hangman. It breaks down the program into modules using concepts like decomposition, abstraction and pattern recognition. Functions are used to define rules, choose a genre, display the hangman as lives are lost, and the core game logic. The program flow involves choosing a genre, displaying rules if needed, running the game function with a random word from the chosen genre list, and allowing the user to play again.

Uploaded by

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

19CSE100

PROBLEM SOLVING AND ALGORTHMIC THINKING


Submitted by
BL.EN.U4ECE22111 Chayanam Karthik

BL.EN.U4ECE22138 S L Saran Kumar

BL.EN.U4ECE22107 Arjun Sailanathan

BL.EN.U4ECE22103 Aravind Jayakrishnan

in partial fulfilment for the award of the degree of

BACHELOR OF TECHNOLOGY
IN
“ELECTRONICS AND COMMUNICATION ENGINEERING”

January-2023

AMRITA SCHOOL OF ENGINEERING,


BANGALORE
AMRITA VISHWA VIDYAPEETHAM
BENGALURU-560035
Aim : To simulate the game Hangman using C.

Procedure: By applying the four corner stones:

Decomposition: We have broken down the entire program of the


game into various modules simplifying it by functions such as genre,
rules, game_func, etc.

Abstraction: We have ensured detailing such as asking if


the user knows how to play or not. The rules are not
displayed if the player already knows how to play.

Pattern Recognition: Many of the recursions and calling of


functions is mainly recognised by patterns. For example, we split
the hangman structure into a function and called when called when
a certain pattern is appeared.

Algorithm: The algorithm for hangman in c is:

include "stdio.h"
include "stdlib.h"
include "string.h"
include "time.h"

void rules()
{
printf("\nThe rules of Hangman are:\n"); printf("A word will be
randomized in your choice of genre\n"); printf("You will
have 5 lifes to guess a letter in that word\n"); printf("If you guess
all letter without losing 5 lifes. You WIN\n"); printf("If you lose all
5 lifes. You LOSE\n"); printf("ENJOY\n\n");
}

int genres()
{ int
ch;
printf("Enter your genre\n");
printf("1) Names\n"); printf("2)
Football\n"); printf("3:
Apps\n");
printf("Enter a choice (1,2 or 3) : ");
scanf("%d",&ch);
if (ch ==
1) return
ch;
else if (ch == 2)

return ch; else if (ch ==


3) return ch; else{
printf("Invalid entry\n");
printf("Try again\n");
genres();
}
}

void dead()
{ printf("\t |------+
\n"); printf("\t | |
\n"); printf("\t | O
\n"); printf("\t | / \\
\n"); printf("\t | |
\n"); printf("\t | / \\
");
}

void life_0()
{ printf("\t |------+
\n"); printf("\t | \n");
printf("\t | \n");
printf("\t | \n");
printf("\t | \n");
printf("\t | ");
}

void life_1() {
printf("\t |------+ \n");
printf("\t | |\n");
printf("\t | \n");
printf("\t | \n");
printf("\t | \n");
printf("\t | ");
}

void life_2()
{ printf("\t |------+
\n"); printf("\t | |
\n"); printf("\t | O
\n"); printf("\t | \n");
printf("\t | \n");
printf("\t | ");
}
void life_3()
{ printf("\t |------+
\n"); printf("\t | |
\n"); printf("\t | O
\n"); printf("\t | / \\
\n"); printf("\t | \n");
printf("\t | ");
}

void life_4()
{ printf("\t |------+
\n"); printf("\t | |
\n"); printf("\t | O
\n"); printf("\t | / \\
\n"); printf("\t | |
\n"); printf("\t | ");
}

void printing(int lives){


if (lives == 5)
life_0(); if (lives == 4)
life_1(); if (lives == 3)
life_2(); if (lives == 2)
life_3(); if (lives == 1)
life_4(); if (lives == 0)
dead();
}

void game_func(char str[])


{

int strsize = strlen(str); int numLives


= 5; int numCorrect = 0; int oldCorrect =
0; int letterGuessed[8] = { 0,0,0,0,0,0,0,0
}; int loopIndex = 0; int reguessed = 0;
char guess[16];
char letter_entered;
while ( numCorrect < strsize ) {
printing(numLives);

printf("\n\n");

for(loopIndex = 0; loopIndex < strsize; loopIndex++) {

if(letterGuessed[loopIndex] == 1) {
printf("%c ", str[loopIndex]);
}
else {
printf("_ ");
}

printf("\n\n");

printf("Number Correct So Far : %d\n",numCorrect);


printf("Enter a guess letter:"); scanf("%s",&guess[0]);

letter_entered = guess[0];
reguessed = 0;

printf("letterEntered:%c\n",letter_entered); oldCorrect

= numCorrect;

for( loopIndex = 0; loopIndex < strsize; loopIndex++) {

if(letterGuessed[loopIndex] == 1) {
if(str[loopIndex] == letter_entered) {
reguessed = 1;
break;
}
continue;
}

if( letter_entered == str[loopIndex] ) {


letterGuessed[loopIndex] = 1;
numCorrect++;
}
}
if( oldCorrect == numCorrect && reguessed == 0) {
numLives--; printf("Sorry, wrong guess\n");
printf("\n\n");
if (numLives == 0) {
break;
}
}
else if( reguessed == 1) {
printf("Already Guessed!!\n");
}
else {
printf("Correct guess :)\n\n\n");
}
}

if (numLives == 0)
{ dead();
printf("\n\nSorry you lose, the word was: '%s'\n", str);
}
else {
printf("\nYOU WIN!!! :)\tThe word was '%s' \n",str);
}

int main()
{

srand(time(NULL));

char arr1[][20] = {"karthik","saran","arjun","aravind","sumukh"}; char


arr2[][20] = {"hazard","messi","ronaldo","kimmich","rashford"}; char
arr3[][20] = {"netflix", "amazon", "twitter", "whatsaap", "facebook"}; int

rand_num = rand() % 5;

int ch2,ch3;
char ch1;

printf("\nWELCOME TO HANGMAN\n");
printf("==================================
====\n");
printf("Do you know how to play? (y/n) : "); scanf("%c",&ch1);

if(ch1 == 'n')
rules();
printf("\n==========
===================
=========\n"); ch2
= genres();

printf("\n\n");

if (ch2 == 1)
game_func(arr1[rand_num]);
else if (ch2 == 2)
game_func(arr2[rand_num]);
else
game_func(arr3[rand_num]);

printf("\nDo you want to play again?(1. yes/2. no) : ");


scanf("%d",&ch3);

if (ch3 == 1)
main(); else
printf("Thank you");
printf("\n");
return 0; }

Conclusion: We have successfully interpreted the working of


the game Hangman using C.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy