如何检查完整列?

时间:2014-02-09 02:27:51

标签: java

我正在构建一个Connect Four游戏,这是我的检查器:

 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没有弹出,但它应该是。

任何线索?

我仍在不断研究我的逻辑,所以如果还有其他缺陷,请指出它们。 :)

1 个答案:

答案 0 :(得分:1)

您应该检查counter是否等于6,因为您声明了:

static final int ROWS = 6;

所以你必须检查一下:

if (counter == ROWS) // not 5

请注意,您要与ROWS进行比较,而不是直接与6进行比较,以避免"magic numbers"

另外,在使用之前不要忘记声明并初始化counter

int counter = 0;