I'm trying to make a simple blackjack game as a Powershell script and found out that my ArrayList
is not behaving as expected. I want to understand why I get a different answer then expected.
The Write-Host $deck
call inside the function prints out the deck as I expect, 52 objects. So far so good.
However, when calling Write-Host $myDeck
the weird part begins. What it will do is, it will first print 0...51 and then my actual deck. So instead of having 52 objects in my ArrayList
I get 104 (52+52). Can anyone explain what really happens here? Because I find this super confusing.
function Get-Deck(){
$ranks = 2,3,4,5,6,7,8,9,10,"Jack","Queen","King","Ace"
$suits = "Spade","Heart","Diamond","Club"
$deck = [System.Collections.ArrayList]::new()
foreach($rank in $ranks){
foreach($suit in $suits){
$card = $rank , $suit
$deck.Add($card)
}
}
Write-Host $deck #prints out the actual array with 52 cards.
return $deck
}
$myDeck = Get-Deck
Write-Host $myDeck #prints out: 0 1 2 3 4 5 6 7 ... 51 2 Spade 2 Heart 2 Diamond ... Ace Club