Skip to content
Open
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
83 changes: 81 additions & 2 deletions HangPerson/HangPerson/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,87 @@
int main(int argc, const char * argv[]) {
@autoreleasepool {

// code goes here...

printf("Let's play Hangman \n \n");

char myWord[] = "anatomy";
// char userWord[ ] = "_______\n";
char userLetter;

int numChances = 5;
int guessCorrect = 0;

int lengthOfWord = 7 ;
int userGuess[7] = {0,0,0,0,0,0,0};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this line an the line above you should use sizeof(myWord) or strlen(myWord) instead of hard coding a number. The more general the better.

int i = 0;
char usedLetter[256];
int trackingLetters=0;

for (int j = 0; j < 256; j++) {
usedLetter[j] = '\0';
}

while (guessCorrect < lengthOfWord) {
printf("\n\nGuess a letter \n");
scanf("%c*c", &userLetter);
fpurge(stdin);
int counter =0;

int checker =0;
for(int j= 0; j<256; j++){
if(usedLetter[j]!='\0' && usedLetter[j]==userLetter){
checker=1;
break;
}
}
if(checker==1){
continue;
}
usedLetter[trackingLetters]=userLetter;
//compare the user's guess to the letters in my word
for (i = 0; i < lengthOfWord; i++) {
if (userLetter == myWord[i]) {
userGuess[i] = 1;
counter =1;
guessCorrect++;

}

} //end of for loop

if (counter==1){
printf("Great, that letter is in the word \n");
}
if (counter==0){
numChances--;
printf("You guessed wrong\n");
}
if(numChances==0){
// printf("You Lose HAHA\n");
break;
}
//print the player's progress
for (i = 0; i < lengthOfWord; i++) {
if (userGuess[i] == 1) {
printf("%c ", myWord[i]);
} else {
printf("_ ");
}

//check if the player won
// if (userLetter == myWord[i]) {
// userGuess[i] = 1; //this will change the correct guess to the correct letter in the userWord
// guessCorrect++; //this increases the userGuess count to let the program know that the user is guessing correctly and will decide whether or not the for loop continues --did the user win or lose
// }
}
trackingLetters++;
}
//place results after the end of the while loop
if (numChances == 0) {
printf("\n Bummer, you didn't guess the word \n");
} else {
printf("\n Check you out! You guessed the right word", myWord);
}

}
return 0;
}