2014-10-18 22 views
4

你剛剛嘗試了一個TicTacToe項目,並被卡住了一個錯誤。我的錯誤與檢查特定對角線的雙贏解決方案有關。TicTacToe可調整大小板,WIN方法錯誤

我需要什麼: 創建嵌套的循環來循環數組斜下來,然後遞增它,所以它會在或大小對角線掃描,直到最後掃描整個陣列。

我所做的: 我試圖讓一個嵌套的循環,將通過行循環,並在添加到櫃檯,直到該行的末尾,則檢查計數器等於內聯(量一排需要贏)。我相信它適用於行和列。

問題: 但對於對角線,我得到一個數組越界異常,我想這是因爲我的一個b添加到這可能是遊戲鍵盤[3] [4]當談到一個3x3的遊戲板。

試圖解決: 我試過一個解決方案,你可以看到是奇怪的放置循環與j。所以我只會去j,不要超過數組限制。

我想知道這背後的邏輯會起作用嗎?

很抱歉,如果代碼是凌亂尤其是增加對循環包含Ĵ

/* 
* Method winner will determine if the symbol (X or O) wins 
* 
* @param symbol will be either X or O 
* @return will return true if the symbol has won from any of the methods 
*/ 
public boolean winner(char symbol) { 

    int counter = 0; 

    /* Scan from ROWS for any symbols inline to win */ 

    for (int i = 0; i < gameBoard.length; i++) { // loop through the rows 
    for (int j = 0; j < gameBoard.length; j++) { // Loop through the columns 
     if (gameBoard[i][j] == symbol) { 
     counter++; 
     } 
     if (gameBoard[i][j] != symbol) { // If the next one in the row is not equal then reset counter to 0 
     counter = 0; 
     } 
     if (counter == inline) { // Counter will only equal inline if there is amount of inliine in a row 
     return true; 
     } 
    } 
    } 

    /* Scan and search for winning conditions in COLUMNS */ 
    for (int i = 0; i < gameBoard.length; i++) { // loop through the rows 
    for (int j = 0; j < gameBoard.length; j++) { // Loop through the columns 
     if (gameBoard[j][i] == symbol) { 
     counter++; 
     } 
     if (gameBoard[j][i] != symbol) { // Reset counter to 0 if not equal to symbol 
     counter = 0; 
     } 
     if (counter == inline) { // If counter reached amount of inline then it must have had amount of inline in a row to win 
     return true; 
     } 
    } 
    } 

    /* Scan for RIGHT DIAGONALS for winning conditions */ 

    // a shifts the position of diagonal to the right by one 
    // after diagonally looping through the board 
    for (int a = 0; a < gameBoard.length; a++) { 

    // i loops diagonally through the board 
    for (int j = gameBoard.length; j < 0; j--) { 
     for (int i = 0; i < j; i++) { 
     if (gameBoard[i][i + a] == symbol) { 
      counter++; 
     } 
     if (gameBoard[i][i + a] != symbol) { 
      counter = 0; 
     } 
     if (counter == inline) { 
      return true; 
     } 
     } 
    } 
    } 

    // b shifts the position of the diagonal down by one 
    for (int b = 1; b < gameBoard.length; b++) { 

    for (int j = gameBoard.length - 1; j < 0; j--) 
    // i loops diagonally through the board 
     for (int i = 0; i < j; i++) { 
     if (gameBoard[i + b][i] == symbol) { 
     counter++; 
     } 
     if (gameBoard[i + b][i] != symbol) { 
     counter = 0; 
     } 
     if (counter == inline) { 
     return true; 
     } 
    } 
    } 

    /* Scan for LEFT DIAGONALS for winning conditions */ 

    // a shifts the position of diagonal to the left by one 
    for (int a = gameBoard.length; a >= 0; a--) { 
    for (int j = gameBoard.length; j < 0; j--) { 
     // i loops diagonally through the board 
     for (int i = 0; i < j; i++) { 
     if (gameBoard[i][a - i] == symbol) { 
      counter++; 
     } 
     if (gameBoard[i][a - i] != symbol) { 
      counter = 0; 
     } 
     if (counter == inline) { 
      return true; 
     } 
     } 
    } 
    } 

    // b shifts the position of the diagonal down by one 
    for (int b = 0; b < gameBoard.length; b++) { 
    for (int j = gameBoard.length - 1; j < 0; j--) { 
     // i loops diagonally in the left direction of through the board 
     for (int i = 0; i < j; i++) { 
     if (gameBoard[i + b][gameBoard.length - i] == symbol) { 
      counter++; 
     } 
     if (gameBoard[i + b][gameBoard.length - i] != symbol) { 
      counter = 0; 
     } 
     if (counter == inline) { 
      return true; 
     } 
     } 
    } 
    } 


    return false; // If it reaches here then no one has won yet and the game is ongoing 

} 
+1

