-
Notifications
You must be signed in to change notification settings - Fork 25
Homework-HangPerson #24
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,61 @@ int main(int argc, const char * argv[]) { | |
| @autoreleasepool { | ||
|
|
||
| // code goes here... | ||
|
|
||
| char word[5] = "pasta"; | ||
| char dashes[5] = "_____"; | ||
| char input; | ||
| char temp; | ||
| int guesses = 7; //Maximum tries. | ||
| int i; | ||
| int j; | ||
| int swaps = 5; | ||
|
|
||
|
|
||
| printf("Lets play a Hangman.\n"); | ||
| printf("I'm thinking of a word. Can you guess it?\n"); | ||
|
|
||
| for(i = 0; i < guesses; i++ ) { | ||
|
|
||
| printf("select a letter: \n"); | ||
|
|
||
| printf("%c %c %c %c %c", dashes[0], dashes[1], dashes[2], dashes[3], dashes[4]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very specific to a 5 character word. It would be better to loop from 0 to sizeof(dashes) and print out each letter individually. |
||
|
|
||
| scanf("%c", &input); | ||
|
|
||
|
|
||
|
|
||
| char vanish; | ||
| scanf("%c", &vanish); | ||
|
|
||
| for(j = 0; j < 5; j++) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar thing here. Instead of 5 you should use sizeof(word) |
||
|
|
||
| // positive match | ||
| if (input == word[j]) { | ||
|
|
||
| swaps--; | ||
|
|
||
| temp = word[j]; | ||
| word[j] = dashes[j]; | ||
| dashes[j] = temp; | ||
|
|
||
|
|
||
| printf("\n%c", dashes[j]); | ||
|
|
||
| } | ||
| if (swaps == 0) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| if (guesses == 7) { | ||
| printf("You lost."); | ||
| } else { | ||
| printf("You win"); | ||
| } | ||
|
|
||
| } | ||
| return 0; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition here shouldn't be based on the number of guess because you could guess the word in 4 guesses. A while loop would be better in this situation. Something along the lines of: