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 void GameState_FromForsythEdwardsNotation_Parses_Board_Successfully() { var expectedBoard = new Board(new[] { Piece.WhiteRook, Piece.WhiteKnight, Piece.WhiteBishop, Piece.WhiteQueen, Piece.WhiteKing, Piece.WhiteBishop, Piece.WhiteKnight, Piece.WhiteRook, Piece.WhitePawn, Piece.WhitePawn, Piece.WhitePawn, Piece.WhitePawn, Piece.WhitePawn, Piece.WhitePawn, Piece.WhitePawn, Piece.WhitePawn, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.None, Piece.BlackPawn, Piece.BlackPawn, Piece.BlackPawn, Piece.BlackPawn, Piece.BlackPawn, Piece.BlackPawn, Piece.BlackPawn, Piece.BlackPawn, Piece.BlackRook, Piece.BlackKnight, Piece.BlackBishop, Piece.BlackQueen, Piece.BlackKing, Piece.BlackBishop, Piece.BlackKnight, Piece.BlackRook, }); var state = GameState.FromForsythEdwardsNotation("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); Assert.IsNotNull(state); for (Cell i = Cell.a1; i <= Cell.h8; i++) { Assert.AreEqual(expectedBoard[i], state.Board[i]); } }
public Grid GetNewGame() { Board b = new Board(true); Grid toReturn = GetGridFromBoard(b); return toReturn; }
private Grid GetGridFromBoard(Board b) { Grid toReturn = new Grid() { CurrentMessage = Message.GetMessage(ChessMessage.NewGame), IsGridShifted = false, MoveNumber = 2, NextPlayerToPlay = 1, NumberColumns = 8, NumberLines = 8 }; toReturn.PawnLocations = GetAllPieces(b); if (b.PawnPromoted) { //We list the propositions : toReturn.NumberLines = 9; toReturn.MoveNumber = 1; } return toReturn; }
private List<Pawn> GetAllPieces(Board b) { List<Pawn> toReturn = new List<Pawn>(); Piece[,] board = b.AllPieces(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (board[i, j] != null) { Piece p = board[i, j]; Pawn toAdd = GetPawn(p, j, i); toReturn.Add(toAdd); if (p is IHasAlreadyMoved && ((IHasAlreadyMoved)p).HasAlreadyMoved) { Pawn infoHasAlreadyMoved = GetPawn(p, j, i); infoHasAlreadyMoved.Coord.x = -infoHasAlreadyMoved.Coord.x; infoHasAlreadyMoved.Coord.y = -infoHasAlreadyMoved.Coord.y; infoHasAlreadyMoved.Name = HaveAlreadyMovedSpecialName; toReturn.Add(infoHasAlreadyMoved); } } } } if (b.EnPassant != null) { Pawn enPassant = new Pawn(EnPassantSpecialName, -(Board.Columns[b.EnPassant.Item1] + 1), -(b.EnPassant.Item2)); toReturn.Add(enPassant); } if (b.PawnPromoted) { //We list the propositions : int currentColumn = 0; foreach (Piece p in b.PawnPromotedChoice()) { Pawn PawnChoice = GetPawn(p, currentColumn, 8); toReturn.Add(PawnChoice); currentColumn++; } } return toReturn; }
private Board GenerateBoardFromGrid(Grid currentState) { Board b = new Board(false); List<Tuple<char, int>> HaveAlreadyMoved = new List<Tuple<char, int>>(); //0 Special Pawn foreach (Pawn pawn in currentState.PawnLocations) { if(pawn.Coord.x < 1 || pawn.Coord.y < 1){ //Special Pawn! if (pawn.Name == EnPassantSpecialName) { b.EnPassant = new Tuple<char, int>(Board.Columns_Inv[Math.Abs(pawn.Coord.x)], Math.Abs(pawn.Coord.y)); } else { if (pawn.Name == HaveAlreadyMovedSpecialName) { var hasAlreadyMoved = new Tuple<char, int>(Board.Columns_Inv[Math.Abs(pawn.Coord.x)], Math.Abs(pawn.Coord.y)); HaveAlreadyMoved.Add(hasAlreadyMoved); } else { throw new Exception(String.Format("Piece Unknown ({0}) or badly located: [{1},{2}]", pawn.Name, pawn.Coord.x, pawn.Coord.y)); } } } else{ if (pawn.Coord.x < 9 && pawn.Coord.y < 9) { var p = GetPiece(pawn); b.PutPiece(p, Board.Columns_Inv[pawn.Coord.x], pawn.Coord.y); } } } foreach (var hasAlreadyMoved in HaveAlreadyMoved) { var piece = b.GetPiece(hasAlreadyMoved.Item1, hasAlreadyMoved.Item2); ((IHasAlreadyMoved)piece).HasAlreadyMoved = true; } return b; }
public Game(Loader loader) { _currentPlayer = new Player(Color.White); _board = new Board(loader); _turns = loader.Turns; }