admin管理员组

文章数量:1122846

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?

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?

Share Improve this question asked yesterday jesmith17jesmith17 637 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 0

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)
    }
}

Your code doesn't work because Player is a struct (value types) so in your draw function, you actually add card to a copy version of an element in players array (which is selectedPlayer). Answer of @workingdog works because it modify array element directly

本文标签: swiftuiSwift arrayappend() not adding elementStack Overflow