Exemple #1
0
 public PieceMetrics this[PieceName pieceName]
 {
     get
     {
         return(_pieceMetrics[(int)pieceName]);
     }
 }
Exemple #2
0
        protected int CountNeighbors(PieceName pieceName)
        {
            int friendlyCount;
            int enemyCount;

            return(CountNeighbors(GetPiece(pieceName), out friendlyCount, out enemyCount));
        }
Exemple #3
0
        private MoveSet GetValidMoves(PieceName pieceName)
        {
            if (null == _cachedValidMovesByPiece)
            {
                _cachedValidMovesByPiece = new MoveSet[EnumUtils.NumPieceNames];
            }

            int pieceNameIndex = (int)pieceName;

            if (null == _cachedValidMovesByPiece[pieceNameIndex])
            {
#if DEBUG
                // MoveSet is not cached in L1 cache
                ValidMoveCacheMetricsSet["ValidMoves." + EnumUtils.GetShortName(pieceName)].Miss();
#endif
                // Calculate MoveSet
                Piece   targetPiece = GetPiece(pieceName);
                MoveSet moves       = GetValidMovesInternal(targetPiece);

                // Populate cache
                _cachedValidMovesByPiece[pieceNameIndex] = moves;
            }
#if DEBUG
            else
            {
                // MoveSet is cached in L1 cache
                ValidMoveCacheMetricsSet["ValidMoves." + EnumUtils.GetShortName(pieceName)].Hit();
            }
#endif

            return(_cachedValidMovesByPiece[pieceNameIndex]);
        }
Exemple #4
0
        protected static bool TryParse(string pieceString, out PieceName pieceName, out Position position)
        {
            if (string.IsNullOrWhiteSpace(pieceString))
            {
                throw new ArgumentNullException(nameof(pieceString));
            }

            pieceString = pieceString.Trim();

            try
            {
                Match match = Regex.Match(pieceString, PieceRegex, RegexOptions.IgnoreCase);

                string nameString     = match.Groups[1].Value;
                string positionString = match.Groups[2].Value;

                pieceName = EnumUtils.ParseShortName(nameString);
                position  = Position.Parse(positionString);

                return(true);
            }
            catch (Exception) { }

            pieceName = PieceName.INVALID;
            position  = null;
            return(false);
        }
Exemple #5
0
        public static Color GetColor(PieceName value)
        {
            switch (value)
            {
            case PieceName.wQ:
            case PieceName.wS1:
            case PieceName.wS2:
            case PieceName.wB1:
            case PieceName.wB2:
            case PieceName.wG1:
            case PieceName.wG2:
            case PieceName.wG3:
            case PieceName.wA1:
            case PieceName.wA2:
            case PieceName.wA3:
                return(Color.White);

            case PieceName.bQ:
            case PieceName.bS1:
            case PieceName.bS2:
            case PieceName.bB1:
            case PieceName.bB2:
            case PieceName.bG1:
            case PieceName.bG2:
            case PieceName.bG3:
            case PieceName.bA1:
            case PieceName.bA2:
            case PieceName.bA3:
                return(Color.Black);
            }

            return(Color.NumColors);
        }
Exemple #6
0
 protected static void Parse(string pieceString, out PieceName pieceName, out Position position)
 {
     if (!TryParse(pieceString, out pieceName, out position))
     {
         throw new ArgumentException(string.Format("Unable to parse \"{0}\".", pieceString));
     }
 }
Exemple #7
0
        public static string BuildMoveString(bool isPass, PieceName startPiece, char beforeSeperator, PieceName endPiece, char afterSeperator)
        {
            if (isPass)
            {
                return(Constants.PassMoveString);
            }

            var sb = new StringBuilder();

            sb.Append(startPiece.ToString());

            if (endPiece != PieceName.INVALID)
            {
                sb.Append(' ');
                if (beforeSeperator != '\0')
                {
                    sb.Append($"{beforeSeperator}{endPiece}");
                }
                else if (afterSeperator != '\0')
                {
                    sb.Append($"{endPiece}{afterSeperator}");
                }
                else
                {
                    sb.Append(endPiece.ToString());
                }
            }

            return(sb.ToString());
        }
