/Home or play chess at
http://chessforeva.appspot.com
......... Google App Engine ........... .... ....

Chess software for fun

trešdiena, 2010. gada 10. novembris

How to connect UCI chess engine even to web-browser on windows locally

This post is more related to programming / not chess. Anyway, it can be useful for connection to UCI interface chess engines. As we know, there are GUIs as Arena, xboard, winboard, etc. to use strong chess programs as Rybka, stoskfish and others. The basic idea (how it's realized) is redirection of stdin-stdout streams, that is input/output of chess engine .exe file. Nothing has changed so long since DOS times, windows based applications use the same starting and managed-capturing of subprocess controls. Nowadays sometimes it's not so convenient if we want to connect high level platforms with great chess features what strong chess programs provide. It is so, because chess programs are long-time-projects made for chess, not for latest social networks nor tweets or youtubes. The solution described here is a ready connector to DOS console based application even from browser. Software development platforms can do HttpRequests by default nowadays and that's enough to run any UCI chess engine on

Windows by using this simple tool (download).

Now about how it's made:

There is an app ExeConnector.exe that should be started. This:
1. starts a local webserver (others can access from outside internet too by http://<your-ip>, be careful, set firewall on this port or don't set it if You want it so);
2. starts a chess engine (DOS application, developed in C,C++ usually) with redirected input/output streams of this program - to/from this webserver;
3. waits for HTTP: requests, sends browser-request based answers;
4. closes all and releases memory after request with "EXIT_EXE" as data.


*NOTES
1.Tool is for Microsoft Windows only.
2.Framework 2 pre-installed as minimum required (can be newer, any normal windows has it already)
3. If You don't know, http://localhost/ is the same as http://127.0.0.1/
4. In downloadable version just cmd.exe as sample provided, so You can get directory structure in browser by:

http://localhost:5050/OUT_BUFFER.txt?OUT_BUFFER=dir c:*.*
http://localhost:5050/IN_BUFFER.txt
Download the chess engine You want and use the same way.
5. Or the best sample is Unity3D chess project where 3D objects love requesting deep chess searches.


TUTORIAL:

ExeConnector.exe is a console application to call as a process, or from command line shell once on starting of serving.

Parameters in Data\ folder.
Put the exe-file name in Exe2Start.dat
Command line arguments write in Arguments.dat (better not to do it, because stdin-stdout redirected, use method below), just in case.
Provide port number to interract with web server for local socket requests in port.dat

For tests:

1.Start ExeConnector.exe and send data to STDIN of this EXE by (DATA SEND)
http://localhost:5050/OUT_BUFFER.txt?OUT_BUFFER=position fen 7k/Q7/2P2K2/8/8/8/8/8 w - - 0 70\ngo depth 10\n
*08.11.2011 modified processing to command ,pause, command,...

2.Wait few seconds and get STDOUT from exe (actually best move in given chess position) through browser by (REQUEST)

http://localhost:5050/IN_BUFFER.txt
Possibly requests for each uci command should be separated (depends on chess engine).

3. At the end of all requests send command to exit processes and close all by
http://localhost:5050/OUT_BUFFER.txt?OUT_BUFFER=EXIT_EXE

There is a developed code for Stockfish chess engine connection to 3D javascript in browser and even battling possibilities with Cuckoo java chess engine. Can play strong chess. Browser is like a GUI for engine and a player controller capturer for javascript based chess board to battle vs other chess engine automatically. A chess game sample Stockfish vs Cuckoo by using this tool.
Unzip this archive on desktop and start the .bat file.


For implementation in other EXEs above (for example Unity3D calls in C#):

Start processes by :
using System.Diagnostics;
using System.Net;

try
{
p=new Process();
p.StartInfo.WorkingDirectory = Path;        // directory of stockfish chess engine
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = ExeFileName;        // stockfish.exe for example...
p.StartInfo.Arguments = "";
p.StartInfo.UseShellExecute = false;
p.Start();
}
catch(Exception e) {}        //if unable to start process...


And read or send to local sockets as http: requests by
HttpWebRequest request = WebRequest.Create("http://localhost:5050/IN_BUFFER.txt") as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
String buffer=reader.ReadToEnd();
reader.Close();
}

Of course, You can now connect it via PHP
$contents=file_get_contents('http://localhost:5050/IN_BUFFER.txt');

or python:
import urlib.request
response = urlib.request.urlopen('http://localhost:5050/IN_BUFFER.txt')
html = response.read()

or by using Http-Requests in javascript,
or any other programming language so used today.

And do not do this if You have no clue what is written here about.


*03.03.2014 There is a DLL library named ExeHandler (for Win32 applications) functions of which can start exe-processes, define stdin/stdout as strings and get output results. Samples for usage in QB64 or Visual FoxPro are listed in readme file.

31 komentārs:

  1. Hello, I get these errors when I started StartStockfish_UCI.bat


    D:\EXEConnector>start http://localhost:5050/UCI.htm

    D:\EXEConnector>ExeConnector.exe
    Listening to port: 5050
    Got EXE filename: Houdini.exe
    Got command line arguments:
    An Exception Occurred while Listening :System.Configuration.ConfigurationErrorsE
    xception: Configuration system failed to initialize ---> System.Configuration.Co
    nfigurationErrorsException: Unrecognized configuration section startup. (D:\EXEC
    onnector\ExeConnector.exe.Config line 3)
    at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignor
    eLocal)
    at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(Configurat
    ionSchemaErrors schemaErrors)
    at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
    at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey
    )
    --- End of inner exception stack trace ---
    at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey
    )
    at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(S
    tring sectionName)
    at System.Configuration.ClientConfigurationSystem.System.Configuration.Intern
    al.IInternalConfigSystem.GetSection(String sectionName)
    at System.Configuration.ConfigurationManager.GetSection(String sectionName)
    at System.Configuration.PrivilegedConfigurationManager.GetSection(String sect
    ionName)
    at System.Net.Configuration.SettingsSectionInternal.get_Section()
    at System.Net.Sockets.Socket.InitializeSockets()
    at System.Net.IPAddress.InternalParse(String ipString, Boolean tryParse)
    at System.Net.IPAddress.Parse(String ipString)
    at Imtiaz.MyWebServer..ctor()

    D:\EXEConnector>pause

    AtbildētDzēst
  2. I have created a working version with houdini. Download, unzip on desktop and start the Houdini_UCI.bat file.
    This error maybe is caused by arguments You trying to send to houdini on starting. Arguments.dat file should be empty.
    You can send options via javascript anyway, just have to apply programming skills.
    Hope it helped.

    AtbildētDzēst
  3. Thanks. It's works. :)

    Btw, this blog is too useful for me.
    I like chess engines. :P

    AtbildētDzēst
  4. Hey, what about Chess 960?
    I mean, can I set the pices position?

    AtbildētDzēst
  5. Yes. There is a script in c0_pgn.js that parses chess notation. I hope it's correct.

    AtbildētDzēst
  6. Ok, thanks. I will look it.

    AtbildētDzēst
  7. No, no. I mean how to send to ExeConnector FEN positions?
    I tried something like :

    http://localhost:5050/OUT_BUFFER.txt?OUT_BUFFER=position fen r1bqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -\n go depth 10 \n

    But ExeConnector doesn't send move and it crashed.

    AtbildētDzēst
  8. Aha :) ExeConnector sends this text to chess engine's stdin stream that expects command by command (line,line). If You look at javascript samples then You see I have separated: 1 command = 1 request.
    If it crashes, just remove chess engine from memory by Ctrl+Alt+Del and End Task for Houdini, then start again the .bat command script.
    It is not the official (right) way to connect chess software. Anyway, I used it in deathmatches of web-chess engines.
    Even 3D Blender can be connected this way.

    AtbildētDzēst
  9. Hmm, I think I don't send the commands correctly.
    I don't use ExeConnector with JavaScript. I'm writing an app by VB.NET.

    I'll look your JavaScript codes, and how exactly you send FEN data to ExeConnector.

    BTW, This problem is when I send position fen. If i'm testing to standart chess, don't have problems.

    AtbildētDzēst
  10. I found good UCI .net sample. You should start chess engine as a process with captured stdin/stdout streams. Just find the proper protocol wiki.

    AtbildētDzēst
  11. Hello Sir

    Very awesome wesite.
    I am starting a kids chess site and want to put the engine code on that
    I am having a problem with the ExeConnector. Everything works when I do a "localhost".
    But when I give an IP, it doesnt find the server

    Is the ExeConenctor good enough only with Localhost. If yes, how do I make the engine run outside the localhost

    Thanks for your help

    I will come back to you with more queries soon

    Regards
    VV

    AtbildētDzēst
    Atbildes
    1. Ok, this version now contains ip.dat file where You can set IP address of Your computer. Don't forget to add firewall exception for ExeConnector.exe (on server) in Control Panel's Networking. The reason why this option wasn't before is that any web-server located far away is not appropriate for intense requesting. The standard UCI is fast. My browser solution is kind of "still working way around" if Windows does it well. In Your case it would be reasonable (and performing the same) to write a php-code that passes requests to chess engine. I mean, stability then is the main issue to avoid 404-pages.
      Thanks for Your interest.

      Dzēst
  12. Hello Sir

    Thanks for an immediate response. I have been able to set the IP and now able to connect via Internet. Awesome and thanks
    I have a few more requests

    I am linking some of the engine and examples on my kidschess site.

    Whichever engine examples have source code, like Fruit Flash and p4wn, I am downloading the code and linking in the normal way. But ones which dont have source code, I am linking via "iframe". Here I am facing a problem - the layouts of some of the boards are not positioning properly. Example Fruit 3D board is getting cut at the top in your site itself and another example Unity 3D, the settings are blocking the board. So I have to rewrite the html to make it optimum. Especially, these days kids are using Tablets, so I HAVE to adjust everything under 900pixels. Current layouts on your site are positioned to around 1300px. So I took the html from your site and JS from the src, but I think some of the JS source is in your domain, so my board is not showing up. Can I request the working version of "c0_chess.js", or any other way, I can redraw the html to make it more compact. I want to do away with 2D board completely, as kids like 3D boards

    Thanks

    AtbildētDzēst
    Atbildes
    1. You may download and optimize any JS or other code You find (use Save Page completely and Notepad after). Unfortunately I'm not too familiar with tablets/mobiles jet and c0_chess.js is an old "art" prior them. I don't think it is easy even to upgrade/modify, because there should be flips, scrolls and zooms nowadays. It means lot's of browsers, testing and bugfixes. And resolution adjustments is a hell to code, believe me. I hope someday I'll do the second try. Most of the time I'm working on chess AIs as such and the code I finish is applicable(or not if it's my fun) to other related projects later. "Chess programming" is the best word for this (and these are deep waters). The 3D boards, to be honest, are just tools to get it playable. I have no resources as chess.com. By the way, applications are technically better in performance, space, graphics, UI controls for tablets and limited devices, especially in case of chess.
      I see that high quality chess publishing and well working software for real device are too distant things. I mean, good chess puzzles, I solve on mondays while drinking coffee, are simple bitmaps while could be advanced javascript tools with analysis and related chess history of all times in background. Think about it. Cheers.

      Dzēst
  13. Hello Sir

    Thanks
    I downloaded the code and will start building a layout for my own use. Will share with you.
    Have one question

    * I downloaded the code. When I am running the code from my local disk (without any changes), I am getting two chess boards (two 3D board and 2 2D boards). Howmuch ever I refresh the browser, there are 2 boards. But the same problem does not appear on your site. Is there any plugin that you are using on your site for the layout. How do i make the ghost boards dissapear

    Thanks

    AtbildētDzēst
    Atbildes
    1. Use Notepad and replace domain and path for img src in c0_chess.js. Get a programmer, I'm busy, sorry. All my code is intended always working fast on google server. So, I will not separate it anyway.

      Dzēst
  14. Hello Sir

    Thanks

    I figured out and solved the first problem of "double" boards, which strangely appears only on local copy and not on your domain. I have figured out that 187 lines of additional code is causing this issue - extra code starts immediately after the "function takeback" ....all the way upto the end of the ##body## tag as follows...."##div id="c0_board"##.....upto ...##div id="c0_divs_99"##.....That is 187 lines of code. After I remove these 187 lines of code, I am able to get rid of the extra 3D and 2D board. So far so good. The layout looks clean, as in your site

    Next, I have to work through the maze of "c0_piece_image_src", which refers to lot of files on your domain and get that organized

    Once that is done, I will optimize the layout..Keeping you posted....Thanks for your support and guidance



    Thanks

    AtbildētDzēst
    Atbildes
    1. Vaz, this all work You want to do (hosting somewhere else) gives You assurance (this is true) but not speed or even any advantage (do You wana seriously compete with google's hardware?). You can simply use the script library (call it by SCRIPT src=, some documentation) and put Your code after this in Your page: position adjustments, additional DIVs with Your content. I have done it for javascripting. Anyway, best regards!

      Dzēst
  15. Sir

    Wow. I did not see that documentation earlier. I just added a the code and I have a running board. I will go the same way suggested by you. It is so much easier

    I have one question. I set up the script. I am not able to input the moves. Can you please tell me where to put my next move, example "d2d3". I want two example (one within the code how to put and another how to put via pgn) (couldnt quite get it from the documentation). I have copied the code below, so you can put the instruction in it

    Thanks


    c0_set_start_position( "e2e4e7e5" );
    c0_moveslist ("d2d3"); why this is not working ?
    c0_position ("");
    c0_move_to( "", "" ); please show me next move d2d3
    c0_take_back();
    c0_moved_callback();
    c0_get_from_PGN(); please show how to kink an external pgn file or how to input pgn moves

    AtbildētDzēst
    Atbildes
    1. Use "view source" everywhere on internet. There ar lot's of 3D-boards on the main site.

      To move: c0_set_start_position( "e2e4e7e5" ); c0_move_to( "d2", "d3" );
      To set FEN: assign c0_start_FEN=.... and simply use c0_set_start_position( "" );
      PGN functionality is like a converter: samples.
      Maybe little clamsy, but works at the end.

      Dzēst
  16. Sir,

    Thanks... That works

    I have one question on the "mychessengine.htm"

    (a) I want the first move to be picked up at random from a list of moves .How Do I program this
    (b) secondly, after my move, again, I want the computer to pick up randomly from another set of moves..How do I do this

    Please help

    AtbildētDzēst
    Atbildes
    1. :)))) use arrays for lists and function math.random()
      But, seriously, You should learn javascript programming at first. Mostly, chess engines calculate all possible legal moves for position (step1) and then start recursive evaluation "what would happen if we go..." (step2). I would suggest step1 for beginning. Create an array BOARD[8][8] with pieces and calculate as humans do: pawn moves this way (set BOARD[y+1][x] = pawn) etc. It is programming, learn to code! And there is a good wiki for chess programmers.

      Dzēst
  17. Hello, just wanted to ask, is this method is to how to connect my engine to my account on a chess website ?!!.

    AtbildētDzēst
    Atbildes
    1. Icsdrone connects standard chess engine to known telnet chess servers, but I don't think it is appreciated by chess players on these servers. Don't know other hacks.

      Dzēst
  18. Hello,

    Do you provide the source code for the ExeConnector.exe?

    Thank you!

    AtbildētDzēst
  19. Please please please how to make ONLY the 2D board? No 3D board.

    Thank you for source code!! Good job.

    AtbildētDzēst
    Atbildes
    1. It's a programming thing. My idea was to connect browser to chess program that's all. If you want javascript in 2D then just develop it. But, if You want 2D chess software that uses chess engines, then I would strongly reccomend SCID (with sources) or Arena for chess only. If You ask a good modern chess 2D program to look at, then please DroidFish (with sources).

      Dzēst
  20. Great site and code. I have a question. I tried the "Stickfish" and "Houdini" examples and it works. I also managed to do the ExeConnector with Fruit. The question is - all the three engines do the same FIRST move. Example for e2e4, the first move for black for all the three engines is d7d6. How can i make the engines make their own moves, as d7d6 for e2e4 is not the most preferred move...Thanks Vaz

    AtbildētDzēst
    Atbildes
    1. Very strange. Maybe it feels like other engine gives answers:) Check the engine .exe in the Data\Exe2Start.dat file. And close all servers before starting one.
      I am not sure You can access chess opening book. Maybe simply place the "book.bin" file in the engine's-exe folder. This is GUI part, no need to calculate. So, play other first move :D

      Dzēst