/// <summary> /// returns the number of ways in which soldier on coord can be captured; /// </summary> /// <param name="board"></param> /// <param name="coord"></param> /// <param name="player"></param> /// <returns></returns> public int CanBeCaptured(Board board, Coordinate coord, Player player) { int num = 0; Rules rule = new Rules(); IList<Coordinate> optionalCoords = rule.OptionalMoves(board, coord, player); IList<Coordinate> coordsInDir = rule.GetMovesInDirection(board, coord, player); //collect all coords behind coord IList<Coordinate> coordsfrombehind = optionalCoords.Where(opCor => !coordsInDir.Contains(opCor)).ToList(); foreach (var cid in coordsInDir) { if (board.GetPlayer(board[cid.X, cid.Y]) == board.GetOpponent(player) && rule.CoordsToCaptureAndDest(board, cid, coord, board.GetOpponent(player)).Count > 0) num++; } foreach (var cfb in coordsfrombehind) { if (board.GetPlayer(board[cfb.X, cfb.Y]) == board.GetOpponent(player) && board.IsKing(coord) && rule.CoordsToCaptureAndDest(board, cfb, coord, board.GetOpponent(player)).Count > 0) num++; } return num; }
/// <summary> /// Returns current board state after moveType from position /// </summary> /// <param name="player"></param> /// <param name="moveType"></param> /// <param name="position"></param> /// <param name="needToContinueEating"></param> /// <param name="mustCapture"></param> /// <returns></returns> public IBoardState GetBoardState(Player player, MoveType moveType, Point position, out bool needToContinueEating,out bool mustCapture) { Rules rule= new Rules(); bool lastmovewasACaptured = false; needToContinueEating = false; this.Board = ConvertBoardStateToBoard(this); var destPoint = ConvertMoveTypeToCoordinate(position, moveType); //returns type point var srcPoint = ConvertPointToCoordinate(position.X, position.Y); // returns type point var srcCoord = new Coordinate { X = srcPoint.X, Y = srcPoint.Y, Status = Board.PieceColor(Board[srcPoint.X, srcPoint.Y]) }; var oppCoord = new Coordinate { X = destPoint.X, Y = destPoint.Y, Status = Board.PieceColor(Board[destPoint.X, destPoint.Y]) }; if (!CheckValidPieceColor(this.Board, srcPoint.X, srcPoint.Y, player)) { needToContinueEating = false; mustCapture = false; return null; } if (!IsEmptyCoord(Board, destPoint.X, destPoint.Y) && CheckValidPieceColor(Board, destPoint.X, destPoint.Y, player)) { needToContinueEating = false; mustCapture = false; return null; } if (!IsEmptyCoord(Board, destPoint.X, destPoint.Y) && !CheckValidPieceColor(Board, destPoint.X, destPoint.Y, player)) { var captures = rule.CoordsToCaptureAndDest(Board, srcCoord, oppCoord, player); if (captures.Count > 0) { foreach (var listOfCap in captures.Keys) { if (listOfCap.Last() == oppCoord) { int length = listOfCap.Count; Coordinate newDestCoord = rule.FindDestByCap(Board, srcCoord, oppCoord); this.Board.UpdateBoard(srcCoord, newDestCoord); this.Board.UpdateCapturedSoldiers(oppCoord, Board.GetOpponent(player)); rule.IsBecameAKing(Board, newDestCoord); Board[oppCoord.X, oppCoord.Y].Status = Piece.None; this.BoardCells = ConvertBoardToBoardState(Board); lastmovewasACaptured = true; drawGame = CheckDraw(Board, Board[newDestCoord.X,newDestCoord.Y], lastmovewasACaptured); if (length > 1) needToContinueEating =true; mustCapture = false; return this; } } mustCapture = true; return null; } } //check if player doesnt have any availble captures- if he does then this move isn't valid var capturesAvaileble = rule.FindCaptures(Board, player); if (capturesAvaileble.Count == 0 ) { if(!rule.IsValidMove(Board, srcCoord, Board[destPoint.X,destPoint.Y],player)) { mustCapture = false; return null; } Board.UpdateBoard(Board[srcPoint.X, srcPoint.Y], Board[destPoint.X, destPoint.Y]); rule.IsBecameAKing(Board, Board[destPoint.X, destPoint.Y]); this.BoardCells = ConvertBoardToBoardState(Board); mustCapture = false; drawGame = CheckDraw(Board, Board[destPoint.X, destPoint.Y], lastmovewasACaptured); return this; } mustCapture = true; return null; }
/// <summary> /// Checkes whether the coordinate can capture /// </summary> /// <param name="board"></param> /// <param name="coordinate"></param> /// <param name="player"></param> /// <returns></returns> private int CanCapture(Board board, Coordinate coordinate, Player player) { Rules rule = new Rules(); IList<Coordinate> coordinatesinDirection = rule.GetMovesInDirection(board, coordinate, player); int max = 0; foreach (var cid in coordinatesinDirection) { if (board.IsOpponentPiece(player, cid)) { IDictionary<IList<Coordinate>, Coordinate> captures = rule.CoordsToCaptureAndDest(board, coordinate, cid, player); if (captures.Count > 0) { if (captures.ElementAt(0).Key.Count > max) { max = captures.ElementAt(0).Key.Count; } } } } return max; }