2013-01-17 4 views
0

나는이 문제에 관해 모든 사람을 계속 괴롭히는 것처럼 느껴진다. 그래서 '이 사람'을 다시 미안하다.다차원 배열을 통과 함

내 4 개의 연속 게임이 이제 4 개의 행을 확인해야하는 지점까지 작동합니다. X와 O가 매력과 같은 열에 추가되고 그리드가 올바르게 표시됩니다. 요점은 지금은 내 다차원 배열의 행에서 네를 확인하려는 것입니다,하지만이 작성해야하는 것처럼 진술하는 경우 사용하는 것은 정말 나에게 갈 길을하지 않는 것 :

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 그리드 (Foreach-Object for 루프 사용)를 빠르게 수행 할 수 있습니까? 그렇다면 기본 예제를 제공 할 수 있습니까?

감사

답변

1

이 PowerShell은 질문보다 알고리즘 문제의 더 (그 중요한 경우 연속 4 회 문자열 "X"또는 "O"를 찾기 위해 노력하고있다). :-) 즉, 간단한 접근 방식은 다음과 같을 것입니다.

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; 
     } 
    } 
} 

방금 ​​입력 했으니까요. 그것은 약간의 오류가있을 수 있지만 그것은 당신에게 사물의 기본적인 요지를 제공해야합니다.

+0

내 경우, 배열이 $ CreateBoard 일 경우 $ CreateBoard [1,0]과 $ StartCol을 $ CreateBoard [0,1]로 바꿉니 까? 다른 변수들도 마찬가지입니다. 귀하의 의견을 보내 주셔서 감사 드리며,이를 위해 노력하겠습니다! –

+0

1 시간 정도 꼼꼼하게 살펴 보았지만 제대로 작동하지는 않습니다. 그러나 그것은 저의 기본적인 지식 일 수 있습니다. –

+0

다시 시도하십시오. 스크립트를 수정하여 실제로 실행할 기회를 얻었습니다. :-) –