rudzen / ChessLib

C# chess library containing a complete data structure and move generation.
MIT License
81 stars 22 forks source link

MakeMove(Move), where the from square is empty leads to unexpected result #28

Closed jongleur1983 closed 4 years ago

jongleur1983 commented 4 years ago

I tried the following code for getting into this (nice looking, so far) library, using the 2020.1 branch:

var game = new Game(new Position());
game.NewGame();
game.MakeMove(new Move(Squares.d2, Squares.d4));
game.MakeMove(new Move(Squares.d2, Squares.d4));

Lines 1-3 do what I expected them to do (and the first MakeMove in line 3 returns true as expected, too). For Line 4, the second MakeMove() I had expected MakeMove to do nothing and return false (or throw some kind of exception) as D2 is empty before this move already.

Instead, afterwards d2 and d4 are empty but MakeMove returned true - which should, according to the comments, say "everything is fine".

I think it's missing a check in Position.MakeMove. I'll try to solve und PR a solution

rudzen commented 4 years ago

Good point. There were no initial check if the piece actually exists when trying to move it. This was under the assumption that only moves which are generated would be applied, but since that is quite an assumption the fix is welcomed.

jongleur1983 commented 4 years ago

@rudzen just for more context - maybe I'm using the wrong library: I'm going to reanimate a quite old project we did at university back in 2009. It's a combination of Chess with a battle mode: The only difference to common chess is: capture isn't a safe thing - if you try to capture a piece of your opponent, this leads to a fight between the two. In this fight, defense on the board is a bonus for the defender, more pieces threatening the same square produces an attacker bonus. In general it's slightly easier for the attacker to win, but far from guaranteed.

In contrast to "usual" chess the losing character has to leave the board, so you can attack and still lose your piece.

Thus I look for a chess library that can provide

move generation and more is a bonus, but for the far future when I might try to implement an AI opponent; for now it's a two-player game without AI option.

rudzen commented 4 years ago

@rudzen just for more context - maybe I'm using the wrong library: I'm going to reanimate a quite old project we did at university back in 2009. It's a combination of Chess with a battle mode: The only difference to common chess is: capture isn't a safe thing - if you try to capture a piece of your opponent, this leads to a fight between the two. In this fight, defense on the board is a bonus for the defender, more pieces threatening the same square produces an attacker bonus. In general it's slightly easier for the attacker to win, but far from guaranteed.

In contrast to "usual" chess the losing character has to leave the board, so you can attack and still lose your piece.

Thus I look for a chess library that can provide

* move validation (validate user input if it's following the rules)
  that's why I expected MakeMove() to validate - providing the bool return value

* modelling the chess game state

* adaptible to the differences I need

move generation and more is a bonus, but for the far future when I might try to implement an AI opponent; for now it's a two-player game without AI option.

Sounds interesting.

The current state of this library can not handle everything you mention mainly because it is a bit rigid some places. But it could give you a starting point as it contains some functionality you most certainly can use. Since your "evaluation" of the game you describe is somewhat similar to what a "normal" chess engine would encompass, it should be possible without too many modifications to make it work.

I plan to allow for additional logic injection into the main functionalities at some point, which should be an improvement imo.

Hope it works out for you.