mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2025-02-26 22:33:53 +01:00
DroidFish: Moved code in ChessBoard.java that is not used by ChessBoardEdit.java to new class ChessBoardPlay.
This commit is contained in:
parent
9810c3d054
commit
1767f8c947
|
@ -5,7 +5,7 @@
|
|||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent">
|
||||
<view
|
||||
class="org.petero.droidfish.ChessBoard"
|
||||
class="org.petero.droidfish.ChessBoardPlay"
|
||||
android:id="@+id/chessboard"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"/>
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent">
|
||||
<view
|
||||
class="org.petero.droidfish.ChessBoard"
|
||||
class="org.petero.droidfish.ChessBoardPlay"
|
||||
android:id="@+id/chessboard"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"/>
|
||||
|
|
|
@ -92,7 +92,7 @@
|
|||
</RelativeLayout>
|
||||
</LinearLayout>
|
||||
<view
|
||||
class="org.petero.droidfish.ChessBoard"
|
||||
class="org.petero.droidfish.ChessBoardPlay"
|
||||
android:id="@+id/chessboard"
|
||||
android:layout_weight="0"
|
||||
android:layout_width="fill_parent"
|
||||
|
|
|
@ -23,11 +23,8 @@ import java.util.Collections;
|
|||
import java.util.List;
|
||||
|
||||
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 org.petero.droidfish.gamelogic.UndoInfo;
|
||||
|
||||
import android.content.Context;
|
||||
|
@ -41,10 +38,8 @@ import android.os.Handler;
|
|||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class ChessBoard extends View {
|
||||
private PGNOptions pgnOptions = null;
|
||||
public abstract class ChessBoard extends View {
|
||||
public Position pos;
|
||||
|
||||
public int selectedSquare;
|
||||
|
@ -56,7 +51,6 @@ public class ChessBoard extends View {
|
|||
int pieceXDelta, pieceYDelta; // top/left pixel draw position relative to square
|
||||
public boolean flipped;
|
||||
public boolean drawSquareLabels;
|
||||
boolean oneTouchMoves;
|
||||
boolean toggleSelection;
|
||||
|
||||
List<Move> moveHints;
|
||||
|
@ -91,10 +85,6 @@ public class ChessBoard extends View {
|
|||
private Paint decorationPaint;
|
||||
private ArrayList<Paint> 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<Move> moves = new MoveGen().legalMoves(pos);
|
||||
Move matchingMove = null;
|
||||
if (prevSq >= 0)
|
||||
matchingMove = matchingMove(prevSq, sq, moves).first;
|
||||
boolean anyMatch = false;
|
||||
if (matchingMove == null) {
|
||||
Pair<Move, Boolean> 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<Move, Boolean> matchingMove(int sq1, int sq2, ArrayList<Move> 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<Move, Boolean>(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()) {
|
||||
|
|
189
DroidFish/src/org/petero/droidfish/ChessBoardPlay.java
Normal file
189
DroidFish/src/org/petero/droidfish/ChessBoardPlay.java
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<Move> moves = new MoveGen().legalMoves(pos);
|
||||
Move matchingMove = null;
|
||||
if (prevSq >= 0)
|
||||
matchingMove = matchingMove(prevSq, sq, moves).first;
|
||||
boolean anyMatch = false;
|
||||
if (matchingMove == null) {
|
||||
Pair<Move, Boolean> 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<Move, Boolean> matchingMove(int sq1, int sq2, ArrayList<Move> 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<Move, Boolean>(matchingMove, anyMatch);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user