Page 1 of 4

Chessboard

Posted: Sat Jul 02, 2016 6:45 am
by Roland Chastain
Hello everybody!

I would like to present my LÖVE chessboard. It's a chessboard with an arbitrator (but without an artificial opponent). To move a piece, drag and drop it.

Image

I made that chessboard in order to test under pleasant conditions my script chessgame.lua, and because I wanted to try to do something with LÖVE. I was surprised to see how easy it was to do what I wanted, with a few lines of code.

It's a programming exercise and my first LÖVE project, so any suggestion about the code is welcome.

All the chess rules are implemented, except threefold repetition. In case of promotion, you cannot choose the piece: it's automatically a queen.

The latest version of the project is on Codeberg.

Re: Chessboard

Posted: Sat Jul 02, 2016 7:14 am
by Ikroth
Wow, this looks really awesome! It would great if when you picked up a piece it showed you where you are able to legally move it.

Re: Chessboard

Posted: Sat Jul 02, 2016 7:39 am
by Roland Chastain
Ikroth wrote:Wow, this looks really awesome!
Thank you very much. :)
Ikroth wrote:It would great if when you picked up a piece it showed you where you are able to legally move it.
Yes, good idea. I will think of a not too complicated way to do that.

Re: Chessboard

Posted: Sat Jul 02, 2016 8:47 am
by whitebear
I love the classic font style chess board. I am so tempted to fork this.

Re: Chessboard

Posted: Sat Jul 02, 2016 8:52 am
by ivan
Not a bad start.
The next obvious step would be to support the PGN format.
Disambiguation is hard to implement without making your code super messy.
So you need a fast way to generate all possible moves to a particular square.
Performance is going to be another concern. For example, loading/validating a large PGN file.
One common optimization is to detect "pinned" pieces:
1. If the moving side is not in check AND
2. If the moving piece is not pinned THEN
a lot of calculations can be avoided - you don't need to test if the move opens check for the moving side.

The Lua code needs a little bit of work too.
You don't want to write things like:

Code: Select all

        elseif isKnight(aBoard[x][y]) or isKing(aBoard[x][y]) then
          if isKnight(aBoard[x][y]) then
I recommend using "locals" for things like "aBoard[x][y]" because it makes the code shorter and it's faster too.

Personally, I don't see a big advantage of using a 2D table for the board representation. IMO, 1D tables make life a lot easier in this case.
I don't like the use of pattern matching here:

Code: Select all

 return (aBoardValue ~= nil) and string.match(aBoardValue, '[Pp]')
you might as well write:

Code: Select all

 return aBoardValue == "P" or aBoardValue == "p"
In fact I don't think the use of strings is that beneficial in this case. But this could be a matter of debate.

But yep, it's a decent start.
Good job.

Re: Chessboard

Posted: Sat Jul 02, 2016 9:39 am
by Roland Chastain
whitebear wrote:I love the classic font style chess board. I am so tempted to fork this.
As far as I am concerned, please feel free to do so. The design of the pieces come from Fritz 1.0. When I began my first chess program, I copied manually the pictures with "0" and "1" in a text file, before I learned to read a picture by code. :)

Later, I added transparency and converted pictures to PNG format.

Re: Chessboard

Posted: Sat Jul 02, 2016 9:55 am
by Roland Chastain
ivan wrote:Not a bad start.
Thank you for your look into my code, and your very interesting suggestions.

Yes, I was thinking of PGN support, but when I studied the specifications, I found that it was too complicated, especially disambiguation as you said. Thank you for sharing your experience in that matter.
ivan wrote:But yep, it's a decent start.
Good job.
Thank you. :)

Re: Chessboard

Posted: Tue Jul 05, 2016 5:34 am
by Roland Chastain
Hello! Here is a new version of the chessboard.

The valid target squares are highlighted. Some improvements have been made in the code. Arbitrator messages are available in french and german languages. To change the language, make a copy (for example) of french.lua named language.lua. If someone would contribute another translation, I would be glad to add it. :)

P.-S. I forgot to mention that I also changed the mouse cursor (to a grabbing hand).

Re: Chessboard

Posted: Tue Jul 05, 2016 6:11 am
by zorg
Here you go: hungarian.lua :3

Code: Select all

literals = {
  ltDraw = 'Döntetlen!',
  ltCheckmate = 'Matt! ',
  ltStalemate = 'Patt!',
  ltCheck = 'Sakk! ',
  ltWhiteToMove = 'Világos lép.',
  ltBlackToMove = 'Sötét lép.',
  ltWhiteWins = 'Világos nyert.',
  ltBlackWins = 'Sötét nyert.'
}

Re: Chessboard

Posted: Tue Jul 05, 2016 6:38 am
by Roland Chastain
zorg wrote:Here you go: hungarian.lua :3
Great! Thank you very much. :)