Skip to content
Open
Show file tree
Hide file tree
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
73 changes: 72 additions & 1 deletion Mythical Creature.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,57 @@ struct Creature {
return Int(numerator / denominator)
}

Copy link
Copy Markdown

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.


// // Part 4: Mythical Creature Interactions

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.
Astral Horn is a unicorn and has a magic power strength of 9 points which translates into 34 points of special ability. Both creatures are good and there is no point in them fighting each other. What can we do? ()

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?")
}
}

}


Expand All @@ -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).
*/

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.

\(creature.ability) \(creature.interactWith(creature: creatureArray.randomElement()!))

Swift evaluates every expression inside the string interpolation before constructing the final string for the print statement. The call to creature.interactWifth(creature: ) contains its own print statement or other side effects, those will execute immediately when that expression is evaluated.

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 {
switch (self.isGood, creature.isGood) {
case (false, false):
return "Both creatures are evil; it's good that they are fighting each other."

case (false, true):
return "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):
return "I guess there is no better time to fight the evil (creature.name), right?!"

default:
return "Both creatures are good and there is no point in them fighting each other. What can we do?" }
}`

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)
Binary file not shown.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ Introduction: This is a submittal of the second homework assignment, it is relat

Purpose: This repository is for grading and feedback purposes.

Breakdown of homework:
1- Part 1: Creating Structs (made on branch: creature-structure) Update: **merged into branch: main and kept branch creature-structure**)
2- Part 2: Fibonacci Creature Abilities (made on branch: creature-abilities) Update: **merged into branch: main and kept on branch creature-abilities**)
3- Part 3: The Mythical Creature (made on branch: mythical-creature) Update: **merged into branch: main and kept on branch mythical-creature**)
Breakdown of homework:<br>
1- Part 1: Creating Structs (made on branch: creature-structure) Update: **merged into branch: main and kept branch creature-structure**)<br>
2- Part 2: Fibonacci Creature Abilities (made on branch: creature-abilities) Update: **merged into branch: main and kept on branch creature-abilities**)<br>
3- Part 3: The Mythical Creature (made on branch: mythical-creature) Update: **merged into branch: main and kept on branch mythical-creature**)<br>
3- Part 4: Mythical Creature Interactions (made on branch: creature-interactions)

Thank you.