Android Open Source - android-chess Move






From Project

Back to project page android-chess.

License

The source code is released under:

MIT License

If you think the Android project android-chess listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jwtc.chess;
/*w  ww.j av  a2  s. com*/
// Move. Wrapper class for the integer representation of a move
// The positional values "from" and "to" are shifted into the integer.
// Also en-passant, castling, hit, first pawn move, promotion and promotion piece
// are part of the move.

public class Move
{
  // mask for a position [0-63], 6 bits.
  private static final int MASK_POS = 0x3F;
  // mask for a boolean [false=0, true=1], 1 bit
  private static final int MASK_BOOL = 1;
  
  // shift values
  private static final int SHIFT_TO = 6;
  private static final int SHIFT_EP = 13;
  // short castling OO
  private static final int SHIFT_OO = 14;
  // long castling OOO
  private static final int SHIFT_OOO = 15;
  private static final int SHIFT_HIT = 16;
  // is the first 2 step move of a pawn
  private static final int SHIFT_FIRSTPAWN = 17;
  // with this move the opponent king is checked
  private static final int SHIFT_CHECK = 18;
  // a pawn is promoted with this move
  private static final int SHIFT_PROMOTION = 19;
  // the piece the pawn is promoted to
  private static final int SHIFT_PROMOTIONPIECE = 20;
  
  // returns the integer representation of the simpelest move, from
  // position @from to position @to
  public static final int makeMove(final int from, final int to)
  {
    return from | (to << SHIFT_TO);
  }
  public static final int makeMoveFirstPawn(final int from, final int to)
  {
    return from | (to << SHIFT_TO) | (1 << SHIFT_FIRSTPAWN);
  }
  public static final int makeMoveHit(final int from, final int to)
  {
    //co.pl("makeMovehit " + from + "-" + to);
    return from | (to << SHIFT_TO) | (1 << SHIFT_HIT);
  }
  public static final int makeMoveEP(final int from, final int to)
  {
    return from | (to << SHIFT_TO) | (1 << SHIFT_HIT) | (1 << SHIFT_EP);
  }
  public static final int makeMoveOO(final int from, final int to)
  {
    return from | (to << SHIFT_TO) | (1 << SHIFT_OO);
  }
  public static final int makeMoveOOO(final int from, final int to)
  {
    return from | (to << SHIFT_TO) | (1 << SHIFT_OOO);
  }
  public static final int makeMovePromotion(final int from, final int to, final int piece, boolean bHit)
  {
    return from | (to << SHIFT_TO) | (1 << SHIFT_PROMOTION) | (piece << SHIFT_PROMOTIONPIECE) | (bHit == true ? (1 << SHIFT_HIT) : 0);
  }
  public static final int setCheck(final int move)
  {
    return move | (1 << SHIFT_CHECK);
  }
  // returns true when "from" and "to" are equal in both arguments
  public static final boolean equalPositions(final int m, final int m2)
  {
    return (m & MASK_POS) == (m2 & MASK_POS) && ((m >> SHIFT_TO) & MASK_POS) == ((m2 >> SHIFT_TO) & MASK_POS);
  }
  // return true when "to" in both arguments are equal
  public static final boolean equalTos(int m, int m2)
  {
    return ((m >> SHIFT_TO) & MASK_POS) == ((m2 >> SHIFT_TO) & MASK_POS);
  }

  // returns "from" of the move
  public static final int getFrom(final int move)
  {
    return move & MASK_POS;
  }
  public static final int getTo(final int move)
  {
    return (move >> SHIFT_TO) & MASK_POS;
  }
  public static final boolean isEP(final int move)
  {
    return ((move >> SHIFT_EP) & MASK_BOOL) == MASK_BOOL;
  }
  public static final boolean isOO(final int move)
  {
    return ((move >> SHIFT_OO) & MASK_BOOL) == MASK_BOOL;
  }
  public static final boolean isOOO(final int move)
  {
    return ((move >> SHIFT_OOO) & MASK_BOOL) == MASK_BOOL;
  }
  public static final boolean isHIT(final int move)
  {
    return ((move >> SHIFT_HIT) & MASK_BOOL) == MASK_BOOL;
  }
  public static final boolean isCheck(final int move)
  {
    return ((move >> SHIFT_CHECK) & MASK_BOOL) == MASK_BOOL;
  }
  public static final boolean isFirstPawnMove(final int move)
  {
    return ((move >> SHIFT_FIRSTPAWN) & MASK_BOOL) == MASK_BOOL;
  }
  public static final boolean isPromotionMove(final int move)
  {
    return ((move >> SHIFT_PROMOTION) & MASK_BOOL) == MASK_BOOL;
  }
  public static final int getPromotionPiece(final int move)
  {
    return move >> SHIFT_PROMOTIONPIECE;
  }

