From 1767f8c947fc3c2f792c15721d7cdcb4d3ce757d Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Mon, 10 Sep 2012 21:46:24 +0000 Subject: [PATCH] DroidFish: Moved code in ChessBoard.java that is not used by ChessBoardEdit.java to new class ChessBoardPlay. --- DroidFish/res/layout-land/main.xml | 2 +- DroidFish/res/layout/main.xml | 2 +- DroidFish/res/layout/main_left_handed.xml | 2 +- .../src/org/petero/droidfish/ChessBoard.java | 149 ++------------ .../org/petero/droidfish/ChessBoardPlay.java | 189 ++++++++++++++++++ .../src/org/petero/droidfish/DroidFish.java | 6 +- 6 files changed, 211 insertions(+), 139 deletions(-) create mode 100644 DroidFish/src/org/petero/droidfish/ChessBoardPlay.java diff --git a/DroidFish/res/layout-land/main.xml b/DroidFish/res/layout-land/main.xml index 9fb4998..d4d5033 100644 --- a/DroidFish/res/layout-land/main.xml +++ b/DroidFish/res/layout-land/main.xml @@ -5,7 +5,7 @@ android:layout_width="fill_parent" android:layout_height="fill_parent"> diff --git a/DroidFish/res/layout/main.xml b/DroidFish/res/layout/main.xml index cfcfd21..d29dd8f 100644 --- a/DroidFish/res/layout/main.xml +++ b/DroidFish/res/layout/main.xml @@ -4,7 +4,7 @@ android:layout_width="fill_parent" android:layout_height="fill_parent"> diff --git a/DroidFish/res/layout/main_left_handed.xml b/DroidFish/res/layout/main_left_handed.xml index 62bf86f..8067371 100644 --- a/DroidFish/res/layout/main_left_handed.xml +++ b/DroidFish/res/layout/main_left_handed.xml @@ -92,7 +92,7 @@ moveHints; @@ -91,10 +85,6 @@ public class ChessBoard extends View { private Paint decorationPaint; private ArrayList moveMarkPaint; - public void setPgnOptions(PGNOptions pgnOptions) { - this.pgnOptions = pgnOptions; - } - public ChessBoard(Context context, AttributeSet attrs) { super(context, attrs); pos = new Position(); @@ -106,7 +96,6 @@ public class ChessBoard extends View { pieceXDelta = pieceYDelta = -1; flipped = false; drawSquareLabels = false; - oneTouchMoves = false; toggleSelection = false; darkPaint = new Paint(); @@ -360,12 +349,11 @@ public class ChessBoard extends View { userSelectedSquare = true; } - protected int getWidth(int sqSize) { return sqSize * 8; } - protected int getHeight(int sqSize) { return sqSize * 8; } - protected int getSqSizeW(int width) { return (width) / 8; } - protected int getSqSizeH(int height) { return (height) / 8; } - - protected int getMaxHeightPercentage() { return 75; } + protected abstract int getWidth(int sqSize); + protected abstract int getHeight(int sqSize); + protected abstract int getSqSizeW(int width); + protected abstract int getSqSizeH(int height); + protected abstract int getMaxHeightPercentage(); @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { @@ -386,13 +374,9 @@ public class ChessBoard extends View { setMeasuredDimension(width, height); } - protected void computeOrigin(int width, int height) { - x0 = (width - sqSize * 8) / 2; - y0 = (height - sqSize * 8) / 2; - } - - protected int getXFromSq(int sq) { return Position.getX(sq); } - protected int getYFromSq(int sq) { return Position.getY(sq); } + protected abstract void computeOrigin(int width, int height); + protected abstract int getXFromSq(int sq); + protected abstract int getYFromSq(int sq); @Override protected void onDraw(Canvas canvas) { @@ -573,10 +557,10 @@ public class ChessBoard extends View { canvas.drawText(s, xCrd, yCrd, labelPaint); } - protected int getXCrd(int x) { return x0 + sqSize * (flipped ? 7 - x : x); } - protected int getYCrd(int y) { return y0 + sqSize * (flipped ? y : 7 - y); } - protected int getXSq(int xCrd) { int t = (xCrd - x0) / sqSize; return flipped ? 7 - t : t; } - protected int getYSq(int yCrd) { int t = (yCrd - y0) / sqSize; return flipped ? t : 7 - t; } + protected abstract int getXCrd(int x); + protected abstract int getYCrd(int y); + protected abstract int getXSq(int xCrd); + protected abstract int getYSq(int yCrd); /** * Compute the square corresponding to the coordinates of a mouse event. @@ -598,108 +582,7 @@ public class ChessBoard extends View { return sq; } - private final boolean myColor(int piece) { - return (piece != Piece.EMPTY) && (Piece.isWhite(piece) == pos.whiteMove); - } - - public Move mousePressed(int sq) { - if (sq < 0) - return null; - cursorVisible = false; - if ((selectedSquare != -1) && !userSelectedSquare) - setSelection(-1); // Remove selection of opponents last moving piece - - if (!oneTouchMoves) { - int p = pos.getPiece(sq); - if (selectedSquare != -1) { - if (sq == selectedSquare) { - if (toggleSelection) - setSelection(-1); - return null; - } - if (!myColor(p)) { - Move m = new Move(selectedSquare, sq, Piece.EMPTY); - setSelection(sq); - userSelectedSquare = false; - return m; - } else - setSelection(sq); - } else { - if (myColor(p)) - setSelection(sq); - } - } else { - int prevSq = userSelectedSquare ? selectedSquare : -1; - if (prevSq == sq) { - if (toggleSelection) - setSelection(-1); - return null; - } - ArrayList moves = new MoveGen().legalMoves(pos); - Move matchingMove = null; - if (prevSq >= 0) - matchingMove = matchingMove(prevSq, sq, moves).first; - boolean anyMatch = false; - if (matchingMove == null) { - Pair match = matchingMove(-1, sq, moves); - matchingMove = match.first; - anyMatch = match.second; - } - if (matchingMove != null) { - setSelection(matchingMove.to); - userSelectedSquare = false; - return matchingMove; - } - if (!anyMatch && (sq >= 0)) { - int p = pos.getPiece(sq); - if (myColor(p)) { - String msg = getContext().getString(R.string.piece_can_not_be_moved); - boolean localized = (pgnOptions != null) && - (pgnOptions.view.pieceType != PGNOptions.PT_ENGLISH); - msg += ": " + TextIO.pieceAndSquareToString(localized, p, sq); - Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show(); - } - } - setSelection(anyMatch ? sq : -1); - } - return null; - } - - /** - * Determine if there is a unique legal move corresponding to one or two selected squares. - * @param sq1 First square, or -1. - * @param sq2 Second square. - * @param moves List of legal moves. - * @return Matching move if unique. - * Boolean indicating if there was at least one match. - */ - private final Pair matchingMove(int sq1, int sq2, ArrayList moves) { - Move matchingMove = null; - boolean anyMatch = false; - for (Move m : moves) { - boolean match; - if (sq1 == -1) - match = (m.from == sq2) || (m.to == sq2); - else - match = (m.from == sq1) && (m.to == sq2) || - (m.from == sq2) && (m.to == sq1); - if (match) { - if (matchingMove == null) { - matchingMove = m; - anyMatch = true; - } else { - if ((matchingMove.from == m.from) && - (matchingMove.to == m.to)) { - matchingMove.promoteTo = Piece.EMPTY; - } else { - matchingMove = null; - break; - } - } - } - } - return new Pair(matchingMove, anyMatch); - } + protected abstract Move mousePressed(int sq); public static class OnTrackballListener { public void onTrackballEvent(MotionEvent event) { } @@ -717,8 +600,8 @@ public class ChessBoard extends View { return false; } - protected int minValidY() { return 0; } - protected int getSquare(int x, int y) { return Position.getSquare(x, y); } + protected abstract int minValidY(); + protected abstract int getSquare(int x, int y); public final Move handleTrackballEvent(MotionEvent event) { switch (event.getAction()) { diff --git a/DroidFish/src/org/petero/droidfish/ChessBoardPlay.java b/DroidFish/src/org/petero/droidfish/ChessBoardPlay.java new file mode 100644 index 0000000..c2307e0 --- /dev/null +++ b/DroidFish/src/org/petero/droidfish/ChessBoardPlay.java @@ -0,0 +1,189 @@ +/* + DroidFish - An Android chess program. + Copyright (C) 2012 Peter Ă–sterlund, peterosterlund2@gmail.com + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package org.petero.droidfish; + +import java.util.ArrayList; + +import org.petero.droidfish.gamelogic.Move; +import org.petero.droidfish.gamelogic.MoveGen; +import org.petero.droidfish.gamelogic.Pair; +import org.petero.droidfish.gamelogic.Piece; +import org.petero.droidfish.gamelogic.Position; +import org.petero.droidfish.gamelogic.TextIO; + +import android.content.Context; +import android.util.AttributeSet; +import android.widget.Toast; + +/** + * Chess board widget suitable for play mode. + * @author petero + */ +public class ChessBoardPlay extends ChessBoard { + private PGNOptions pgnOptions = null; + boolean oneTouchMoves; + + public ChessBoardPlay(Context context, AttributeSet attrs) { + super(context, attrs); + oneTouchMoves = false; + } + + public void setPgnOptions(PGNOptions pgnOptions) { + this.pgnOptions = pgnOptions; + } + + @Override + protected int getXCrd(int x) { return x0 + sqSize * (flipped ? 7 - x : x); } + @Override + protected int getYCrd(int y) { return y0 + sqSize * (flipped ? y : 7 - y); } + @Override + protected int getXSq(int xCrd) { int t = (xCrd - x0) / sqSize; return flipped ? 7 - t : t; } + @Override + protected int getYSq(int yCrd) { int t = (yCrd - y0) / sqSize; return flipped ? t : 7 - t; } + + @Override + protected int getWidth(int sqSize) { return sqSize * 8; } + @Override + protected int getHeight(int sqSize) { return sqSize * 8; } + @Override + protected int getSqSizeW(int width) { return (width) / 8; } + @Override + protected int getSqSizeH(int height) { return (height) / 8; } + @Override + protected int getMaxHeightPercentage() { return 75; } + + @Override + protected void computeOrigin(int width, int height) { + x0 = (width - sqSize * 8) / 2; + y0 = (height - sqSize * 8) / 2; + } + @Override + protected int getXFromSq(int sq) { return Position.getX(sq); } + @Override + protected int getYFromSq(int sq) { return Position.getY(sq); } + + @Override + protected int minValidY() { return 0; } + @Override + protected int getSquare(int x, int y) { return Position.getSquare(x, y); } + + + private final boolean myColor(int piece) { + return (piece != Piece.EMPTY) && (Piece.isWhite(piece) == pos.whiteMove); + } + + public Move mousePressed(int sq) { + if (sq < 0) + return null; + cursorVisible = false; + if ((selectedSquare != -1) && !userSelectedSquare) + setSelection(-1); // Remove selection of opponents last moving piece + + if (!oneTouchMoves) { + int p = pos.getPiece(sq); + if (selectedSquare != -1) { + if (sq == selectedSquare) { + if (toggleSelection) + setSelection(-1); + return null; + } + if (!myColor(p)) { + Move m = new Move(selectedSquare, sq, Piece.EMPTY); + setSelection(sq); + userSelectedSquare = false; + return m; + } else + setSelection(sq); + } else { + if (myColor(p)) + setSelection(sq); + } + } else { + int prevSq = userSelectedSquare ? selectedSquare : -1; + if (prevSq == sq) { + if (toggleSelection) + setSelection(-1); + return null; + } + ArrayList moves = new MoveGen().legalMoves(pos); + Move matchingMove = null; + if (prevSq >= 0) + matchingMove = matchingMove(prevSq, sq, moves).first; + boolean anyMatch = false; + if (matchingMove == null) { + Pair match = matchingMove(-1, sq, moves); + matchingMove = match.first; + anyMatch = match.second; + } + if (matchingMove != null) { + setSelection(matchingMove.to); + userSelectedSquare = false; + return matchingMove; + } + if (!anyMatch && (sq >= 0)) { + int p = pos.getPiece(sq); + if (myColor(p)) { + String msg = getContext().getString(R.string.piece_can_not_be_moved); + boolean localized = (pgnOptions != null) && + (pgnOptions.view.pieceType != PGNOptions.PT_ENGLISH); + msg += ": " + TextIO.pieceAndSquareToString(localized, p, sq); + Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show(); + } + } + setSelection(anyMatch ? sq : -1); + } + return null; + } + + /** + * Determine if there is a unique legal move corresponding to one or two selected squares. + * @param sq1 First square, or -1. + * @param sq2 Second square. + * @param moves List of legal moves. + * @return Matching move if unique. + * Boolean indicating if there was at least one match. + */ + private final Pair matchingMove(int sq1, int sq2, ArrayList moves) { + Move matchingMove = null; + boolean anyMatch = false; + for (Move m : moves) { + boolean match; + if (sq1 == -1) + match = (m.from == sq2) || (m.to == sq2); + else + match = (m.from == sq1) && (m.to == sq2) || + (m.from == sq2) && (m.to == sq1); + if (match) { + if (matchingMove == null) { + matchingMove = m; + anyMatch = true; + } else { + if ((matchingMove.from == m.from) && + (matchingMove.to == m.to)) { + matchingMove.promoteTo = Piece.EMPTY; + } else { + matchingMove = null; + break; + } + } + } + } + return new Pair(matchingMove, anyMatch); + } +} diff --git a/DroidFish/src/org/petero/droidfish/DroidFish.java b/DroidFish/src/org/petero/droidfish/DroidFish.java index 88c0d06..49b00e7 100644 --- a/DroidFish/src/org/petero/droidfish/DroidFish.java +++ b/DroidFish/src/org/petero/droidfish/DroidFish.java @@ -154,7 +154,7 @@ public class DroidFish extends Activity implements GUIInterface { // FIXME!!! Selection dialog for going into variation // FIXME!!! Use two engines in engine/engine games - private ChessBoard cb; + private ChessBoardPlay cb; private static DroidChessController ctrl = null; private boolean mShowThinking; private boolean mShowStats; @@ -479,7 +479,7 @@ public class DroidFish extends Activity implements GUIInterface { @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); - ChessBoard oldCB = cb; + ChessBoardPlay oldCB = cb; String statusStr = status.getText().toString(); initUI(false); readPrefs(); @@ -522,7 +522,7 @@ public class DroidFish extends Activity implements GUIInterface { moveList.setMovementMethod(LinkMovementMethod.getInstance()); thinking.setFocusable(false); - cb = (ChessBoard)findViewById(R.id.chessboard); + cb = (ChessBoardPlay)findViewById(R.id.chessboard); cb.setFocusable(true); cb.requestFocus(); cb.setClickable(true);