+ 1,對你的外部安排感到非常滿意直到編輯! – 2014-10-18 04:14:10

回答

1

至於我在你的代碼中看到,你必須得到Array Index Out Of Bounds Exception。我假設你嘗試實現經典的井字遊戲,所以我們正在處理3x3矩陣。這裏是你的遊戲板是如何建立索引:

[0.0] [1.0] [2.0]

[0.1] [1.1] [2.1]

[0.2] [1.2] [2.2]

因此,這是在循環與右對角線會發生什麼:
int a增量0 - > 2
int j遞減2 - > 0
int i增量0 - > 2


所以你的循環是這樣的:
[0.0 + 0] - >我+ + [1.1 + 0] - >我+ + [2.2 + 0] j--
[0.0 + 0] - >我+ + [1.1 + 0] j--
[0.0 + 0]一個++
[0.0 + 1] - >我++ [1.1 + 1] - >我+ + [2.2 + 1] j-- < - - 你走出陣列。


此外,在通過主對角線檢查之後,您會通過[0.0] [1.1],這根本不是對角線,而且您已經在循環中做了這些操作。即使是在底部對角線上移動也不需要([0.1] [1.2]),因爲您之前已經在循環中完成了這一操作。所以通過[0.0] [1.1] [2。2]將爲你工作。


我相信這是無效的方式來檢查勝利條件。您可以通過存儲找到的元素的位置來擺脫3個循環。

+0

現在的問題非常清楚,謝謝你的回答。在添加那些怪異的循環之前,我遇到了問題,並且我意識到現在我是如何無效的。我只是添加了for循環,希望能夠修復掃描過去的數組。 我用for循環的目標是掃描每個對角線行。但是,tic tac腳趾運行在自定義的棋盤上,也可以自定義內聯(連續獲得所需的數量)。想知道這種方法是否適用於大型自定義尺寸的井字棋牌。 – 2014-10-19 00:16:48

0

抱歉不能保證格式的意見,所以我會後我來到這裏

/* Scan for RIGHT DIAGONALS for winning conditions */ 

int j = gameBoard.length 

    for (int a = 0; a < gameBoard.length; a++) { 

// i loops diagonally through the board 
    for (int i = 0; i < j; i++) { 
    if (gameBoard[i][i + a] == symbol) { 
     counter++; 
    } 
    if (gameBoard[i][i + a] != symbol) { 
     counter = 0; 
    } 
    if (counter == inline) { 
     return true; 
    } 
    } j--; // Incrementing after the i for loop. 
} 

output: 
a:0 i:0 j:3 
[0.0+0] --> i++ [1.1+0] --> i++ [2.2+0] /*end for loop. i:2 j:3 a: 0 */ 
j-- a++ 
[0.0+1] --> i++ [1.1+1] /*end for loop. i:1 j:2 a: 1*/ 
j-- a++ 
[0.0+2] /* end for loop. i:0 j: 1 a:2 */ 

和陣列停留在邊界,同時檢查對角線。所以我認爲思想可以在更大範圍內發揮作用。