DroidFish: Moved FEN/PGN parse error messages to strings.xml.

This commit is contained in:
Peter Osterlund 2011-12-17 12:49:33 +00:00
parent 5c09f71347
commit 6fb74d81b5
5 changed files with 57 additions and 19 deletions

View File

@ -370,6 +370,18 @@ you are not actively using the program.\
<string name="delete_file_question">Delete file?</string> <string name="delete_file_question">Delete file?</string>
<string name="delete_named_file">Delete file %s?</string> <string name="delete_named_file">Delete file %s?</string>
<string name="err_too_few_spaces">Too few spaces</string>
<string name="err_invalid_piece">Invalid piece</string>
<string name="err_invalid_side">Invalid side</string>
<string name="err_invalid_castling_flags">Invalid castling flags</string>
<string name="err_invalid_en_passant_square">Invalid en passant square</string>
<string name="err_white_num_kings">White must have exactly one king</string>
<string name="err_black_num_kings">Black must have exactly one king</string>
<string name="err_king_capture_possible">King capture possible</string>
<string name="err_too_many_rows">Too many rows</string>
<string name="err_too_many_columns">Too many columns</string>
<string name="err_pawn_on_first_last_rank">Pawn on first/last rank</string>
<string name="option_new_game">New Game</string> <string name="option_new_game">New Game</string>
<string name="option_file">File</string> <string name="option_file">File</string>
<string name="option_resign_game">Resign game</string> <string name="option_resign_game">Resign game</string>

View File

@ -754,7 +754,7 @@ public class DroidFish extends Activity implements GUIInterface {
String pgn = data.getAction(); String pgn = data.getAction();
ctrl.setFENOrPGN(pgn); ctrl.setFENOrPGN(pgn);
} catch (ChessParseError e) { } catch (ChessParseError e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show();
} }
} }
break; break;
@ -776,6 +776,13 @@ public class DroidFish extends Activity implements GUIInterface {
} }
} }
private final String getParseErrString(ChessParseError e) {
if (e.resourceId == -1)
return e.getMessage();
else
return getString(e.resourceId);
}
private final void setBoardFlip() { private final void setBoardFlip() {
boolean flipped = boardFlipped; boolean flipped = boardFlipped;
if (autoSwapSides) { if (autoSwapSides) {
@ -1049,7 +1056,7 @@ public class DroidFish extends Activity implements GUIInterface {
try { try {
ctrl.setFENOrPGN(fenPgn); ctrl.setFENOrPGN(fenPgn);
} catch (ChessParseError e) { } catch (ChessParseError e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show();
} }
} }
break; break;

View File

@ -207,11 +207,18 @@ public class EditBoard extends Activity {
status.setText(""); status.setText("");
return true; return true;
} catch (ChessParseError e) { } catch (ChessParseError e) {
status.setText(e.getMessage()); status.setText(getParseErrString(e));
} }
return false; return false;
} }
private final String getParseErrString(ChessParseError e) {
if (e.resourceId == -1)
return e.getMessage();
else
return getString(e.resourceId);
}
static final int EDIT_DIALOG = 0; static final int EDIT_DIALOG = 0;
static final int SIDE_DIALOG = 1; static final int SIDE_DIALOG = 1;
static final int CASTLE_DIALOG = 2; static final int CASTLE_DIALOG = 2;
@ -292,7 +299,7 @@ public class EditBoard extends Activity {
} catch (ChessParseError e) { } catch (ChessParseError e) {
if (e.pos != null) if (e.pos != null)
cb.setPosition(e.pos); cb.setPosition(e.pos);
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show();
} }
cb.setSelection(-1); cb.setSelection(-1);
checkValid(); checkValid();

View File

@ -26,9 +26,8 @@ public class ChessParseError extends Exception {
private static final long serialVersionUID = -6051856171275301175L; private static final long serialVersionUID = -6051856171275301175L;
public Position pos; public Position pos;
public int resourceId = -1;
public ChessParseError() {
}
public ChessParseError(String msg) { public ChessParseError(String msg) {
super(msg); super(msg);
pos = null; pos = null;
@ -37,4 +36,16 @@ public class ChessParseError extends Exception {
super(msg); super(msg);
this.pos = pos; this.pos = pos;
} }
public ChessParseError(int resourceId) {
super("");
pos = null;
this.resourceId = resourceId;
}
public ChessParseError(int resourceId, Position pos) {
super("");
this.pos = pos;
this.resourceId = resourceId;
}
} }

View File

