I am trying to sink what you said in. Below is how I got my code to work, but maybe it is not the preferred way. Would you suggest another way? Would it make make sence to use write-output along with return? Any suggestions would be appreciated
this is the computer algorithm for a tic tac toe game
# this is the grid pattern used for moves. 4 is the center move
#012
#345
#678
#for testing some values were filled in for the moves
$usedmoves = (0,4,6,8,2)
$computermoves = (0,8,2)
$humanmoves = (6,4)
function computer-move {
$bestmoves = ( 4,0,2,6,8,1,3,7,5 )
$win = ((0,1,2),(0,4,8),(0,3,6),(2,4,6),(2,5,8),(1,4,7),(3,4,5),(6,7,8))
#this first part checks if the computer can go for a win
#if the win is possible it returns that data
foreach ($i in $win){
$check=@()
if ($i[0] -in $computermoves){
$check+=$i[0]
}
if ($i[1] -in $computermoves){
$check+=$i[1]
}
if ($i[2] -in $computermoves){
$check+=$i[2]
}
if ($check.count -eq 2){
foreach ($k in $i){
if (($k -notin $computermoves) -and ($k -notin $usedmoves)){
$result = $k
$result
return
}
}
}
}
#the second most important move is to block any wins.
#this checks for any near wins by the opponent and blocks them
foreach ($i in $win){
$check = @()
if ($i[0] -in $humanmoves){
$check+=$i[0]
}
if ($i[1] -in $humanmoves){
$check+=$i[1]
}
if ($i[2] -in $humanmoves){
$check+=$i[2]
}
if ($check.count -eq 2){
foreach ($k in $i){
if (($k -notin $humanmoves) -and ($k -notin $usedmoves)){
$result = $k
$result
return
}
}
}
}
#if the computer can't win or does not need to block a win it will
#pick its best move by starting with the center, then corners, then sides
foreach ($i in $bestmoves){
if ($i -notin $usedmoves){
$result = $i
$result
return
}
}
}
$a = computer-move
Write-Host $a