In: Computer Science
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void clrScreen(int lines){
int i = 0;
for( i = 0; i < lines; ++i ){
printf("\n");
}
return;
}
void printRules(void){
printf("\t|*~*~*~*~*~*~*~*~*~ How to Play ~*~*~*~*~*~*~*~*~*~|\n");
printf("\t| This is a 2 player game. Player 1 enters the |\n");
printf("\t| word player 2 has to guess. Player 2 gets a |\n");
printf("\t| number of guesses equal to twice the number |\n");
printf("\t| of characters. EX: If the word is 'example' |\n");
printf("\t| player 2 gets 14 guesses. |\n");
printf("\t|*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~|\n");
clrScreen(10);
return;
}
//------------------------------------------------------------------------------------------------------------
/* /\DO NOT MODIFY ABOVE THIS LINE /\*/
void playGame(){
int correctGuess = 1;
char garbage;
clrScreen(40);
printRules();
printf("Player 1: Enter a word smaller than 50 characters: ");
clrScreen(40);
if( -1 == correctGuess ){
printf("CoNgRaTuLaTiOnS!!!!!! You figured out the word!!!\n");
printf("\t\t%s\n");
} else {
printf("You didn't figure out the word.....it was %s\n");
printf("Better luck next time!\n");
}
printf("Press 'enter' to continue to main menu.\n");
scanf("%c", &garbage);
}
int menu(void){
int loop = 1;
while( loop ){
clrScreen(40);
printf("*~*~*~*~*~*~*~*~*~*~Welcome to Hangman!*~*~*~*~*~*~*~*~*~*~\n");
printf("\t1.) Play the game\n");
printf("\t2.) Quit\n");
printf("Please make a selection: ");
}
}
/*
hangman game RULES:
2 player game
player 1
enter a word for player 2 to guess
enter a number of guesses player 2 gets to have. It must be at least 2x as big
as the number of letters in the word.
For example, if you enter the word 'sky' you must give the player at least 6 guesses.
player 2
try to guess the word player 1 has entered.
you get X number of guesses
*/
int main(void){
return 0;
}
//In c programming language
//please help me finish this hangman program. Thank you.
C Program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void clrScreen(int lines){
int i = 0;
for( i = 0; i < lines; ++i ){
printf("\n");
}
return;
}
void printRules(void){
printf("\t|*~*~*~*~*~*~*~*~*~ How to Play
~*~*~*~*~*~*~*~*~*~|\n");
printf("\t| This is a 2 player game. Player 1 enters the
|\n");
printf("\t| word player 2 has to guess. Player 2 gets a
|\n");
printf("\t| number of guesses equal to twice the number
|\n");
printf("\t| of characters. EX: If the word is 'example'
|\n");
printf("\t| player 2 gets 14 guesses. |\n");
printf("\t|*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~|\n");
clrScreen(10);
return;
}
//------------------------------------------------------------------------------------------------------------
/* /\DO NOT MODIFY ABOVE THIS LINE /\*/
//Function that checks whether a character is already guessed or
not
int isPresent(char ch, char *word, int len)
{
int k;
//Iterating over so far guessed word
for(k=0; k<len; k++)
{
//If already guessed
if(word[k] == ch)
return 1;
}
//New guess
return 0;
}
//Playing game
void playGame(){
int i, j, k, tries, wordLen, guessed=0;
char guess,cont;
int found = 0;
char guessWord[50], *answer;
clrScreen(10);
printRules();
//Getting word
printf("\nEnter Secret word: ");
scanf("%s", guessWord);
clrScreen(40);
//Finding length of string
wordLen = strlen(guessWord);
//Number of tries
tries = 2*wordLen;
//Allocating space
answer = (char*)malloc(sizeof(char)*(wordLen));
//Initializing answer to -'s
for(j=0; j<wordLen; j++)
{
answer[j] = '-';
}
answer[j] = '\0';
//Iterate till there are tries or word is guessed
for(i=0; i<tries && guessed != wordLen; )
{
//Reading guess
printf("\n\n Guess a letter (you have %d tries left): ", (tries -
i));
scanf(" %c", &guess);
//Checking for already guessed
if(isPresent(guess, answer, wordLen))
{
printf("\n\n %c guess has already been used. Try again.",
guess);
}
else
{
//New guess
found = 0;
//Checking each position
for(j=0; j<wordLen; j++)
{
if(guessWord[j] == guess)
{
//Replacing - with character
found = 1;
answer[j] = guess;
guessed++;
}
}
//If right guess
if(found)
{
printf("\n Right! Word so far: ");
}
else
{
//Wrong guess
printf("\n Wrong! Try again. Word so far is: ");
}
//Printing answer so far
printf("%s", answer);
i++;
}
}
//Checking for winning status
if(guessed == wordLen)
{
printf("\n You Won!!! \n");
}
else
{
printf("\n You Lose!!! \n\n The word is: %s \n", guessWord);;
}
}
int menu(void){
clrScreen(5);
printf("*~*~*~*~*~*~*~*~*~*~Welcome to
Hangman!*~*~*~*~*~*~*~*~*~*~\n");
printf("\t1.) Play the game\n");
printf("\t2.) Quit\n");
printf("Please make a selection: ");
}
/*
hangman game RULES:
2 player game
player 1
enter a word for player 2 to guess
enter a number of guesses player 2 gets to have. It must be at
least 2x as big
as the number of letters in the word.
For example, if you enter the word 'sky' you must give the player
at least 6 guesses.
player 2
try to guess the word player 1 has entered.
you get X number of guesses
*/
int main(void)
{
int option;
while(1)
{
//Printing menu
menu();
//Reading user choice
scanf("%d", &option);
//Checking option
if(option == 1)
{
playGame();
}
else
{
return 0;
}
}
return 0;
}
___________________________________________________________________________________________________
Sample Run: