4

First of all I am not a professional programmer, but a student with a programming hobby so I am sorry if my question is stupid.

I want to write a chess engine to be used on the web and had already started writing it in JavaScript. After searching around I however found out that JavaScript is not ideal for this task. I have already written the user interface in JavaScript and would prefer if I could keep it while rewriting the game logic in python. Is this possible?

10
  • 2
    JavaScript is no less ideal for this task than Python. Commented Sep 11, 2016 at 0:42
  • @FengyangWang The problem with JavaScript is that I want to use a bitboard, which requires 64-bit integers. JavaScript only has "53-bit" floating point numbers. Commented Sep 11, 2016 at 1:47
  • 4
    Python does not have built-in 64-bit integers either (int will not be as fast as native 64-bit ints, even in Python 2, because of overflow checks); if your goal is performance, you will not get it with either language. To use 64-bit integers in JavaScript, it is typical to use a library which offers them. Commented Sep 11, 2016 at 1:51
  • 4
    Additionally, bitboards do not actually need full support of 64-bit integers; it suffices to pass around a pair of 32-bit integers containing the high and low bits. Commented Sep 11, 2016 at 1:52
  • 1
    @LasseMøldrup And if you are really so concerned about speed, you can pass around 32-bit integers and add type "hints" such as ~~x or x|0, which JavaScript JIT compilers are very good at optimizing. Commented Sep 11, 2016 at 2:04

2 Answers 2

3

As you can see in the comments python will not resolve all your problems. But you can use third-party libraries.

Also, you can use some web frameworks to make web-site with Java-script UI but Python logic.

Flask will be a good boilerplate for you.

2

There are three separate concerns in your app.

  • User interface. If it's web-based, it's HTML + JavaScript, or some language that efficiently compiles to JavaScript (there are a few). The UI is likely not excessively sophisticated (e.g. way simpler than Gmail), so you can pick any framework you're comfortable with.

  • A web server part. This part receives HTTP requests from the UI and sends them to the engine. This is likely not very complicated, too, and can be implemented in any language you're comfortable with, e.g. JavaScript (Node) may be fine; endless options exist.

  • The chess engine. This part receives moves (in whatever format you consider convenient), generates moves, keeps the position on the board and time, etc. What do you need for that greatly depends on your requirements, ambition, resources at hand, etc. I'd take a high-performance, statically compiled language for that. There's a wide choice; off the top of my head, I'd consider OCaml, Go, Java, C#, Rust, D, and even C++ if I were an expert in it. All of them provide good, or at least decent, data structures for efficient computation, including some down-to-bit-level calculations you mentioned. You could consider using OpenCL or CUDA for parts of your computation that afford parallelization of such type.

If all you're looking for is a proof of concept and / or study, then Python, or any other easy-to-inspect, dynamic language might be more suitable. You will lose a lot of performance (though numpy, numba, and cython may help in certain places), but you'll have much easier time watching how the insides of your engine work. It will definitely help to write a better next version.

As you see, the three parts are all relatively independent. If I were you I'd start with the engine, with the crudest interface (reading / writing text strings), would wire it up to a web server eventually (again with the crudest, simplest possible static HTML forms), and after that would work on a nice UI.

Chances are there are nice UIs for open-source chess programs already; maybe some of them could be reused, partly or even whole. But without an engine a UI is sort of pointless. I'd not start with it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.