Project 8: 7 Swifty Words – Part 2

by | March 19, 2019

Project 8: 7 Swifty Words – Part 2

Jeff Summers

March 19, 2019

Part two of Project 8 was not without some challenges. It was nothing like yesterday where I think I wore off all of my fingerprints typing in all of the UI code for our 7 Small Words game. I can appreciate the need to know how to write the user interface programmatically but that was brutal. Nothing will make you appreciate the Storyboard more than creating a screen with all of the elements and their associated constraints. Yikes!

So, Part 2 couldn’t possibly be as bad could it? Well as it turns out the answer to that was no. Oh, it was challenging but not in a finger-numbing way. As with yesterday, I am not going to go deeply into the details we were taught. Instead I am going to again deviate and add a little bit that may help others who are following the 100 Days of Swift program.

At the conclusion of the second day on this project we had a working game. For those who might not be taking part, this project is creating a 7 Small Words game where you are given 7 clues for 7 words and on the screen are parts of words that you use to answer the clues on the 7 words. I am sure I butchered that description but hopefully you get what I was saying.

While I was somewhat pleased with myself that I was able to complete this part of the project and nearly understand all of the concepts being taught I couldn’t help but feel as though our little game was missing something. First of all, it only has two levels. That’s not much of a game so I added another 8 levels for a total of 10. There are web sites that have puzzles in PDF format that you can download to create your own levels.

The second issue was that I thought the user should be notified when they had completed all the levels of the game. Nothing amazing like dancing clowns or playing music to celebrate (hey what do you want from a beginner and besides the game only has 2 levels as presented so really is it much of an accomplishment?)

What I decided was that when the final level was completed that the system would display a celebratory background with a dialog box of congratulations for beating all the levels. First thing I did was define a variable called maxLevel where I put in the largest level in the game. I probably could have derived the number from the number of files I uploaded into the project, but I’ll leave that for another day. Most of the code for this I put into the submitTapped function as it seemed the best place to check since that is where we determined if the level was complete.

@objc func submitTapped(_ sender: UIButton) {
guard let answerText = currentAnswer.text else { return }

if let solutionPosition = solutions.firstIndex(of: answerText) {
activatedButtons.removeAll()

var splitAnswers = answersLabel.text?.components(separatedBy: "\n")
splitAnswers?[solutionPosition] = answerText
answersLabel.text = splitAnswers?.joined(separator: "\n")

currentAnswer.text = ""
score += 1

if score % 7 == 0 {
var alertTitle = ""
var alertMessage = ""
var buttonTitle = ""

if level < maxLevel { alertTitle = "Well Done!" alertMessage = "Are you ready for the next level?" buttonTitle = "Let's Go!" } else { // Set background to balloons and hide fields view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!) scoreLabel.isHidden = true cluesLabel.isHidden = true answersLabel.isHidden = true currentAnswer.isHidden = true submit.isHidden = true clear.isHidden = true // Reset level to 0 before calling levelUp level = 0 alertTitle = "Congratulations!" alertMessage = "You have beaten all the current levels!" buttonTitle = "Let's Play Again!" } let ac = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert) ac.addAction(UIAlertAction(title: buttonTitle, style: .default, handler: levelUp)) present(ac, animated: true) } } }
In here I check to see if the level is less than the maxLevel. If it is, I pass the title, message, and button title to go to the next level. If the level is greater than the maxLevel then I change the background color to an image which I had to implicitly unwrap which was safe since I put it there (although I probably should go back and make it safer by using an if let statement). I then hid all the labels and buttons to focus just on the background and dialog box. I finished passing a title, message, and button title. I also set the level to 0. This way when levelUp is called from the alert box it will increment to 1 and start over.

func levelUp(action: UIAlertAction) {
// Make sure level is within the game parameters
if level < maxLevel { // Set background to color and show buttons view.backgroundColor = colorLiteral(red: 0.9678038955, green: 0.8971671462, blue: 0.7053951621, alpha: 1) scoreLabel.isHidden = false cluesLabel.isHidden = false answersLabel.isHidden = false currentAnswer.isHidden = false submit.isHidden = false clear.isHidden = false } // Increment level level += 1 solutions.removeAll(keepingCapacity: true) loadLevel() for btn in letterButtons { btn.isHidden = false } }

In the levelUp function I again checked whether the level is less than the maxLevel. This time I set the background back to a color and make all the labels and buttons visible again to play the level.

While these changes don’t really do much for the game play itself, it does make a little nicer user experience by letting them know when the game is finished.

The funny thing about this particular project, my wife and kids saw this on the simulator, and I had a hard time getting my computer back to continue working as they wanted to play the game. I ended up installing it on the family’s iPad just so I could continue programming. I guess that’s the sign of a useful application.

Share this Article

Posted by Jeff Summers

Author, technologist, and baseball aficionado specializing in information technology and developing new and creative ways to interact with the world around us. My goal is to extend the boundaries of what is possible and find ways to make the world a better place while having fun.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Stay Connected

Search

More Articles

Pace Layered Architecture Categorization

Change is inevitable. Today’s computing environments are constantly in flux from internal and external forces that put demands on the systems and necessitate changes to maintain its value stream. Technology currency is a constant struggle that organizations must...

TBM, Another Holy Grail?

Technology Business Management or TBM as a term was coined around 2012 although many of the concepts making up this framework have been around for almost as long as there has been IT. In its simplest form, TBM represents the integration of business, technology, and...

Visionary or Dreamer?

Thinking back over your life you’ve met and worked with countless personality types. You have been a part of teams that struggled to complete simple tasks and the work felt meaningless. Perhaps your goal in these times was to just trudge through the mud hoping the...

Archives