슈뢰딩거의 고등어

(11일차, 12일차) 14일 만에 IOS 앱 끝내기 챌린지 : State Properties, If 조건문 본문

14일만에 IOS 앱 끝내기 챌린지

(11일차, 12일차) 14일 만에 IOS 앱 끝내기 챌린지 : State Properties, If 조건문

슈뢰딩거의 고등어 2023. 3. 4. 16:15

https://youtu.be/2oT32JUH1s0

 

https://youtu.be/kwz9-EjL7dY

Deal 버튼을 누를때마다 카드들이 랜덤으로 변경되고, 카드를 비교하여 점수를 주는 것을 확인 할 수 있다.

//
//  ContentView.swift
//  war challenge
//
//  Created by jhmin on 2023/02/26.
//

import SwiftUI

struct ContentView: View {
    
    // Initiate state properties
    // private : Since It's only this content view that depends on these state properties
    @State private var playerCard = "card5"
    @State private var cpuCard = "card9"
    @State private var playerScore = 0
    @State private var cpuScore = 0
    
    var body: some View {
        ZStack {
            Image("background").ignoresSafeArea()
            VStack {
                Spacer()
                Image("logo")
                Spacer()
                
                HStack {
                    Spacer()
                    // use variable instead of text
                    Image(playerCard)
                    
                    Spacer()
                    // use variable instead of text
                    Image(cpuCard)
                    Spacer()
                }
                
                Spacer()
                Button(action: {
                    // Generate a random number between 2 and 14
                    let playerRand = Int.random(in: 2...14)
                    let cpuRand = Int.random(in: 2...14)
                    
                    // Update the cards
                    playerCard = "card" + String(playerRand)
                    cpuCard = "card" + String(cpuRand)
                    
                    // Update the score
                    if(playerRand > cpuRand) {
                        playerScore += 1
                    }
                    else if(playerRand < cpuRand) {
                        cpuScore += 1
                    }
                    
                }, label: {
                    Image("dealbutton")
                })
                Spacer()
                
                HStack {
                    Spacer()
                    
                    VStack {
                        Text("Player")
                            .font(.headline)
                            .foregroundColor(Color.white)
                            .padding(.bottom, 10.0)
                        // change into String
                        Text(String(playerScore))
                            .font(.largeTitle)
                            .padding()
                            .fontWeight(.bold)
                            .foregroundColor(Color.white)
                    }
                    Spacer()
                    
                    VStack {
                        Text("CPU")
                            .font(.headline)
                            .foregroundColor(Color.white)
                            .padding(.bottom, 10.0)
                        // change into String
                        Text(String(cpuScore))
                            .font(.largeTitle)
                            .padding()
                            .fontWeight(.bold)
                            .foregroundColor(Color.white)
                    }
                    Spacer()
                }
                Spacer()
            }
        }
    }
}

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

(12일차) IF

let a = 1
let b = 2
let c = 3
let d = "hello"
let e = false
let f = true
let g = true

// && : and
// || : or
if (f || e) && g {
    print("Hello")
}
else if e {
    print("e is true")
}
else if g {
    print("g is true")
}
else {
    print("Catch All")
}
Comments