UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

DAY 25. Challenge.

Forums > 100 Days of SwiftUI

Hi! How I can remove the Picker into sort of setting menu, maybe how to create such a settings menu?

How to do so, that user by himself can assign picture for each of units. From precomposed options? In the beginning I thought to create a dictionary, like this; but didn't succeded to implement it. Also, when I tried ForEach on dictionary, it didn't work. Why?

var emojis = ["Rock": "πŸ‘Š", "Paper": "🀚","Scissors": "✌️"]

How to make the emoji larger. If there's other option besides adjusting Text's font?

How I can do an (poping up and fading) animation "+1" near the score label when winning the round?

How I can do a menu for if I want to add more games in the app?

Can you look at determineVictory() function, if it's implemented ok?

Can you recommend some good course(/source) to learn how to work with gitHub? So, I can upload code there instead of here.

Answer questions that you've liked. Thanks!!

var emojisTheme1 = ["πŸ‘Š", "🀚", "✌️"]
var emojisTheme2 = ["πŸͺ¨", "πŸ—ž", "βœ‚οΈ"]
var emojisTheme3 = ["πŸ—»","πŸ“œ","βœ„"]

import SwiftUI

struct ContentView: View {
    @Environment(\.colorScheme) var colorScheme
    @State private var gameStyles = [emojisTheme1, emojisTheme2, emojisTheme3]
    @State private var gameStyle = 1
    private var moves: [String] {
        return gameStyles[gameStyle]
    }
    @State private var randomMove = Int.random(in: 0...2)
    @State private var shouldWin = Bool.random()
    @State private var playerScore = 0
    private var numberOfRounds = 10
    @State private var currentRound = 0

    @State private var showingRulesAlert = true

    var body: some View {
        VStack {

            // Picker for choosing game theme
            Section (header: Text("Choose the game theme")){
                Picker("Choose Game theme", selection: $gameStyle) {
                                ForEach (0 ..< gameStyles.count) {
                                    Text("\(gameStyles[$0][0])")
                                }
                }.pickerStyle(SegmentedPickerStyle())
                .background(Color.gray)
            }
            Spacer()

            Text("Score: \(playerScore)")
                .padding()

            // Shows computers' move. When game ends, shows only Robot icon
            ZStack {
                Text("πŸ€–")
                    .offset(x: 25, y: 25)
                    .font(.body)
                    .opacity(currentRound < numberOfRounds ? 1 : 0)
                Text(currentRound < numberOfRounds ? moves[randomMove] : "πŸ€–")
                    .font(.largeTitle)
                    .frame(width: 100, height: 100)
                    .foregroundColor(colorScheme == .dark ? .white : .black)
                Text(currentRound < numberOfRounds ? "" : "πŸ€™")
                    .font(.title2)
                    .offset(x: -17, y: 10)

            } .padding(20)

            // If the game ended, hides the prompt and shows play again button
            if currentRound < numberOfRounds {
                HStack {
                    Text("You should")
                    Text(shouldWin ? "win" : "lose")
                        .foregroundColor(shouldWin ? .green : .red)
                }

            } else {
                HStack {
                    Text("New Game")
                    Button(action: {
                        currentRound = 0
                        playerScore = 0
                    }, label: {
                        Image(systemName: "arrow.clockwise")
                    })
                }
            }

            HStack(spacing: 45) {
                ForEach(0 ..< 3) { number in
                    Button(action: {
                        // checking victory, shuffling computers' move and prompt, updating round counter
                        determineVictory(userMove: number, computerMove: moves[randomMove], shouldWin)
                        shouldWin = Bool.random()
                        randomMove = Int.random(in: 0...2)
                        currentRound += 1

                    }, label: {
                        Text(moves[number])
                            .foregroundColor(colorScheme == .dark ? .white : .black)
                    })

                }
            }
                .font(.largeTitle)
                .disabled(currentRound == numberOfRounds ? true : false)
                .opacity(currentRound == numberOfRounds ? 0.3 : 1)
                .padding(100)
        }
        // Alert with rules of the game
        .alert(isPresented: $showingRulesAlert, content: {
            Alert(title: Text("πŸ€–: Hi there!"), message: Text("My name is Robbie, I am a small transistor on your telephone CPU chip.\n Let's play 'Rock, Paper, Scissors'\n In each round I'll give you a promt with your goal for the round."), dismissButton: .default(Text("Got it!")))

        })
    }

    func determineVictory(userMove: Int, computerMove: String, _ shouldWin: Bool) {
        var userWins: Bool

        // moves = ["Rock", "Paper", "Scissors"]
        if (userMove == 1 && computerMove == moves[0]) ||
            (userMove == 2 && computerMove == moves[1]) ||
            (userMove == 0 && computerMove == moves[2]) {

            userWins = true
        } else {
            userWins = false
        }

        if (!shouldWin && userWins == false) ||
            (shouldWin && userWins == true) {
            userWins = true
            playerScore += 1
        } else {
            userWins = false
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I found this thread vey interesting : https://www.hackingwithswift.com/forums/100-days-of-swiftui/can-t-find-mistake-in-day-25-project/784/787

3      

You're on day 25. Over time you will have answer to most if not all of your questions. Also, all of your questions have answers online.

Tip: Get comfortable searching and finding answers without asking others. This is probably the most important skill for a developer.

1- Settings Menu: You want to look at "Form".

2- Emoji is characters as in text characters. That's why you increase the size with font modifiers.

3- While you are asking for a simple animation. You are still on day 25. You will get to learn more about animations. Otherwise, how to animate is a question that has many many answers online. Pick the ones you prefer.

4- How to do a menu... again... keep going through the course. The point is to learn how to do this.

5- determineVictory works. That's step one. The challenge is then for you to try and make it better. That's up to you.

6- Github is the best place to learn about github. They have a tutorial section there.

My answers are simple. On purpose. Use the above, or even your questions on google, and you will get tons of great articles and resources that answer all your questions in depth.

MOST important: Give it time. Don't try to skip ahead. You are still on day 25.

6      

@MarcusKay thank you very much for response!

https://lab.github.com/ here's a link to learning lab on GitHub. Didn't know they have such a thing. Thanks!

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.