public void GameState_Board_Is_Immutable_After_Construction() { var array = new Piece[64]; array[0] = Piece.WhiteRook; var target = new GameState(array, Piece.White, Castling.KQkq, Cell.None, 0, 1); //*** Initial state: a1 should have a WhiteRook Assert.AreEqual(Piece.WhiteRook, target.Board[Cell.a1]); //*** Outside array is mutated array[0] = Piece.BlackRook; //*** Expected: board a1 should maintain its inital state Assert.AreEqual(Piece.WhiteRook, target.Board[Cell.a1]); //*** Also, converting the board back to an array var array2 = target.Board.ToArray(); //*** And mutating the returned array, shouldn't modify the original board array2[0] = Piece.BlackRook; //*** Expected: board a1 should maintain its inital state Assert.AreEqual(Piece.WhiteRook, target.Board[Cell.a1]); }
public void addPiece(Piece piece, int x, int y) { if(selectPiece(x, y) != null) { throw new ArgumentException("There is already a piece on that square."); } Grid[x, y] = piece; }
public PgnMove(string pgnString) { Match m = r.Match(pgnString); if (m.Groups["Move"].Value == "") { ColorToPlay = PieceColor.Black; } else { ColorToPlay = PieceColor.White; } if (m.Groups["Castling"].Value == "") { PieceToPlay = StringToPiece(m.Groups["Piece"].Value, ColorToPlay); FromFile = StringToFile(m.Groups["File"].Value); FromRank = StringToRank(m.Groups["Rank"].Value); DestinationSquare = StringToSquare(m.Groups["Destination"].Value); PromotionPiece = StringToPromotion(m.Groups["Promotion"].Value); } else if (m.Groups["Castling"].Value == "O-O") { if (ColorToPlay == PieceColor.White) { DestinationSquare = Square.G1; PieceToPlay = Piece.WhiteKing; FromFile = 4; FromRank = 0; } if (ColorToPlay == PieceColor.Black) { DestinationSquare = Square.G8; PieceToPlay = Piece.BlackKing; FromFile = 4; FromRank = 7; } } else if (m.Groups["Castling"].Value == "O-O-O") { if (ColorToPlay == PieceColor.White) { DestinationSquare = Square.C1; PieceToPlay = Piece.WhiteKing; FromFile = 4; FromRank = 0; } if (ColorToPlay == PieceColor.Black) { DestinationSquare = Square.C8; PieceToPlay = Piece.BlackKing; FromFile = 4; FromRank = 7; } } }
public GameState(Piece[] board, Piece active, Castling castling, Cell enpassant, int draw, int move) { if (board.Length != 64) { throw new ArgumentException("Board must be 64 squares", nameof(board)); } if (active != Piece.White && active != Piece.Black) { throw new ArgumentException("Must be White or Black", nameof(active)); } Board = new Board((Piece[])board.Clone()); Active = active; Castling = castling; Enpassant = enpassant; DrawClock = draw; Move = move; }
public static bool CanMove(Piece piece, Direction dir) { ISet<Direction> allowableDirections; return Piece.TryGetValue(piece, out allowableDirections) && allowableDirections.Contains(dir); }
private static Piece[] ParseBoard(string state, ref int i) { var retval = new Piece[64]; var cell = 64 - 8; char c; while ((c = state[i]) != ' ') { if (c >= '1' && c <= '8') { i++; for (var j = c - '0'; j > 0; j--) { retval[cell++] = Piece.None; } } else if (c == '/') { i++; if (cell % 8 != 0) { throw new ArgumentException(); } cell -= 16; } else if (_PieceChar.TryGetFirst(c, out retval[cell])) { i++; cell++; } else { throw new ArgumentException(); } } i++; return retval; }
public override Image PieceImage(Piece piece) { return m_pieces[(int)piece]; }
private Pawn GetPawn(Piece p, int i, int j) { return new Pawn(((int)p.ChessColor) + p.GetType().Name, i + 1, j + 1); }
public Board(Piece[] pieces) { _pieces = pieces; }
public Move PromoteTo(Piece promotion) { return new Move(From, To, promotion); }
public Move(Cell from, Cell to, Piece promotion = Piece.None) { From = from; To = to; Promotion = promotion; }
private void SetEditorPiece(Piece piece) { m_pictureBoxWhiteKing.BorderStyle = BorderStyle.FixedSingle; m_pictureBoxWhiteQueen.BorderStyle = BorderStyle.FixedSingle; m_pictureBoxWhiteRook.BorderStyle = BorderStyle.FixedSingle; m_pictureBoxWhiteBishop.BorderStyle = BorderStyle.FixedSingle; m_pictureBoxWhiteKnight.BorderStyle = BorderStyle.FixedSingle; m_pictureBoxWhitePawn.BorderStyle = BorderStyle.FixedSingle; m_pictureBoxBlackKing.BorderStyle = BorderStyle.FixedSingle; m_pictureBoxBlackQueen.BorderStyle = BorderStyle.FixedSingle; m_pictureBoxBlackRook.BorderStyle = BorderStyle.FixedSingle; m_pictureBoxBlackBishop.BorderStyle = BorderStyle.FixedSingle; m_pictureBoxBlackKnight.BorderStyle = BorderStyle.FixedSingle; m_pictureBoxBlackPawn.BorderStyle = BorderStyle.FixedSingle; m_editorPiece = (m_editorPiece == piece) ? Piece.None : piece; switch (m_editorPiece) { case Piece.WhiteKing: m_pictureBoxWhiteKing.BorderStyle = BorderStyle.Fixed3D; break; case Piece.WhiteQueen: m_pictureBoxWhiteQueen.BorderStyle = BorderStyle.Fixed3D; break; case Piece.WhiteRook: m_pictureBoxWhiteRook.BorderStyle = BorderStyle.Fixed3D; break; case Piece.WhiteBishop: m_pictureBoxWhiteBishop.BorderStyle = BorderStyle.Fixed3D; break; case Piece.WhiteKnight: m_pictureBoxWhiteKnight.BorderStyle = BorderStyle.Fixed3D; break; case Piece.WhitePawn: m_pictureBoxWhitePawn.BorderStyle = BorderStyle.Fixed3D; break; case Piece.BlackKing: m_pictureBoxBlackKing.BorderStyle = BorderStyle.Fixed3D; break; case Piece.BlackQueen: m_pictureBoxBlackQueen.BorderStyle = BorderStyle.Fixed3D; break; case Piece.BlackRook: m_pictureBoxBlackRook.BorderStyle = BorderStyle.Fixed3D; break; case Piece.BlackBishop: m_pictureBoxBlackBishop.BorderStyle = BorderStyle.Fixed3D; break; case Piece.BlackKnight: m_pictureBoxBlackKnight.BorderStyle = BorderStyle.Fixed3D; break; case Piece.BlackPawn: m_pictureBoxBlackPawn.BorderStyle = BorderStyle.Fixed3D; break; } }
public abstract Image PieceImage(Piece piece);
public void DrawPiece(Square square, Piece piece) { if (square != Square.None && piece != Piece.None) { Image image = PieceImage(piece); switch (m_viewFromBlack) { case true: m_picturePainter.DrawImage(image, ((7 - ChessFacade.File(square)) * SquareSize) + EdgeSize, (ChessFacade.Rank(square) * SquareSize) + EdgeSize, SquareSize, SquareSize); break; case false: m_picturePainter.DrawImage(image, (ChessFacade.File(square) * SquareSize) + EdgeSize, ((7 - ChessFacade.Rank(square)) * SquareSize) + EdgeSize, SquareSize, SquareSize); break; } } }