0

Basically, I am implementing a reversi app for android, and I am currently trying to update an element in a 2d array. The call is in an onClickListener which is in a loop that was used to set up the reversi board. The problem is that once a piece has been placed, the element isPositionEmpty is supposed to change to false, however, it does not. Here is a snippet of the code:

for(int n = 0; n < 8; n ++){

        ...


    for(int i = 0; i < 8; i++ ){
        final ImageView button = new ImageView(this);

        final int countN = n;
        final int countI = i;

                    ...

button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String buttonID = String.valueOf(button.getId());
                Log.d("buttonPressedID",buttonID);

                Log.d("isPositionEmpty", boardString);



                board[countI][countN].isPositionEmpty = false;

Help is greatly appreciated! thanks in advance!

2 Answers 2

1

Looks like you mixed up your x and y values? board[countN][countY] would be in the same order that you built the 2-d array.

board[countN][countI].isPositionEmpty = false;

You only have an excerpt of your code, but I would make a class to keep track of each square that extends an ImageView / ImageButton. Then it will take care of itself and you won't have to set it yourself - the logic will be in the class as a side-affect of the action of actually filling the square. For example you could call a method recordMove(int move) that is either player one or player two. Then all the logic to change the image and see if it is empty is handled within the class.

Then you would only have to create the custom buttons/views in a nested loop and they would take care of themselves. Pass in one listener too- no need to make 64 anonymous listeners.

Its hard to mess it up when you organize it this way.

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

Comments

0

I don't know why this particular case fails. In general, your code seems to mix data and UI a lot. I would try to seperate the data (e.g. all data that makes up your board) from the views. Write a class that only stores information about the states.

Than in another class, you can draw all views based upon the data in the view. Something that might get you started is the following mix of pseudo and code. It's far from complete, but settings up a structure like this, will prevent many more problems in the future. GOOD LUCK

class Square
{
    int buttonId;
    int rownum;
    int colnum;
    int state;    // can be empty || white || black
    //.. maybe more things you want to store   

    // some functions:
    void addPiece();
    void removePiece();
    void setButtonId(int id);

}

class Board
{
    // 2D array of Squares e.g. ArrayList<ArrayList<Square>>

    // some functions:
    void createEmptyBoard()    // which creates an empty board    
    ArrayList<ArrayList<Square> getAllSquares();
    ArrayList<Square> getAllWhiteSquare();
    ArrayList<Square> getAllBlackSquares();

}


/**
* You Activity should be able to draw the board, without any knowledge of the 
* underlying data. Vice Versa: your Board class, should know exactly where all
* pieces are, and what color they have, but should not care about what happens
* when they are clicked. Or, if they can be clicked.
*
* Your activity only has to make sure, that if views are clicked/dragged or moved
* that the Board class will be updated accordingly
*/
class YourActivity
{
    Board board;

    // some methods:
    drawBoard(Board board)
    {
        ArrayList<Square> white = board.getAllWhiteSquares();
        ArrayList<Square> black = board.getAllBlackSquares();

        for(Square w : white){
            Button b = new Button();    // create a button
            w.setButtonId(b.getId());   // store the button id in the square

            b.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    Square clickedSquare = board.getSquareWithId(v.getId());
                }
            );
        }
    }
}

1 Comment

Thanks for the help, i will definitely take this into consideration!

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.