Exemple #8
0
        private void GetValidSlides(PieceName target, Position currentPosition, HashSet <Position> visitedPositions, int currentRange, int?maxRange, MoveSet validMoves)
        {
            if (!maxRange.HasValue || currentRange < maxRange.Value)
            {
                for (int slideDirection = 0; slideDirection < EnumUtils.NumDirections; slideDirection++)
                {
                    Position slidePosition = currentPosition.NeighborAt(slideDirection);

                    if (!visitedPositions.Contains(slidePosition) && !HasPieceAt(slidePosition))
                    {
                        // Slide position is open

                        int right = EnumUtils.RightOf(slideDirection);
                        int left  = EnumUtils.LeftOf(slideDirection);

                        if (HasPieceAt(currentPosition.NeighborAt(right)) != HasPieceAt(currentPosition.NeighborAt(left)))
                        {
                            // Can slide into slide position
                            Move move = new Move(target, slidePosition);

                            if (validMoves.Add(move))
                            {
                                // Sliding from this position has not been tested yet
                                visitedPositions.Add(move.Position);

                                GetValidSlides(target, slidePosition, visitedPositions, currentRange + 1, maxRange, validMoves);
                            }
                        }
                    }
                }
            }
        }
Exemple #9
0
        private bool IsPinned(PieceName pieceName, out int noisyCount, out int quietCount)
        {
            noisyCount = 0;
            quietCount = 0;

            bool isPinned = true;

            foreach (Move move in GetValidMoves(pieceName))
            {
                if (move.PieceName == pieceName)
                {
                    isPinned = false;
                }

                if (IsNoisyMove(move))
                {
                    noisyCount++;
                }
                else
                {
                    quietCount++;
                }
            }

            return(isPinned);
        }
Exemple #10
0
 public void ToggleLastMovedPiece(PieceName pieceName)
 {
     if (pieceName != PieceName.INVALID)
     {
         Value ^= _hashPartByLastMovedPiece[(int)pieceName];
     }
 }
Exemple #11
0
 public static Piece Get(PieceName pieceName)
 {
     if (pieceName == PieceName.Amazon || pieceName == PieceName.Arrow)
     {
         throw new ArgumentException("Don't use this for amazons or arrows. Must specify an owner");
     }
     return(_pieces[(int)pieceName, 0]);
 }
Exemple #12
0
        public Position GetPiecePosition(PieceName pieceName)
        {
            if (pieceName == PieceName.INVALID)
            {
                throw new ArgumentOutOfRangeException("pieceName");
            }

            return(GetPiece(pieceName).Position);
        }
Exemple #13
0
    private void Start()
    {
        InitializeEvents();

        // Inicializacion de varibales
        added = init = stopLeft = stopRight = stopRotate = false;
        type  = Type();
        table = GameObject.FindGameObjectWithTag("Table");
    }
Exemple #14
0
        public static string GetShortName(PieceName pieceName)
        {
            if (pieceName == PieceName.INVALID)
            {
                return("");
            }

            return(PieceShortName[(int)pieceName]);
        }
Exemple #15
0
        private void PieceCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Canvas pieceCanvas = sender as Canvas;

            if (null != pieceCanvas)
            {
                PieceName clickedPiece = EnumUtils.ParseShortName(pieceCanvas.Name);
                VM.PieceClick(clickedPiece);
            }
        }
Exemple #16
0
        private void Init(PieceName pieceName, Position position)
        {
            if (pieceName == PieceName.INVALID)
            {
                throw new ArgumentOutOfRangeException("pieceName");
            }

            PieceName = pieceName;
            Position  = position;
        }
Exemple #17
0
        public static Piece Get(PieceName pieceName, Owner owner)
        {
            int ownerNum = (int)owner;

            if (ownerNum > 0)
            {
                ownerNum--;
            }
            return(_pieces[(int)pieceName, ownerNum]);
        }
Exemple #18
0
 public AmazonPieceControl(PieceName pieceName, Owner owner)
 {
     InitializeComponent();
     Piece      = Piece.Get(pieceName, owner);
     PieceImage = PieceToImage(Piece);
     if (PieceImage != null)
     {
         Cell.Children.Add(PieceImage);
     }
 }
 public static IPiece CreatePiece(PieceName Piece, PieceColor Color)
 {
     return(Piece switch
     {
         PieceName.Bishop => new Bishop(Color),
         PieceName.Queen => new Queen(Color),
         PieceName.Rook => new Rook(Color),
         PieceName.King => new King(Color),
         PieceName.Knight => new Knight(Color),
         _ => new Pawn(Color),
     });
        public MoveCommand(IntegerVector2 startPosition, PieceName pieceName, IntegerVector2 endPosition)
        {
            if (pieceName == PieceName.None)
            {
                throw new InvalidOperationException();
            }

            this.startPosition = startPosition;
            this.pieceName     = pieceName;
            this.endPosition   = endPosition;
        }
Exemple #21
0
        public static string ToBoardSpacePieceName(PieceName pieceName)
        {
            string name = EnumUtils.GetShortName(pieceName);

            if (null != name && name.Length > 0)
            {
                name = name[0].ToString().ToLower() + name.Substring(1);
            }

            return(name);
        }
Exemple #22
0
        internal void CanvasClick(double cursorX, double cursorY)
        {
            if (AppVM.EngineWrapper.CurrentTurnIsHuman)
            {
                CanvasCursorX = cursorX;
                CanvasCursorY = cursorY;

                PieceName clickedPiece    = AppVM.EngineWrapper.GetPieceAt(CanvasCursorX, CanvasCursorY, CanvasHexRadius, AppVM.ViewerConfig.HexOrientation);
                Position  clickedPosition = AppVM.EngineWrapper.GetTargetPositionAt(CanvasCursorX, CanvasCursorY, CanvasHexRadius, AppVM.ViewerConfig.HexOrientation);

                // Make sure the first move is on the origin, no matter what
                if (Board.BoardState == BoardState.NotStarted && AppVM.EngineWrapper.TargetPiece != PieceName.INVALID)
                {
                    if (AppVM.EngineWrapper.TargetPosition == Position.Origin)
                    {
                        AppVM.EngineWrapper.TargetPiece = PieceName.INVALID;
                    }
                    else
                    {
                        clickedPosition = Position.Origin;
                    }
                }

                if (AppVM.EngineWrapper.TargetPiece == PieceName.INVALID && clickedPiece != PieceName.INVALID)
                {
                    // No piece selected, select it
                    AppVM.EngineWrapper.TargetPiece = clickedPiece;
                }
                else if (AppVM.EngineWrapper.TargetPiece != PieceName.INVALID)
                {
                    // Piece is selected
                    if (clickedPiece == AppVM.EngineWrapper.TargetPiece || clickedPosition == AppVM.EngineWrapper.TargetPosition)
                    {
                        // Unselect piece
                        AppVM.EngineWrapper.TargetPiece = PieceName.INVALID;
                    }
                    else
                    {
                        // Get the move with the clicked position
                        Move targetMove = new Move(AppVM.EngineWrapper.TargetPiece, clickedPosition);
                        if (!AppVM.ViewerConfig.BlockInvalidMoves || AppVM.EngineWrapper.CanPlayMove(targetMove))
                        {
                            // Move is selectable, select position
                            AppVM.EngineWrapper.TargetPosition = clickedPosition;
                        }
                        else
                        {
                            // Move is not selectable, (un)select clicked piece
                            AppVM.EngineWrapper.TargetPiece = clickedPiece;
                        }
                    }
                }
            }
        }
Exemple #23
0
        internal void PieceClick(PieceName clickedPiece)
        {
            if (AppVM.EngineWrapper.CurrentTurnIsHuman)
            {
                if (AppVM.EngineWrapper.TargetPiece == clickedPiece)
                {
                    clickedPiece = PieceName.INVALID;
                }

                AppVM.EngineWrapper.TargetPiece = clickedPiece;
            }
        }
Exemple #24
0
        private void VerifyCanMoveWithoutBreakingHive(MockBoard board, PieceName pieceName, bool canMoveExpected)
        {
            Assert.IsNotNull(board);

            Piece piece = board.GetPiece(pieceName);

            Assert.IsNotNull(piece);

            bool canMoveActual = board.CanMoveWithoutBreakingHive(piece);

            Assert.AreEqual(canMoveExpected, canMoveActual);
        }
Exemple #25
0
        internal Piece(Color color, PieceName name, bool hidden = false)
        {
#if DEBUG
            if (name == PieceName.General && hidden)
            {
                throw new InvalidOperationException("Tướng không thể úp !");
            }
#endif
            this.color  = color;
            this.name   = name;
            this.hidden = hidden;
        }
Exemple #26
0
        public static BugType GetBugType(PieceName pieceName)
        {
            switch (pieceName)
            {
            case PieceName.WhiteQueenBee:
            case PieceName.BlackQueenBee:
                return(BugType.QueenBee);

            case PieceName.WhiteSpider1:
            case PieceName.WhiteSpider2:
            case PieceName.BlackSpider1:
            case PieceName.BlackSpider2:
                return(BugType.Spider);

            case PieceName.WhiteBeetle1:
            case PieceName.WhiteBeetle2:
            case PieceName.BlackBeetle1:
            case PieceName.BlackBeetle2:
                return(BugType.Beetle);

            case PieceName.WhiteGrasshopper1:
            case PieceName.WhiteGrasshopper2:
            case PieceName.WhiteGrassHopper3:
            case PieceName.BlackGrasshopper1:
            case PieceName.BlackGrasshopper2:
            case PieceName.BlackGrassHopper3:
                return(BugType.Grasshopper);

            case PieceName.WhiteSoldierAnt1:
            case PieceName.WhiteSoldierAnt2:
            case PieceName.WhiteSoldierAnt3:
            case PieceName.BlackSoldierAnt1:
            case PieceName.BlackSoldierAnt2:
            case PieceName.BlackSoldierAnt3:
                return(BugType.SoldierAnt);

            case PieceName.WhiteMosquito:
            case PieceName.BlackMosquito:
                return(BugType.Mosquito);

            case PieceName.WhiteLadybug:
            case PieceName.BlackLadybug:
                return(BugType.Ladybug);

            case PieceName.WhitePillbug:
            case PieceName.BlackPillbug:
                return(BugType.Pillbug);
            }

            throw new ArgumentOutOfRangeException("pieceName");
        }
        // 現在はColumnSelectorを継承しているが, ColumnSelectorの内部処理は分離してPureC#にした方がいいかも

        protected override void OnColumnSelected(IntegerVector2 start, IntegerVector2 via, IntegerVector2 last)
        {
            IGame game = GameController.Instance.Game;

            if (game == null)
            {
                return;
            }

            PieceName     pieceName     = game.Board.GetPiece(start).Name;
            PieceMoveData pieceMoveData = new PieceMoveData(string.Empty, start, pieceName, last);

            GameController.Instance.ServerDelegate.PostMoveData(pieceMoveData);
        }
Exemple #28
0
        private void VerifyMoveProperties(Move actualMove, PieceName expectedPieceName, Position expectedPosition)
        {
            Assert.IsNotNull(actualMove);

            Assert.AreEqual(expectedPieceName, actualMove.PieceName);

            if (expectedPieceName != PieceName.INVALID)
            {
                Assert.AreEqual(EnumUtils.GetColor(expectedPieceName), actualMove.Color);
                Assert.AreEqual(EnumUtils.GetBugType(expectedPieceName), actualMove.BugType);
            }

            Assert.AreEqual(expectedPosition, actualMove.Position);
        }
        public ICommand CreateMoveCommandInstance(string[] words)
        {
            if (!analyzer.CheckFormat(words, 4) /* || words[0] != CommandEnum.move.ToString()*/)
            {
                return(null);
            }

            IntegerVector2 startPosition = analyzer.GetPosition(words[1]);
            PieceName      pieceName     = PieceName.None;

            Enum.TryParse(words[2], true, out pieceName);
            IntegerVector2 endPosition = analyzer.GetPosition(words[3]);

            return(new MoveCommand(startPosition, pieceName, endPosition));
        }
Exemple #30
0
        public static string NormalizeBoardSpaceMoveString(string moveString)
        {
            if (string.IsNullOrWhiteSpace(moveString))
            {
                throw new ArgumentNullException(nameof(moveString));
            }

            moveString = moveString.Trim();

            if (moveString.Equals(Move.PassString, StringComparison.InvariantCultureIgnoreCase))
            {
                return(Move.PassString.ToLower());
            }

            string[] moveStringParts = moveString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            PieceName movingPiece = EnumUtils.ParseShortName(moveStringParts[0]);

            if (moveStringParts.Length == 1)
            {
                return(ToBoardSpacePieceName(movingPiece));
            }

            string targetString   = moveStringParts[1].Trim('-', '/', '\\');
            int    seperatorIndex = moveStringParts[1].IndexOfAny(new char[] { '-', '/', '\\' });

            PieceName targetPiece = EnumUtils.ParseShortName(targetString);

            if (seperatorIndex < 0)
            {
                return(string.Format("{0} {1}", ToBoardSpacePieceName(movingPiece), ToBoardSpacePieceName(targetPiece)));
            }

            char seperator = moveStringParts[1][seperatorIndex];

            if (seperatorIndex == 0)
            {
                // Moving piece on the left-hand side of the target piece
                return(string.Format("{0} {1}{2}", ToBoardSpacePieceName(movingPiece), seperator, ToBoardSpacePieceName(targetPiece)));
            }
            else if (seperatorIndex == targetString.Length)
            {
                // Moving piece on the right-hand side of the target piece
                return(string.Format("{0} {1}{2}", ToBoardSpacePieceName(movingPiece), ToBoardSpacePieceName(targetPiece), seperator));
            }

            return(null);
        }