Hello, can anyone please help me understand why sometimes this code makes 1 or 2 cards get dealt, the code is copied from the war card game and it should (as far as i understand always deal 3 cards, i mean why would it mostly deal 3 and sometimes “decide” to deal 2 or 1?
code:
struct ContentView: View {
@State private var card1 = "card14"
@State private var card2 = "card14"
@State private var card3 = "card14"
@State var bet = 0
@State var win = 0
var body: some View {
ZStack {
Image ("background").ignoresSafeArea()
VStack {
Group{
Image("ptc").resizable().aspectRatio(contentMode: .fit).frame(width: 300.0, height: 70.0)
HStack {
Image(card1)
Image(card2)
Image(card3)
}
HStack {
Spacer()
VStack {
Text("Bet")
.font(.headline)
.foregroundColor(Color.white)
Text(String(bet))
.font(.largeTitle)
.foregroundColor(Color.white)
}
Spacer()
VStack {
Text("Win")
.font(.headline)
.foregroundColor(Color.white)
Text(String(win))
.font(.largeTitle)
.foregroundColor(Color.white)
}
Spacer()
}
}
Group{
Image("numbers").resizable().frame(width: 400.0, height: 180.0)
Text("Pairs +")
Button(action:{
// Generate a random number between 2 and 14
let card1Rand = Int.random(in: 2...14)
let card2Rand = Int.random(in: 2...14)
let card3Rand = Int.random(in: 2...14)
// Update cards
card1 = "card" + String(card1Rand)
card2 = "card" + String(card2Rand)
card3 = "card" + String(card3Rand)
}, label: {Image("deal_button").resizable().frame(width: 150.0, height: 70.0)
})
}
HStack {
Group{
Button(action:{}, label: {Image("25_chip").resizable().frame(width: 80.0, height: 90.0)
})
Button(action:{}, label: {Image("100_chip").resizable().frame(width: 80.0, height: 90.0)
})
Button(action:{}, label: {Image("500_chip").resizable().frame(width: 80.0, height: 90.0)
})
Button(action:{}, label: {Image("1k_chip").resizable().frame(width: 80.0, height: 90.0)
})
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}