DroidFish: Added support for default player name.

This commit is contained in:
Peter Osterlund 2012-04-07 15:45:34 +00:00
parent 926fe6b214
commit e6e4d6b213
5 changed files with 78 additions and 5 deletions

View File

@ -357,10 +357,14 @@ you are not actively using the program.\
<string name="option_about">About / Help</string>
<string name="prefs_playing_options">Playing Options</string>
<string name="prefs_playerName_title">Player Name</string>
<string name="prefs_playerName_summary">Default player name in new games</string>
<string name="prefs_boardFlipped_title">Flip Board</string>
<string name="prefs_boardFlipped_summary">View board from black side</string>
<string name="prefs_autoSwapSides_title">Auto Swap Sides</string>
<string name="prefs_autoSwapSides_summary">Automatically swap sides when new game started. Also overrides Flip Board setting</string>
<string name="prefs_playerNameFlip_title">Flip Board: Player</string>
<string name="prefs_playerNameFlip_summary">Use Player Name to flip board automatically</string>
<string name="prefs_engine_settings">Engine Settings</string>
<string name="prefs_strength_title">Strength</string>
<string name="prefs_strength_summary">Only supported by internal engines</string>

View File

@ -3,6 +3,12 @@
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/prefs_playing_options">
<EditTextPreference
android:key="playerName"
android:title="@string/prefs_playerName_title"
android:summary="@string/prefs_playerName_summary"
android:defaultValue="Player">
</EditTextPreference>
<CheckBoxPreference
android:key="boardFlipped"
android:title="@string/prefs_boardFlipped_title"
@ -15,7 +21,13 @@
android:summary="@string/prefs_autoSwapSides_summary"
android:defaultValue="false">
</CheckBoxPreference>
</PreferenceCategory>
<CheckBoxPreference
android:key="playerNameFlip"
android:title="@string/prefs_playerNameFlip_title"
android:summary="@string/prefs_playerNameFlip_summary"
android:defaultValue="false">
</CheckBoxPreference>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/prefs_engine_settings">
<org.petero.droidfish.SeekBarPreference

View File

@ -154,8 +154,10 @@ public class DroidFish extends Activity implements GUIInterface {
private GameMode gameMode;
private boolean mPonderMode;
private int mEngineThreads;
private String playerName;
private boolean boardFlipped;
private boolean autoSwapSides;
private boolean playerNameFlip;
private TextView status;
private ScrollView moveListScroll;
@ -239,6 +241,7 @@ public class DroidFish extends Activity implements GUIInterface {
if (pgn != null) {
try {
ctrl.setFENOrPGN(pgn);
setBoardFlip(true);
} catch (ChessParseError e) {
}
}
@ -566,9 +569,11 @@ public class DroidFish extends Activity implements GUIInterface {
private final void readPrefs() {
int modeNr = getIntSetting("gameMode", 1);
gameMode = new GameMode(modeNr);
playerName = settings.getString("playerName", "Player");
boardFlipped = settings.getBoolean("boardFlipped", false);
autoSwapSides = settings.getBoolean("autoSwapSides", false);
setBoardFlip();
playerNameFlip = settings.getBoolean("playerNameFlip", false);
setBoardFlip(true);
boolean drawSquareLabels = settings.getBoolean("drawSquareLabels", false);
cb.setDrawSquareLabels(drawSquareLabels);
cb.oneTouchMoves = settings.getBoolean("oneTouchMoves", false);
@ -849,6 +854,7 @@ public class DroidFish extends Activity implements GUIInterface {
try {
String fen = data.getAction();
ctrl.setFENOrPGN(fen);
setBoardFlip(true);
} catch (ChessParseError e) {
}
}
@ -858,6 +864,7 @@ public class DroidFish extends Activity implements GUIInterface {
try {
String pgn = data.getAction();
ctrl.setFENOrPGN(pgn);
setBoardFlip(true);
} catch (ChessParseError e) {
Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show();
}
@ -888,8 +895,46 @@ public class DroidFish extends Activity implements GUIInterface {
return getString(e.resourceId);
}
private final int nameMatchScore(String name, String match) {
if (name == null)
return 0;
String lName = name.toLowerCase();
String lMatch = match.toLowerCase();
if (name.equals(match))
return 6;
if (lName.equals(lMatch))
return 5;
if (name.startsWith(match))
return 4;
if (lName.startsWith(lMatch))
return 3;
if (name.contains(match))
return 2;
if (lName.contains(lMatch))
return 1;
return 0;
}
private final void setBoardFlip() {
setBoardFlip(false);
}
private final void setBoardFlip(boolean matchPlayerNames) {
boolean flipped = boardFlipped;
if (playerNameFlip && matchPlayerNames && (ctrl != null)) {
final TreeMap<String,String> headers = new TreeMap<String,String>();
ctrl.getHeaders(headers);
int whiteMatch = nameMatchScore(headers.get("White"), playerName);
int blackMatch = nameMatchScore(headers.get("Black"), playerName);
if (( flipped && (whiteMatch > blackMatch)) ||
(!flipped && (whiteMatch < blackMatch))) {
flipped = !flipped;
boardFlipped = flipped;
Editor editor = settings.edit();
editor.putBoolean("boardFlipped", boardFlipped);
editor.commit();
}
}
if (autoSwapSides) {
if (gameMode.analysisMode()) {
flipped = !cb.pos.whiteMove;
@ -1002,6 +1047,11 @@ public class DroidFish extends Activity implements GUIInterface {
return getApplicationContext();
}
@Override
public String playerName() {
return playerName;
}
/** Report a move made that is a candidate for GUI animation. */
public void setAnimMove(Position sourcePos, Move move, boolean forward) {
if (animateMoves && (move != null))
@ -1178,6 +1228,7 @@ public class DroidFish extends Activity implements GUIInterface {
String fenPgn = clipboard.getText().toString();
try {
ctrl.setFENOrPGN(fenPgn);
setBoardFlip(true);
} catch (ChessParseError e) {
Toast.makeText(getApplicationContext(), getParseErrString(e), Toast.LENGTH_SHORT).show();
}
@ -1550,6 +1601,7 @@ public class DroidFish extends Activity implements GUIInterface {
editor.commit();
gameMode = new GameMode(gameModeType);
ctrl.setGameMode(gameMode);
setBoardFlip(true);
}
}
});

View File

@ -91,4 +91,7 @@ public interface GUIInterface {
/** Return application context. */
public Context getContext();
/** Get the default player name. */
public String playerName();
}

View File

@ -522,7 +522,8 @@ public class DroidChessController {
/** Get PGN header tags and values. */
public final synchronized void getHeaders(Map<String,String> headers) {
game.tree.getHeaders(headers);
if (game != null)
game.tree.getHeaders(headers);
}
/** Set PGN header tags and values. */
@ -833,8 +834,9 @@ public class DroidChessController {
if (strength < 1000)
engine += String.format(" (%.1f%%)", strength * 0.1);
}
String white = gameMode.playerWhite() ? "Player" : engine;
String black = gameMode.playerBlack() ? "Player" : engine;
String player = gui.playerName();
String white = gameMode.playerWhite() ? player : engine;
String black = gameMode.playerBlack() ? player : engine;
game.tree.setPlayerNames(white, black);
}
}