Android Open Source - android-checkers-app Board View






From Project

Back to project page android-checkers-app.

License

The source code is released under:

Apache License

If you think the Android project android-checkers-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.unlv.sudo.checkers.views;
//ww  w.  j  a va 2 s .c om
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import edu.unlv.sudo.checkers.model.Board;
import edu.unlv.sudo.checkers.model.Game;
import edu.unlv.sudo.checkers.model.Location;
import edu.unlv.sudo.checkers.model.Piece;
import edu.unlv.sudo.checkers.model.Rules;
import edu.unlv.sudo.checkers.model.Team;
import edu.unlv.sudo.checkers.service.GameService;
import edu.unlv.sudo.checkers.service.impl.GameServiceImpl;

/**
 * This view represents the checkers board.
 */
public class BoardView extends View {

    private static final Logger LOGGER = Logger.getLogger(BoardView.class.getName());

    private static final int BOARD_COLOR_LIGHT = Color.LTGRAY;
    private static final int BOARD_COLOR_DARK = Color.DKGRAY;
    private static final int PIECE_COLOR_RED = Color.RED;
    private static final int PIECE_COLOR_BLACK = Color.BLACK;
    private static final int PIECE_COLOR_KING = Color.WHITE;
    private static final int SELECT_COLOR_PIECE = Color.GREEN;
    private static final int SELECT_COLOR_SPACE = Color.YELLOW;

    private Game game;
    private int boardWidth;

    private Piece selectedPiece;
    private List<Location> moves;

    public BoardView(final Context context) {
        super(context);
        setOnTouchListener(new CheckersOnTouchListener(this));
    }

    public BoardView(final Context context, final AttributeSet attributes) {
        super(context, attributes);
        setOnTouchListener(new CheckersOnTouchListener(this));
    }

