Exemplo n.º 1
0
        /// <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;
        }