Complicating code in C#

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

Maybe never use inheritance unless it inherits from an interface.
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

I used this because I thought brackets look ugly. But then I ended up with annoying type casts.
So I think I never use it again. Don't know how to implement that correctly and using it without type casting.

Code: Select all

  
public interface ICoordSet : IHSet<ICoord>


public interface IHSet<T>: IEnumerable<T>
{
        IHSet<T> Insert(T field);
        IHSet<T> Union(IHSet<T> fieldSet);
        IHSet<T> Minus(IHSet<T> fieldSet);

        IHSet<T> Complement();
       
        bool Contains(T member);
        int Count();

        bool Empty();
 }

Now I 'm using this instead:

Code: Select all

   public interface ICoordSet : IEnumerable<ICoord>
    {

        ICoordSet Insert(ICoord field);
        ICoordSet Union(ICoordSet fieldSet);
        ICoordSet Minus(ICoordSet fieldSet);

        ICoordSet Complement();
        
 ... etc.
Repairing another hundred compile errors of course when lucky.
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

Took me at least a day and much stress. Because of bug in move generation.
Its because of rewrite and glue-ing all pieces together with buggy conversions.
Don't try this at home.

Code: Select all

public struct BitBoardCoord : ICoord
       public int Index
        {
            get;
            private set;
        }
 
        public  ulong ToBitBoardValue()
        {
            return (((ulong)2) << Index);
        }
 
Should be:

Code: Select all

public struct BitBoardCoord : ICoord 
         ...
        
        public  ulong ToBitBoardValue()
        {
            return (((ulong)1) << Index);
        }
 

Don't forget source code it immutable although it is software. So first time right.
That is do refactoring as soon as possible. Or start from scratch. Or enjoy the debugging misery.

I hate debugging.
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

Found another very silly bug. Costing half a day.
This time it was in predefined coords.
So it didn't find some castling moves.

I can chose between two evil ways. Writing unit tests that almost never seem to fail or spending days on debugging.
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: Complicating code in C#

Post by mvanthoor »

The third way is starting over, and writing a program that doesn't have a gazillion objects and interfaces for things you're never going to dynamically replace.
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

Yes I may have problems with understanding interfaces. I also remember interfaces reducing compilation time.
If I would write small programs and never changed the code then there is a small chance I would use less interfaces I guess.

O wait there was another advantage. I hate reading long files while interfaces are much shorter. Always takes me minutes to recollect that I can "collapse to definitions" in C#/visual studio
Last edited by Henk on Thu Feb 04, 2021 1:11 pm, edited 1 time in total.
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

mvanthoor wrote: Thu Feb 04, 2021 12:41 pm The third way is starting over, and writing a program that doesn't have a gazillion objects and interfaces for things you're never going to dynamically replace.
It's true I better had not started the rewrites of last two or three months. Costing too much time.

But for now I am still proud of this interface. I am sure I would not have invented this from scratch

Code: Select all

    /// <summary>
    /// Generic interface for a chessboard used by IChessPosition 
    /// Has no information about squares
    /// </summary>
    public interface IChessBoard
    {
        IChessBoard Move(IMoveBase move);
        IChessBoard PutOnBoard(IPieceType pieceType, ICoord location);      
        ICoordSet GetSquares(IPieceType pieceType);
        ICoordSet GetSquares(IPieceKind kind);

        /// <summary>
        /// Get squares occupied by pieces of a color
        /// </summary>  
        /// <returns>coordinates of the squares</returns>
        ICoordSet GetSquares(IPieceKind kind, IPieceColor pieceColor);
        ICoordSet GetSquares(IPieceColor pieceColor);
        ICoordSet Occupiers { get; }
        bool Occupied(ICoord coord);
        IPieceColor OccupierColor(ICoord coord);
        IPieceKind PieceKind(ICoord coord);  
        IPieceType PieceSort(ICoord coord);
        byte[] Bytes();     
    }
    
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

Edit: Onebutlast method should be called PieceType instead of PieceSort. Forgot to change that.
Perhaps also Squares should be renamed to Shapes. For in 3 or 4D chess a square might for instance be a cube.
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

Makes no sense too. GetSquares should be called GetCoords. Next attempt:

Code: Select all

  /// <summary>
    /// Generic interface for a chessboard used by IChessPosition 
    /// Has no information about squares
    /// </summary>
    public interface IChessBoard
    {
        IChessBoard Move(IMoveBase move);
        IChessBoard PutOnBoard(IPieceType pieceType, ICoord location);      
        ICoordSet GetCoords(IPieceType pieceType);
        ICoordSet GetCoords(IPieceKind kind);

        /// <summary>
        /// Get coordinates of locations of  pieces of a color on this board
        /// </summary> 
        ICoordSet GetCoords(IPieceKind kind, IPieceColor pieceColor);
        ICoordSet GetCoords(IPieceColor pieceColor);
        ICoordSet Occupiers { get; }
        bool Occupied(ICoord coord);
        IPieceColor OccupierColor(ICoord coord);
        IPieceKind PieceKind(ICoord coord);  
        IPieceType PieceType(ICoord coord);
        byte[] Bytes();     
    }
 
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Complicating code in C#

Post by Henk »

Trying to eliminate square. But maybe not possible or too much work. Already split it up in square containing properties for move generation and square containing properties for evaluation. Only property of square should be its coordinate perhaps so that would mean you can eliminate square.
Last edited by Henk on Fri Feb 05, 2021 12:14 pm, edited 1 time in total.