Page 2 of 5

Re: Chessboard

Posted: Sat Sep 14, 2019 11:52 am
by Roland Chastain
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

Re: Chessboard

Posted: Mon Sep 16, 2019 5:40 am
by ivan
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!

Re: Chessboard

Posted: Mon Sep 16, 2019 9:26 am
by Roland Chastain
@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

Re: Chessboard

Posted: Mon Sep 16, 2019 10:07 am
by ivan
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.

Re: Chessboard

Posted: Mon Sep 16, 2019 11:37 am
by pgimeno
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.

Re: Chessboard

Posted: Tue Sep 17, 2019 5:54 am
by Roland Chastain
@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.

Re: Chessboard

Posted: Thu Sep 19, 2019 6:42 am
by ivan
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!

Re: Chessboard

Posted: Thu Sep 19, 2019 10:58 am
by monolifed
window title can contain if black or white should move

Re: Chessboard

Posted: Fri Sep 20, 2019 8:58 am
by Roland Chastain
@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.

Re: Chessboard

Posted: Sat Sep 21, 2019 6:24 am
by Roland Chastain
@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.