From 6fb74d81b5c1ef1375970117f6f1106df7ca266f Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Sat, 17 Dec 2011 12:49:33 +0000 Subject: [PATCH] DroidFish: Moved FEN/PGN parse error messages to strings.xml. --- DroidFish/res/values/strings.xml | 12 +++++++++ .../src/org/petero/droidfish/DroidFish.java | 11 ++++++-- .../droidfish/activities/EditBoard.java | 11 ++++++-- .../droidfish/gamelogic/ChessParseError.java | 15 +++++++++-- .../petero/droidfish/gamelogic/TextIO.java | 27 ++++++++++--------- 5 files changed, 57 insertions(+), 19 deletions(-) diff --git a/DroidFish/res/values/strings.xml b/DroidFish/res/values/strings.xml index e17e9d6..c9dcc6e 100644 --- a/DroidFish/res/values/strings.xml +++ b/DroidFish/res/values/strings.xml @@ -370,6 +370,18 @@ you are not actively using the program.\ Delete file? Delete file %s? + Too few spaces + Invalid piece + Invalid side + Invalid castling flags + Invalid en passant square + White must have exactly one king + Black must have exactly one king + King capture possible + Too many rows + Too many columns + Pawn on first/last rank + New Game File Resign game diff --git a/DroidFish/src/org/petero/droidfish/DroidFish.java b/DroidFish/src/org/petero/droidfish/DroidFish.java index 88489b6..3a84f7a 100644 --- a/DroidFish/src/org/petero/droidfish/DroidFish.java +++ b/DroidFish/src/org/petero/droidfish/DroidFish.java @@ -754,7 +754,7 @@ public class DroidFish extends Activity implements GUIInterface { String pgn = data.getAction(); ctrl.setFENOrPGN(pgn); } catch (ChessParseError e) { - Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show(); } } 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() { boolean flipped = boardFlipped; if (autoSwapSides) { @@ -1049,7 +1056,7 @@ public class DroidFish extends Activity implements GUIInterface { try { ctrl.setFENOrPGN(fenPgn); } catch (ChessParseError e) { - Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show(); } } break; diff --git a/DroidFish/src/org/petero/droidfish/activities/EditBoard.java b/DroidFish/src/org/petero/droidfish/activities/EditBoard.java index cb83ccb..3a59f2f 100644 --- a/DroidFish/src/org/petero/droidfish/activities/EditBoard.java +++ b/DroidFish/src/org/petero/droidfish/activities/EditBoard.java @@ -207,11 +207,18 @@ public class EditBoard extends Activity { status.setText(""); return true; } catch (ChessParseError e) { - status.setText(e.getMessage()); + status.setText(getParseErrString(e)); } 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 SIDE_DIALOG = 1; static final int CASTLE_DIALOG = 2; @@ -292,7 +299,7 @@ public class EditBoard extends Activity { } catch (ChessParseError e) { if (e.pos != null) 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); checkValid(); diff --git a/DroidFish/src/org/petero/droidfish/gamelogic/ChessParseError.java b/DroidFish/src/org/petero/droidfish/gamelogic/ChessParseError.java index 856e952..62a68cc 100644 --- a/DroidFish/src/org/petero/droidfish/gamelogic/ChessParseError.java +++ b/DroidFish/src/org/petero/droidfish/gamelogic/ChessParseError.java @@ -26,9 +26,8 @@ public class ChessParseError extends Exception { private static final long serialVersionUID = -6051856171275301175L; public Position pos; + public int resourceId = -1; - public ChessParseError() { - } public ChessParseError(String msg) { super(msg); pos = null; @@ -37,4 +36,16 @@ public class ChessParseError extends Exception { super(msg); 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; + } } diff --git a/DroidFish/src/org/petero/droidfish/gamelogic/TextIO.java b/DroidFish/src/org/petero/droidfish/gamelogic/TextIO.java index 2e56b9c..80f88b4 100644 --- a/DroidFish/src/org/petero/droidfish/gamelogic/TextIO.java +++ b/DroidFish/src/org/petero/droidfish/gamelogic/TextIO.java @@ -21,6 +21,8 @@ package org.petero.droidfish.gamelogic; import java.util.ArrayList; import java.util.List; +import org.petero.droidfish.R; + /** * @@ -34,7 +36,7 @@ public class TextIO { Position pos = new Position(); String[] words = fen.split(" "); 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++) { words[i] = words[i].trim(); @@ -67,11 +69,11 @@ public class TextIO { case 'r': safeSetPiece(pos, col, row, Piece.BROOK); col++; break; case 'q': safeSetPiece(pos, col, row, Piece.BQUEEN); 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) { - throw new ChessParseError("Invalid side", pos); + throw new ChessParseError(R.string.err_invalid_side, pos); } pos.setWhiteMove(words[1].charAt(0) == 'w'); @@ -96,7 +98,7 @@ public class TextIO { case '-': break; 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]; if (!epString.equals("-")) { 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)); } @@ -139,17 +141,17 @@ public class TextIO { } } 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) { - 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 Position pos2 = new Position(pos); pos2.setWhiteMove(!pos.whiteMove); if (MoveGen.inCheck(pos2)) { - throw new ChessParseError("King capture possible", pos); + throw new ChessParseError(R.string.err_king_capture_possible, pos); } fixupEPSquare(pos); @@ -187,18 +189,17 @@ public class TextIO { } } } - if (!epValid) { + if (!epValid) pos.setEpSquare(-1); - } } } 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 (col > 7) throw new ChessParseError("Too many columns"); + if (row < 0) throw new ChessParseError(R.string.err_too_many_rows); + if (col > 7) throw new ChessParseError(R.string.err_too_many_columns); if ((p == Piece.WPAWN) || (p == Piece.BPAWN)) { 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); }