22: project 2, part three

그루두·2024년 5월 2일
0

100 days of SwiftUI

목록 보기
31/108

100 days of swiftui: 22
https://www.hackingwithswift.com/100/swiftui/22

challenege

Here are three ways you should try extending this app to make sure you fully understand what’s going on:

  1. Add an @State property to store the user’s score, modify it when they get an answer right or wrong, then display it in the alert and in the score label.
  2. When someone chooses the wrong flag, tell them their mistake in your alert message – something like “Wrong! That’s the flag of France,” for example.
  3. Make the game show only 8 questions, at which point they see a final alert judging their score and can restart the game.

solution

  1. 점수 설정하고 화면에 나타내기
    @State private var score = 0
	// ...
    func guessFlag(selectedAnswer: Int) {
        if selectedAnswer == selectedCountry {
            result = "Correct"
            score += 10
        } else {
            result = "Wrong"
            score -= 10
        }
        showingScore = true
    }

코드 파일
https://github.com/treesofgroo/Ios-GuessFlags/commit/0f845c68de270a3a108eb1392a8764eb8309e9a9

  1. 깃발을 선택했을 때 답안 메시지 보이기
    func guessFlag(selectedAnswer: Int) {
        if selectedAnswer == selectedCountry {
            result = "Correct! It's the flag of \(countries[selectedAnswer])"
            score += 10
        } else {
            result = "Wrong! That's the flag of \(countries[selectedAnswer])"
            score -= 10
        }
        showingScore = true
    }

코드 파일
https://github.com/treesofgroo/Ios-GuessFlags/commit/41d409b3b57bcb76b29d911f740b1dcf3afdc931

  1. 8개의 문제이 끝난 후 새로 게임을 세팅하기
  • 현재 문제의 순서를 나타내는 questionStatus 설정하기
  • 새 문제로 세팅할 때 위 변수 1 증가시키기
  • 8개의 문제가 끝났을 때 위 변수 1로 세팅하고 점수도 초기화하기, 메시지에서 total score 보여주기

코드 파일
https://github.com/treesofgroo/Ios-GuessFlags/commit/42ac001cebd80a7004a3df97930d4087f28584c9

오타 수정 (최종)
https://github.com/treesofgroo/Ios-GuessFlags/commit/19baa4a5b0f044f2c03b49480841888cabb7cf54

완성 화면

첫 문제, 문제 중, 마지막 문제 후

profile
계속 해보자

0개의 댓글

관련 채용 정보