I am a beginner who wrote this simple hangmanHangman game in C for fun and to practice programming. I am looking for advice on optimizing this code and making it adhere to best practices. Are there too many variables, conditionals, and loops? Thank you!
I am a beginner who wrote this simple hangman game in C for fun and to practice programming. I am looking for advice on optimizing this code and making it adhere to best practices. Are there too many variables, conditionals, and loops? Thank you!
I am a beginner who wrote this simple Hangman game in C for fun and to practice programming. I am looking for advice on optimizing this code and making it adhere to best practices. Are there too many variables, conditionals, and loops?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
int main(void)
{
//get a word
char words[12][10] = {"depend", "rich", "twig", "godly", "fang", "increase", "breakable", "stitch", "pumped", "pine", "shrill", "cable"};
srand(time(NULL));
int r = rand() % 10;12;
char* word = words[r];
int length = strlen(word);
int maxIncorrect = 5;
char correct[26] = {'\0'};
char incorrect[26] = {'\0'};
int amountCorrect = 0;
int amountIncorrect = 0;
//repeat until maxIncorrect wrong guesses
while (amountIncorrect < maxIncorrect)
{
char guess = '\0';
bool inWord = false;
bool allCorrect = true;
printf("\n________________________________________\n\n");
//print blanks and correcly guessed letters
for (int i = 0; i < length; i++)
{
inWord = false;
for (int j = 0; j < 26; j++)
{
if (word[i] == correct[j])
{
inWord = true;
}
}
if (inWord)
{
printf("%c ", word[i]);
}
else
{
printf("_ ");
allCorrect = false;
}
}
//stop if all letters have been correctly guessed
if (allCorrect)
{
printf("\n\nCorrect!\n");
return 0;
}
//print incorrect guesses
printf("\n\nIncorrect guesses: ");
for (int i = 0; i < amountIncorrect; i++)
{
printf("%c", incorrect[i]);
}
printf("\n");
inWord = false;
bool valid = true;
printf("\n%i incorrect guesses remaining\n", maxIncorrect - amountIncorrect);
printf("Enter a single lowercase letter: ");
//get user's guess and check if it is valid (a lowercase letter that has not been guessed before)
scanf("%c%*c", &guess);
for (int i = 0; i < amountCorrect; i++)
{
if (guess == correct[i])
{
valid = false;
}
}
for (int i = 0; i < amountIncorrect; i++)
{
if (guess == incorrect[i])
{
valid = false;
}
}
if (guess < 97 || guess > 122)
{
valid = false;
}
//go back to top of loop guess is invalid
if (!valid)
{
printf("\nInvalid\n");
}
//check if guess is part of the word or not
else
{
for (int i = 0; i < length; i++)
{
if (guess == word[i])
{
inWord = true;
}
}
if (inWord == true)
{
correct[amountCorrect] = guess;
amountCorrect++;
}
else
{
incorrect[amountIncorrect] = guess;
amountIncorrect++;
}
}
}
printf("\nThe word was %s\n", word);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
int main(void)
{
//get a word
char words[12][10] = {"depend", "rich", "twig", "godly", "fang", "increase", "breakable", "stitch", "pumped", "pine", "shrill", "cable"};
srand(time(NULL));
int r = rand() % 10;
char* word = words[r];
int length = strlen(word);
int maxIncorrect = 5;
char correct[26] = {'\0'};
char incorrect[26] = {'\0'};
int amountCorrect = 0;
int amountIncorrect = 0;
//repeat until maxIncorrect wrong guesses
while (amountIncorrect < maxIncorrect)
{
char guess = '\0';
bool inWord = false;
bool allCorrect = true;
printf("\n________________________________________\n\n");
//print blanks and correcly guessed letters
for (int i = 0; i < length; i++)
{
inWord = false;
for (int j = 0; j < 26; j++)
{
if (word[i] == correct[j])
{
inWord = true;
}
}
if (inWord)
{
printf("%c ", word[i]);
}
else
{
printf("_ ");
allCorrect = false;
}
}
//stop if all letters have been correctly guessed
if (allCorrect)
{
printf("\n\nCorrect!\n");
return 0;
}
//print incorrect guesses
printf("\n\nIncorrect guesses: ");
for (int i = 0; i < amountIncorrect; i++)
{
printf("%c", incorrect[i]);
}
printf("\n");
inWord = false;
bool valid = true;
printf("\n%i incorrect guesses remaining\n", maxIncorrect - amountIncorrect);
printf("Enter a single lowercase letter: ");
//get user's guess and check if it is valid (a lowercase letter that has not been guessed before)
scanf("%c%*c", &guess);
for (int i = 0; i < amountCorrect; i++)
{
if (guess == correct[i])
{
valid = false;
}
}
for (int i = 0; i < amountIncorrect; i++)
{
if (guess == incorrect[i])
{
valid = false;
}
}
if (guess < 97 || guess > 122)
{
valid = false;
}
//go back to top of loop guess is invalid
if (!valid)
{
printf("\nInvalid\n");
}
//check if guess is part of the word or not
else
{
for (int i = 0; i < length; i++)
{
if (guess == word[i])
{
inWord = true;
}
}
if (inWord == true)
{
correct[amountCorrect] = guess;
amountCorrect++;
}
else
{
incorrect[amountIncorrect] = guess;
amountIncorrect++;
}
}
}
printf("\nThe word was %s\n", word);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
int main(void)
{
//get a word
char words[12][10] = {"depend", "rich", "twig", "godly", "fang", "increase", "breakable", "stitch", "pumped", "pine", "shrill", "cable"};
srand(time(NULL));
int r = rand() % 12;
char* word = words[r];
int length = strlen(word);
int maxIncorrect = 5;
char correct[26] = {'\0'};
char incorrect[26] = {'\0'};
int amountCorrect = 0;
int amountIncorrect = 0;
//repeat until maxIncorrect wrong guesses
while (amountIncorrect < maxIncorrect)
{
char guess = '\0';
bool inWord = false;
bool allCorrect = true;
printf("\n________________________________________\n\n");
//print blanks and correcly guessed letters
for (int i = 0; i < length; i++)
{
inWord = false;
for (int j = 0; j < 26; j++)
{
if (word[i] == correct[j])
{
inWord = true;
}
}
if (inWord)
{
printf("%c ", word[i]);
}
else
{
printf("_ ");
allCorrect = false;
}
}
//stop if all letters have been correctly guessed
if (allCorrect)
{
printf("\n\nCorrect!\n");
return 0;
}
//print incorrect guesses
printf("\n\nIncorrect guesses: ");
for (int i = 0; i < amountIncorrect; i++)
{
printf("%c", incorrect[i]);
}
printf("\n");
inWord = false;
bool valid = true;
printf("\n%i incorrect guesses remaining\n", maxIncorrect - amountIncorrect);
printf("Enter a single lowercase letter: ");
//get user's guess and check if it is valid (a lowercase letter that has not been guessed before)
scanf("%c%*c", &guess);
for (int i = 0; i < amountCorrect; i++)
{
if (guess == correct[i])
{
valid = false;
}
}
for (int i = 0; i < amountIncorrect; i++)
{
if (guess == incorrect[i])
{
valid = false;
}
}
if (guess < 97 || guess > 122)
{
valid = false;
}
//go back to top of loop guess is invalid
if (!valid)
{
printf("\nInvalid\n");
}
//check if guess is part of the word or not
else
{
for (int i = 0; i < length; i++)
{
if (guess == word[i])
{
inWord = true;
}
}
if (inWord == true)
{
correct[amountCorrect] = guess;
amountCorrect++;
}
else
{
incorrect[amountIncorrect] = guess;
amountIncorrect++;
}
}
}
printf("\nThe word was %s\n", word);
return 0;
}
Hangman game in C
I am a beginner who wrote this simple hangman game in C for fun and to practice programming. I am looking for advice on optimizing this code and making it adhere to best practices. Are there too many variables, conditionals, and loops? Thank you!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
int main(void)
{
//get a word
char words[12][10] = {"depend", "rich", "twig", "godly", "fang", "increase", "breakable", "stitch", "pumped", "pine", "shrill", "cable"};
srand(time(NULL));
int r = rand() % 10;
char* word = words[r];
int length = strlen(word);
int maxIncorrect = 5;
char correct[26] = {'\0'};
char incorrect[26] = {'\0'};
int amountCorrect = 0;
int amountIncorrect = 0;
//repeat until maxIncorrect wrong guesses
while (amountIncorrect < maxIncorrect)
{
char guess = '\0';
bool inWord = false;
bool allCorrect = true;
printf("\n________________________________________\n\n");
//print blanks and correcly guessed letters
for (int i = 0; i < length; i++)
{
inWord = false;
for (int j = 0; j < 26; j++)
{
if (word[i] == correct[j])
{
inWord = true;
}
}
if (inWord)
{
printf("%c ", word[i]);
}
else
{
printf("_ ");
allCorrect = false;
}
}
//stop if all letters have been correctly guessed
if (allCorrect)
{
printf("\n\nCorrect!\n");
return 0;
}
//print incorrect guesses
printf("\n\nIncorrect guesses: ");
for (int i = 0; i < amountIncorrect; i++)
{
printf("%c", incorrect[i]);
}
printf("\n");
inWord = false;
bool valid = true;
printf("\n%i incorrect guesses remaining\n", maxIncorrect - amountIncorrect);
printf("Enter a single lowercase letter: ");
//get user's guess and check if it is valid (a lowercase letter that has not been guessed before)
scanf("%c%*c", &guess);
for (int i = 0; i < amountCorrect; i++)
{
if (guess == correct[i])
{
valid = false;
}
}
for (int i = 0; i < amountIncorrect; i++)
{
if (guess == incorrect[i])
{
valid = false;
}
}
if (guess < 97 || guess > 122)
{
valid = false;
}
//go back to top of loop guess is invalid
if (!valid)
{
printf("\nInvalid\n");
}
//check if guess is part of the word or not
else
{
for (int i = 0; i < length; i++)
{
if (guess == word[i])
{
inWord = true;
}
}
if (inWord == true)
{
correct[amountCorrect] = guess;
amountCorrect++;
}
else
{
incorrect[amountIncorrect] = guess;
amountIncorrect++;
}
}
}
printf("\nThe word was %s\n", word);
return 0;
}
lang-c