首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >遍历多维数组

遍历多维数组
EN

Stack Overflow用户
提问于 2013-01-18 02:19:53
回答 1查看 1.1K关注 0票数 0

我觉得我一直在烦每个人,真为“这家伙又来了”感到抱歉。

我的四连胜游戏现在可以工作了,直到需要检查是否有四连胜为止。X和O像符号式地添加到列中,并且网格显示正确。现在的重点是,我想在多维数组中检查行中的4,但是使用if语句似乎不是我真正想要的方法,因为我必须这样写:

代码语言:javascript
复制
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!"} 

对于每一列,然后是行,然后是对角线。现在的问题是:有没有一种快速的方法来通过我的10x10网格(我猜是使用for循环的Foreach-Object?),如果有,你能提供一个基本的例子吗?(我尝试在一行中查找字符串"X“或"O”的4倍,如果这很重要的话)

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-18 03:07:50

这更像是一个算法问题,而不是一个PowerShell问题。:-)也就是说,一种简单的方法是这样的:

代码语言:javascript
复制
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;
        }
    }
}

我刚刚在SO中直接输入了这个。它可能有一些错误,但它应该会给你事情的基本要点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14385563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档