2014-02-09 14 views
0

我建立一個連接四個遊戲,這是我的檢查:如何檢查整列?

static final int ROWS = 6; 

    public void checkIfFull(int colu) 
{ 
    /*deleted*/ 
} 

這是當用戶點擊一個按鈕調用:

if (e.getSource() == b1) 
    { 
     checkIfFull (0); 


    } 

這是我的方法在定位令牌最低開等級:

public void setPos(int column) 
{ 
    if (demise) return; //this is for when the game is already over and the user 
still attempts to click on a button 

    int row; 
    for (row = 0; row < ROWS; ++row) 
    { 
     if (slots[row][column] > 0) 
      break; 
    } 

    if (row > 0) 
    { 
     //save current player 
     slots[--row][column] = active; 
     //change turns 
     /*if (active == GREEN) 
     { 
      active = RED; 
      activeMe(); 
     } 
     else if (active == RED) 
     { 
      active = GREEN; 
      activeMe(); 
     } I moved this to setCircle method*/ 

     setCircle(active, row, column); //gui method used to fill up the empty circle 
    } 


} 

,當我填寫了一欄的一瞬間,在JOptionPane中不會彈出,但它應該。

任何線索?

我仍在不斷研究我的邏輯,所以如果還有其他缺陷可以看到,請將它們指出來。 :)

+0

#1更具體的問題。你遇到的問題是什麼?您是否收到錯誤(如果是這樣,請發佈堆棧跟蹤)?你是否觀察到意想不到的行爲,如果是,它是什麼,以及期望的行爲是什麼? –

+0

@Teeg對不起,應該更具體。此刻joptionpane應該彈出列滿時彈出,但它不 – user3026693

+0

@HovercraftFullOfEels yep只是做 – user3026693

回答

1

您應該檢查counter是否等於6,因爲你已經聲明:

static final int ROWS = 6; 

所以你必須檢查:

if (counter == ROWS) // not 5 

請注意,您比較反對ROWS和不要直接用6來避免"magic numbers"

另外,不要忘記使用它之前聲明和初始化counter

int counter = 0; 
+0

是有道理的,但循環從0開始,但?所以5結束是最大值? – user3026693

+0

是的,但從0到5,有6個數字。 – Christian

+0

雖然我的循環中的條件是行 user3026693