Android Open Source - android-chess U I






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.android.chess;
// w ww  . jav a  2 s  .co m
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import jwtc.chess.*;
import jwtc.chess.board.*;


public class UI extends GameControl
{
  // last mouse click position
  protected int m_iFrom;
  
  
  // searchthread message handling
  public static final int MSG_MOVE = 1;
  public static final int MSG_UCI_MOVE = 2;
  public static final int MSG_PLAY = 3;
  public static final int MSG_TEXT = 5;
  protected Handler m_searchThreadUpdateHandler = new Handler(){
        /** Gets called on every message that is received */
        // @Override
        public void handleMessage(Message msg) {
             
            Log.i("searchThreadUpdateHandler", "handle " + msg.what);
            // do move
            if(msg.what == MSG_MOVE){
              
              enableControl();
              int move = msg.getData().getInt("move");
              if(move == 0){
                setMessage("No move found");
              } else
                move(move, "", true);
              
              updateState();
              playNotification();
            }
            else if(msg.what == MSG_UCI_MOVE){
             enableControl();
             doUCIMove(msg.getData().getInt("from"), msg.getData().getInt("to"), msg.getData().getInt("promo"));
             playNotification();
            }
            else if(msg.what == MSG_TEXT){
              setEngineMessage(msg.getData().getString("text"));
            }
            else if(msg.what == MSG_PLAY){
              play();
            } 
             
            super.handleMessage(msg);
        }
   }; 

  
  
  public UI()
  {
    m_iFrom = -1;
    m_bActive = true;
  }
  
  public void start()
  {
    updateState();
  }
  
  public void newGame()
  {
    super.newGame();
    updateState();
  }
  public void undo()
  {
    super.undo();
    updateState();
  }

  protected boolean requestMove(int from, int to)
  {
    //Log.i("requestMove debug", m_game.getBoard().getPGNMoves(new ChessBoard()));
    if(_jni.isEnded() != 0)
      return false;
    
    if(_jni.requestMove(from, to) == 0)
    {
      setMessage(R.string.msg_illegal_move);
      return false;
    }
    
    addPGNEntry(_jni.getNumBoard()-1, _jni.getMyMoveToString(), "", _jni.getMyMove(), true);
    
    updateState();
    if(_jni.isEnded() == 0 && getPlayMode() == HUMAN_PC){
      play();
    }
    return true;
  }
  
  
  // in case of ambigious castle (Fischer random), this methodis called to handle the move
  protected void requestMoveCastle(int from, int to){
    if(_jni.isEnded() != 0)
      return;
    
    _jni.doCastleMove(from, to);
    addPGNEntry(_jni.getNumBoard()-1, _jni.getMyMoveToString(), "", _jni.getMyMove(), true);
    
    updateState();
    if(_jni.isEnded() == 0 && getPlayMode() == HUMAN_PC){
      play();
    }
  }
  
  protected void doUCIMove(int from, int to, int promo){
    if(promo > 0){
      _jni.setPromo(promo);
    }

    if(_jni.isEnded() != 0)
      return ;
    
    if(_jni.requestMove(from, to) == 0)
    {
      setMessage(R.string.msg_illegal_move); // UCI should make legal move
      return ;
    }
    
    addPGNEntry(_jni.getNumBoard()-1, _jni.getMyMoveToString(), "", _jni.getMyMove(), true);
    
    updateState();
  }
  
  @Override
  public void updateState()
  {
    super.updateState();
    paintBoard();
    
     
    
    //if(m_choiceMode.getSelectedIndex() == HUMAN_HUMAN)
    //  setMessage(m_game.getBoard().getPGNMoves(m_game.getBoardRefurbish()));
    
    //co.pl(ChessBoard.bitbToString(m_game.getBoard().bitbAttacked()));
  }
  
  public int chessStateToR(int s){
    switch(s)
    {
    case ChessBoard.MATE: return R.string.state_mate;
    case ChessBoard.DRAW_MATERIAL: return R.string.state_draw_material; 
    case ChessBoard.CHECK: return R.string.state_check; 
    case ChessBoard.STALEMATE: return R.string.state_draw_stalemate; 
    case ChessBoard.DRAW_50: return R.string.state_draw_50; 
    case ChessBoard.DRAW_REPEAT: return R.string.state_draw_repeat;
    default: return R.string.state_play; 
    }
  }
  
  public void paintBoard(){
    setMessage("paint from UI");
  }

  // handle call from clickedEvent with parameters for the x and y coords of the point
  public boolean handleClick(int index)
  {
    //m_textStatus.setText("");
    if(false == m_bActive)
    {
      setMessage(R.string.msg_wait);
      return false;
    }
    
    if(m_iFrom == -1)
    {
      int turn = _jni.getTurn();
      if(_jni.pieceAt(turn, index) == BoardConstants.FIELD)
      {
        return false;
      }
      m_iFrom = index;
      paintBoard();
    }
    else
    {
      // test and make move if valid move
      boolean bValid = requestMove(m_iFrom, index);
      m_iFrom = -1;
      if(false == bValid){
        paintBoard();
        return false;
      }
    }  
    return true;
  }
    
  public int getPlayMode()
  {
    return HUMAN_PC; //m_choiceMode.getSelectedIndex();
  }
  
  public void playNotification(){
    
  }
  @Override
  public void setMessage(String sMsg)
  {
  }
  @Override
  public void setEngineMessage(String sText)
  {
  }
  public void setMessage(int res){
    
  }

  @Override
  public void sendMessageFromThread(String sText)
  {
    Message m = new Message();
    Bundle b = new Bundle();
    m.what = MSG_TEXT;
    b.putString("text", sText);
    m.setData(b);
    m_searchThreadUpdateHandler.sendMessage(m);
  }
  @Override
  public void sendMoveMessageFromThread(int move){
    Message m = new Message();
    Bundle b = new Bundle();
    b.putInt("move", move);
    m.what = MSG_MOVE;
    m.setData(b);
    m_searchThreadUpdateHandler.sendMessage(m);
  }
  @Override
  public void sendUCIMoveMessageFromThread(int from, int to, int promo){
    Message m = new Message();
    Bundle b = new Bundle();
    b.putInt("from", from);
    b.putInt("to", to);
    b.putInt("promo", promo);
    m.what = MSG_UCI_MOVE;
    m.setData(b);
    m_searchThreadUpdateHandler.sendMessage(m);
  }
}




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