Blackjack Lab: Part 2
Your assumptions are your windows on the world. Scrub them off every once in a while, or the light won't come in. -Isaac Asimov
Overview
In this lab, you will finish the UI for the simple Blackjack simulation you wrote in the previous lab.
Introduction
The UI will have a button the user can press to run a simulation. Once the game simulation has been completed, the UI should be updated to show the winner of the blackjack game.
The UI will look like this:
Open BlackJack2.xcworkspace
to get started.
Directions
The completed source code from the previous lab has been copied to this project, so you won't need to re-implement that. A playGame()
method has been added to the ViewController
class, too. Other than that, the implementation of this app is up to you.
User Interface
The first thing to implement is the user interface. Open up Main.storyboard
in Interface Builder to get started. A view controller has already been added to the storyboard.
- Drag a new text label onto the storyboard. Change the font to Avenir Next Bold, size 42.
- Drag another text label onto the view controller. Put this label below the label you just created. Change the font to Avenir Next Heavy, size 48. Change the text so simply be a question mark (?).
- Add a button to the view controller. Place it a bit below the second label. Change the text in the button to say Play Game.
- You need to create an IB outlet for the second text label, which will let the user know whether the house or player won the simulated game of Blackjack. Open up
ViewController.swift
in an assistant editor. Control-drag an IB outlet from the second text label to theViewController
class. Call itwinnerLabel
. - You also need to create an IB action for the button. Control-drag an IB action from the Play Game button to the
ViewController
class. Name the IB actionplayGamePressed
.
ViewController
Now it's time to implement the code to run the simulation in the ViewController
class. Open ViewController.swift
in Xcode's code editor. Some of ViewController
has been implemented; in particular, a method called playGame()
that will run one game simulation and return the winner of the game.
Here are the parts of ViewController
that you need to finish to run the game:
- Add a property of type
Dealer
calleddealer
. It should be assigned aDealer
instance. - Implement
playGamePressed(_:)
. In this method, you should do the following:- Create a new
Dealer
and assign it to thedealer
property. - Call
playGame()
and get the winner of the simulated game. - Change
winnerLabel
's text to be the name of the winner.
- Create a new
Finishing Up
Your app should now work! Build and run the project. In the simulator, press Play Game to run a game simulation and see the winner!