Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hangman game #5173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions c_projects/Hangman/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hangman game

This C project is the classic Hangman game.
58 changes: 58 additions & 0 deletions c_projects/Hangman/hangman.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {
int flag, fin_count, count = 6;
char ch;
char ans[7] = {"*******"};
char word[7] = {"monsoon"} ;
printf("START\n");
printf("Action: To guess a season\n");

do {
printf("Enter a letter: ");
ch = getchar();
flag = 0;
for(int i=0;i<7;i++) {
if(word[i]==ch && ans[i]=='*') {
ans[i] = ch;
flag = 1;
}
else {
continue;
}
}
if(flag == 1) {
printf("Answer: ");
for(int i=0;i<7;i++) {
printf("%c", ans[i]);
}
printf("\n");
printf("\nKeep going!\n");
}
else if(flag==0){
printf("\nOops!\n");
printf("%d more tries left\n", count);
printf("Answer: ");
for(int i=0;i<7;i++) {
printf("%c", ans[i]);
}
printf("\n");
count--;
}
for(int i=0;i<7;i++) {
fin_count = 0;
if(ans[i]!='*'){
fin_count++;
}
else {
continue;
}
}
if(fin_count==7){
printf("You won!\n");
break;
}
}while(count>=0);
}