/// <summary> /// Calucalte new game boards which are optional moves /// </summary> /// <param name="board"></param> /// <param name="player"></param> /// <returns></returns> public IDictionary<Board, IList<Coordinate>> CalculateNewBoardsFromCoordinates(Board board, Player player) { IDictionary<Board, IList<Coordinate>> newBoardsPositions = new Dictionary<Board, IList<Coordinate>>(); for (int i = 1; i <= board.Size; i++) { if (board.IsOwner(player, board[i])) { IList<Coordinate> coordinateList = GetMovesInDirection(board, board[i], player); foreach (Coordinate coordinate in coordinateList.Reverse()) { //if a soldier of mine exists in this coord then this coord is not optional; if (board.IsOwner(player, coordinate)) { coordinateList.Remove(coordinate); } //if an oppenent soldier exsist in this coord try capturing him! else if (board.IsOpponentPiece(player, coordinate)) { IList<Coordinate> destList = CoordsToCaptureAndDest(board, board[i], coordinate, player).Values.ToList(); if (destList.Count != 0) { // destList is all the coordinates presenting the board after the capture. foreach (Coordinate coord in destList.Reverse()) { Board nBoard = board.Copy(); nBoard.UpdateBoard(nBoard[nBoard.Search(board[i])], coord); IsBecameAKing(nBoard, coord); IList<Coordinate> temp = new List<Coordinate>(); temp.Add(nBoard[nBoard.Search(board[i])]); temp.Add(nBoard[nBoard.Search(coord)]); newBoardsPositions.Add(nBoard, temp); } } } else if (!board.IsAloacted(coordinate)) { //create new board that represnt board after the move Board nBoard = board.Copy(); nBoard.UpdateBoard(nBoard[nBoard.Search(board[i])], coordinate); IsBecameAKing(nBoard, coordinate); IList<Coordinate> temp = new List<Coordinate>(); temp.Add(nBoard[nBoard.Search(board[i])]); temp.Add(nBoard[nBoard.Search(coordinate)]); newBoardsPositions.Add(nBoard, temp); } } } } return newBoardsPositions; }
/// <summary> /// Get all valid moves for a coordinate /// </summary> /// <param name="board"></param> /// <param name="coordinate"></param> /// <param name="player"></param> /// <returns></returns> public IList<Coordinate> OptionalMoves(Board board, Coordinate coordinate, Player player) { IList<Coordinate> coordinateList = new List<Coordinate>(); //Check that player own the piece on that coordinate if (board.IsOwner(player, coordinate)) { //Check in bounds if (InBounds(board, coordinate.X + 1, coordinate.Y + 1)) { var temp1 = new Coordinate(board[coordinate.X + 1, coordinate.Y + 1]); coordinateList.Add(temp1); } //Check in bounds if (InBounds(board, coordinate.X + 1, coordinate.Y - 1)) { var temp2 = new Coordinate(board[coordinate.X + 1, coordinate.Y - 1]); coordinateList.Add(temp2); } //Check in bounds if (InBounds(board, coordinate.X - 1, coordinate.Y + 1)) { var temp3 = new Coordinate(board[coordinate.X - 1, coordinate.Y + 1]); coordinateList.Add(temp3); } //Check in bounds if (InBounds(board, coordinate.X - 1, coordinate.Y - 1)) { var temp4 = new Coordinate(board[coordinate.X - 1, coordinate.Y - 1]); coordinateList.Add(temp4); } } return coordinateList; }
/// <summary> /// Get Moves In Direction /// </summary> /// <param name="board"></param> /// <param name="coordinate"></param> /// <param name="player"></param> /// <returns></returns> public IList<Coordinate> GetMovesInDirection(Board board, Coordinate coordinate, Player player) { //Finds all optional move for player IList<Coordinate> coordinateList = OptionalMoves(board, coordinate, player); if (coordinateList.Count != 0) { //checks if player is the owner of the piece if (board.IsOwner(player, coordinate)) { foreach (Coordinate item in coordinateList.Reverse()) { //Removing coordinates which are not in the piece direction white - forward and black backword (according to board orientation) if (board.IsBlack(coordinate) && (board.IsSoldier(coordinate))) { if (item.X > coordinate.X) { coordinateList.Remove(item); } } if (board.IsWhite(coordinate) && (board.IsSoldier(coordinate))) { if (item.X < coordinate.X) { coordinateList.Remove(item); } } } } } return coordinateList; }
/// <summary> /// Find dictionary of all captures on board per player /// </summary> /// <param name="board"></param> /// <param name="player"></param> /// <returns>dictionary of 2 lists : first list of coordinates (captured coordinates) , second coordinates (src-destination cords)-</returns> public IDictionary<IList<Coordinate>, IList<Coordinate>> FindCaptures(Board board, Player player) { IDictionary<IList<Coordinate>, IList<Coordinate>> res = new Dictionary<IList<Coordinate>, IList<Coordinate>>(); for (int i = 1; i <= 32; i++) { if (board.IsOwner(player, board[i])) { var coordsInDir = GetMovesInDirection(board, board[i], player); if (coordsInDir.Count == 0) break; foreach (Coordinate coordindir in coordsInDir) { if (board.IsOpponentPiece(player, coordindir)) { var coordsToJumpTo = CoordsToCaptureAndDest(board, board[i], coordindir, player); if (coordsToJumpTo.Count != 0) { foreach (var item in coordsToJumpTo) { res.Add(item.Key, new List<Coordinate> {board[i], item.Value}); } } } } } } return res; }