Let's write a Chess GUI from scratch in Java

Sort:
Avatar of watcha

Why have an own Chess GUI?

Because Chess GUIs - even the most sophisticated ones - don't do exactly what I want.

Just to give you an example, a little but annoying thing: they don't record board flip information in PGNs. When I open one of my own games in which I'm black, the board is not flipped to reflect my viewpoint of the game. I have to do this manually every time I open this game.

Why Java?

Because I always run into portability issues when I want to share my program with others. Java is all about portability so I decided to switch to Java. I'm completely new to this langauge ( a started learning it a week ago ), but already I have a very basic GUI which can do the following:

- drag and drop move input

- UCI engine analysis

- load and save basic PGNs ( single game wihout comments, variations etc. )

- report and set from fen

- move legality check

All you need to run this program is the latest version of Java installed on your computer.

You can obtain it here:

https://github.com/javachessgui/javachessgui

Avatar of TurboFish

I'm now learning Python, and I understand that it too can create GUIs, including browser-based ones.  I would like to try to use Python to write a chess-GUI (and in the long-run a neural-net based chess engine).  Do you know of any disadvantages to using Python for these sorts of projects?

Avatar of PossibleOatmeal

On many GUIs there is a place to enter your name and when it detects your name in the PGN file, it will flip the board for black when you play black.

For instance, in Scid vs. PC, click Options, Player Names as shown below:

Avatar of watcha

@TurboFish:

I have reasonable understanding of Perl ( and how to write a GUI in Perl Tk ), but I have zero experience with Python. Also Python - which as far as I know is an interpreted language - seems to be a bit slow for writing an engine. But I'm really curious how a neural-net engine looks like.

@pawnpatrol:

I want this to be a general feature. I want the GUI to remember from which point of view I was looking at a certain game - even if it is not my game - so I made this feature automatic, always when a PGN is saved, the flip information is stored in the PGN header.

Another example which I miss badly in other GUIs is that they don't have a 'Make analyzed move' button, which makes the move the engine is currently thinking to be the best during an ongoing infinite search ( may be there is some key combination that does the trick, but no explicit button ). I have this in my own GUI.

Avatar of PossibleOatmeal

I'm sure you have lots of things you want to implement, but again, you might want to check other GUIs more thoroughly first.  Your second missing feature is also present in SCID vs. PC and probably others.

Click the + sign to make the currently analyzed move on the board:

Avatar of watcha

I'm not familiar with SCID. I could not find out how to do it with Arena, which is my preferred GUI.

Avatar of PossibleOatmeal

Might be a worth a shot to try other GUIs before jumping right to writing your own.  If adding a note to a pgn file to flip the board turns out to be the only thing missing, you could probably just add it yourself since Scid vs. PC is open source.  Beats starting from scratch.

Personally, I can't stand Arena.

Avatar of TurboFish
watcha wrote:

@TurboFish:

I have reasonable understanding of Perl ( and how to write a GUI in Perl Tk ), but I have zero experience with Python. Also Python - which as far as I know is an interpreted language - seems to be a bit slow for writing an engine. But I'm really curious how a neural-net engine looks like.

@pawnpatrol:

I want this to be a general feature. I want the GUI to remember from which point of view I was looking at a certain game - even if it is not my game - so I made this feature automatic, always when a PGN is saved, the flip information is stored in the PGN header.

Another example which I miss badly in other GUIs is that they don't have a 'Make analyzed move' button, which makes the move the engine is currently thinking to be the best during an ongoing infinite search ( may be there is some key combination that does the trick, but no explicit button ). I have this in my own GUI.

watcha, you're correct that Python is normally considered as "essentially an interpretted language", but it can be compiled to speed it up (or so I have read).  I will soon be able to compare the speed of some uncompiled programs written in both Python and PHP (the latter seems pretty fast to me, but I don't have any other basis for comparison).

As for neural-net based chess engines, this has been done before (with pretty weak playing strength).  I plan to use a normal GUI and a legal-move enforcement module wrtitten in the usual hand-coded rule-based approach.  That should be the easy part.  The actuall move selection will be performed by a multi-layer (back-propagating) neural net trained on GM games.  I realize that the learning curve will likely be extremely slow and inconsistent (if at all).  But I've always wanted to learn to create a neural net, so I'll be happy if my chess neural-net can just accumulate a plus score against a random move generator! 

Avatar of HGMuller

When I could not find a GUI that was doing what I needed (namely play Chess variants with more than 6 different piece types), I just took WinBoard (which was open source), and modified it. Saved me tons of time.

I agree that it is very convenient to have a GUI that you can make do anything you want, by changing its program. But starting from scratch is a truly enormous job. It would take you years before you have someting as advanced as WinBoard or Scid vs. PC.

Avatar of watcha

The point is that I don't want anything advanced.

As you rightly pointed out, what I really like is full control so that all my dirty little needs can be fulfilled. I have written it, so I understand it. Modifying programs of others is not the answer for me, because there can always be hidden traps which only come out if an outsider modifies the program who does not understand its logic fully. This happened to Stockfish, when it was modified to play atomic chess ( Atomkraft ). The program is terribly strong among atomic chess engines, but it crashes a lot. Stockfish is too elaborate a program to be modified by outsiders in a foolproof way.

Avatar of watcha

When I'm analyzing a position, I want to know the evaluation without having to look somewhere else at the engine panel figuring out the eval in a sea of information. Just give me the number in the middle of the board with font size 80 :)

Avatar of Mal_Smith
[COMMENT DELETED]
Avatar of watcha

I have learned some lessons during writing my first Java program. I started to write the second version from a new basis. It is more object oriented now, and the functions of objects are better separated ( I mixed the GUI functions and core functions in the first version which is a mistake ).

I made the board resizable and configurable:

I also want to handle variations, but in a simplistic way. It gave me a headache how to represent a game tree in single dimension which lets me parse the tree but at the same time I have a clear view of the line currently investigated.

Branching nodes are marked with (*). Once you reach a branching position, the alternative moves open up:

The blue move represents the main line, the red move is an alternative line. Once you click on one of the alternative moves, the move list from this move on changes to represent this continuation. So the move list is always linear and simple, yet it gives you and idea where the tree is branching and gives you straightforward means to enter those branches.

https://github.com/javachessgui/javachessgui2

Avatar of watcha

Now that complicated PGNs ( with comments, variations, sub-variations, multiple line move lists ) are firmly under my control my next problem is how to deal with PGNs that contain more than one game.

This is really nasty. GUIs solve this by popping up a panel with the game list and then you select the actual game from this list. When you modify the game and want to save it there is again a question: save it back to the original game, or to append it to the end of the PGN as a new game. Really awkward.

What I want is a one PGN file - one game approach. No game list panel.

The solution that I came up with ( and I don't know of any GUI that does this ) is when you try to open a PGN with multiple games, there is a preprocessing step: the PGN is split into separate games and these games are stored in the app's own pgn directory in a new directory created with the name of the original pgn file. The games are named game1, game2 ... respectively. If the PGN contains information to the name of the players, then this appears in the name ( this is done in a safe way: special characters, spaces etc. are omitted in order to provide a safe file name ).

This is what my program creates out of the first round of Norway Chess 2015:

The user is then redirected to this new directory and can select a single game. If this game is modified and saved back then it goes to this directory so the original PGN file remains intact.

Avatar of social_climber

What a horrible language java is.

Avatar of stopandthink
social_climber wrote:

What a horrible language java is.

Java is the god of all OOP languages. 

Avatar of watcha

What is attractive in Java for me:

- portability ( Java virtual machine )

- high speed compared to the fact that there is no need to recompile the program for a different platform, the jar executable is the same for every platform

- core modules provide extensive functionality

- JavaFX provides a very capable GUI environment

- completely free, you have access to the full capability of the NetBeans IDE

With Microsoft Visual Studio the really juicy classes are not free and you can access only a limited subset of the IDE with the free version. It is a pain to write a native Windows GUI application with free Microsoft tools.

Avatar of stopandthink
watcha wrote:

What is attractive in Java for me:

- portability ( Java virtual machine )

- high speed compared to the fact that there is no need to recompile the program for a different platform, the jar executable is the same for every platform

- core modules provide extensive functionality

- JavaFX provides a very capable GUI environment

- completely free, you have access to the full capability of the NetBeans IDE

With Microsoft Visual Studio the really juicy classes are not free and you can access only a limited subset of the IDE with the free version. It is a pain to write a native Windows GUI application with free Microsoft tools.

"Microsoft Visual Studio" ew.

Avatar of watcha

JavaFX does the browsing for me.

I'm playing at a correspondence site which lists your games in bulk in one large page. Games are not sorted according to your turn, so you have to scroll through this large page to find the games which are your turn which is very ugly.

Here comes JavaFX. It has a very powerful browser class, so it takes that much amount of code to download this monstrous page, find the first game which is my turn and load it for me:

Also if I put this page to a ScrollPane, then I can actually make my move in the JavaFX window. So now I'm playing correspondence fully in Java, without using Chrome.

Avatar of watcha

I learned some lessons while writing my first ever Java program.

While JavaFX gives you a very capable and good looking GUI environment, it takes a lot of coding to make this happen. Configuring the widgets one by one, adding all event handlers, making sure that the state of those widgets is reflected in the configuration ( which is stored on disk ) takes a lot of effort. In addition Java's sytax is quite rigid.

I decided that the best way out of this is having an own scripting language that builds up the GUI elements and configures them for common sense behaviour and implements this behaviour.

Once I have this capability, creating the GUI is 90% done by editing the scripts ( which are stored in the project's resources folder ):

Of course the first thing with every language is the Hello World app, here it is:

Since a GUI can be thought of as a tree of nodes, we are constructing the children of the root node, here is only one, a label with text "Hello World".

The code to start up from this script and the result:

Of course you can do better than this. It takes a couple of dozen lines in this language to cook up something that already looks like a chess GUI:

Thanks to the built in behaviours you can in no time configure this blank environment for the good:

The remarkable thing that 90% of this program consists of pre built behaviours, and only 10% need to be done in actual Java code. It is very is easy to add new elements here, because it is only the script that needs to be modified, all the rest is taken care of by the GuiBuilder class.