예제 #1
0
        /// <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;
        }
예제 #2
0
 /// <summary>
 /// in case this move isn't a capture- check if valid
 /// </summary>
 /// <param name="board"></param>
 /// <param name="srcCoord"></param>
 /// <param name="destCoord"></param>
 /// <param name="player"></param>
 /// <returns></returns>
 public bool IsValidMove(Board board, Coordinate srcCoord, Coordinate destCoord, Player player)
 {
     //Check if the move is in bounds
     if (!(InBounds(board, srcCoord.X, srcCoord.Y)) || !(InBounds(board, destCoord.X, destCoord.Y)))
         return false;
     //Check if is in direction and not more than 1 step
     if ((Math.Abs(srcCoord.X - destCoord.X) > 1) || (Math.Abs(srcCoord.Y - destCoord.Y) > 1))
         return false;
     //Find all availables move for the piece
     IList<Coordinate> coordsInDir = GetMovesInDirection(board, srcCoord, player);
     //crossing data with available moves and free location
     return coordsInDir.Contains(destCoord) && !board.IsAloacted(destCoord);
 }