Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:26:22+00:00 2026-06-17T11:26:22+00:00

I feel like I keep bugging everyone about this, so sorry for ‘this guy

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:26:23+00:00Added an answer on June 17, 2026 at 11:26 am

    This is more of an algorithm question than a PowerShell question. 🙂 That said, one simple approach would be something like this:

    function FindFourInARow($brd, $startRow, $startCol)
    {
        $numRows = $brd.GetLength(0);
        $numCols = $brd.GetLength(0);
    
        #search horizontal
        $found = 0;
        $cnt  = 0;
        for ($col = $startCol; $col -lt $numCols -and $cnt -lt 4; $col++, $cnt++)
        {
            if ($brd[$startRow, $col] -eq 'X')
            {
                $found += 1
                if ($found -eq 4) { return $true }
            }
        }
    
        #search vertical
        $found = 0;
        $cnt = 0;
        for ($row = $startRow; $row -lt $numRows -and $cnt -lt 4; $row++, $cnt++)
        {
            if ($brd[$row, $startCol] -eq 'X')
            {
                $found += 1
                if ($found -eq 4) { return $true }
            }
        }
    
        #search diag forwards
        $found = 0;
        $row = $startRow
        $col = $startCol
        $cnt = 0;
        for (;$row -lt $numRows -and $col -lt $numCols -and $cnt -lt 4; 
              $row++, $col++, $cnt++)
        {
            if ($brd[$row, $col] -eq 'X')
            {
                $found += 1
                if ($found -eq 4) { return $true }
            }
        }
    
        return $false
    }
    
    # TODO: implement search diag backwards
    
    $brd = New-Object 'char[,]' 10,10
    $brd[2,2] = $brd[3,3] = $brd[4,4] = $brd[5,5] = 'X'
    
    $numRows = 10
    $numCols = 10
    for ($r = 0; $r -lt ($numRows - 3); $r++)
    {
        for ($c = 0; $c -lt ($numCols - 3); $c++)
        {
            if (FindFourInARow $brd $r $c)
            {
                "Found four in a row at $r,$c"
                $r = $numRows
                break;
            }
        }
    }
    

    I’ve just typed this directly into SO. It’s likely got some errors but it should give you the basic gist of things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have scoured the internet for this answer, and I feel like I keep
I feel like I'm asking a lot of questions, but I keep getting stuck.
I feel like I am not writing this correctly and this is my first
I feel like such a noob asking this but, for some reason a horizontal
I feel like I've only ever seen this here on SO, but I can't
I feel like there's a pretty simple way to do this, but I'm not
I feel like this is something I should already know, but I'm just not
I'm feeling very, very stupid right now... I feel like I must be missing
I feel like this question ought to have been covered, but I can't seem
I asked this as part of another question but feel like it should have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.