2012-01-29 17 views
0

我想做一個checkWin方法返回true,如果有一個勝利的組合。我正在使用嵌套for循環。棋盤大小和勝率大小在遊戲類中預定義,並在初始化2D陣列和檢查勝利時傳遞給棋盤類。我希望能夠在任何尺寸的棋盤上使用此檢查方法獲得任何尺寸的組合。這樣我就可以重複使用多種遊戲的方法。 Tic Tac Toe,Connect Four,Gomoku等測試贏得二維數組的字符與預定義數組和贏得長度 - Java

爲什麼它不能正常工作?我很確定我的if語句是錯誤的。我會在一些測試中獲勝,但不會在其他測試中獲勝。而且我從來沒有在西北對角線贏得勝利,只有在一個東北對角線上。我可以很容易地測試某些長度,但是我無法測試任何尺寸的長度的任何尺寸的棋盤。

/** 
* Tests board initialized with size parameter in class constructor. 
* Utilizes nested loops to check all rows, columns, and diagonals for win. 
* 
* @param WIN_SIZE, The size to check for valid win. 
* @return, Returns true if there is a winner. 
*/ 
public boolean checkWin(int WIN_SIZE) 
{ 
    //Test columns 
    //Loop through columns 
    for (int col = 0; col < size; col++) 
    { 
     //Loop through rows 
     for (int row = 0; row < size; row++) 
     { 
      //Loop through win length 
      for (int counter = 0; counter < WIN_SIZE; counter++) 
      { 
       try 
       { 
       if (board[counter][col] == '_' || board[counter][col] != board[counter+1][col]) 
       { 
        //Break if spot is blank or if next index does not match. 
        break; 
       } 
       if (counter == WIN_SIZE - 1) 
       { 
        //Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination 
        return true; 
       } 
       } 
       catch (ArrayIndexOutOfBoundsException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

    //Test rows 
    //Loop through rows 
    for (int row = 0; row < size; row++) 
    { 
     //Loop through columns 
     for (int col = 0; col < size; col++) 
     { 
      //Loop through win length 
      for (int counter = 0; counter < WIN_SIZE; counter++) 
      { 
       try 
       { 
        if (board[row][counter+col] == '_' || board[row][counter+col] != board[row][counter+col+1]) 
        { 
         //Break if spot is blank or if next index does not match 
         break; 
        } 
        if (counter == WIN_SIZE - 1) 
        { 
         //Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination 
         return true; 
        } 
       } 
       catch (ArrayIndexOutOfBoundsException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 


    //Test Diagonals North-West 
    //Loop through column 
    for (int col = 0; col < size; col++) 
    { 
     //Loop through rows 
     for (int row = 0; row < size; row++) 
     { 
      //Loop through length of a valid win 
      for (int counter = 0; counter < WIN_SIZE; counter++) 
      { 
       try 
       { 
        if (board[row+counter][counter] == '_' || board[row+counter][counter] != board[row+counter+1][counter+1]) 
        { 
         //Break if spot is blank or if next index does not match 
         break; 
        } 
        else if (counter == WIN_SIZE - 1) 
        { 
         //Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination 
         return true; 
        } 
       } 
       catch (ArrayIndexOutOfBoundsException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

    //Test Diagonals North-West 
    //Loop through rows 
    for (int col = 0; col < size; col++) 
    { 
     //Loop through columns 
     for (int row = 0; row < size; row++) 
     { 
      //Loop through length of a valid win 
      for (int counter = 0; counter < WIN_SIZE; counter++) 
      { 
       try 
       { 
        if (board[row-counter][counter] == '_' || board[row-counter][counter] != board[row-counter-1][counter+1]) 
        { 
         //Break if spot is blank or if next index does not match 
         break; 
        } 
        else if (counter == WIN_SIZE - 1) 
        { 
         //Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination 
         return true; 
        } 
       } 
       catch(ArrayIndexOutOfBoundsException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
    //Returns false if no winner is found 
    return false; 
} 
+0

我這麼把功課標籤上的這隻小狗 – Bohemian 2012-01-29 19:34:51

回答

0

考慮您的代碼進行以下檢查:

if (board[counter][col] == '_' || board[counter][col] != board[counter+1][col]) 

if (board[row+counter][counter] == '_' || board[row+counter][counter] != board[row+counter+1][counter+1]) 

if (board[row-counter][counter] == '_' || board[row-counter][counter] != board[row-counter-1][counter+1]) 

當你索引board,您使用的是rowcount,或者colcount。但是,每個檢查都應該使用全部三個變量。否則,你只能找到在板子左側或頂部邊緣開始的組合。

它看起來像你在一個循環(測試行)中得到它的權利,但在其他三個循環中沒有。

+0

好。這就說得通了。我會繼續玩弄它,但感謝您的意見! – SNOLLYGOSTER 2012-01-29 19:34:53

+0

謝謝,我想通了。 – SNOLLYGOSTER 2012-01-29 22:02:20

0

感謝我想通了:

//Test Rows 
board[row][counter+col] == '_' || board[row][counter+col] != board[row][counter+col+1] 

//Test Columns 
board[counter+row][counter+col] == '_' || board[counter+row][counter+col] !=  board[counter+row+1][counter+col+1] 

//Test NW Diagonal 
(board[counter+row][counter+col] == '_' || board[counter+row][counter+col] != board[counter+row+1][counter+col+1]) 

//Test NE Diagonal 
board[row-counter][col+counter] == '_' || board[row-counter][col+counter] != board[row-counter-1][col+counter+1]