void CalculateAndDisplay(IntegerVector2 position, IReadOnlyPiece movingPiece)
        {
            var logicPositions = movePredictor.PredictMoveableColumns(position, movingPiece);
            var worldPositions = ConvertLogicPositionToWorldPosition(logicPositions);

            UpdateMarker(worldPositions);
        }
 public PieceMovementMessage(IReadOnlyPlayer player, IReadOnlyPiece piece, IntegerVector2 startPosition, IntegerVector2 endPosition)
 {
     Player        = player;
     Piece         = piece;
     StartPosition = startPosition;
     EndPosition   = endPosition;
 }
Example #3
0
    public void swapXandY(IntegerVector2 point)
    {
        int holder = point.getX();

        point.setX(point.getY());
        point.setY(holder);
    }
Example #4
0
        public void RequestToSetPiece(IntegerVector2 position, IPiece piece)
        {
            if (game.CurrentPlayer != player)
            {
                logger.Log(new NotYourTurnMessage());
                return;
            }

            if (piece == null)
            {
                logger.Log(new NoPieceSelectedMessage());
                return;
            }

            IBoard board = game.Board;

            logger.Log(new SetPieceMessage(player, piece, position));
            bool result = board.SetPiece(position, piece);

            if (!result)
            {
                logger.Log(new SetPieceFailureMessage());
            }
            else
            {
                player.UseCapturedPiece(piece);
                game.TurnEnd();
            }
        }
Example #5
0
        IEnumerable <IntegerVector2> PredictMoveableColumns(IntegerVector2 hypotheticalPosition, IReadOnlyPiece movingPiece)
        {
            List <IntegerVector2> result = new List <IntegerVector2>();

            if (movingPiece == null || !board.IsOnBoard(hypotheticalPosition))
            {
                return(result);
            }

            for (int i = 0; i < board.Width; i++)
            {
                for (int j = 0; j < board.Height; j++)
                {
                    IntegerVector2 currentPosition  = new IntegerVector2(i, j);
                    bool           isFrontPlayer    = movingPiece.Owner != null && movingPiece.Owner.Encampment == Encampment.Front;
                    IntegerVector2 relativePosition = (currentPosition - hypotheticalPosition) * (isFrontPlayer ? -1 : 1);
                    PieceMovement  unused;
                    if (board.IsOnBoard(currentPosition) &&
                        movingPiece.TryToGetPieceMovementByRelativePosition(relativePosition, out unused))
                    {
                        result.Add(currentPosition);
                    }
                }
            }

            return(result);
        }
Example #6
0
        bool CommonCheck(IntegerVector2 startPosition)
        {
            if (game.CurrentPlayer != player)
            {
                logger.Log(new NotYourTurnMessage());
                return(false);
            }

            var board = game.Board;
            var piece = board.GetPiece(startPosition);

            if (piece == null)
            {
                logger.Log(new NoPieceSelectedMessage());
                return(false);
            }

            if (piece.Owner != null && piece.Owner != player)
            {
                logger.Log(new NotYourPieceMessage());
                return(false);
            }

            return(true);
        }
Example #7
0
        public PieceMoveTransaction(IPlayer player, VerifiedMove verifiedMove, PositionArrayAccessor <IPiece> pieces, IFieldEffectChecker fieldEffectChecker,
                                    IValueInputProvider <int> valueProvider, bool isTurnEnd)
        {
            Assert.IsNotNull(verifiedMove);
            Assert.IsNotNull(verifiedMove.Player);
            Assert.IsNotNull(player);
            Assert.AreEqual(verifiedMove.Player, player);
            Assert.IsNotNull(pieces);
            Assert.IsNotNull(fieldEffectChecker);
            Assert.IsNotNull(valueProvider);

            this.player        = player;
            this.pieces        = pieces;
            this.valueProvider = valueProvider;

            startPosition = verifiedMove.MovingPiece.Position;    //worldPathに開始地点は含まれないのでこの方法で開始地点を取得
            viaPosition   = verifiedMove.ViaPositionNode;
            endPosition   = verifiedMove.WorldPath.Last();

            this.worldPath = verifiedMove.WorldPath;
            this.isTurnEnd = isTurnEnd;

            pieceMover        = new Mover(pieces);
            waterEntryChecker = new WaterEntryChecker(3, fieldEffectChecker, valueProvider);
            moveFinisher      = new MoveFinisher(pieceMover, new Capturer(pieces));
        }
