Remove GUI stuff from gamelogic.Piece class

This commit is contained in:
Peter Osterlund 2020-04-10 08:30:43 +02:00
parent a7aacdc1bb
commit 951c67dcb3
7 changed files with 99 additions and 77 deletions

View File

@ -24,7 +24,6 @@ import org.petero.droidfish.gamelogic.Move;
import org.petero.droidfish.gamelogic.MoveGen;
import org.petero.droidfish.gamelogic.Piece;
import org.petero.droidfish.gamelogic.Position;
import org.petero.droidfish.gamelogic.TextIO;
import org.petero.droidfish.view.ChessBoard;
import android.content.Context;
@ -147,7 +146,7 @@ public class ChessBoardPlay extends ChessBoard {
String msg = getContext().getString(R.string.piece_can_not_be_moved);
int pieceType = (pgnOptions == null) ? PGNOptions.PT_LOCAL
: pgnOptions.view.pieceType;
msg += ": " + TextIO.pieceAndSquareToString(pieceType, p, sq);
msg += ": " + PieceFontInfo.pieceAndSquareToString(pieceType, p, sq);
DroidFishApp.toast(msg, Toast.LENGTH_SHORT);
}
}

View File

@ -54,7 +54,6 @@ import org.petero.droidfish.gamelogic.DroidChessController;
import org.petero.droidfish.gamelogic.ChessParseError;
import org.petero.droidfish.gamelogic.Game;
import org.petero.droidfish.gamelogic.Move;
import org.petero.droidfish.gamelogic.Piece;
import org.petero.droidfish.gamelogic.Position;
import org.petero.droidfish.gamelogic.TextIO;
import org.petero.droidfish.gamelogic.GameTree.Node;
@ -710,12 +709,12 @@ public class DroidFish extends Activity
}
// Unicode code points for chess pieces
private static final String figurinePieceNames = Piece.NOTATION_PAWN + " " +
Piece.NOTATION_KNIGHT + " " +
Piece.NOTATION_BISHOP + " " +
Piece.NOTATION_ROOK + " " +
Piece.NOTATION_QUEEN + " " +
Piece.NOTATION_KING;
private static final String figurinePieceNames = PieceFontInfo.NOTATION_PAWN + " " +
PieceFontInfo.NOTATION_KNIGHT + " " +
PieceFontInfo.NOTATION_BISHOP + " " +
PieceFontInfo.NOTATION_ROOK + " " +
PieceFontInfo.NOTATION_QUEEN + " " +
PieceFontInfo.NOTATION_KING;
private void setPieceNames(int pieceType) {
if (pieceType == PGNOptions.PT_FIGURINE) {

View File

@ -0,0 +1,69 @@
/*
DroidFish - An Android chess program.
Copyright (C) 2020 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 org.petero.droidfish.gamelogic.Piece;
import org.petero.droidfish.gamelogic.TextIO;
public class PieceFontInfo {
// Unicode for color neutral chess pieces
public static final char NOTATION_KING = 0xe050;
public static final char NOTATION_QUEEN = 0xe051;
public static final char NOTATION_ROOK = 0xe052;
public static final char NOTATION_BISHOP = 0xe053;
public static final char NOTATION_KNIGHT = 0xe054;
public static final char NOTATION_PAWN = 0xe055;
// Unicode for white chess pieces
private static final char WHITE_KING = 0x2654;
// private static final char WHITE_QUEEN = 0x2655;
// private static final char WHITE_ROOK = 0x2656;
// private static final char WHITE_BISHOP = 0x2657;
// private static final char WHITE_KNIGHT = 0x2658;
// private static final char WHITE_PAWN = 0x2659;
// Unicode for black chess pieces
// private static final char BLACK_KING = 0x265A;
// private static final char BLACK_QUEEN = 0x265B;
// private static final char BLACK_ROOK = 0x265C;
// private static final char BLACK_BISHOP = 0x265D;
// private static final char BLACK_KNIGHT = 0x265E;
// private static final char BLACK_PAWN = 0x265F;
/** Converts the piece into a character for the figurine font. */
public static char toUniCode(int p) {
// As we assume the coding of the pieces is sequential, lets do some math.
return (char)(WHITE_KING + p - 1);
}
/** Convert a piece and a square to a string, such as Nf3. */
public static String pieceAndSquareToString(int currentPieceType, int p, int sq) {
StringBuilder ret = new StringBuilder();
if (currentPieceType == PGNOptions.PT_FIGURINE) {
ret.append(PieceFontInfo.toUniCode(p));
} else {
boolean localized = currentPieceType != PGNOptions.PT_ENGLISH;
ret.append(localized ? TextIO.pieceToCharLocalized(p, true)
: TextIO.pieceToChar(p, true));
}
ret.append(TextIO.squareToString(sq));
return ret.toString();
}
}

View File

@ -69,11 +69,11 @@ public final class Util {
for (int p = Piece.WPAWN; p >= Piece.WKING; p--) {
int diff = pos.nPieces(p) - pos.nPieces(Piece.swapColor(p));
while (diff < 0) {
whiteString.append(Piece.toUniCode(Piece.swapColor(p)));
whiteString.append(PieceFontInfo.toUniCode(Piece.swapColor(p)));
diff++;
}
while (diff > 0) {
blackString.append(Piece.toUniCode(p));
blackString.append(PieceFontInfo.toUniCode(p));
diff--;
}
}

View File

@ -909,7 +909,7 @@ public class GameTree {
if (p == Piece.EMPTY)
sb.append(ret.charAt(i));
else
sb.append(TextIO.pieceToCharLocalized(p));
sb.append(TextIO.pieceToCharLocalized(p, false));
}
ret = sb.toString();
}
@ -917,7 +917,7 @@ public class GameTree {
}
/** Get PGN result string corresponding to the current position. */
public final String getPGNResultString() {
private final String getPGNResultString() {
String gameResult = "*";
switch (getGameState()) {
case ALIVE:

View File

@ -39,30 +39,6 @@ public class Piece {
public static final int nPieceTypes = 13;
// Unicode for color neutral chess pieces
public static final char NOTATION_KING = 0xe050;
public static final char NOTATION_QUEEN = 0xe051;
public static final char NOTATION_ROOK = 0xe052;
public static final char NOTATION_BISHOP = 0xe053;
public static final char NOTATION_KNIGHT = 0xe054;
public static final char NOTATION_PAWN = 0xe055;
// Unicode for white chess pieces
public static final char WHITE_KING = 0x2654;
public static final char WHITE_QUEEN = 0x2655;
public static final char WHITE_ROOK = 0x2656;
public static final char WHITE_BISHOP = 0x2657;
public static final char WHITE_KNIGHT = 0x2658;
public static final char WHITE_PAWN = 0x2659;
// Unicode for black chess pieces
public static final char BLACK_KING = 0x265A;
public static final char BLACK_QUEEN = 0x265B;
public static final char BLACK_ROOK = 0x265C;
public static final char BLACK_BISHOP = 0x265D;
public static final char BLACK_KNIGHT = 0x265E;
public static final char BLACK_PAWN = 0x265F;
/**
* Return true if p is a white piece, false otherwise.
* Note that if p is EMPTY, an unspecified value is returned.
@ -81,10 +57,4 @@ public class Piece {
return EMPTY;
return isWhite(pType) ? pType + (BKING - WKING) : pType - (BKING - WKING);
}
/** Converts the piece into a character for the material diff. */
public static char toUniCode(int p) {
// As we assume the coding of the pieces is sequential, lets do some math.
return (char)(WHITE_KING + p - 1);
}
}

View File

@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.petero.droidfish.PGNOptions;
import org.petero.droidfish.R;
@ -366,9 +365,9 @@ public class TextIO {
localized = false;
int p = pos.getPiece(move.from);
if (localized)
ret.append(pieceToCharLocalized(p));
ret.append(pieceToCharLocalized(p, false));
else
ret.append(pieceToChar(p));
ret.append(pieceToChar(p, false));
int x1 = Position.getX(move.from);
int y1 = Position.getY(move.from);
int x2 = Position.getX(move.to);
@ -418,9 +417,9 @@ public class TextIO {
ret.append((char) (y2 + '1'));
if (move.promoteTo != Piece.EMPTY) {
if (localized)
ret.append(pieceToCharLocalized(move.promoteTo));
ret.append(pieceToCharLocalized(move.promoteTo, false));
else
ret.append(pieceToChar(move.promoteTo));
ret.append(pieceToChar(move.promoteTo, false));
}
}
UndoInfo ui = new UndoInfo();
@ -724,7 +723,7 @@ public class TextIO {
ret.append(dark ? ".. |" : " |");
} else {
ret.append(Piece.isWhite(p) ? ' ' : '*');
String pieceName = pieceToChar(p);
String pieceName = pieceToChar(p, false);
if (pieceName.length() == 0)
pieceName = "P";
ret.append(pieceName);
@ -738,40 +737,26 @@ public class TextIO {
return ret.toString();
}
/** Convert a piece and a square to a string, such as Nf3. */
public static String pieceAndSquareToString(int currentPieceType, int p, int sq) {
StringBuilder ret = new StringBuilder();
if (currentPieceType == PGNOptions.PT_FIGURINE) {
ret.append(Piece.toUniCode(p));
} else {
boolean localized = (currentPieceType != PGNOptions.PT_ENGLISH);
if ((p == Piece.WPAWN) || (p == Piece.BPAWN))
ret.append(localized ? pieceNames[0] : "P");
else
ret.append(localized ? pieceToCharLocalized(p) : pieceToChar(p));
}
ret.append(squareToString(sq));
return ret.toString();
}
private static String pieceToChar(int p) {
public static String pieceToChar(int p, boolean namedPawn) {
switch (p) {
case Piece.WQUEEN: case Piece.BQUEEN: return "Q";
case Piece.WROOK: case Piece.BROOK: return "R";
case Piece.WBISHOP: case Piece.BBISHOP: return "B";
case Piece.WKNIGHT: case Piece.BKNIGHT: return "N";
case Piece.WKING: case Piece.BKING: return "K";
case Piece.WKING: case Piece.BKING: return "K";
case Piece.WQUEEN: case Piece.BQUEEN: return "Q";
case Piece.WROOK: case Piece.BROOK: return "R";
case Piece.WBISHOP: case Piece.BBISHOP: return "B";
case Piece.WKNIGHT: case Piece.BKNIGHT: return "N";
case Piece.WPAWN: case Piece.BPAWN: if (namedPawn) return "P";
}
return "";
}
public static String pieceToCharLocalized(int p) {
public static String pieceToCharLocalized(int p, boolean namedPawn) {
switch (p) {
case Piece.WQUEEN: case Piece.BQUEEN: return pieceNames[4];
case Piece.WROOK: case Piece.BROOK: return pieceNames[3];
case Piece.WBISHOP: case Piece.BBISHOP: return pieceNames[2];
case Piece.WKNIGHT: case Piece.BKNIGHT: return pieceNames[1];
case Piece.WKING: case Piece.BKING: return pieceNames[5];
case Piece.WKING: case Piece.BKING: return pieceNames[5];
case Piece.WQUEEN: case Piece.BQUEEN: return pieceNames[4];
case Piece.WROOK: case Piece.BROOK: return pieceNames[3];
case Piece.WBISHOP: case Piece.BBISHOP: return pieceNames[2];
case Piece.WKNIGHT: case Piece.BKNIGHT: return pieceNames[1];
case Piece.WPAWN: case Piece.BPAWN: if (namedPawn) return pieceNames[0];
}
return "";
}