Android Open Source - android-chess U C I Wrapper






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.algorithm;
/*from  w w w.  j  av a 2  s  .c  o m*/
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;
import jwtc.chess.GameControl;
import jwtc.chess.Pos;

public class UCIWrapper {

  protected GameControl m_control;
  private BufferedReader _reader;//, _readerError;
    private PrintWriter _writer;
    private Process _process;
    
    private Pattern _pattMove = Pattern.compile("([a-h]{1}[1-8]{1})([a-h]{1}[1-8]{1})(q|r|b|n)?");
    
    private static final String TAG = "UCIWrapper";
    
  public UCIWrapper(GameControl gc){
    m_control = gc;
    _process = null;
  }
  
  public boolean isReady(){
    return (_process != null);
  }
  
  public void init(String sEnginePath){
    
    
    
    try {
      Log.i(TAG, "intitializing " + sEnginePath);
        // Executes the command.
      _process = Runtime.getRuntime().exec(sEnginePath);
        
        // Reads stdout.
        _reader = new BufferedReader(new InputStreamReader(_process.getInputStream()));
        //_readerError = new BufferedReader(new InputStreamReader(_process.getErrorStream()));
        _writer = new PrintWriter(new OutputStreamWriter(_process.getOutputStream()));
        
        new Thread(new Runnable(){
            public void run(){
        
            Matcher match; String tmp; int from, to,  iPos, iLine = 0;
            try {
              
              //while ((read = _reader.read(buffer)) > 0) {
              //    output.append(buffer, 0, read);
              //tmp = output.toString();
              
              int i = 0;
              
              while(true){
                tmp = _reader.readLine();
                  
                  //Log.i(TAG, iLine + ">>" + tmp);
                  //m_control.sendMessageFromThread();
                  
                  // parse response
                  iPos = tmp.indexOf("info");

                  if(iPos >= 0){
                    if(i % 10 == 0){
                      Log.i(TAG, tmp);
                      String[] arr = tmp.split(" ");
                      tmp = "";
                      for(int j = 0; j < arr.length; j++){
                      
                        if(arr[j].equals("depth")){
                          tmp += arr[j] + " " + arr[j+1] + "\t";
                        } else if(arr[j].equals("nodes")){
                          tmp += arr[j] + " " + arr[j+1] + "\t";
                        } else if(arr[j].equals("nps")){
                          tmp += arr[j] + " " + arr[j+1] + "\t";
                        } if(arr[j].equals("depth")){
                          
                          //TODO
                        }
                        
                      }
                      if(tmp.length() > 0){
                        m_control.sendMessageFromThread(tmp);
                      }
                    }
                    i++;
                  } else {
                    iPos = tmp.indexOf("bestmove");
                    //Log.i(TAG, "bestmove = " + iPos);
                    if(iPos >= 0){
                      //
                      tmp = tmp.substring(iPos + 9);
                      //Log.i(TAG, tmp);
                      tmp = tmp.substring(0, tmp.indexOf(" "));
                      Log.i(TAG, tmp);
                      match = _pattMove.matcher(tmp);
                        if(match.matches()){
                          
                          from = Pos.fromString(match.group(1));
                          to = Pos.fromString(match.group(2));
                          Log.i(TAG, match.group(1) + "-" + match.group(2) + " move " + from + ", " + to);
                          m_control.sendUCIMoveMessageFromThread(from, to, 0);
                          
                          i = 0;
                        }
                    }
                  }
                  
                  iLine++;
              }
            }catch(Exception ex){
              if(_process != null){
                _process.destroy();
              _process = null;
              }
              Log.e(TAG, "run error: " + ex);
            }
            }
        }).start();
        
        sendCommand("uci");
        
    }catch(Exception e){
      Log.e(TAG, "init error: " + e);
    }
  }
  
  public void play(int msecs, int ply){
    sendCommand("ucinewgame");

    sendCommand("position fen " + m_control.getJNI().toFEN());
    
    if(msecs > 0){
      sendCommand("go movetime " + msecs);
    } else {
      sendCommand("go depth " + ply);
    }
  }
  
  public void quit(){
    sendCommand("quit");
    if(_process != null){
      _process.destroy();
      _process = null;
    }
  }

  public void sendCommand(final String cmd){
    if(_process != null){
      _writer.println(cmd);
      _writer.flush();
      Log.i(TAG, "sendCommand: " + cmd);
    }
  }
  public void stop(){
    sendCommand("stop");
  }
  
  public static void install(final InputStream in, final String sEngine){
    new Thread(new Runnable(){
          public void run(){
        // TODO
        
         try {
            OutputStream out = new FileOutputStream("/data/data/jwtc.android.chess/" + sEngine);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
              out.write(buf, 0, len);
            }
            
            out.close();
            in.close();
          
            runConsole("/system/bin/ls /data/data/jwtc.android.chess/");
            runConsole("/system/bin/chmod 744 /data/data/jwtc.android.chess/" + sEngine);

            Log.i(TAG, "install completed");
         } catch(Exception ex){
           
           Log.e(TAG, "install error: " + ex.toString());
         }
          }
      }).start();
  }
  
  public static String runConsole(String sCmd){
      try {
        // Executes the command.
        Process process = Runtime.getRuntime().exec(sCmd);
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        int read;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();
        
        // Waits for the command to finish.
        process.waitFor();
        
        return output.toString();
        
      } catch (Exception e) {
        return null;
    }
    }
}




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