Example #8
0
 void UpdateOutOfBoard(IntegerVector2 position)
 {
     transform.localScale = PieceStrageScale;
     if (pieceCollider != null)
     {
         pieceCollider.enabled = true;
     }
 }
Example #9
0
 public CaptureResult(bool isSuccess, IPiece capturer, IPiece captured, IPlayer formerOwner, IntegerVector2 from)
 {
     IsSuccess   = isSuccess;
     Capturer    = capturer;
     Captured    = captured;
     FormerOwner = formerOwner;
     From        = from;
 }
        static IEnumerable <IntegerVector2> CalculateStraightPath(IntegerVector2 startPosition, IntegerVector2 endPosition, PieceMovement pieceMovement, bool isFrontPlayersPiece)
        {
            var relativeStart2EndPath = GetPath(startPosition, endPosition, isFrontPlayersPiece, pieceMovement);
            //順序は保証されていないにも関わらず保証されたものとして利用
            var worldPath = relativeStart2EndPath.Select(value => startPosition + value * (isFrontPlayersPiece ? -1 : 1));

            return(worldPath);
        }
        void CallPieceSelectedEvent(IntegerVector2 position)
        {
            var game  = Application.GameController.Instance.Game;
            var board = game.Board;
            var piece = board.GetPiece(position);

            onPieceSelected.Invoke(piece);
        }
Example #12
0
    public List <IntegerVector2> rasterize(IntegerVector2 start, IntegerVector2 end)
    {
        List <IntegerVector2> toBeDrawn = new List <IntegerVector2>();

        int dx           = end.getX() - start.getX();
        int dy           = end.getY() - end.getY();
        int TwoDy        = 2 * dy;
        int TwoDYminusDX = TwoDy - dx;
        int p            = TwoDYminusDX;
        int y            = 0;

        bool XandYSwapped = false;

        if (dx < 0)
        {
            toBeDrawn = rasterize(end, start);
        }

        if (dy < 0)
        {
            toBeDrawn = rasterize(negateY(start), negateY(end));

            toBeDrawn = NegateYList(toBeDrawn);
        }

        if (dy > dx)
        {
            swapXandY(start);
            swapXandY(end);
            XandYSwapped = true;
        }

        y = start.getY();
        for (int i = start.getX(); i < end.getX(); i++)
        {
            if (p > 0)
            {
                y++;
                p = p - TwoDYminusDX;
            }

            else
            {
                p = p + TwoDy;
            }

            toBeDrawn.Add(new IntegerVector2(i, y));
        }



        if (XandYSwapped)
        {
            toBeDrawn = swapXandY(toBeDrawn);
        }

        return(toBeDrawn);
    }
        static IEnumerable <IntegerVector2> GetPath(IntegerVector2 from, IntegerVector2 to, bool isFrontPlayersPiece, PieceMovement pieceMovement)
        {
            IntegerVector2 relativePosition = (to - from) * (isFrontPlayersPiece ? -1 : 1);
            var            result           = pieceMovement.GetPath(relativePosition);

            Assert.IsNotNull(result);

            return(result);
        }
Example #14
0
 public PieceMoveData(string senderId, IntegerVector2 start, Terminologies.PieceName piece, IntegerVector2 end, byte numberOfstick = byte.MaxValue)
 {
     this.senderId      = senderId;
     this.startX        = start.x;
     this.startY        = start.y;
     this.piece         = piece;
     this.endX          = end.x;
     this.endY          = end.y;
     this.numberOfstick = numberOfstick;
 }
        static IEnumerable <IntegerVector2> CalculateBrockenPath(IntegerVector2 startPosition, IntegerVector2 viaPosition, IntegerVector2 endPosition,
                                                                 PieceMovement start2ViaPieceMovement, PieceMovement via2EndPieceMovement, bool isFrontPlayersPiece)
        {
            var start2ViaPath = CalculateStraightPath(startPosition, viaPosition, start2ViaPieceMovement, isFrontPlayersPiece);
            var via2EndPath   = CalculateStraightPath(viaPosition, endPosition, via2EndPieceMovement, isFrontPlayersPiece);

            var tempPath = start2ViaPath.Concat(via2EndPath);   //順序は保証されていないにも関わらず保証されたものとして利用

            return(tempPath);
        }