    public BoardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnTouchListener(new CheckersOnTouchListener(this));
    }

    public void setGame(final Game game) {
        this.game = game;
        invalidate();
    }

    public void setSelectedPiece(final Piece piece) {
        this.selectedPiece = piece;
        invalidate();
    }

    public void setMoves(final List<Location> moves) {
        this.moves = moves;
        invalidate();
    }

    public Game getGame() {
        return this.game;
    }

    public int getBoardWidth() {
        return this.boardWidth;
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        this.boardWidth = Math.min(canvas.getWidth(), canvas.getHeight());

        if (game == null) {
            LOGGER.warning("Attempting to render a null game.");
            return;
        }

        drawBoard(game.getBoard(), canvas);
        drawSelectedPiece(selectedPiece, canvas);
        drawSelectedMoves(moves, canvas);
        drawPieces(game.getBoard(), canvas);
    }

    private void drawBoard(final Board board, final Canvas canvas) {

        if (board == null) {
            LOGGER.warning("Attempting to render board with no board set.");
            return;
        }

        final int boardWidth = Math.min(canvas.getHeight(), canvas.getWidth());
        final float squareWidth = (float) boardWidth / board.getSpacesPerSide();

        boolean lightSquare = true;
        for (int xPos = 0; xPos < board.getSpacesPerSide(); xPos++) {
            lightSquare = !lightSquare;
            for (int yPos = 0; yPos < board.getSpacesPerSide(); yPos++) {
                lightSquare = !lightSquare;

                final Paint paint = new Paint();
                paint.setColor(lightSquare ? BOARD_COLOR_LIGHT : BOARD_COLOR_DARK);

                final float left = xPos * squareWidth;
                final float right = (xPos + 1) * squareWidth - 1;
                final float top = yPos * squareWidth;
                final float bottom = (yPos + 1) * squareWidth - 1;

                canvas.drawRect(left, top, right, bottom, paint);
            }
        }
    }

    private void drawPieces(final Board board, final Canvas canvas) {

        if (board == null) {
            LOGGER.warning("Attempting to render board with no board set.");
            return;
        }

        final int boardWidth = Math.min(canvas.getHeight(), canvas.getWidth());
        final float squareWidth = (float) boardWidth / board.getSpacesPerSide();
        final float pieceRadius = squareWidth * .75F / 2;

        for (Piece piece : board.getPieces()) {
            final Paint paint = new Paint();
            paint.setColor(piece.getTeam() == Team.RED ? PIECE_COLOR_RED : PIECE_COLOR_BLACK);

            final int x = piece.getLocation().getX();
            final int y = piece.getLocation().getY();

            final float left = x * squareWidth + (squareWidth / 2);
            final float top = y * squareWidth + (squareWidth / 2);

            canvas.drawCircle(left, top, pieceRadius, paint);

            if (piece.isKing()) {
                paint.setColor(PIECE_COLOR_KING);
                canvas.drawLine(left, top - pieceRadius, left, top + pieceRadius, paint);
                canvas.drawLine(left - pieceRadius, top, left + pieceRadius, top, paint);
            }
        }
    }

    private void drawSelectedPiece(final Piece piece, final Canvas canvas) {
        if (piece == null) {
            return;
        }

        if (game == null) {
            LOGGER.warning("Attempting to render board with no game set.");
            return;
        }

        final int boardWidth = Math.min(canvas.getHeight(), canvas.getWidth());
        final float squareWidth = (float) boardWidth / game.getBoard().getSpacesPerSide();

        final Paint paint = new Paint();
        paint.setColor(SELECT_COLOR_PIECE);

        final int x = piece.getLocation().getX();
        final int y = piece.getLocation().getY();

        final float left = x * squareWidth + 4;
        final float right = (x + 1) * squareWidth - 5;
        final float top = y * squareWidth + 4;
        final float bottom = (y + 1) * squareWidth - 5;

        canvas.drawRect(left, top, right, bottom, paint);
    }

    private void drawSelectedMoves(final List<Location> moves, final Canvas canvas) {
        if (moves == null) {
            return;
        }

        if (game == null) {
            LOGGER.warning("Attempting to render board with no game set.");
            return;
        }

        final int boardWidth = Math.min(canvas.getHeight(), canvas.getWidth());
        final float squareWidth = (float) boardWidth / game.getBoard().getSpacesPerSide();

        final Paint paint = new Paint();
        paint.setColor(SELECT_COLOR_SPACE);

        for (Location location : moves) {
            final int x = location.getX();
            final int y = location.getY();

            final float left = x * squareWidth + 4;
            final float right = (x + 1) * squareWidth - 5;
            final float top = y * squareWidth + 4;
            final float bottom = (y + 1) * squareWidth - 5;

            canvas.drawRect(left, top, right, bottom, paint);
        }
    }

    /**
     * This {@link GameService.Listener} renders game events to the screen.
     */
    private class GameListener implements GameService.Listener {

        final Context context;

        /**
         * Construct a game listener with a context on which to show errors.
         * @param context the context on which to show errors
         */
        public GameListener(final Context context) {
            this.context = context;
        }

        @Override
        public void onGame(Game game) {
            setGame(game);
        }

        @Override
        public void onError(Exception exception) {
            final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
            alertBuilder.setTitle("Error");
            alertBuilder.setMessage("Unable to get game from server: " + exception.getMessage());
            alertBuilder.show();
        }
    }

    /**
     * This {@link OnTouchListener} handles touches on the checkers game board.
     */
    private class CheckersOnTouchListener implements OnTouchListener {
        private final GameService gameService = GameServiceImpl.getInstance();
        private final BoardView boardView;

        private Piece selectedPiece;
        private List<Location> moves;

        /**
         * Construct a {@link CheckersOnTouchListener} with the provided {@link BoardView}.
         * @param boardView the {@link BoardView} to operate upon
         */
        public CheckersOnTouchListener(final BoardView boardView) {
            this.boardView = boardView;
        }

        @Override
        public boolean onTouch(final View view, final MotionEvent motionEvent) {
            if (motionEvent.getAction() != MotionEvent.ACTION_DOWN) {
                return true;
            }

            if (boardView.getGame() == null) {
                return false;
            }

            final Game game = boardView.getGame();
            final Board board = game.getBoard();

            final float x = motionEvent.getX();
            final float y = motionEvent.getY();

            final int boardWidth = boardView.getBoardWidth();
            final float squareWidth = (float) boardWidth / board.getSpacesPerSide();

            final Location location = new Location((int) Math.floor(x / squareWidth), (int) Math.floor(y / squareWidth));
            final Piece piece = board.getPieceAtLocation(location);

            //TODO: case where king piece comes around to it's original spot
            if (piece != null && piece.getTeam() != game.getTurn()) {
                return true;
            } else if (piece != null) {
                selectedPiece = piece;
                moves = new ArrayList<>();
            } else if (selectedPiece != null && moves.size() > 0 && moves.get(moves.size() - 1).equals(location)) {
                Rules.move(game, selectedPiece, moves);
                gameService.move(game, new GameListener(boardView.getContext()));
                selectedPiece = null;
                moves = new ArrayList<>();
                game.clearTurn();
            } else if (selectedPiece != null && Rules.isValidMove(selectedPiece, moves, location, board)) {
                moves.add(location);
            }

            setSelectedPiece(selectedPiece);
            setMoves(moves);

            return true;
        }
    }
}




