You have a basic understanding of arrays, state, views, images, text, and more, so let’s put them together: your challenge is to make a brain training game that challenges players to win or lose at rock, paper, scissors.
So, very roughly:
So, if the app chose “Rock” and “Win” the player would need to choose “Paper”, but if the app chose “Rock” and “Lose” the player would need to choose “Scissors”.
Hacking with Swift+ subscribers can get a complete video solution for this checkpoint here: Solution to Rock, Paper, Scissors. If you don’t already subscribe, you can start a free trial today.
To solve this challenge you’ll need to draw on skills you learned in tutorials 1 and 2:
@Stateproperties to store the app’s current choice and whether the player should win or lose.
Int.random(in:)to select a random move. You can use it for whether the player should win too if you want, but there’s an even easier choice:
Bool.random()is randomly true or false. After the initial value, use
toggle()between rounds so it’s always changing.
VStackshowing the player’s score, the app’s move, and whether the player should win or lose. You can use
if shouldWinto return one of two different text views.
font()modifier to adjust the size of your text. If you’re using emoji for the three moves, they also scale. Tip: You can ask for very large system fonts using
.font(.system(size: 200))– they’ll be a fixed size, but at least you can make sure they are nice and big!
I’m going to provide some hints below, but I suggest you try to complete as much of the challenge as you can before reading them.
["Rock", "Paper", "Scissors"]your array of winning moves would be
["Paper", "Scissors", "Rock"].
This should be a fun exercise: there’s a little bit of Swift, a little bit of SwiftUI, and a little bit of logic, all wrapped up in a game where you can really go to town on theming if you want to.
SPONSORED Thorough mobile testing hasn’t been efficient testing. With Waldo Sessions, it can be! Test early, test often, test directly in your browser and share the replay with your team.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.