Example #16
0
        public MoveCommand(IntegerVector2 startPosition, PieceName pieceName, IntegerVector2 endPosition)
        {
            if (pieceName == PieceName.None)
            {
                throw new InvalidOperationException();
            }

            this.startPosition = startPosition;
            this.pieceName     = pieceName;
            this.endPosition   = endPosition;
        }
Example #17
0
        void UpdateView(Unit unit)
        {
            IntegerVector2 position = piece.Position;

            if (position == new IntegerVector2(-1, -1))
            {
                UpdateOutOfBoard(position);
                return;
            }

            UpdateOnBoard(position);
        }
    // internal function to find path dont use this one from outside
    private static List <CellInfo> _ImpFindPath(Map map, IntegerVector2 startPos, IntegerVector2 targetPos)
    {
        CellInfo startNode  = map.GetCell(startPos.x, startPos.y).info;
        CellInfo targetNode = map.GetCell(targetPos.x, targetPos.y).info;

        List <CellInfo>    openSet   = new List <CellInfo>();
        HashSet <CellInfo> closedSet = new HashSet <CellInfo>();

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            CellInfo currentNode = openSet[0];
            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)
                {
                    currentNode = openSet[i];
                }
            }

            openSet.Remove(currentNode);
            closedSet.Add(currentNode);

            if (currentNode == targetNode)
            {
                return(RetracePath(map, startNode, targetNode));
            }

            foreach (CellInfo neighbour in map.GetNeighbours(currentNode))
            {
                if (neighbour.cell.type.GetType() != typeof(Empty) || closedSet.Contains(neighbour))
                {
                    continue;
                }

                int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) * (int)(10.0f * neighbour.penalty);
                if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                {
                    neighbour.gCost  = newMovementCostToNeighbour;
                    neighbour.hCost  = GetDistance(neighbour, targetNode);
                    neighbour.parent = currentNode;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }

        return(null);
    }
Example #19
0
    // Use this for initialization
    void Start()
    {
        //For testing purposes
        Vector2 start = new Vector2(234, 502);
        Vector2 end   = new Vector2(241, 506);

        IntegerVector2 startPoint = new IntegerVector2(start, height, width);
        IntegerVector2 endPoint   = new IntegerVector2(end, height, width);

        List <IntegerVector2> toBeDrawn = rasterize(startPoint, endPoint);

        print(toBeDrawn);
    }
        /// <summary>
        /// 返却されるPathには開始地点は含まれない。
        /// </summary>
        /// <param name="startPosition"></param>
        /// <param name="viaPosition"></param>
        /// <param name="endPosition"></param>
        /// <param name="pieces"></param>
        /// <param name="start2ViaPieceMovement"></param>
        /// <param name="via2EndPieceMovement"></param>
        /// <returns></returns>
        public static IEnumerable <IntegerVector2> CalculatePath(IntegerVector2 startPosition, IntegerVector2 viaPosition, IntegerVector2 endPosition,
                                                                 IReadOnlyPositionArrayAccessor <IReadOnlyPiece> pieces, PieceMovement start2ViaPieceMovement, PieceMovement via2EndPieceMovement)
        {
            //startPositionは各直線ではなく全体の開始地点でなければならない
            //インスタンス変数化?
            bool isFrontPlayersPiece = IsFrontPlayersPiece(pieces, startPosition);

            if (viaPosition == endPosition)
            {
                return(CalculateStraightPath(startPosition, endPosition, start2ViaPieceMovement, isFrontPlayersPiece));
            }
            return(CalculateBrockenPath(startPosition, viaPosition, endPosition, start2ViaPieceMovement, via2EndPieceMovement, isFrontPlayersPiece));
        }