@ -21,6 +21,8 @@ package org.petero.droidfish.gamelogic;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.petero.droidfish.R;
/** /**
* *
@ -34,7 +36,7 @@ public class TextIO {
Position pos = new Position(); Position pos = new Position();
String[] words = fen.split(" "); String[] words = fen.split(" ");
if (words.length < 2) { if (words.length < 2) {
throw new ChessParseError("Too few spaces"); throw new ChessParseError(R.string.err_too_few_spaces);
} }
for (int i = 0; i < words.length; i++) { for (int i = 0; i < words.length; i++) {
words[i] = words[i].trim(); words[i] = words[i].trim();
@ -67,11 +69,11 @@ public class TextIO {
case 'r': safeSetPiece(pos, col, row, Piece.BROOK); col++; break; case 'r': safeSetPiece(pos, col, row, Piece.BROOK); col++; break;
case 'q': safeSetPiece(pos, col, row, Piece.BQUEEN); col++; break; case 'q': safeSetPiece(pos, col, row, Piece.BQUEEN); col++; break;
case 'k': safeSetPiece(pos, col, row, Piece.BKING); col++; break; case 'k': safeSetPiece(pos, col, row, Piece.BKING); col++; break;
default: throw new ChessParseError("Invalid piece", pos); default: throw new ChessParseError(R.string.err_invalid_piece, pos);
} }
} }
if (words[1].length() == 0) { if (words[1].length() == 0) {
throw new ChessParseError("Invalid side", pos); throw new ChessParseError(R.string.err_invalid_side, pos);
} }
pos.setWhiteMove(words[1].charAt(0) == 'w'); pos.setWhiteMove(words[1].charAt(0) == 'w');
@ -96,7 +98,7 @@ public class TextIO {
case '-': case '-':
break; break;
default: default:
throw new ChessParseError("Invalid castling flags", pos); throw new ChessParseError(R.string.err_invalid_castling_flags, pos);
} }
} }
} }
@ -108,7 +110,7 @@ public class TextIO {
String epString = words[3]; String epString = words[3];
if (!epString.equals("-")) { if (!epString.equals("-")) {
if (epString.length() < 2) { if (epString.length() < 2) {
throw new ChessParseError("Invalid en passant square", pos); throw new ChessParseError(R.string.err_invalid_en_passant_square, pos);
} }
pos.setEpSquare(getSquare(epString)); pos.setEpSquare(getSquare(epString));
} }
@ -139,17 +141,17 @@ public class TextIO {
} }
} }
if (wKings != 1) { if (wKings != 1) {
throw new ChessParseError("White must have exactly one king", pos); throw new ChessParseError(R.string.err_white_num_kings, pos);
} }
if (bKings != 1) { if (bKings != 1) {
throw new ChessParseError("Black must have exactly one king", pos); throw new ChessParseError(R.string.err_black_num_kings, pos);
} }
// Make sure king can not be captured // Make sure king can not be captured
Position pos2 = new Position(pos); Position pos2 = new Position(pos);
pos2.setWhiteMove(!pos.whiteMove); pos2.setWhiteMove(!pos.whiteMove);
if (MoveGen.inCheck(pos2)) { if (MoveGen.inCheck(pos2)) {
throw new ChessParseError("King capture possible", pos); throw new ChessParseError(R.string.err_king_capture_possible, pos);
} }
fixupEPSquare(pos); fixupEPSquare(pos);
@ -187,18 +189,17 @@ public class TextIO {
} }
} }
} }
if (!epValid) { if (!epValid)
pos.setEpSquare(-1); pos.setEpSquare(-1);
}
} }
} }
private static final void safeSetPiece(Position pos, int col, int row, int p) throws ChessParseError { private static final void safeSetPiece(Position pos, int col, int row, int p) throws ChessParseError {
if (row < 0) throw new ChessParseError("Too many rows"); if (row < 0) throw new ChessParseError(R.string.err_too_many_rows);
if (col > 7) throw new ChessParseError("Too many columns"); if (col > 7) throw new ChessParseError(R.string.err_too_many_columns);
if ((p == Piece.WPAWN) || (p == Piece.BPAWN)) { if ((p == Piece.WPAWN) || (p == Piece.BPAWN)) {
if ((row == 0) || (row == 7)) if ((row == 0) || (row == 7))
throw new ChessParseError("Pawn on first/last rank"); throw new ChessParseError(R.string.err_pawn_on_first_last_rank);
} }
pos.setPiece(Position.getSquare(col, row), p); pos.setPiece(Position.getSquare(col, row), p);
} }