Created
April 23, 2022 11:46
-
-
Save aidiary/0f0d8b59aed9296fd04b1705701af523 to your computer and use it in GitHub Desktop.
2021 SwiftUI Tutorial for Beginners (3.5 hour Masterclass)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// war-challenge | |
// | |
// Created by Koichiro Mori on 2022/04/20. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
@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() | |
Image(playerCard) | |
Spacer() | |
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 cpuRand > playerRand { | |
cpuScore += 1 | |
} | |
}, label: { | |
Image("dealbutton") | |
}) | |
Spacer() | |
HStack { | |
Spacer() | |
VStack { | |
Text("Player") | |
.font(.headline) | |
.foregroundColor(Color.white) | |
.padding(.bottom, 10.0) | |
Text(String(playerScore)) | |
.font(.largeTitle) | |
.foregroundColor(Color.white) | |
} | |
Spacer() | |
VStack { | |
Text("CPU") | |
.font(.headline) | |
.foregroundColor(Color.white) | |
.padding(.bottom, 10.0) | |
Text(String(cpuScore)) | |
.font(.largeTitle) | |
.foregroundColor(Color.white) | |
} | |
Spacer() | |
} | |
Spacer() | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment