Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chessboard on android

I'm using a gridlayout of 8x8. For each square I'm using 2 Imageview, one for the square, one for the piece. Here is the code :

activity_chess.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ChessActivity" >

<GridView
    android:id="@+id/chessboard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:gravity="center_horizontal"
    android:numColumns="8" >
</GridView>

square.xml :

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/square"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:background="#000080"
    android:orientation="vertical" > 

    <ImageView 
        android:id="@+id/square_background"
        android:layout_width="35dp"
        android:layout_height="35dp" />

     <ImageView
        android:id="@+id/piece"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</FrameLayout> 

My problem

Everytime I'm moving a piece and the move is legit, I reload the adapter of the gridlayout, and it (the piece I just moved) disappear for half a second (I guess the adapter is reloading the whole grid).

I guess it could work with a workaround such as putting a picture of the piece on the top until the adapter is done loading, but I wanna know what's the best solution of if I am doing something wrong.

Here is the code for the adapter of the gridlayout :

package com.example.adapter;

import com.example.business.King;
import com.example.business.Piece;
import com.example.lessonchess.ChessActivity;
import com.example.lessonchess.R;

import android.content.ClipData;
import android.content.Context;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class SquareAdapter extends BaseAdapter{

private Context mContext;
private LayoutInflater mInflater;
private Piece[] lp;

Piece currentPiece = null;

FrameLayout flcp;
ImageView imgvcp = null;

private int whiteTurn;


// CHESSBOARD
// references to our images
private Integer[] chessboardIds = {
        R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare,
        R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare,
        R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare,
        R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare,
        R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare,
        R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare,
        R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare,
        R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare,
        R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare,
        R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare,
        R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare,
        R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare,
        R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare,
        R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare,
        R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare,
        R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare,
};

static class ViewHolder {
    public ImageView square;
    public ImageView piece;
}


public SquareAdapter(Context c, Piece[] listPiece, int turn) {
    mContext = c;
    Context context = c.getApplicationContext();
    mInflater = LayoutInflater.from(context);
    lp = listPiece;
    whiteTurn = turn;
}

@Override
public int getCount() {
    return chessboardIds.length;
}

@Override
public Object getItem(int arg0) {
    return null;
}

@Override
public long getItemId(int arg0) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    if (rowView == null) {  // if it's not recycled, initialize some attributes

        rowView = mInflater.inflate(R.layout.square, null);


        ViewHolder viewHolder = new ViewHolder();
        viewHolder.square = (ImageView) rowView.findViewById(R.id.square_background);
        viewHolder.square.setImageResource(chessboardIds[position]);
        viewHolder.piece = (ImageView) rowView.findViewById(R.id.piece);
        viewHolder.piece.setImageResource(lp[position].getRessource());

        lp[position].setCurrentSquare(position);

        // Assign the touch listener to your view which you want to move
        viewHolder.piece.setOnTouchListener(new MyTouchListener());

        viewHolder.square.setOnDragListener(new MyDragListener());

        rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();

//      if(lp[position] != null ){
//          holder.piece.setImageResource(((Piece) lp[position]).getRessource());

//      }



    //      if(currentPiece == null){
    //          currentPiece = new Piece();
    //      }else{
    //          Log.v("Test", "Test class " + `lp[position].getClass().toString());`
    //      }

//      lp[position].setCurrentSquare(position);
        holder.piece.setTag(lp[position]);

    return rowView;
}


// This defines your touch listener
private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);


            flcp = (FrameLayout) view.getParent();
            imgvcp = (ImageView) flcp.getChildAt(1);
            currentPiece = (Piece) view.getTag();

            //              ((ChessActivity) mContext).setNewAdapter(((Piece) view.getTag()).getPossibleMoves());

            return true;
        } else {
            return false;
        }
    }
} 

class MyDragListener implements OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();

        FrameLayout fl2;
        ImageView imgv2;

        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            //              Log.v("Test", "Entered start");
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            //              Log.v("Test", "Entered drag");
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            break;
        case DragEvent.ACTION_DROP:
            //              Log.v("Test", "Entered drop");
            fl2 = (FrameLayout) v.getParent();
            imgv2 = (ImageView) fl2.getChildAt(1);
            Piece square = (Piece) imgv2.getTag();

            if(currentPiece.getPossibleMoves().contains(square.getCurrentSquare())){
                //                  imgv.setImageResource(currentPiece.getRessource());
                Piece destinationPiece = lp[square.getCurrentSquare()];
                lp[square.getCurrentSquare()] = currentPiece;
                lp[currentPiece.getCurrentSquare()] = new Piece();

                if((yourBeingCheckedDumbAss(currentPiece))
                        ||((destinationPiece instanceof King)
                                &&(destinationPiece.getColor() != currentPiece.getColor()))
                                ||(whiteTurn != currentPiece.getColor())){
                    imgvcp.setImageResource(currentPiece.getRessource());
                    imgvcp.setVisibility(View.VISIBLE);
                    lp[square.getCurrentSquare()] = destinationPiece;
                    lp[currentPiece.getCurrentSquare()] = currentPiece;
                }else{
                    imgvcp.setImageResource(currentPiece.getRessource());
                    ((ChessActivity) mContext).setNewAdapter(lp);
                }

            }else{
                imgvcp.setVisibility(View.VISIBLE);
            }

            break;
        case DragEvent.ACTION_DRAG_ENDED:
        default:
            break;
        }
        return true;
    }

    public boolean movementValid(){

        return false;

    }
}


public boolean yourBeingCheckedDumbAss(Piece p){

    boolean isCheck = false;

    int positionOfMyKing = 0;


    for (int i = 0; i <= 63; i++){
        if ((lp[i].getColor()== p.getColor()
                && (lp[i] instanceof King))){
            positionOfMyKing = i;
        }
    }

    for (int i = 0; i <= 63; i++){
        if ((lp[i].getColor()!= -1)
                && (lp[i].getColor()!= p.getColor())){
            if(lp[i].getPossibleMoves().contains(positionOfMyKing)){
                isCheck = true;
            }
        }

    }

    return isCheck;



}


}

Here is the method setNewAdapter in ChessActivity.xml :

    public void setNewAdapter(Piece[] p){
    alp = p;
    chessboardGridView.setAdapter(new SquareAdapter(this, p, whiteTurn));
    if (whiteTurn == 0){
        whiteTurn = 1;
    }else{
        whiteTurn = 0;
    }

}

Thanks for reading !

like image 527
Sebastien FERRAND Avatar asked Jul 18 '26 13:07

Sebastien FERRAND


1 Answers

Here is what I did : simply not refresh the adapter everytime. I do test and move everything myself on the drag n drop, I just set a new adapter for the gridlayout at the very beginning. It is MUCH faster.

like image 83
Sebastien FERRAND Avatar answered Jul 20 '26 03:07

Sebastien FERRAND