예제 #1
0
        public CigarDirection(string directionString)
        {
            // Similar functionality to cigar string parsing
            int head = 0;

            for (int i = 0; i < directionString.Length; ++i)
            {
                if (Char.IsDigit(directionString, i))
                {
                    continue;
                }
                // TODO check for unexpected chars

                var length        = uint.Parse(directionString.Substring(head, i - head));
                var directionChar = directionString[i];
                var direction     = DirectionHelper.GetDirection(directionChar.ToString());
                var op            = new DirectionOp()
                {
                    Direction = direction, Length = (int)length
                };

                Directions.Add(op);
                head = i + 1;
            }
            if (head != directionString.Length)
            {
                throw new Exception(string.Format("Unexpected format in direction string: {0}", directionString));
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            if (args != null && args.Any())
            {
                // This is to ensure that argument count is always odd number 3,5,7 etc
                if ((args.Count() + 1) % 2 != 0)
                {
                    System.Console.WriteLine("The arguments passed are not correct. Please retry");
                    return;
                }

                //Here we can get the rquest and assing it to robot
                var container = new UnityContainer();

                ResiterTypes(container);

                var   program = container.Resolve <ProgramStarter>();
                Robot robot   = null;
                for (int i = 0; i < args.Count(); i++)
                {
                    string argument = args[i].ToString();
                    // Now this will be space separated string
                    // We need to get the data from it
                    string[] argss = argument.Split(' ');

                    if (i == 0)
                    {
                        continue;
                        // we still haven't got anything to map "5 5" to anything
                        // it should be written here
                    }
                    // every even argument should be robot object
                    // The robot object must be the argument in even order
                    if ((i + 1) % 2 == 0)
                    {
                        robot   = new Robot();
                        robot.x = argss[0] != null?Convert.ToInt32(argss[0].Trim()) : 0;

                        robot.y = argss[1] != null?Convert.ToInt32(argss[1].Trim()) : 0;

                        robot.DirectionOfMovement = DirectionHelper.GetDirection(argss[2] != null ? Convert.ToChar(argss[2].Trim()) : 'N');
                    }
                    try
                    {
                        // because we have 0 index system 1 is even and 2 is odd
                        // every odd argument should be instruction
                        if (i % 2 == 0)// This will make sure that third argument is mapped as instruction
                        {
                            System.Console.WriteLine(string.Format("processing {0} {1} {2} and {3}", robot.x.ToString(), robot.y.ToString(), robot.DirectionOfMovement.ToString(), args[i].ToString()));
                            program.Run(robot, args[i].Trim());
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine("Something went wrong : " + ex.InnerException);
                    }
                }
            }
        }
예제 #3
0
        public static PlayerAction GetTurnToAction(Tank tank, int x, int y)
        {
            PlayerAction action = new PlayerAction();

            action.Direction        = DirectionHelper.GetDirection(tank.X, tank.Y, x, y);
            action.PlayerActionType = PlayerActionType.Turn;
            return(action);
        }
예제 #4
0
        public void GetDirectionNorthTest()
        {
            var North = new Direction()
            {
                CardinalCompassPoint = 'N', Left = 'W', Right = 'E'
            };
            var NorthTest = DirectionHelper.GetDirection('N');

            Assert.AreEqual(North, NorthTest);
        }
예제 #5
0
 /// <summary>
 /// We assume the two tanks are in each other's sight.
 /// </summary>
 /// <param name="tank"></param>
 /// <param name="enemyTanks"></param>
 /// <returns></returns>
 public static bool GetIsUnderFire(int x, int y, Tank[] enemyTanks)
 {
     foreach (Tank enemyTank in enemyTanks)
     {
         if (DirectionHelper.GetDirection(enemyTank.X, enemyTank.Y, x, y) == enemyTank.Direction)
         {
             return(true);
         }
     }
     return(false);
 }
예제 #6
0
    private void OnCollisionEnter2D(Collision2D col)
    {
        var grid = map.layoutGrid;

        var contact = col.contacts[0];

        var mapCell = grid.WorldToCell(transform.position);

        Direction collisionDirection = DirectionHelper.GetDirection((Vector3)mapCell, transform.position);

        player.OnCollide(part, col, mapCell);
    }
예제 #7
0
        public override PlayerAction Play(Tank tank)
        {
            if (TankHelper.EnemyInFront(tank, Opponent.Tank1, Opponent.Tank2, map))
            {
                return(PlayerActionHelper.GetAttackAction(tank));
            }

            MyWanderingTank myTank       = MyWanderingTank.GetMyTank(tank);
            PlayerAction    pAction      = new PlayerAction();
            Block           currentBlock = map.Blocks[tank.X, tank.Y];
            BlockNeighbours neighbours   = map.GetNeighbours(currentBlock);
            Block           frontBlock   = neighbours.GetNeighbour(tank.Direction);

            if (myTank.Turned)
            {
                pAction.PlayerActionType = PlayerActionType.Forward;
                pAction.Direction        = tank.Direction;
                myTank.Turned            = false;
                return(pAction);
            }

            if (MathHelper.GetRandomNumber(0, 3) != 0 && frontBlock != null && frontBlock.Pattern == Pattern.Clear)
            {
                pAction.PlayerActionType = PlayerActionType.Forward;
                pAction.Direction        = tank.Direction;
                myTank.Turned            = false;
                return(pAction);
            }

            //Turn
            int randomNumber   = MathHelper.GetRandomNumber(0, 100) + myTank.Number;// % 4;
            var myNeighbours   = map.GetNeighbours(currentBlock);
            var passableBlocks = myNeighbours.Neighbours.Where(c => c.Passable && c != frontBlock).ToList();

            myTank.Turned = true;
            var passableBlocksCount = passableBlocks.Count();

            if (passableBlocksCount == 0)
            {
                pAction.PlayerActionType = PlayerActionType.Forward;
                pAction.Direction        = tank.Direction;
                myTank.Turned            = false;
                return(pAction);
            }
            var index     = randomNumber % passableBlocksCount;
            var nextBlock = passableBlocks[index];

            pAction.Direction        = DirectionHelper.GetDirection(currentBlock.X, currentBlock.Y, nextBlock.X, nextBlock.Y);
            pAction.PlayerActionType = PlayerActionType.Turn;
            return(pAction);
        }
예제 #8
0
        public DirectionInfo(string directionString)
        {
            try
            {
                var directionTokens = directionString.Split(_delimiter);
                foreach (var directionToken in directionTokens)
                {
                    var length    = Int32.Parse(directionToken.Substring(0, directionToken.Length - 1));
                    var direction = DirectionHelper.GetDirection(directionToken.Substring(directionToken.Length - 1));

                    Directions.Add(new DirectionOp {
                        Length = length, Direction = direction
                    });
                }

                //Validate(); nope. wild west, now.
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Unable to parse direction string '{0}'", directionString), ex);
            }
        }
예제 #9
0
    public void JumpToMap(float delay1, Vector3 position, float delay2, Action callback)
    {
        Vector2   start = transform.position;
        Vector2   end   = position;
        Vector2   control;
        Direction direction = DirectionHelper.GetDirection(start, end);

        // Up
        if (direction.IsUp())
        {
            // Left
            if (direction.IsLeft())
            {
                animator.SetTrigger(nhayNgang);
                transform.SetHorizontalFlip(false);

                control = new Vector2((start.x + end.x) * 0.5f, start.y + Mathf.Sqrt(Mathf.Abs(end.x - start.x)) * controlHeightFactor);
            }
            // Right
            else if (direction.IsRight())
            {
                animator.SetTrigger(nhayNgang);
                transform.SetHorizontalFlip(true);

                control = new Vector2((start.x + end.x) * 0.5f, start.y + Mathf.Sqrt(Mathf.Abs(end.x - start.x)) * controlHeightFactor);
            }
            else
            {
                animator.SetTrigger(nhayLen);
                transform.SetHorizontalFlip(false);

                control = new Vector2(start.x, end.y + controlDeltaY);
            }
        }
        else
        {
            // Left
            if (direction.IsLeft())
            {
                animator.SetTrigger(nhayNgang);
                transform.SetHorizontalFlip(false);

                control = new Vector2((start.x + end.x) * 0.5f, start.y + Mathf.Sqrt(Mathf.Abs(end.x - start.x)) * controlHeightFactor);
            }
            // Right
            else if (direction.IsRight())
            {
                animator.SetTrigger(nhayNgang);
                transform.SetHorizontalFlip(true);

                control = new Vector2((start.x + end.x) * 0.5f, start.y + Mathf.Sqrt(Mathf.Abs(end.x - start.x)) * controlHeightFactor);
            }
            else
            {
                animator.SetTrigger(nhayXuong);
                transform.SetHorizontalFlip(false);

                control = new Vector2(start.x, start.y + controlDeltaY);
            }
        }

        var        jump   = SequenceAction.Create(QuadBezierAction.BezierTo(control, end, 0.5f), CallFuncAction.Create(() => { animator.SetTrigger(nghiXuong); }));
        BaseAction action = null;

        if (delay1 > 0)
        {
            if (delay2 > 0)
            {
                action = SequenceAction.Create(DelayAction.Create(delay1), jump, DelayAction.Create(delay2));
            }
            else
            {
                action = SequenceAction.Create(DelayAction.Create(delay1), jump);
            }
        }
        else
        {
            if (delay2 > 0)
            {
                action = SequenceAction.Create(jump, DelayAction.Create(delay2));
            }
            else
            {
                action = jump;
            }
        }

        // Jump
        gameObject.Play(action, callback);
    }
        /* Trying to move checker
         * Calculating the movement
         * ------------------------------------------------------------------------------
         */
        private void DoMovement(BaseChecker previouslySelectedChecker, Point toPosition)
        {
            string    logMessage = Constants.localized.logTurn + ' ';
            PathPoint turns;

            // Erase highlighting on all checkers
            DeselectAllCheckers();

            // If the move does not start
            if (state.ActiveCheckerPathPoint == null)
            {
                turns = previouslySelectedChecker.GetPossibleTurns(); // Calculation of all possible chains of moves for a checker
                state.ActiveCheckerPathPoint = turns;
            }
            else
            {
                turns = state.ActiveCheckerPathPoint; // Continuation of the movement
            }
            // If the position is not a deadlock
            if (!turns.IsDeadEnd())
            {
                state.ActiveChecker = previouslySelectedChecker;

                // Movement
                for (int i = 0; i < 4; i++)
                {
                    foreach (PathPoint point in turns[i])
                    {
                        if (toPosition == point.Position)
                        {
                            Point fromPosition = new Point()
                            {
                                X = previouslySelectedChecker.Position.BoardPosition.X,
                                Y = previouslySelectedChecker.Position.BoardPosition.Y
                            };
                            logMessage += previouslySelectedChecker.GetPrintablePosition() + $" {Constants.localized.logMoveTo} ";
                            // Moving the checker to the selected position
                            previouslySelectedChecker.MoveTo(new Point(toPosition.X, toPosition.Y));
                            logMessage += previouslySelectedChecker.GetPrintablePosition();
                            Game.userLogController.WriteMessage(logMessage);
                            // Defining the direction of movement
                            Point moveDirection = new Point()
                            {
                                X = toPosition.X - fromPosition.X > 0 ? 1 : -1,
                                Y = toPosition.Y - fromPosition.Y > 0 ? 1 : -1
                            };

                            // Determination of the eaten checkers
                            TryEatEnemyCheckers(previouslySelectedChecker, toPosition, fromPosition, moveDirection);

                            // If the checker can become a king
                            if (previouslySelectedChecker is Pawn && previouslySelectedChecker.Position.BoardPosition.Y ==
                                (previouslySelectedChecker.Direction == CheckerMoveDirection.Upstairs ? 0 : 7))
                            {
                                // Replacement of a checker by a king
                                King newKing = new King(previouslySelectedChecker.Side, previouslySelectedChecker.Position);
                                state.AllCheckers.Remove(previouslySelectedChecker);
                                state.AllCheckers.Add(newKing);

                                // Adding to the list of drawings
                                Game.drawingController.AddToDrawingList(newKing);
                                previouslySelectedChecker.Destroy();

                                // Calculation of the further path for a king
                                state.ActiveChecker = newKing;
                                if (state.ActiveCheckerPathPoint.afterAggression)
                                {
                                    state.ActiveCheckerPathPoint = newKing.GetPossibleTurns(DirectionHelper.GetOppositeDirection(DirectionHelper.GetDirection(moveDirection)));
                                    if (state.ActiveCheckerPathPoint.TryGetAggresiveDirections().Count == 0)
                                    {
                                        // End of turn
                                        EndTurn();
                                        return;
                                    }
                                    else
                                    {
                                        DeselectAllCheckers();
                                        state.ActiveChecker.selected = true;
                                        HighlightPossibleTurns(state.ActiveChecker);
                                    }
                                }
                                else
                                {
                                    EndTurn();
                                    return;
                                }
                            }

                            // Find the point he went to
                            // Calculate whether it is possible to continue the movement
                            foreach (PathPoint waypoint in state.ActiveCheckerPathPoint[DirectionHelper.GetDirection(moveDirection)])
                            {
                                if (waypoint.Position == toPosition)
                                {
                                    if (!waypoint.IsOnlyFinalTraces())
                                    {
                                        // Continuation of the movement
                                        state.ActiveCheckerPathPoint = waypoint;
                                        DeselectAllCheckers();
                                        state.ActiveChecker.selected = true;
                                        HighlightPossibleTurns(state.ActiveChecker);
                                    }
                                    else
                                    {
                                        // End of turn
                                        EndTurn();
                                    }
                                }
                            }
                            return;
                        }
                    }
                }
            }
            else
            {
                // Deadlock position, end of turn
                if (state.ActiveChecker != null || state.ActiveCheckerPathPoint != null)
                {
                    state.ActiveChecker          = null;
                    state.ActiveCheckerPathPoint = null;
                }
            }
        }
예제 #11
0
 /// <summary>
 /// We assume the two tanks are in each other's sight.
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <param name="enemyTank"></param>
 /// <returns></returns>
 public static bool GetIsUnderFire(int x, int y, Tank enemyTank)
 {
     return(DirectionHelper.GetDirection(enemyTank.X, enemyTank.Y, x, y) == enemyTank.Direction);
 }
예제 #12
0
 /// <summary>
 /// Suppose they are on each other's sight.
 /// </summary>
 /// <param name="tank"></param>
 /// <param name="t"></param>
 /// <returns></returns>
 public static bool GetIsPointedBy(Tank t1, Tank t2)
 {
     return(t2.Direction == DirectionHelper.GetDirection(t2.X, t2.Y, t1.X, t1.Y));
 }
예제 #13
0
 /// <summary>
 /// We assume the two tanks are in each other's sight.
 /// </summary>
 /// <param name="tank"></param>
 /// <param name="enemyTank1"></param>
 /// <returns></returns>
 public static bool GetIsUnderFire(Tank tank, Tank enemyTank)
 {
     return(DirectionHelper.GetDirection(enemyTank.X, enemyTank.Y, tank.X, tank.Y) == enemyTank.Direction);
 }
예제 #14
0
        static void Main(string[] args)
        {
            try
            {
                List <Rover> rovers = new List <Rover>();

                Console.WriteLine("Mars platosunun sınır değerlerini giriniz:");

                var plateauInput      = Console.ReadLine();
                var plateauInputArray = plateauInput?.Split(' ');

                if (plateauInputArray == null && plateauInputArray.Length < 2)
                {
                    throw new Exception("Sınır değerleri uygun değil");
                }

                Console.WriteLine("Mars platosunun sınır değerlerini giriniz:");

                var plateau = new Plateau();

                if (Int32.TryParse(plateauInputArray[0], out int xLenght))
                {
                    plateau.xLenght = xLenght;
                }
                else
                {
                    throw new Exception("Sınır X değeri uygun değil");
                }

                if (Int32.TryParse(plateauInputArray[1], out int yLenght))
                {
                    plateau.yLenght = yLenght;
                }
                else
                {
                    throw new Exception("Sınır Y değeri uygun değil");
                }

                var roverOk = "e";
                while (roverOk.ToLower() == "e")
                {
                    Console.WriteLine("Mars aracı için konum giriniz:");

                    var roverPositionInput = Console.ReadLine();

                    if (string.IsNullOrWhiteSpace(roverPositionInput))
                    {
                        throw new Exception("Mars aracı için konum hatalı");
                    }

                    Console.WriteLine("Mars aracı için komutları giriniz:");

                    var roverCommands = Console.ReadLine();

                    if (string.IsNullOrWhiteSpace(roverCommands))
                    {
                        throw new Exception("Mars aracı için komut hatalı");
                    }

                    var roverPositionInputArray = roverPositionInput.Split(" ");

                    if (roverPositionInputArray == null && roverPositionInputArray.Length < 3)
                    {
                        throw new Exception("Konum değerleri uygun değil");
                    }

                    var position = new Position();

                    if (Int32.TryParse(roverPositionInputArray[0], out int x))
                    {
                        position.X = x;
                    }
                    else
                    {
                        throw new Exception("X değeri uygun değil");
                    }

                    if (Int32.TryParse(roverPositionInputArray[1], out int y))
                    {
                        position.Y = y;
                    }
                    else
                    {
                        throw new Exception("Y değeri uygun değil");
                    }

                    position.Direction = DirectionHelper.GetDirection(roverPositionInputArray[2].ToUpper().First());

                    rovers.Add(new Rover()
                    {
                        Commands = roverCommands, CurruntPosition = position
                    });

                    Console.WriteLine("Yeni Mars aracı için giriş yapacak mısınız?(e/h):");

                    roverOk = Console.ReadLine();
                }

                var roverControl = new RoverMotion(plateau);

                foreach (var rover in rovers)
                {
                    foreach (var command in rover.Commands)
                    {
                        rover.CurruntPosition = roverControl.GetNextPosition(rover.CurruntPosition, command);
                    }

                    Console.WriteLine(rover.CurruntPosition.ToString());
                }


                Console.WriteLine("Çıkış için bir tuşa basınız");
                Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }