1

I am trying to develop a card game where 8 of the dealer's and player's cards show face down originally. When either the player or dealer are hit or dealt a card, I want to "flip" the card face up to reveal the player's or dealer's face down card.

I have attempted in SwiftUI using @ObservableObject and .environmentObject to pass the instances around globally. At one point, I had it working on a primitive nature but I cannot remember how I did it. I am fairly new to SwiftUI;

@State var dealerHand: [String]
@State var playerHand: [String]
@State var dealerHasCard:[Bool]
@State var playerHasCard:[Bool]
    var body: some View {
        HStack(spacing: -30.0) {
            Spacer()
            Image(dealerHasCard[0] ? "\($dealerHand[0])" : faceDown)
            Image(dealerHasCard[1] ? "\($dealerHand[1])" : faceDown)
            Image(dealerHasCard[2] ? "\($dealerHand[2])" : faceDown)
            Image(dealerHasCard[3] ? "\($dealerHand[3])" : faceDown)
            Image(dealerHasCard[4] ? "\($dealerHand[4])" : faceDown)
            Image(dealerHasCard[5] ? "\($dealerHand[5])" : faceDown)
            Image(dealerHasCard[6] ? "\($dealerHand[6])" : faceDown)
            Image(dealerHasCard[7] ? "\($dealerHand[7])" : faceDown)
            Spacer()
        }
        Text("You")
            .font(.title)
            .foregroundColor(.blue)

        HStack(spacing: -30.0) {
            Spacer()
            Image(playerHasCard[0] ? "\($playerHand[0])" : faceDown)
            Image(playerHasCard[1] ? "\($playerHand[1])" : faceDown)
            Image(playerHasCard[2] ? "\($playerHand[2])" : faceDown)
            Image(playerHasCard[3] ? "\($playerHand[3])" : faceDown)
            Image(playerHasCard[4] ? "\($playerHand[4])" : faceDown)
            Image(playerHasCard[5] ? "\($playerHand[5])" : faceDown)
            Image(playerHasCard[6] ? "\($playerHand[6])" : faceDown)
            Image(playerHasCard[7] ? "\($playerHand[7])" : faceDown)
            Spacer()
        }
    }
}
func showCards(dealerHand: [String], playerHand: [String], dealerHasCards: [Bool], playerHasCards: [Bool]) -> ([String], [String], [Bool], [Bool]) {

    var dealerCards:[String] = ["","","","","","","",""]
    var playerCards:[String] = ["","","","","","","",""]
    var dealerHasCards:[Bool] = [false,false,false,false,false,false,false,false]
    var playerHasCards:[Bool] = [false,false,false,false,false,false,false,false]
    
    for i in 0..<dealerHand.count {
        if dealerHand[i] != "" {
            dealerCards[i] = dealerHand[i]
            dealerHasCards[i] = true
        } else {
            dealerCards[i] = faceDown
            dealerHasCards[i] = false
        }
     }

    for i in 0..<playerHand.count {
        if playerHand[i] != "" {
            playerCards[i] = playerHand[i]
            playerHasCards[i] = true
        } else {
            playerCards[i] = faceDown
            playerHasCards[i] = false
        }
     }
   
    if gameOver {
        message = checkResults(playerScore: playerScore, dealerScore: dealerScore)
    }
    
    return (dealerCards, playerCards, dealerHasCards, playerHasCards)
}
  • 1
    `...I want to "flip" the card face up to reveal the player's or dealer's face down card.` where do you want to do this, and where is the code for it. What is `func showCards(...)` for, where do you use it in your view? Show a minimal reproducible code that we can compile and run, see: [minimal code](https://stackoverflow.com/help/minimal-reproducible-example) – workingdog support Ukraine Mar 13 '23 at 03:31
  • I think I have added the necessary structs for minimal code except for creating the project itself. – user3487784 Mar 15 '23 at 16:28

0 Answers0