/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int check_guess ( char word[], char guess );
int hidden_word ( char display_word[], char word[], unsigned char guesses[26] );
int hidden_word_2 ( char display_word[], char word[], unsigned char guesses[26] );
int main()
{
char word[] = "foobarBAZ";
char display_word[] = "_________";
char guess[] = "abcefgzdABCDZ* or";
char guesses[26];
memset(guesses, 0, sizeof(guesses));
printf("\ntest check_guess:\n\n");
for(int i = 0; guess[i] != '\0'; i++)
{
int check = check_guess(word, guess[i]);
printf("<%c> %s %d\n", guess[i], word, check);
}
printf("\ntest hidden_word:\n\n");
for(int i = 0; guess[i] != '\0'; i++)
{
if(isalpha(guess[i]))
{
guesses[tolower(guess[i]) - 'a'] = 1;
}
int result = hidden_word ( display_word, word, guesses ); // does not work
printf("<%c> %s %d\n", guess[i], display_word, result);
}
printf("\ntest hidden_word_2:\n\n");
memset(guesses, 0, sizeof(guesses));
for(int i = 0; guess[i] != '\0'; i++)
{
if(isalpha(guess[i]))
{
guesses[tolower(guess[i]) - 'a'] = 1;
}
int result = hidden_word_2 ( display_word, word, guesses );
printf("<%c> %s %d\n", guess[i], display_word, result);
}
return 0;
}
int check_guess ( char word[], char guess ){
int length = strlen(word);
int i;
for (i = 0; i < length; ++i) {
if (guess == word[i])
return 1;
}
return 0;
}
// hidden_word()
// Creates the word that is displayed to the user, with all the correctly guessed letters
// shown, and the rest displayed as underscores. Any non-letters (punctuation, etc) are displayed.
// The function takes two strings as inputs. word[] is the word that the player is trying to guess,
// and display_word[] is the output string to be displayed to the player. The guesses array is a binary
// array of size 26 indicating whether each letter (a-z) has been guessed yet or not.
// Returns 0 if successful, -1 otherwise.
int hidden_word ( char display_word[], char word[], unsigned char guesses[26] ){
int l = strlen(display_word);
if (l != strlen(word))
return -1;
//printf("%d", l);
int i;
for (i = 0; i < l; ++i) {
char c1 = (display_word[i] >= 'A' && display_word[i] <= 'Z') ? display_word[i] + 32 : display_word[i];
char c2 = (word[i] >= 'A' && word[i] <= 'Z') ? word[i] + 32 : word[i];
if (c1 == '_') {
if (guesses[c2 - 97] == 1)
return -1;
else {
guesses[c2 - 97] = 1;
}
}
else if ((c1 >= 'a' && c1 <= 'z') || (c1 >= 'A' && c1 <= 'Z')) {
if (c2 != c1)
return -1;
}
}
return 0;
}
// hidden_word()
// Creates the word that is displayed to the user, with all the correctly guessed letters
// shown, and the rest displayed as underscores. Any non-letters (punctuation, etc) are displayed.
// The function takes two strings as inputs. word[] is the word that the player is trying to guess,
// and display_word[] is the output string to be displayed to the player. The guesses array is a binary
// array of size 26 indicating whether each letter (a-z) has been guessed yet or not.
// Returns 0 if successful, -1 otherwise.
int hidden_word_2 ( char display_word[], char word[], unsigned char guesses[26] ){
int result = 0;
int l = strlen(display_word);
if (l != strlen(word))
return -1;
//printf("%d", l);
int i;
for (i = 0; i < l; ++i) {
char c2 = (word[i] >= 'A' && word[i] <= 'Z') ? word[i] + 32 : word[i];
if (guesses[c2 - 97] == 1)
{
display_word[i] = word[i];
}
else
{
display_word[i] = '_';
result = -1;
}
}
return result;
}