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
58 changes: 56 additions & 2 deletions HangPerson/HangPerson/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -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++ ) {
Copy link
Contributor

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:

int won = 0;
int lost = 0;
while (won == 0 && lost == 0) { 
  // play game
}


printf("select a letter: \n");

printf("%c %c %c %c %c", dashes[0], dashes[1], dashes[2], dashes[3], dashes[4]);
Copy link
Contributor

Choose a reason for hiding this comment

The 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++) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
}