Example #21
0
    public static void DrawCircle(Texture2D whitePixel, SpriteBatch spritbatch, IntegerVector2 center, float radius, Color color, int lineWidth = 2, int segments = 16)
    {
        Vector2[] vertex    = new Vector2[segments];
        double    increment = Math.PI * 2.0 / segments;
        double    theta     = 0.0;

        for (int i = 0; i < segments; i++)
        {
            vertex[i] = center.ToVector2() + radius * new Vector2((float)Math.Cos(theta), (float)Math.Sin(theta));
            theta    += increment;
        }
        DrawPolygon(whitePixel, spritbatch, vertex, segments, color, lineWidth);
    }
Example #22
0
    //produce desired type of object with position
    public static IObject GetObject(ObjectTypes objType, IntegerVector2 pos)
    {
        switch (objType)
        {
        case ObjectTypes.Barrack:
            return(new Barrack(pos));

        case ObjectTypes.PowerPlant:
            return(new PowerPlant(pos));

        default:
            return(new Empty());
        }
    }
        async UniTaskVoid RequestToMove(IntegerVector2 start, IntegerVector2 via, IntegerVector2 last)
        {
            isLockSelecting = true;
            if (via == last)
            {
                await MovePieceUseCaseFactory.Create(firstOrSecond, valueProvider).RequestToMovePiece(start, last);
            }
            else
            {
                await MovePieceUseCaseFactory.Create(firstOrSecond, valueProvider).RequestToMovePiece(start, via, last);
            }

            isLockSelecting = false;
        }
        private void Start()
        {
            Transform[,] columns = GetComponent <IMapProvider <Transform> >().GetMap();

            for (int x = 0; x < columns.GetLength(0); x++)
            {
                for (int y = 0; y < columns.GetLength(1); y++)
                {
                    ColumnView     columnView     = columns[x, y].gameObject.AddComponent <ColumnView>();
                    IntegerVector2 columnPosition = new IntegerVector2(x, y);
                    columnView.OnClick.TakeUntilDestroy(this).Select(_ => columnPosition).Subscribe(onColumnClicked.Invoke);
                }
            }
        }
        // 現在は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);
        }
        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));
        }
Example #27
0
        public async UniTask RequestToMovePiece(IntegerVector2 startPosition, IntegerVector2 endPosition)
        {
            if (!CommonCheck(startPosition))
            {
                return;
            }

            var board = game.Board;

            logger.Log(new PieceMovementMessage(player, board.GetPiece(startPosition), startPosition, endPosition));

            var result = await board.MovePiece(startPosition, endPosition, player, inputProvider);

            OnPieceMoved(result);
        }
Example #28
0
        public void OnClickColumn(IntegerVector2 position)
        {
            IGame game = GameController.Instance.Game;

            if (game == null || selectedPiece == null)
            {
                return;
            }

            if (position != NonePosition && game.CurrentPlayer == selectedPiece.Owner)
            {
                SetPieceUseCaseFactory.Create(game.CurrentTurn).RequestToSetPiece(position, selectedPiece);
            }

            selectedPiece = null;
        }
    public static List <IntegerVector2> FindPath(Map map, IntegerVector2 startPos, IntegerVector2 targetPos)
    {
        // find path
        List <CellInfo> nodes_path = _ImpFindPath(map, startPos, targetPos);

        // convert to a list of points and return
        List <IntegerVector2> ret = new List <IntegerVector2>();

        if (nodes_path != null)
        {
            foreach (CellInfo node in nodes_path)
            {
                ret.Add(new IntegerVector2(node.position.x, node.position.y));
            }
        }
        return(ret);
    }
