Chessboard

Show off your games, demos and other (playable) creations.
User avatar
Roland Chastain
Prole
Posts: 29
Joined: Sat Feb 07, 2015 2:30 pm
Location: France
Contact:

Re: Chessboard

Post by Roland Chastain » Sat Sep 14, 2019 11:52 am

Hello! I discovered a bug. In some situations the program used to crash on a castling move. It's fixed. Please see the first post of the discussion.

Starting from that code, I created a UCI chess engine able to play Fischer random chess variant. When I have time, I will include the AI into the chessboard.

Regards.

Roland

User avatar
ivan
Party member
Posts: 1520
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Chessboard

Post by ivan » Mon Sep 16, 2019 5:40 am

Good to see you still working on this project.
It's not bad but I think your code could use a little bit more work.
For example, you are using global functions for everything which is considered sloppy and pollutes the global space _G.
Why would you have code like:

Code: Select all

string.char(
    string.byte('a') + aX1 - 1,
    string.byte('1') + aY1 - 1,
    string.byte('a') + aX2 - 1,
    string.byte('1') + aY2 - 1
  )
You already have the square names table right there:

Code: Select all

LSquareName[aY1][aX1]..LSquareName[aY2][aX2]
Your "IsColor" function is not very efficient either:

Code: Select all

function IsColor(aBoardValue, aColor)
  --assert(string.match(aColor, '[wb]'))
  return (aBoardValue ~= nil) and string.match(aBoardValue, (aColor == 'w') and '[PNBRQK]' or '[pnbrqk]')
end
Pattern matching is much harder to read and slower compared to:

Code: Select all

function IsWhitePiece(aBoardValue)
  return aBoardValue:upper() == boardValue
end
function IsColor(aBoardValue, aColor)
  return (IsWhitePiece(aBoardValue) and 'w' or 'b') == aColor
end
Heck, it would be easier to just cache the 'IsWhitePiece' function:

Code: Select all

local iswhite = { P = true, N = true, B = true, R = true, Q = true, K = true }
function IsWhitePiece(aBoardValue)
  return iswhite[aBoardValue] == true
end
So yea, it's a good project just needs to take advantage of the Lua programming language more.
Good luck!

User avatar
Roland Chastain
Prole
Posts: 29
Joined: Sat Feb 07, 2015 2:30 pm
Location: France
Contact:

Re: Chessboard

Post by Roland Chastain » Mon Sep 16, 2019 9:26 am

@ivan

Thank you very much for taking time to read my code and suggest improvements. Much appreciated.

I made the modifications that you suggested. Now I have to use the new code in the Löve chessboard. I am also thinking of using a GUI library.

Regards.

Roland

User avatar
ivan
Party member
Posts: 1520
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Chessboard

Post by ivan » Mon Sep 16, 2019 10:07 am

Well done.
Like I mentioned, the "standard" Lua way is to move your code in a module.
Example of a stateless module:

Code: Select all

local lib = {}
function lib.movePiece(board, from,to)
 ..
end
function lib.getPiece(board, position)
  ..
end
return lib
Also, you want to think about the memory-related stuff, for example, is your lib stateless or does it store information?
If the lib is not stateless then you can use something like closures. The easiest option looks like:

Code: Select all

return function()
  local lib = {}
  lib.board = ...
  function lib.movePiece(from, to)
    ...
  end
  function lib.getPiece(position)
    ...
  end
  return lib
end
The latter technique would allow you to easily run multiple instances/games at the same time.

Either way is better than using globals.

User avatar
pgimeno
Party member
Posts: 1872
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

Re: Chessboard

Post by pgimeno » Mon Sep 16, 2019 11:37 am

ivan wrote:
Mon Sep 16, 2019 10:07 am
Well done.
Like I mentioned, the "standard" Lua way is to move your code in a module.
Example of a stateless module:

Code: Select all

local lib = {}
function lib.movePiece(board, from,to)
 ..
end
function lib.getPiece(board, position)
  ..
end
return lib
Just a nitpick, this is not stateless in itself. The library is free to store stuff in the 'lib' table, thus holding state.

ivan wrote:
Mon Sep 16, 2019 10:07 am
Also, you want to think about the memory-related stuff, for example, is your lib stateless or does it store information?
If the lib is not stateless then you can use something like closures. The easiest option looks like:

Code: Select all

return function()
  local lib = {}
  lib.board = ...
  function lib.movePiece(from, to)
    ...
  end
  function lib.getPiece(position)
    ...
  end
  return lib
end
The latter technique would allow you to easily run multiple instances/games at the same time.
Even better:

Code: Select all

local function lib_movePiece(self, from, to)
  ...
end

local function lib_getPiece(self, position)
  ...
end

...

return function ()
  local lib = {}
  lib.movePiece = lib_movePiece
  lib.getPiece = lib_getPiece
  ...
  return lib
end
This way, the functions are shared among all instances, rather than creating a set of functions for each instance.

ivan wrote:
Mon Sep 16, 2019 10:07 am
Either way is better than using globals.
Agreed.

User avatar
Roland Chastain
Prole
Posts: 29
Joined: Sat Feb 07, 2015 2:30 pm
Location: France
Contact:

Re: Chessboard

Post by Roland Chastain » Tue Sep 17, 2019 5:54 am

@ivan, pgimeno

Thank you for messages. The suggested modification has been done.

Now the Löve chessboard and the UCI engine share the same code. I attach the new version of the chessboard to the first post of the discussion.

User avatar
ivan
Party member
Posts: 1520
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Chessboard

Post by ivan » Thu Sep 19, 2019 6:42 am

Good job Roland,
I still see some globals left over in there like "FileLetter". You can catch these by including strict.lua at the beginning of your code.
Good luck!

User avatar
ingsoc451
Citizen
Posts: 88
Joined: Sat Feb 06, 2016 9:42 pm
Location: Oceania

Re: Chessboard

Post by ingsoc451 » Thu Sep 19, 2019 10:58 am

window title can contain if black or white should move
eom

User avatar
Roland Chastain
Prole
Posts: 29
Joined: Sat Feb 07, 2015 2:30 pm
Location: France
Contact:

Re: Chessboard

Post by Roland Chastain » Fri Sep 20, 2019 8:58 am

@ivan

Thank you for your kind help. I will give a try to strict.lua. It sounds interesting.

@ingsoc45

Thank you for your message. You can already know which is the active color, by reading the message under the chessboard. Of course the appearance of the application could be improved, but for now I am working on the code of my chess module.

User avatar
Roland Chastain
Prole
Posts: 29
Joined: Sat Feb 07, 2015 2:30 pm
Location: France
Contact:

Re: Chessboard

Post by Roland Chastain » Sat Sep 21, 2019 6:24 am

@ivan

The modification that you suggested has been done. Thank you. Code updated in the GitHub repository and in the first post of the discussion.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests