本文整理汇总了C#中ChessGame类的典型用法代码示例。如果您正苦于以下问题:C# ChessGame类的具体用法?C# ChessGame怎么用?C# ChessGame使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChessGame类属于命名空间,在下文中一共展示了ChessGame类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Generate
public List<String> Generate(ChessGame game)
{
var result = game.Info.Select(tag => "[" + tag.Key + " \"" + tag.Value + "\"]").ToList();
result.Add("");
result.Add(GenerateMovetext(game));
return result;
}
示例2: TestAfter1e4
public static void TestAfter1e4()
{
ChessGame game = new ChessGame();
game.ApplyMove(new Move("E2", "E4", Player.White), true);
string fen = game.GetFen();
Assert.AreEqual("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", fen);
}
示例3: GetValidMoves
public override ReadOnlyCollection<Move> GetValidMoves(Position from, bool returnIfAny, ChessGame game, Func<Move, bool> gameMoveValidator)
{
ChessUtilities.ThrowIfNull(from, "from");
List<Move> validMoves = new List<Move>();
Piece piece = game.GetPieceAt(from);
int l0 = game.BoardHeight;
int l1 = game.BoardWidth;
for (int i = -7; i < 8; i++)
{
if (i == 0)
continue;
if (from.Rank + i > 0 && from.Rank + i <= l0)
{
Move move = new Move(from, new Position(from.File, from.Rank + i), piece.Owner);
if (gameMoveValidator(move))
{
validMoves.Add(move);
if (returnIfAny)
return new ReadOnlyCollection<Move>(validMoves);
}
}
if ((int)from.File + i > -1 && (int)from.File + i < l1)
{
Move move = new Move(from, new Position(from.File + i, from.Rank), piece.Owner);
if (gameMoveValidator(move))
{
validMoves.Add(move);
if (returnIfAny)
return new ReadOnlyCollection<Move>(validMoves);
}
}
}
return new ReadOnlyCollection<Move>(validMoves);
}
示例4: Set
public void Set(ChessGame g)
{
idx = -1;
move = null;
total_moves = 0;
if (g == null)
{
player = ChessGamePlayer.
CreatePlayer ();
return;
}
player = g.HasTag ("FEN") ? ChessGamePlayer.
CreateFromFEN (g.
GetTagValue ("FEN",
null)) :
ChessGamePlayer.CreatePlayer ();
game = g;
int n = game.Moves.Count;
if (n > 0)
{
total_moves = n;
}
if (total_moves == 0)
hasNext = false;
else
hasNext = true;
}
示例5: TestAfter1c5
public static void TestAfter1c5()
{
ChessGame game = new ChessGame();
game.ApplyMove(new Move("E2", "E4", Player.White), true);
game.ApplyMove(new Move("C7", "C5", Player.Black), true);
string fen = game.GetFen();
Assert.AreEqual("rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2", fen);
}
示例6: GetValidMoves
public override ReadOnlyCollection<Move> GetValidMoves(Position from, bool returnIfAny, ChessGame game, Func<Move, bool> gameMoveValidator)
{
ChessUtilities.ThrowIfNull(from, "from");
ReadOnlyCollection<Move> horizontalVerticalMoves = new Rook(Owner).GetValidMoves(from, returnIfAny, game, gameMoveValidator);
if (returnIfAny && horizontalVerticalMoves.Count > 0)
return horizontalVerticalMoves;
ReadOnlyCollection<Move> diagonalMoves = new Bishop(Owner).GetValidMoves(from, returnIfAny, game, gameMoveValidator);
return new ReadOnlyCollection<Move>(horizontalVerticalMoves.Concat(diagonalMoves).ToList());
}
示例7: TestMovingWhiteKingLosingCastlingRights
public static void TestMovingWhiteKingLosingCastlingRights()
{
ChessGame game = new ChessGame();
game.ApplyMove(new Move("E2", "E4", Player.White), true);
game.ApplyMove(new Move("C7", "C5", Player.Black), true);
game.ApplyMove(new Move("E1", "E2", Player.White), true);
string fen = game.GetFen();
Assert.AreEqual("rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPPKPPP/RNBQ1BNR b kq - 1 2", fen);
}
示例8: TestMovingBlackKingLosingCastlingRights
public static void TestMovingBlackKingLosingCastlingRights()
{
ChessGame game = new ChessGame();
game.ApplyMove(new Move("E2", "E4", Player.White), true);
game.ApplyMove(new Move("E7", "E5", Player.Black), true);
game.ApplyMove(new Move("G1", "F3", Player.White), true);
game.ApplyMove(new Move("E8", "E7", Player.Black), true);
string fen = game.GetFen();
Assert.AreEqual("rnbq1bnr/ppppkppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQ - 2 3", fen);
}
示例9: IsValidMove
public override bool IsValidMove(Move move, ChessGame game)
{
ChessUtilities.ThrowIfNull(move, "move");
ChessUtilities.ThrowIfNull(game, "game");
Position origin = move.OriginalPosition;
Position destination = move.NewPosition;
PositionDistance posDelta = new PositionDistance(origin, destination);
if ((posDelta.DistanceX != 2 || posDelta.DistanceY != 1) && (posDelta.DistanceX != 1 || posDelta.DistanceY != 2))
return false;
return true;
}
示例10: Parse
public ChessGame Parse(IEnumerable<String> pgn, IChessMoveNotation moveNotation)
{
var lines = pgn.GetEnumerator();
var metainfo = ParseTags(lines);
var movetext = ParseMovetext(lines);
var game = new ChessGame(metainfo);
foreach (ChessMove cm in ParseMoves(movetext, moveNotation, game.Players))
game.AddMove(cm);
return game;
}
示例11: CanCastle
protected virtual bool CanCastle(Position origin, Position destination, ChessGame game)
{
if (!HasCastlingAbility) return false;
if (Owner == Player.White)
{
if (origin.File != File.E || origin.Rank != 1)
return false;
if (game.IsInCheck(Player.White))
return false;
if (destination.File == File.C)
{
if (!game.CanWhiteCastleQueenSide || game.GetPieceAt(File.D, 1) != null
|| game.GetPieceAt(File.C, 1) != null
|| game.GetPieceAt(File.B, 1) != null
|| game.WouldBeInCheckAfter(new Move(new Position(File.E, 1), new Position(File.D, 1), Player.White), Player.White)
|| game.WouldBeInCheckAfter(new Move(new Position(File.E, 1), new Position(File.C, 1), Player.White), Player.White))
return false;
}
else
{
if (!game.CanWhiteCastleKingSide || game.GetPieceAt(File.F, 1) != null
|| game.GetPieceAt(File.G, 1) != null
|| game.WouldBeInCheckAfter(new Move(new Position(File.E, 1), new Position(File.F, 1), Player.White), Player.White)
|| game.WouldBeInCheckAfter(new Move(new Position(File.E, 1), new Position(File.G, 1), Player.White), Player.White))
return false;
}
}
else
{
if (origin.File != File.E || origin.Rank != 8)
return false;
if (game.IsInCheck(Player.Black))
return false;
if (destination.File == File.C)
{
if (!game.CanBlackCastleQueenSide || game.GetPieceAt(File.D, 8) != null
|| game.GetPieceAt(File.C, 8) != null
|| game.GetPieceAt(File.B, 8) != null
|| game.WouldBeInCheckAfter(new Move(new Position(File.E, 8), new Position(File.D, 8), Player.Black), Player.Black)
|| game.WouldBeInCheckAfter(new Move(new Position(File.E, 8), new Position(File.C, 8), Player.Black), Player.Black))
return false;
}
else
{
if (!game.CanBlackCastleKingSide || game.GetPieceAt(File.F, 8) != null
|| game.GetPieceAt(File.G, 8) != null
|| game.WouldBeInCheckAfter(new Move(new Position(File.E, 8), new Position(File.F, 8), Player.Black), Player.Black)
|| game.WouldBeInCheckAfter(new Move(new Position(File.E, 8), new Position(File.G, 8), Player.Black), Player.Black))
return false;
}
}
return true;
}
示例12: ChessForm
public ChessForm()
{
InitializeComponent();
chessPanel.BackColor = Color.Gray;
GamePoint.SIDE_LENGTH = chessPanel.Height / 8;
if (chessPanel.Height != chessPanel.Width)
{
throw new Exception("Chess game needs a square panel");
}
game = new ChessGame(chessPanel.Height);
Invalidate();
}
示例13: IsValidMove
public override bool IsValidMove(Move move, ChessGame game)
{
ChessUtilities.ThrowIfNull(move, "move");
Position origin = move.OriginalPosition;
Position destination = move.NewPosition;
PositionDistance distance = new PositionDistance(origin, destination);
if ((distance.DistanceX != 1 || distance.DistanceY != 1)
&& (distance.DistanceX != 0 || distance.DistanceY != 1)
&& (distance.DistanceX != 1 || distance.DistanceY != 0)
&& (distance.DistanceX != 2 || distance.DistanceY != 0))
return false;
if (distance.DistanceX != 2)
return true;
return CanCastle(origin, destination, game);
}
示例14: IsValidMove
public override bool IsValidMove(Move move, ChessGame game)
{
bool validByStandardRules = base.IsValidMove(move, game);
if (validByStandardRules) return true;
if (move.OriginalPosition.Rank == 1 &&
move.NewPosition.Rank == 3 &&
move.OriginalPosition.File == move.NewPosition.File &&
game.GetPieceAt(move.NewPosition) == null &&
game.GetPieceAt(new Position(move.OriginalPosition.File, move.OriginalPosition.Rank + 1)) == null)
// Horde pawns at the first rank can also move two squares on their first move. However, these can't be en-passant captured.
{
return true;
}
return false;
}
示例15: GenerateMovetext
public String GenerateMovetext(ChessGame game)
{
var movetext = new StringBuilder();
var moveCount = 1;
foreach (var m in game.Moves)
{
if (moveCount % 2 == 1)
movetext.Append((moveCount+1)/2 + ". ");
movetext.Append(m + " ");
moveCount++;
}
if (movetext.Length > 0)
movetext.Remove(movetext.Length - 1, 1);
return movetext.ToString();
}