Example #30
0
        public VerifiedMove VerifyMove(IReadOnlyPlayer player, IntegerVector2 startPosition, IntegerVector2 viaPosition, IntegerVector2 endPosition)
        {
            Assert.IsTrue(IsOnBoard(startPosition));
            Assert.IsTrue(IsOnBoard(viaPosition) || viaPosition == new IntegerVector2(-1, -1));
            Assert.IsTrue(IsOnBoard(endPosition));

            //経路計算前の合法性チェック
            bool           areViaAndLastSame      = viaPosition == endPosition;
            IReadOnlyPiece movingPiece            = pieces.Read(startPosition);
            IReadOnlyPiece viaPiece               = pieces.Read(viaPosition);
            IReadOnlyPiece originalPiece          = pieces.Read(endPosition); //元からある駒の意味で使っているが, 英語があってるか不明.
            bool           isTargetNull           = movingPiece == null;
            bool           isViaPieceNull         = viaPiece == null;         //
            bool           isOwner                = !isTargetNull && movingPiece.IsOwner(player);
            bool           isSameOwner            = !isTargetNull && originalPiece != null && originalPiece.Owner == movingPiece.Owner;
            PieceMovement  start2ViaPieceMovement = PieceMovement.Default;
            PieceMovement  via2EndPieceMovement   = PieceMovement.Default;
            bool           isMoveableToVia        = !isTargetNull && movingPiece.TryToGetPieceMovement(viaPosition, out start2ViaPieceMovement);                                                    //
            bool           isMoveableToLast       = !isTargetNull && (areViaAndLastSame || movingPiece.TryToGetPieceMovement(startPosition + endPosition - viaPosition, out via2EndPieceMovement)); //

            if (isTargetNull || (!areViaAndLastSame && isViaPieceNull) || !isOwner || isSameOwner || !isMoveableToVia || !isMoveableToLast)                                                         //
            {
                return(VerifiedMove.InvalidMove);
            }

            //経路計算
            //worldPathに開始地点は含まれない
            var worldPath       = WorldPathCalculator.CalculatePath(startPosition, viaPosition, endPosition, pieces, start2ViaPieceMovement, via2EndPieceMovement);
            var viaPositionNode = worldPath.First(value => value == viaPosition);  //経由点を通過するのが1回だけだということは保証されている.

            //経路の合法性チェック
            var surmountLimit = via2EndPieceMovement.Surmountable ? 1 : 0;
            var noPieceOnPath = worldPath.Where(node => node != viaPosition).Where(node => node != endPosition)
                                .Select(node => pieces.Read(node)).Where(piece => piece != null)
                                .Count() <= surmountLimit;

            if (!noPieceOnPath)
            {
                return(VerifiedMove.InvalidMove);
            }

            return(new VerifiedMove(movingPiece, player, worldPath, viaPositionNode));
        }
Example #31
0
 /// <summary>
 /// Returns the labyrinth cell at the given position.
 /// </summary>
 /// <param name="coord">Labyrinth coordinate</param>
 /// <returns>Labyrinth cell</returns>
 public LabyrinthCell GetLabyrinthCell(IntegerVector2 coord)
 {
     return cells[coord.X, coord.Z];
 }
Example #32
0
    /// <summary>
    /// Instantiates a labyrinth cell at a specific position.
    /// </summary>
    /// <param name="x">X-Position</param>
    /// <param name="z">Z-Position</param>
    protected LabyrinthCell CreateLabyrinthCell(IntegerVector2 position)
    {
        // Instantiate cell
        LabyrinthCell c = Instantiate(labyrinthCellPrefab) as LabyrinthCell;
        c.LabyrinthPosition = position;

        // Save the cell into the array
        cells[position.X, position.Z] = c;

        // Set cell parameters
        c.name = "Cell_" + position.X + "_" + position.Z;
        c.transform.parent = this.transform;
        c.transform.position = new Vector3(position.X - (labyrinthSize.X / 2f) + 0.5f, 0f, position.Z - (labyrinthSize.Z / 2f) + 0.5f);   // Cell size --> 1x1
        //c.transform.position = new Vector3(position.X + 0.5f, 0f, position.Z + 0.5f);   // Cell size --> 1x1

        return c;
    }
Example #33
0
 /// <summary>
 /// Checks if the given coordinate is inside the labyrinth grid.
 /// </summary>
 /// <param name="coord">Labyrinth coordinate</param>
 /// <returns>True if it is inside the labyrinth grid.</returns>
 public bool ContainsLabyrinthCoordinate(IntegerVector2 coord)
 {
     return coord.X >= 0 && coord.X < labyrinthSize.X &&
         coord.Z >= 0 && coord.Z < labyrinthSize.Z;
 }