Chessboard
- Roland Chastain
- Prole
- Posts: 41
- Joined: Sat Feb 07, 2015 2:30 pm
- Location: France
- Contact:
Re: Chessboard
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
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
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:
You already have the square names table right there:
Your "IsColor" function is not very efficient either:
Pattern matching is much harder to read and slower compared to:
Heck, it would be easier to just cache the 'IsWhitePiece' function:
So yea, it's a good project just needs to take advantage of the Lua programming language more.
Good luck!
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
)
Code: Select all
LSquareName[aY1][aX1]..LSquareName[aY2][aX2]
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
Code: Select all
function IsWhitePiece(aBoardValue)
return aBoardValue:upper() == boardValue
end
function IsColor(aBoardValue, aColor)
return (IsWhitePiece(aBoardValue) and 'w' or 'b') == aColor
end
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
Good luck!
- Roland Chastain
- Prole
- Posts: 41
- Joined: Sat Feb 07, 2015 2:30 pm
- Location: France
- Contact:
Re: Chessboard
@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
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
Well done.
Like I mentioned, the "standard" Lua way is to move your code in a module.
Example of a stateless module:
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:
The latter technique would allow you to easily run multiple instances/games at the same time.
Either way is better than using globals.
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
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
Either way is better than using globals.
Re: Chessboard
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 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
Even better: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:The latter technique would allow you to easily run multiple instances/games at the same time.Code: Select all
return function() local lib = {} lib.board = ... function lib.movePiece(from, to) ... end function lib.getPiece(position) ... end return lib end
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
Agreed.
- Roland Chastain
- Prole
- Posts: 41
- Joined: Sat Feb 07, 2015 2:30 pm
- Location: France
- Contact:
Re: Chessboard
@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.
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
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!
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
window title can contain if black or white should move
- Roland Chastain
- Prole
- Posts: 41
- Joined: Sat Feb 07, 2015 2:30 pm
- Location: France
- Contact:
Re: Chessboard
@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.
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.
- Roland Chastain
- Prole
- Posts: 41
- Joined: Sat Feb 07, 2015 2:30 pm
- Location: France
- Contact:
Re: Chessboard
@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.
The modification that you suggested has been done. Thank you. Code updated in the GitHub repository and in the first post of the discussion.
Who is online
Users browsing this forum: Google [Bot] and 2 guests