2014-03-01 19 views
0

我有一個類似下面的數組。如何在一行中找到三個相同的數字?

int[][] myArray = 
     {{1, 2, 3, 0, 0, 1} 
     {1, 0, 4, 4, 0, 1} 
     {1, 2, 4, 3, 4, 0} 
     {2, 2, 0, 0, 2, 2} 
     {3, 0, 0, 3, 0, 0} 
     {4, 2, 3, 0, 0, 0}} 

它會說因爲第一列中三個1中的1而獲勝。兩個人不會贏,因爲他們不在「排」。

我想做某種勝利檢查,以便它在同一行,對角線或列中找到三個相同的數字。有點像井字遊戲,但有一個更大的網格。在我使用一組混亂的if語句和goto語句之前。 (它是用Basic編寫的。)我嘗試過使用一個系統,它從最後一個放置的部分找到了指示,其中有一些相同的指示,但它沒有正常工作。我怎樣才能以簡單和可維護的方式來做到這一點?

嘗試代碼:

private static boolean checkBoardCombinations(int[][] board, int inputX, int inputY) { 
     int currentPlayer = board[inputX-1][inputY-1]; 
     boolean[][] directions = new boolean[3][3]; 
     for(int y = 0; y >= -2; y--){ 
      for(int x = 0; x >= -2; x--){ 
       if(inputX+x >= 0 && inputX+x <= 7 && inputY+y >= 0 && inputY+y <= 7 
         && (board[inputX+x][inputY+y] == currentPlayer)){ 
        //System.out.printf("X: %s Y: %s", inputX+x, inputY+y); 
        directions[x+2][y+2] = true; 
       } 
       else{ 
        directions[x+2][y+2] = false; 
       } 
       //System.out.printf("X: %s Y: %s B: %s,", inputX+x, inputY+y, directions[x+2][y+2]); 
      } 
      //System.out.println(); 
     } 
     /* 
     for(int x = 0; x <= 2; x++){ 
      for(int y = 0; y <= 2; y++){ 
       System.out.print(directions[x][y] + " "); 
      } 
       System.out.println(); 
     } 
     */ 
     return false; 

    } 
+0

ü應用什麼邏輯或粘貼代碼會更好 – Kick

+0

待辦事項3個相同的值必須是一個接一個,還是可以分散在行,對角線或列? – halex

+0

這些數字的範圍是什麼?他們只是個位數字嗎?或者他們可以有任何價值?或者他們只是0,1,2,3和4? –

回答

1

假設玩家的數量是已知的,您可以遍歷所有的球員一個接一個,並檢查是否有玩家正在形成所需的長度或不連接。

這樣的代碼看起來像以下:

private int[][] grid; // Your array of size ROWS x COLUMNS 
private final int ROWS = 6, COLUMNS = 6; 
private final int CONSECUTIVE_CONNECTION_REQUIRED = 3; 

// Returns true if given playerType is forming a connection, else false. 
public boolean checkGrid(int playerType) 
{ 
    // Check downward 
    for (int i = 0; i <= ROWS - CONSECUTIVE_CONNECTION_REQUIRED; i++) 
    { 
    for (int j = 0; j < COLUMNS; j++) 
    { 
     int counter = 0; 
     for (int k = i; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++) 
     { 
     if (grid[k][j] == playerType) 
      counter++; 
     } 

     if (counter == CONSECUTIVE_CONNECTION_REQUIRED) 
     return true; 
    } 
    } 

    // Check across 
    for (int i = 0; i <= COLUMNS - CONSECUTIVE_CONNECTION_REQUIRED; i++) 
    { 
    for (int j = 0; j < ROWS; j++) 
    { 
     int counter = 0; 
     for (int k = i; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++) 
     { 
     if (grid[j][k] == playerType) 
      counter++; 
     } 

     if (counter == CONSECUTIVE_CONNECTION_REQUIRED) 
     return true; 
    } 
    } 

    // Check left to right diagonally 
    for (int i = 0; i <= ROWS - CONSECUTIVE_CONNECTION_REQUIRED; i++) 
    { 
    for (int j = 0; j <= COLUMNS - CONSECUTIVE_CONNECTION_REQUIRED; j++) 
    { 
     int counter = 0; 
     for (int k = i, m = j; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++, m++) 
     { 
     if (grid[k][m] == playerType) 
      counter++; 
     } 

     if (counter == CONSECUTIVE_CONNECTION_REQUIRED) 
     return true; 
    } 
    } 

    // Check right to left diagonally 
    for (int i = 0; i <= ROWS - CONSECUTIVE_CONNECTION_REQUIRED; i++) 
    { 
    for (int j = COLUMNS - 1; j >= COLUMNS - CONSECUTIVE_CONNECTION_REQUIRED; j--) 
    { 
     int counter = 0; 
     for (int k = i, m = j; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++, m--) 
     { 
     if (grid[k][m] == playerType) 
      counter++; 
     } 

     if (counter == CONSECUTIVE_CONNECTION_REQUIRED) 
     return true; 
    } 
    } 

    return false; 
} 

哪裏playerType爲0,1,2,3等等...

您可以使用checkGrid()方法類似以下內容:

for(int i = MIN_PLAYER_NUMBER; i <= MAX_PLAYER_NUMBER; i++) 
{ 
    if(checkGrid(i)) 
    { 
    // Player i is forming the connection!!! 
    } 
} 

但是,如果你不想迭代你的網格很多次,然後放下你的二維數組,並使用帶有鄰接列表表示的圖。爲此寫一個適當的API,使您可以輕鬆地對特定表示進行更改,然後您可以查找是否有任何播放器在圖中以特定長度連接或不連接。

+0

什麼是鄰接表表示圖? –

+0

@Cammy_the_block:它是一個數據結構。就像數組,列表,地圖,集合,樹木一樣,都有圖形。圖可以用許多方式表示,並且一種這樣的表示是通過使用鄰接表。在互聯網上搜索以瞭解更多信息。 –

1

雖然你已經接受一個答案,我認爲也提交你我的回答對多樣性:)

public static void main (String[] args) 
{ 
    int[][] myArray = 
    {{1, 2, 3, 0, 0, 1}, 
    {1, 0, 4, 4, 0, 1}, 
    {1, 2, 4, 3, 4, 0}, 
    {2, 2, 0, 0, 2, 2}, 
    {3, 0, 0, 3, 0, 0}, 
    {4, 2, 3, 0, 0, 0}}; 
    System.out.println(testForWinner(myArray)); 
} 

/** 
* Returns -1 if no winner 
*/ 
static int testForWinner(int[][] ar) { 
    for(int i=0; i<ar.length; i++) { 
     for(int j=0; j<ar[i].length; j++) { 
      if(checkNext(ar, i, j, 0, 1, 1)) { //Check the element in the next column 
       return ar[i][j]; 
      } 
      for(int k=-1; k<=1; k++) { //Check the three adjacent elements in the next row 
       if(checkNext(ar, i, j, 1, k, 1)) { 
        return ar[i][j]; 
       } 
      } 
     } 
    } 
    return -1; 
} 

/** 
* Step number `step` starting at `ar[i][j]` in direction `(di, dj)`. 
* If we made 3 steps we have a winner 
*/ 
static boolean checkNext(int[][] ar, int i, int j, int di, int dj, int step) { 
    if(step==3) { 
     return true; 
    } 
    if(i+di<0 || i+di>ar.length-1 || j+dj<0 || j+dj>ar[i].length-1) { 
     return false; 
    } 
    if(ar[i+di][j+dj]==ar[i][j]) { 
     return checkNext(ar, i+di, j+dj, di, dj, step+1); 
    } 
    return false; 
} 

看到它在行動:http://ideone.com/Ou2sRh

+0

我不明白整個步驟的事情。你總是用step來做checkNext。另外你的解決方案對我來說太困難了。 :P –

+0

@Cammy_the_block'checkNext'裏面我用'step + 1'遞歸地調用方法。這樣我沿着一個方向走,直到我沿着3個相等的值進行了3個步驟。我的目標是爲我的解決方案使用遞歸:) – halex

相關問題