  // returns pgn alike string representation of the move - not full pgn format because then more information is needed
  public static final String toDbgString(final int move)
  {
    if(Move.isOO(move))
      return "O-O";
    if(Move.isOOO(move))
      return "O-O-O";
    return "[" + Pos.toString(Move.getFrom(move)) + (Move.isHIT(move) ? "x" : "-") + Pos.toString(Move.getTo(move)) + (Move.isCheck(move) ? "+" : "") + (Move.isEP(move) ? " ep" : "") + "]";
  }
  
}




Java Source Code List

jwtc.android.chess.ChessFieldView.java
jwtc.android.chess.ChessImageView.java
jwtc.android.chess.ChessPreferences.java
jwtc.android.chess.ChessViewBase.java
jwtc.android.chess.ChessView.java
jwtc.android.chess.GamesListView.java
jwtc.android.chess.HtmlActivity.java
jwtc.android.chess.ImageCacheObject.java
jwtc.android.chess.MyBaseActivity.java
jwtc.android.chess.MyPGNProvider.java
jwtc.android.chess.PGNView.java
jwtc.android.chess.SaveGameDlg.java
jwtc.android.chess.UI.java
jwtc.android.chess.convergence.Connection.java
jwtc.android.chess.convergence.ConvergenceActivity.java
jwtc.android.chess.convergence.RestServer.java
jwtc.android.chess.iconifiedlist.IconifiedTextListAdapter.java
jwtc.android.chess.iconifiedlist.IconifiedTextView.java
jwtc.android.chess.iconifiedlist.IconifiedText.java
jwtc.android.chess.ics.CustomCommands.java
jwtc.android.chess.ics.ICSChatDlg.java
jwtc.android.chess.ics.ICSChessView.java
jwtc.android.chess.ics.ICSClient.java
jwtc.android.chess.ics.ICSConfirmDlg.java
jwtc.android.chess.ics.ICSMatchDlg.java
jwtc.android.chess.ics.TelnetSocket.java
jwtc.android.chess.ics.TimesealInputStream.java
jwtc.android.chess.ics.TimesealOutputStream.java
jwtc.android.chess.ics.TimesealPipe.java
jwtc.android.chess.ics.TimesealingSocket.java
jwtc.android.chess.puzzle.ChessViewPractice.java
jwtc.android.chess.puzzle.ChessViewPuzzle.java
jwtc.android.chess.puzzle.MyPuzzleProvider.java
jwtc.android.chess.puzzle.practice.java
jwtc.android.chess.puzzle.puzzle.java
jwtc.android.chess.tools.FileListView.java
jwtc.android.chess.tools.PGNProcessor.java
jwtc.android.chess.tools.importactivity.java
jwtc.android.chess.tools.pgntool.java
jwtc.android.chess.main.java
jwtc.android.chess.options.java
jwtc.android.chess.setup.java
jwtc.android.chess.start.java
jwtc.android.timeseal.TimesealingSocket.java
jwtc.android.timeseal.streams.a.java
jwtc.android.timeseal.streams.b.java
jwtc.android.timeseal.streams.c.java
jwtc.chess.ChessPuzzleProvider.java
jwtc.chess.GameControl.java
jwtc.chess.JNI.java
jwtc.chess.Move.java
jwtc.chess.PGNColumns.java
jwtc.chess.PGNEntry.java
jwtc.chess.PGNProvider.java
jwtc.chess.Pos.java
jwtc.chess.Valuation.java
jwtc.chess.algorithm.SearchAlgorithmRunner.java
jwtc.chess.algorithm.UCIWrapper.java
jwtc.chess.board.BoardConstants.java
jwtc.chess.board.BoardHashKeys.java
jwtc.chess.board.BoardMembers.java
jwtc.chess.board.BoardStatics.java
jwtc.chess.board.ChessBoard.java