Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to set up a customizable score card for a card game?

So I'm working on an app called "Cardgame Ref," and it is basically a "one place" app to look up rules and resources and keep score in several different card games. Sure, there are apps out there like this already, but mine is mainly for my family (but I'm going to distribute it anyway haha).

The way I have it set up now is with a navigation controller and a "home screen" with three buttons: "Scorecards," "Rules," and "Resources" - each of which have their own hierarchy. I already have the rules and resources set up with web views containing PDFs and even printing capabilities for these PDFs.

I'm struggling with the score cards. I want to set up a settings page for the score cards and allow the user to set game-specific rules. For example, from the home screen the user taps "Scorecards," -> "Spades" -> and then an empty scorecard. I want a button in the right area of the nav bar where the user can change settings/rules such as number of players, player names, number of points to win, bid variations, point bonuses, etc. then save these settings and pass all the data back to the score card. I also want another button in the score card that brings up another view that allows the user to tally up the points in a round and adds those points to the score card when the user clicks "Done."

One of these days, it would be cool to add the functionality to save and load score cards (either in progress or complete) and even offer in-app purchases for other games. This app really is trying to be the ONE app for being the ref in a card game - hence the app's name.

So my question is: how should I set this up and what about some tips for coding? I'm still new to programming in Swift, but I know about some major concepts such as classes, structs, functions, collections, etc. For this app, I already have everything else set up - the layout, graphics, and rules. These score cards are the ONLY thing left I have to build in order to call this app complete, so any advice is greatly appreciated!

To actually see an example of what I'm trying to set up, go to the App Store and search for a hearts scoreboard. The icon is lime green with red cards on it. This app is the style that I'm trying to go for - but in a more elegant way.

1 Answer

Hi Andrew,

Sounds like a cool app!

Let me know if I'm misunderstanding, but it sounds like you're struggling with how to store data / settings, more than how to implement the UI and logic you need for the app. If that's the case, this app seems like it will possibly have a simple enough data structure that you could save these settings in NSUserDefaults. You should read the Apple documentation for them, and also google around for some tutorials.

They're actually pretty easy to use. For example, you might have some code that runs when a user selects the number of players that looks like this:

setSettings.swift
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(4, forKey: "numberOfPlayers")
defaults.synchronize()

Then later, you could access them in some other part of your code like this:

getSettings.swift
let defaults = NSUserDefaults.standardUserDefaults()

if let players = defaults.integerForKey("numberOfPlayers") {
    // do some game stuff
}

If you have questions, feel free to respond here, or post another question in the forum once you've had a chance to play around with them.

If I'm completely missing your question, let me know :).

Cheers,

Greg

Hi there! Thanks for answering!

I actually figured out a way to solve the problem the number of players issue through the UI. I've taken some screenshots of how the app flows and what I want to accomplish with the scorecards:

App: https://imageshack.com/i/paXtjA2Lj Scorecard Setup: https://imageshack.com/i/p5bCbUs6p

I'm sure I can get the logic implemented. So my problem is passing the data from one view to another. How do I pass the text from the text fields in the Settings pane to change the players' names on the scorecards? How do I add a popup alert when the score reaches the value of the slider in the Settings pane? And how do disable the buttons on the new deal page after the points on that page total to a certain value?

Basically, there is no user input on the scorecard itself. All the input comes from the settings page (which sets the rules) and the new deal page that tallies up the score for that round.

Would a "prepareForSegue" function help with this?

Lastly, can you combine multiple view controllers into a single swift file and just make separate classes within the file? If not, we're looking at about 50 view controllers and 50 swift files. Surely, there's a simpler way.

I apologize for asking so many things. For all I know, there's a simple solution and I always tend to think too hard. Any help is always appreciated, even if there's a course on Treehouse that can help me solve my own problems. :)

Hi Andrew,

Yes, prepareForSegue is going to be your new best friend. I would strongly recommend the course Build a Playlist Browser with Swift, which covers managing multiple view controllers at length.

The basic idea is that you have a couple view controllers (in your case, a settings VC and a game VC), and the content is changed programmatically. This works so long as your layout is the same (or at least close) between different versions of these screens (e.g. the hearts game and spades have different content, but shown in the same place).

Hope this makes sense!

-Greg