0

I am creating a Two Dimensional Array of Cells consisting of JButtons (If that makes sense) to create a game similar to tic-tac-toe. Example of what im trying to accomplish Forgot to add that if a cell is surrounded, the game is won

I am currently lost at editing the for loop to account for assigning either the one or zero from that current cell, and assigning it to a local variable.

When I say topNum = board[row-1][col]; , how will I go about retrieving either the one or zero assigned to that cell. (I am using a setText property to assign the number to the cell.)

Thanks for your help

public boolean isWinner() {
    for (int row = 0; row < this.sizeRow; row++) {
        for (int col = 0; col < this.sizeCol; col++) {
            Cell num = board[row][col];

            // not a winner if this cell is null
            if (num != null) {

                // Look at above cell
                Integer topNum = -1;
                if (row > 0) {
                    topNum = board[row-1][col];
                    if (topNum == null) break;
                }                 

                // Look at right cell
                Integer rightNum = -1;
                if (col == this.sizeCol-1) {
                    rightNum = board[row][col+];
                    if (rightNum == null) break;
                }

                // Look at left cell
                Integer leftNum = -1;
                if (col == this.sizeCol-1) {
                    rightNum = board[row][col-];
                    if (rightNum == null) break;
                }

                // Look at bottom cell
                Integer bottomNum = -1;
                if (row > 0) {
                    topNum = board[row+1][col];
                    if (topNum == null) break;
                }  


                // do similar for left cell and bottom cell

                // Check that topNum is not our current cell
                // then check that topNum is the same number as right left and bottom OR those cells are -1, which
                // means that side was on an edge.
                if (!num.equals(topNum)) {
                    if ((topNum.equals(rightNum) || rightNum.equals(-1))
                      && (topNum.equals(leftNum) || leftNum.equals(-1))
                      && (topNum.equals(bottomNum) || bottomNum.equals(-1)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
1
  • 1
    There are a few solutions floating around for tic-tac-toe and connect four, which give algorithms for determining a winning state based on linear flows. In your case, the basic idea is to look at the current cell and based on a x/y delta, march around all the surrounding cells (x-1, y-1; x; y-1, x+1, y-1, so forth and so one), the problem is allowing for a situation where a cell is along the edge, which I assume would return blocked Commented Oct 4, 2017 at 2:57

2 Answers 2

2

Start with something simple, determine if the a cell is currently occupied by the player

protected boolean isOffBoard(int x, int y) {
    return x < 0 || y < 0 || y >= board.length || x >= board[y].length;
}

protected boolean isOccupied(int x, int y) {
    if (isOffBoard(x, y)) {
        return true;
    }

    return board[y][x] != null;
}

protected boolean isOccupiedByPlayer(int player, int x, int y) {
    if (isOffBoard(x, y)) {
        return true;
    }
    if (!isOccupied(x, y)) {
        return false;
    }

    return board[y][x] == player;
}

These three methods do a simple job

  • Determine if the cell is off the board or not
  • Determine if the cell is occupied or not
  • Determine if the cell is occupied by the specific player

This is handy, because now we can simply do a circular check about a specific point and determine if a players surrounded or not, something like this...

protected boolean isSurroundedBy(int player, int x, int y) {
    return isOccupiedByPlayer(player, x - 1, y - 1)
            && isOccupiedByPlayer(player, x, y - 1)
            && isOccupiedByPlayer(player, x + 1, y - 1)
            && isOccupiedByPlayer(player, x - 1, y)
            && isOccupiedByPlayer(player, x + 1, y)
            && isOccupiedByPlayer(player, x - 1, y + 1)
            && isOccupiedByPlayer(player, x, y + 1)
            && isOccupiedByPlayer(player, x + 1, y + 1);
}

This simply checks to see if the cells surrounding a given point is occupied by a specific player

I used the following to test it...

board[1][1] = 0;
System.out.println("");
System.out.println("1x1 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 1, 1));
System.out.println("isSurrounded = " + isSurroundedBy(1, 1, 1));
board = new Integer[3][3];
board[0][0] = 0;
System.out.println("");
System.out.println("0x0 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 0, 0));
System.out.println("isSurrounded = " + isSurroundedBy(1, 0, 0));
board[1][0] = 1;
board[1][1] = 1;
board[0][1] = 1;
System.out.println("");
System.out.println("0x0 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 0, 0));
System.out.println("1x1 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 1, 1));
System.out.println("isSurrounded = " + isSurroundedBy(1, 0, 0));

board = new Integer[3][3];
board[0][0] = 0;
board[1][0] = 1;
board[1][1] = 0;
board[0][1] = 1;
System.out.println("");
System.out.println("0x0 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 0, 0));
System.out.println("1x1 isOccupiedByPlayer 0 = " + isOccupiedByPlayer(0, 1, 1));
System.out.println("isSurrounded = " + isSurroundedBy(1, 0, 0));

Which outputs...

1x1 isOccupiedByPlayer 0 = true
isSurrounded = false

0x0 isOccupiedByPlayer 0 = true
isSurrounded = false

0x0 isOccupiedByPlayer 0 = true
1x1 isOccupiedByPlayer 0 = false
isSurrounded = true

0x0 isOccupiedByPlayer 0 = true
1x1 isOccupiedByPlayer 0 = true
isSurrounded = false

Now, this may not meet you absolute needs, but should give you a jumping off point

Sign up to request clarification or add additional context in comments.

Comments

0

If your using setText() to assign the one or zero to the cell. I would use getText() to retrieve the assigned value.

2 Comments

How would I go about using getText() in the previous code? I am just unaware where to put it.
@DieselGV IHMO, you're doing the right thing to start with, you should based you logic of the model and not the state of the UI, the UI should only represent the state of the model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.