Java Source Code List

com.android.volley.AuthFailureError.java
com.android.volley.CacheDispatcher.java
com.android.volley.Cache.java
com.android.volley.DefaultRetryPolicy.java
com.android.volley.ExecutorDelivery.java
com.android.volley.NetworkDispatcher.java
com.android.volley.NetworkError.java
com.android.volley.NetworkResponse.java
com.android.volley.Network.java
com.android.volley.NoConnectionError.java
com.android.volley.ParseError.java
com.android.volley.RequestQueue.java
com.android.volley.Request.java
com.android.volley.ResponseDelivery.java
com.android.volley.Response.java
com.android.volley.RetryPolicy.java
com.android.volley.ServerError.java
com.android.volley.TimeoutError.java
com.android.volley.VolleyError.java
com.android.volley.VolleyLog.java
com.android.volley.toolbox.AndroidAuthenticator.java
com.android.volley.toolbox.Authenticator.java
com.android.volley.toolbox.BasicNetwork.java
com.android.volley.toolbox.ByteArrayPool.java
com.android.volley.toolbox.ClearCacheRequest.java
com.android.volley.toolbox.DiskBasedCache.java
com.android.volley.toolbox.HttpClientStack.java
com.android.volley.toolbox.HttpHeaderParser.java
com.android.volley.toolbox.HttpStack.java
com.android.volley.toolbox.HurlStack.java
com.android.volley.toolbox.ImageLoader.java
com.android.volley.toolbox.ImageRequest.java
com.android.volley.toolbox.JsonArrayRequest.java
com.android.volley.toolbox.JsonObjectRequest.java
com.android.volley.toolbox.JsonRequest.java
com.android.volley.toolbox.NetworkImageView.java
com.android.volley.toolbox.NoCache.java
com.android.volley.toolbox.PoolingByteArrayOutputStream.java
com.android.volley.toolbox.RequestFuture.java
com.android.volley.toolbox.StringRequest.java
com.android.volley.toolbox.Volley.java
edu.unlv.sudo.checkers.CheckersApplication.java
edu.unlv.sudo.checkers.CheckersBoard.java
edu.unlv.sudo.checkers.model.Board.java
edu.unlv.sudo.checkers.model.Game.java
edu.unlv.sudo.checkers.model.Location.java
edu.unlv.sudo.checkers.model.Piece.java
edu.unlv.sudo.checkers.model.Rules.java
edu.unlv.sudo.checkers.model.Team.java
edu.unlv.sudo.checkers.model.exception.InvalidBoardException.java
edu.unlv.sudo.checkers.model.exception.InvalidMoveException.java
edu.unlv.sudo.checkers.model.exception.OutOfTurnException.java
edu.unlv.sudo.checkers.service.GameService.java
edu.unlv.sudo.checkers.service.impl.GameServiceImpl.java
edu.unlv.sudo.checkers.util.VolleySingleton.java
edu.unlv.sudo.checkers.views.BoardView.java