본문 바로가기
IT/swift

100 days of SwiftUI - Day25

by 가능성1g 2024. 11. 21.
반응형

정리의 시간과 마지막 도전!

가위바위보 게임이지만 승리를 정해놓고 가위바위보를 내기!!

/*
* 
So, very roughly:

Each turn of the game the app will randomly pick either rock, paper, or scissors.
Each turn the app will alternate between prompting the player to win or lose.
The player must then tap the correct move to win or lose the game.
If they are correct they score a point; otherwise they lose a point.
The game ends after 10 questions, at which point their score is shown.

*/

//
//  ContentView.swift
//  RockScissors
//
//  Created by HanTJ on 11/21/24.
//

import SwiftUI

struct ContentView: View {
    let RPS = ["가위","바위","보"]
    let WL  = ["이겨라","져라"]
    
    @State private var gameCount = 1
    @State private var indexRPS = Int.random(in: 0...2)
    @State private var indexWL = Int.random(in: 0...1)
    
    @State private var Score = 0
    
    var body: some View {
        VStack {
            Text("라운드: \(gameCount)")
                .font(.largeTitle)
            Text(RPS[indexRPS])
                .font(.title)
            Text(WL[indexWL])
                .font(.title)
            HStack{
                Button("가위"){
                    judege("가위")
                }
                Spacer()
                Button("바위"){
                    judege("바위")
                }
                Spacer()
                Button("보"){
                    judege("보")
                }
            }
            .padding(50)
            
            if gameCount >= 10 {
                Text("Score: \(Score)")
            }
        }
        .padding()
    }
    
    func judege(_ value:String){
        
        if WL[indexWL] == "이겨라" {
            
            switch RPS[indexRPS] {
            case "가위" where value == "바위": Score+=1
            case "바위" where value == "보" : Score+=1
            case "보"  where value == "가위": Score+=1
            default:
                break
            }
        } else if WL[indexWL] == "져라" {
            
            switch RPS[indexRPS] {
            case "가위" where value == "보": Score+=1
            case "바위" where value == "가위" : Score+=1
            case "보"  where value == "바위": Score+=1
            default:
                break
            }
        }
        
        gameCount+=1
        if gameCount > 10 {
            gameCount = 1
            Score = 0
        }
        indexWL = Int.random(in: 0...1)
        indexRPS = Int.random(in: 0...2)
    }
}

#Preview {
    ContentView()
}

가위바위보 체크 로직은 바꿀수 있는 아이디어가 떠올랐지만 이대로 궈궈!

반응형