I feel like I keep bugging everyone about this, so sorry for ‘this guy again’.
My four in a row game now works, up until the point where it needs to be checked for a four in a row. The X and O get added to columns like a charm and the grid is showing correct. The point now is that I want to check for a four in a row in my multidimensional array, but using if statements doesn’t really seem the way to go to me as I would have to write this:
if ($CreateBoard[1,0] -and $CreateBoard[1,1] -and $CreateBoard[1,2] -and $CreateBoard[1,3] -eq "X") {Write-host "Exit script!"}
if ($CreateBoard[1,1] -and $CreateBoard[1,2] -and $CreateBoard[1,3] -and $CreateBoard[1,4] -eq "X") {Write-host "Exit script!"}
if ($CreateBoard[1,2] -and $CreateBoard[1,3] -and $CreateBoard[1,4] -and $CreateBoard[1,5] -eq "X") {Write-host "Exit script!"}
if ($CreateBoard[1,3] -and $CreateBoard[1,4] -and $CreateBoard[1,5] -and $CreateBoard[1,6] -eq "X") {Write-host "Exit script!"}
if ($CreateBoard[1,4] -and $CreateBoard[1,5] -and $CreateBoard[1,6] -and $CreateBoard[1,7] -eq "X") {Write-host "Exit script!"}
if ($CreateBoard[1,5] -and $CreateBoard[1,6] -and $CreateBoard[1,7] -and $CreateBoard[1,8] -eq "X") {Write-host "Exit script!"}
if ($CreateBoard[1,6] -and $CreateBoard[1,7] -and $CreateBoard[1,8] -and $CreateBoard[1,9] -eq "X") {Write-host "Exit script!"}
if ($CreateBoard[1,7] -and $CreateBoard[1,8] -and $CreateBoard[1,9] -and $CreateBoard[1,10] -eq "X") {Write-host "Exit script!"}
for every single column, then row, and then go diagonal. Question now is: Is there a fast way to go through my 10×10 grid (I guess using Foreach-Object of for loops?), and if yes, could you provide a basic example? (I am trying to find 4 times the string “X” OR “O” in a row if that matters)
Thanks
This is more of an algorithm question than a PowerShell question. 🙂 That said, one simple approach would be something like this:
I’ve just typed this directly into SO. It’s likely got some errors but it should give you the basic gist of things.