swiftui

Swift array.append() not adding element


Working on a simple card game. Trying make the logic work to "Draw" a card from the deck and add it to the players hand.

Here is the relevant code snippets.

import Foundation

struct Player: Identifiable {
    var id: UUID = UUID()
    var name: String
    var cards: [Card] = []
    var plays: [Plays] = []
    let human: Bool
    
    init(name: String, human: Bool) {
        self.name = name
        self.human = human
    }
    
    mutating func addCard(card: Card)
    {
        print("Adding card \(card.id) to the players cards")
        cards.append(card)
        print("Player now has \(cards.count) cards")
    }
    
}

class Game: ObservableObject {
    @Published var deck:Deck = Deck()
    @Published var discard: [Card] = []
    @Published var players: [Player]
    @Published var round: Int = 1
    
    init(){
        players = []
        players.append(Player(name: "Player 1", human: false))
        players.append(Player(name: "Player 2", human: false))
        players.append(Player(name: "Player 3", human: false))
        players.append(Player(name: "Player 4", human: true))
        //self.deal()
    }
    
    func deal() {
        if players[0].cards.isEmpty {
            for _ in 1...7 {
                players[0].addCard(card: deck.deal())
                players[1].addCard(card: deck.deal())
                players[2].addCard(card: deck.deal())
                players[3].addCard(card: deck.deal())
            }
            print("All the cards are dealt and player 4 now has \(players[3].cards.count) cards")
        } else {
            print("Players already have 7 cards")
        }
    }
    
    
    func discard(card: Card, player: Player) {
        var selectedPlayer = players.first(where: { $0.id == player.id })
        selectedPlayer?.cards.removeAll(where: { $0.id == card.id})
        discard.append(card)
    }
    
    func draw(player: Player){
        var selectedPlayer = players.first(where: { $0.id == player.id })
        let drawnCard = deck.deal()
        print("They drew the \(drawnCard.rank.name) of \(drawnCard.suit.name) with ID \(drawnCard.id)")
        selectedPlayer?.addCard(card: drawnCard)
        
    }
    
}

What I can't understand is the when I call the player.addCard(card: card) from inside the deal() function on the Game class, it works perfectly and adds the cards like expected. But calling the same func from inside the draw func (towards the bottom of the game class) doesn't actually add the card to the players hand.

I can see from the print statements inside the addCard func that it's getting called, and that the ID of the card it's passing is correct (and different for each tapGesture that initates it).

Any ideas why the array.append() isn't actually adding to the array?


Solution

  • try this approach using firstIndex and using the players[index] directly, as shown in the code example

    func draw(player: Player){
        if let index = players.firstIndex(where: { $0.id == player.id }) {
            let drawnCard = deck.deal()
            print("They drew the \(drawnCard.rank.name) of \(drawnCard.suit.name) with ID \(drawnCard.id)")
            players[index].addCard(card: drawnCard)
        }
    }