-
Notifications
You must be signed in to change notification settings - Fork 0
submit part 4: mythical creature interactions #1
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: main
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 |
|---|---|---|
|
|
@@ -28,6 +28,57 @@ struct Creature { | |
| return Int(numerator / denominator) | ||
| } | ||
|
|
||
| // // Part 4: Mythical Creature Interactions | ||
|
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. It is considered good style to remove code that is commented out. If you need to leave the code commented out, add a comment explaining why for the reviewer. |
||
| // func interactWith(creature: Creature) { | ||
| // | ||
| // switch (self.isGood, creature.isGood) { | ||
| // case (false, false): | ||
| // let combinedPowers = self.magicPower + creature.magicPower | ||
| // print("Both creatures are evil and have a combined magic power of\(combinedPowers).") | ||
| // | ||
| // case (false, true): | ||
| // let combinedPowers = self.magicPower - creature.magicPower | ||
| // if combinedPowers > 0 { | ||
| // print("To beat the evil \(self.name), you have to increase \(creature.name)'s magic power by more than \(abs(combinedPowers)) points.") | ||
| // } else if combinedPowers < 0 { | ||
| // print("\(creature.name) is strong enough to beat \(self.name). Look out for any unexpected evil backup") | ||
| // } else { | ||
| // print("Both creatures are equally strong. Find a way to increase \(creature.name)'s chances of winning.") | ||
| // } | ||
| // | ||
| // case (true, false): | ||
| // let combinedPowers = self.magicPower - creature.magicPower | ||
| // if combinedPowers > 0 { | ||
| // print("\(self.name) is strong enough to beat \(creature.name). Look out for any unexpected evil backup") | ||
| // } else if combinedPowers < 0 { | ||
| // print("To beat the evil \(creature.name), you have to increase \(self.name)'s magic power by more than \(abs(combinedPowers)) points.") | ||
| // } else { | ||
| // print("Both creatures are equally strong. Find a way to increase \(self.name)'s chances of winning.") | ||
| // } | ||
| // | ||
| // default: | ||
| // let combinedPowers = self.magicPower + creature.magicPower | ||
| // print("Both creatures are good and have a combined magic power of\(combinedPowers).") | ||
| // } | ||
| // } | ||
|
|
||
| // Part 4: Mythical Creature Interactions | ||
| func interactWith(creature: Creature) { | ||
|
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 function exhibits a behavior that is not discussed in the content. In Swift, if a function doesn’t explicitly return a value, it returns Void (an empty tuple, ()). This is why you were seeing () in your output. This also plays a part in the issue you found when trying to single-print a statement with String Interpolation. |
||
| switch (self.isGood, creature.isGood) { | ||
| case (false, false): | ||
| print("Both creatures are evil; it's good that they are fighting each other.") | ||
|
|
||
| case (false, true): | ||
| print("The evil \(self.name) is fighting the good \(creature.name). Do you think we can convince \(self.name) to turn to the right side?") | ||
|
|
||
| case (true, false): | ||
| print("I guess there is no better time to fight the evil \(creature.name), right?!") | ||
|
|
||
| default: | ||
| print("Both creatures are good and there is no point in them fighting each other. What can we do?") | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
@@ -45,10 +96,30 @@ creatureCatalog.append(astralHorn) | |
| creatureCatalog.append(infernoDrake) | ||
| creatureCatalog.append(emberSoul) | ||
|
|
||
| // Part 3: The Mythical Creature | ||
| func describeCreature(creatureArray: [Creature]) { | ||
| for creature in creatureArray { | ||
| print("\(creature.name) is a \(creature.description) and has \(creature.ability)") | ||
|
|
||
| // Part 4: Mythical Creature Interactions | ||
| print("\(creature.interactWith(creature: creatureArray.randomElement()!))") | ||
| } | ||
| } | ||
|
|
||
| // Examples related to part 4: Mythical Creature Interactions | ||
| describeCreature(creatureArray: creatureCatalog) | ||
| astralHorn.interactWith(creature: infernoDrake) | ||
|
|
||
|
|
||
|
|
||
| print("\n--- this is the end of the homework ---\n") | ||
|
|
||
| /* The homework is done before this line. However, the func describeCreature has 2 print functions to deliver what I was asked of. If I want to combine the content of the two print functions into one, what is printed inside the console down is done in reverse (i.e. it calls the interactWith function first then the content of the first print function). I don't know why is this and I hope you can help understand this behavior (this is why I separated the content into 2 separate print functions). | ||
| */ | ||
|
|
||
|
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. Nice catch! The reason why the output from below appears first is due to how String Interpolation works.
Swift evaluates every expression inside the string interpolation before constructing the final string for the print statement. The call to In other words, as Swift processes the interpolated expressions, it calls the function, and its side effects (like printing) occur right away, which is why you see its output before the overall string is printed. You can resolve this issue by modifying interactWith(creature: ) to return a String. `func interactWith2(creature: Creature) -> String { case (false, true): case (true, false): default: This change will preserve the order of output as intended and remove the extra () that are printed. |
||
| func describeCreature2(creatureArray: [Creature]) { | ||
| for creature in creatureArray { | ||
| print("\(creature.name) is a \(creature.description) and has \(creature.ability) \(creature.interactWith(creature: creatureArray.randomElement()!))") | ||
| } | ||
| } | ||
|
|
||
| describeCreature2(creatureArray: creatureCatalog) | ||
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.
Meets expectations!
It looks like your other branches have parts 1 through 3, and this branch has part 4. I was able to combine them, so there was no problem.