public void Move(Cell src, Cell dst) { Console.WriteLine($"move from {src} to {dst}"); var piece = Board.ContainsKey(src) ? Board[src] : null; if (piece == null) { Console.WriteLine($"no piece at {src}"); } else { var context = new MoveValidationContext { ActivePlayer = ActivePlayer, Src = src, Board = Board, Piece = piece }; piece.ValidatingMovement(context); var moveOutput = context.Find(dst); if (moveOutput != null) { MoveExecutor.ExecuteMove ( new MoveExecutionContext { Type = moveOutput.Type, Src = src, Dst = moveOutput.Cell, Piece = piece, Board = Board, Captured = Captured } ); TogglePlayerTurn(); if (WhiteKingCaptured()) { Winner = EPlayer.Black; } else if (BlackKingCaptured()) { Winner = EPlayer.White; } } else { Console.WriteLine($"no valid move from {src} to {dst}, reason: {context.InvalidMessage}"); } } BoardPresenter.Print(Board); CapturedPresenter.Print(Captured); }
public void Move(Cell src, Cell dst) { Console.WriteLine($"move from {src} to {dst}"); //get piece var piece = Board.ContainsKey(src) ? Board[src] : null; if (piece == null) { Console.WriteLine($"no piece at {src}"); } else { //is move valid? var validationOutput = MoveValidator.IsMoveValid(ActivePlayer, piece, src, dst, Board); if (!validationOutput.Valid) { Console.WriteLine($"piece at {src} cannot move to {dst}, {validationOutput.InvalidMessage}"); } else { //execute move MoveExecutor.ExecuteMove(validationOutput, piece, src, dst, Board, Captured); //toggle player ActivePlayer = ActivePlayer == EPlayer.White ? EPlayer.Black : EPlayer.White; if (Captured.Count(x => x.Player == EPlayer.White && x is King) == 1) { Winner = EPlayer.Black; } else if (Captured.Count(x => x.Player == EPlayer.White && x is King) == 1) { Winner = EPlayer.White; } } } BoardPresenter.Print(Board); }