I am defining a function to switch from Player “X” to player “O”. When I run this little block of code without the function, it gives me back an X. When I run this with a function defined it returns as O. What makes the difference between running it normally and running it from a Function ?
$playgame = "True"
$player = "O"
#Function
Switch-play
Write-host $player
#Switch Player turn
Function Switch-Play{
if ($playgame = "True") {
if ($player -eq "X") {$player = "O"}
else {$player = "X"}
}
}
Thanks
EDIT: At first I was in doubt about defining the variables as $script:player, but that didn’t really solve anything.
EDIT: changing to Switch-Play rather than Switch-play
PS C:\Users\scout> $playgame = "True"
$player = "O"
$player
Switch-Play
$player
Switch-Play
$player
#Switch Player turn
Function Switch-Play{
if ($playgame = "True") {
if ($player -eq "X") {$player = "O"}
else {$player = "X"}
}
}
O
O
O
Variable Scope issue here. Change function like this:
Reading about scope: http://technet.microsoft.com/en-us/library/hh847849.aspx