ThanksmaksimKorzh wrote: ↑Tue Dec 08, 2020 7:36 pmMarcel, I always enjoy reading your code snippets))) It's like if I was reading my own code but if I had a better habits)


Moderators: hgm, Dann Corbit, Harvey Williamson
ThanksmaksimKorzh wrote: ↑Tue Dec 08, 2020 7:36 pmMarcel, I always enjoy reading your code snippets))) It's like if I was reading my own code but if I had a better habits)
Code: Select all
public struct BitBoardCoord : ICoord
{
public int Value
{
get;
private set;
}
public BitBoardCoord(ulong bit)
{
if (bit == 0) Value = -1;
Value = (int)Math.Log(bit, 2);
}
public BitBoardCoord(coord coord)
{
Value = (int)coord;
}
public override string ToString()
{
return ((coord)Value).ToString();
}
}
public interface ICoord
{
int Value
{
get;
}
}
Why do you need coordinates anyway? Chess engines don't use coordinates internally, most of the time. Square numbers from 0 to and including 63 is more efficient. If you need coordinates as a reference (for example E1 and G1 for castling, that sort of stuff) just define a few constants with the correct square number.
Code: Select all
pub struct Squares;
impl Squares {
// White side squares that are important for castling
pub const A1: Square = 0;
pub const B1: Square = 1;
pub const C1: Square = 2;
pub const D1: Square = 3;
pub const E1: Square = 4;
pub const F1: Square = 5;
pub const G1: Square = 6;
pub const H1: Square = 7;
// Black side squares that are important for castling
pub const A8: Square = 56;
pub const B8: Square = 57;
pub const C8: Square = 58;
pub const D8: Square = 59;
pub const E8: Square = 60;
pub const F8: Square = 61;
pub const G8: Square = 62;
pub const H8: Square = 63;
// White EP-squares start/end
pub const A3: Square = 16;
pub const H3: Square = 23;
// Black EP-squares start/end
pub const A6: Square = 40;
pub const H6: Square = 47;
}
Code: Select all
const EP_SQUARES_WHITE: RangeInclusive<Square> = Squares::A3..=Squares::H3;
const EP_SQUARES_BLACK: RangeInclusive<Square> = Squares::A6..=Squares::H6;
When someone says I want to play capablanca chess or three dimensional chess or whatever idiotic chess variation then you want to keep thinks similar to reduce complexity.mvanthoor wrote: ↑Thu Dec 10, 2020 4:54 pmWhy do you need coordinates anyway? Chess engines don't use coordinates internally, most of the time. Square numbers from 0 to and including 63 is more efficient. If you need coordinates as a reference (for example E1 and G1 for castling, that sort of stuff) just define a few constants with the correct square number.
I have this struct, which just collects a bunch of constants under one name:
And then, in my FEN-reader, for example:Code: Select all
pub struct Squares; impl Squares { // White side squares that are important for castling pub const A1: Square = 0; pub const B1: Square = 1; pub const C1: Square = 2; pub const D1: Square = 3; pub const E1: Square = 4; pub const F1: Square = 5; pub const G1: Square = 6; pub const H1: Square = 7; // Black side squares that are important for castling pub const A8: Square = 56; pub const B8: Square = 57; pub const C8: Square = 58; pub const D8: Square = 59; pub const E8: Square = 60; pub const F8: Square = 61; pub const G8: Square = 62; pub const H8: Square = 63; // White EP-squares start/end pub const A3: Square = 16; pub const H3: Square = 23; // Black EP-squares start/end pub const A6: Square = 40; pub const H6: Square = 47; }
So I can say things like (pseudocode): "if square in EP_SQUARES_WHITE { .... }"Code: Select all
const EP_SQUARES_WHITE: RangeInclusive<Square> = Squares::A3..=Squares::H3; const EP_SQUARES_BLACK: RangeInclusive<Square> = Squares::A6..=Squares::H6;
You are making things much more difficult for yourself than you need to. If you'd want to, the only things you need in a chess engine are strings, characters, and some ints. (And maybe some derivative types such as arrays of ints, and the like.)
Code: Select all
public enum PieceType2 { whitePawn, whiteKnight, whiteBishop, whiteRook, whiteQueen, whiteKing, blackPawn, blackKnight, blackBishop, blackRook, blackQueen, blackKing, none }
There is noting wrong with using an enum, if you insist on writing inefficient code use the struct.Henk wrote: ↑Fri Dec 11, 2020 10:16 amI think enum below is terrible code as well. Mixing pieceColor with pieceKind.
Maybe better use a structCode: Select all
public enum PieceType2 { whitePawn, whiteKnight, whiteBishop, whiteRook, whiteQueen, whiteKing, blackPawn, blackKnight, blackBishop, blackRook, blackQueen, blackKing, none }
Piece {
int Color;
int Kind;
}
or something.
Code: Select all
enum PieceType { White, Black, whitePawn, blackPawn, whiteKnight, blackKnight, etc. };
I really don't understand what you are trying to achieve, why complicate matters so much, it is all a piece of cake.