Chess uci - how to send to engine

edited August 2016 in How To...

Hello,

I have a chess gui and want it to use the chess engine fruit (eg.).

How can I communicate with the engine after launch? I can start it with open but after each move I have to send data with uci

Thanks!

Chrisir

Answers

  • I don't need to know how to make the uci itself but rather the way how to communicate to fruit, is it via println or... what is the equivalent to stdOut?

  • Does Fruit have any API documents? Without them I can't see how to interface with it.

  • how can I build a pipe?

  • Taking up smoking eh :D

    No idea how to create a pipe in this situation but you would need Fruit to accept one end of it.

  • Examples:
    ---------
    
    This is how the communication when the engine boots can look like:
    
    GUI     engine
    
    // tell the engine to switch to UCI mode
    uci
    
    // engine identify  
          id name Shredder
            id author Stefan MK
    
    // engine sends the options it can change
    // the engine can change the hash size from 1 to 128 MB
            option name Hash type spin default 1 min 1 max 128
    
    // the engine supports Nalimov endgame tablebases
            option name NalimovPath type string default <empty>
            option name NalimovCache type spin default 1 min 1 max 32
    
    // the engine can switch off Nullmove and set the playing style
           option name Nullmove type check default true
            option name Style type combo default Normal var Solid var Normal var Risky
    
    // the engine has sent all parameters and is ready
            uciok
    
    // Note: here the GUI can already send a "quit" command if it just wants to find out
    //       details about the engine, so the engine should not initialize its internal
    //       parameters before here.
    // now the GUI sets some values in the engine
    // set hash to 32 MB
    setoption name Hash value 32
    
  • // this is supposed to be a chess program later. 
    
    // this is the GUI part.
    
    // It is supposed to communicates with the chess-engine "fruit".  
    
    // see http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#start()
    // http://stackoverflow.com/questions/5724646/how-to-pipe-input-to-java-program-with-bash
    
    import java.lang.ProcessBuilder;
    import java.io.*;
    
    //InputStreamReader isReader = new InputStreamReader(System.in); // is = inputstream 
    //BufferedReader bufReader = new BufferedReader(isReader);
    
    //OutputStreamWriter isWriter = new OutputStreamWriter(System.out); //  
    //BufferedWriter bufWriter  = new BufferedWriter(isWriter);
    
    ProcessBuilder pb;
    Process p=null;
    
    InputStream in    = null; 
    OutputStream out  = null;
    
    // this directs the discourse 
    int lineToSay = -1; // ????
    boolean guiHasToSaySomething=true; 
    
    // -------------------------------------------------------
    
    void setup() {
      size(700, 400);
    
      pb = new ProcessBuilder( "C:\\Program Files\\Fruit\\Fruit2.2.1.exe", "" );
      // pb.directory(new File("C:\\Program Files\\Fruit\\"));
    
      try {
        p = pb.start();
        println("success 1");
      } 
      catch (Exception e) {
        println("Error");
      }
    
      if (p!=null) {
        // // success
        println("success 2");
        // now get streams - http : // stackoverflow.com/questions/22563986/understanding-getinputstream-and-getoutputstream
        in = p.getInputStream();
        out = p.getOutputStream();
        if (in==null||out==null) {
          println("Error 2 - failed ");
          exit();
        }
      } else {
        println("Error - failed ");
        //exit();
        //return;
      }
      background (0);
    }
    
    void draw() {
    
      background (110);
      fill(255);
      text (lineToSay, 20, 22); 
    
      // getting from engine this:  
      String inputStr = "";
      inputStr = listen();
      // println(inputStr);
      if (inputStr!=null) {
        print(inputStr);
    
        switch (lineToSay) {
    
        case -1:
          lineToSay ++;
          guiHasToSaySomething=true; 
          break; 
    
        case 0:
          // say( "uci\n"); // with \n  !!!!!
          if (inputStr.equals("uciok")||inputStr.contains("uciok")) {
            lineToSay ++;        
            guiHasToSaySomething=true;
            //  println ("Here 2");
          }
          break;
    
        case 1:    
          //say( "isready\n");
          inputStr=inputStr.trim();
          if (inputStr.equals("readyok")||inputStr.contains("readyok")) {
            lineToSay ++;
            //  lineToSay ++;
            guiHasToSaySomething=true;
            println ("Here 1");
          }
          break;
    
    
        case 2:
        case 3:
          // say( "debug off\n"); 
          if (inputStr.equals("readyok")||inputStr.contains("readyok")) {
            guiHasToSaySomething=true;
            println ("  Here 2 " + lineToSay);
            lineToSay ++;
          }
          //lineToSay ++;
          //guiHasToSaySomething=true;
    
          break ; 
    
        case 6:
          if (inputStr.equals("bestmove")||inputStr.contains("bestmove")) {
            println (" !!!!!!!!!!!!!!!!!!!!!!!!!!!! bestmove 2 " + lineToSay);
          }
          break; 
    
        default:
          println ("Here default " + lineToSay);
          delay(111); 
          lineToSay ++;
          guiHasToSaySomething=true;
          break;
        } // switch
      } // if
      else {
        delay(111); 
        lineToSay ++;
        guiHasToSaySomething=true;
      }
    
      // --------------------------------------------
    
      if (guiHasToSaySomething) {
        switch (lineToSay) {
        case 0:
          say( "uci\n"); // with \n  !!!!!
          break;
    
        case 1:    
          say( "isready\n");
          break;
    
        case 2:
          guiHasToSaySomething=true;
          // say( "debug off\n"); 
          say( "isready\n");
          break;
    
        case 3:    
          guiHasToSaySomething=true;
          //  say( "ucinewgame\n");
          say( "isready\n");
          break; 
    
        case 4: 
          // now the GUI sets some values in the engine
          // set hash to 32 MB
          //   guiHasToSaySomething=true;
          say("setoption name Hash value 32\n"); 
          guiHasToSaySomething=true;
    
          say( "position startpos moves e2e4 e7e5\n");  
          guiHasToSaySomething=true;
    
          say("go infinite\n");
    
          guiHasToSaySomething=true;
    
    
          delay(2222);
          // lineToSay ++;
          say("stop\n");
    
          guiHasToSaySomething=true;
          delay(22);
          // lineToSay ++;
    
          break; 
    
        case 5: 
          // init tbs
          guiHasToSaySomething=true;
          say("setoption name NalimovCache value 1\n"); 
          delay(22);
          lineToSay ++;
          break; 
    
        case 6: 
          guiHasToSaySomething=true;
          say("setoption name NalimovPath value d:\tb;c\tb\n");
          break;
    
        case 7:    
          guiHasToSaySomething=true;
          say( "isready\n");
          break;
    
        case 8:
          guiHasToSaySomething=true;
          // say( "position e2e4\n");
          println ("Here 1");
          say( "position startpos moves e2e4 e7e5\n"); 
          // position e2e4\ngo\n
          break;
    
        case 9:    
          guiHasToSaySomething=true;
          //  say( "go\n");
          say("go infinite\n");
          // position e2e4\ngo\n
          break;
        } // switch
        //
      }
    
      println ("Here draw() " + lineToSay);
      //
    } // func 
    
    // ----------------------------------------
    
    void say (String str) {
    
      println(""); 
      println("----->"+str);
      //  println(lineToSay);
    
      out = p.getOutputStream();
    
      try {
        byte buf[] = str.getBytes();
        out.write(buf);
      }
      catch (Exception e) {
        println("Can't write ! ");
      }
    
      try {
        out.flush();
      }
      catch (Exception e) {
        println("Can't flush ! ");
      }
    
      // !!!!!!!!!!!!!!!!!!!!!!!! kill all speaking here  
      guiHasToSaySomething=false;
    }//func 
    
    String listen() {
    
      // gui gets from engine
    
      int c;
      String inputStr = "";
      char stop1 = (char) char(13); 
    
      try {
    
        // http : // stackoverflow.com/questions/22563986/understanding-getinputstream-and-getoutputstream
    
        // while ((c=in.read()) != -1)
        while ((c=in.read()) != 13)
        {
          //  print((char) c);
          inputStr += (char) c;
        }
    
        // println (inputStr);
      }
      catch (Exception e) {
        println("Can't read");
      }
    
      if (inputStr != null && !inputStr.equals("")) {
        return inputStr ;
      } else {
        println("inputStr is null");
        return null;
      }
    }
    
    // ----
    
  • almost there....

  • edited August 2016

    so the solution was to use ProcessBuilder

    see Bird's comment here:

    https://forum.processing.org/two/discussion/comment/73515/#Comment_73515

Sign In or Register to comment.