Ejemplo n.º 1
0
 private static void PositionXChanged(StreamMessage streamMessage, ClientRobot robot, Position oldPosition)
 {
     if (robot.IsCloserToYAxis(oldPosition))
     {
         if (robot.Position.X >= 0)
         {
             TurnLeft(streamMessage, robot);
         }
         else
         {
             TurnRight(streamMessage, robot);
         }
     }
     else
     {
         if (robot.Position.X >= 0)
         {
             TurnRight(streamMessage, robot);
         }
         else
         {
             TurnLeft(streamMessage, robot);
         }
     }
 }
Ejemplo n.º 2
0
 private static DriveAnswerCommand robotDriveAround(ClientRobot robot)
 {
     if (robot.Power > robot.Motor.ROTATE_IN)
     {
         return(robot.Drive(robot.AngleDrive, robot.Motor.ROTATE_IN));
     }
     else
     {
         if (0 <= robot.AngleDrive && robot.AngleDrive < 90)
         {
             return(robot.Drive(180, 100));
         }
         else if (90 <= robot.AngleDrive && robot.AngleDrive < 180)
         {
             return(robot.Drive(270, 100));
         }
         else if (180 <= robot.AngleDrive && robot.AngleDrive < 270)
         {
             return(robot.Drive(0, 100));
         }
         else if (270 <= robot.AngleDrive && robot.AngleDrive < 360)
         {
             return(robot.Drive(90, 100));
         }
     }
     return(null);
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            ClientRobot.Connect(args);
            Tank mujRobot = new Tank("My first tank", ClientRobot.TEAM_NAME);

            /* Odkud níže piště kód*/
        }
Ejemplo n.º 4
0
        private static void GetToYAxis(StreamMessage streamMessage, ClientRobot robot)
        {
            if (robot.Position.X == 0)
            {
                return;
            }

            while (robot.Position.X != 0)
            {
                var oldPosition = robot.Position;
                Move(streamMessage, robot);
                if (robot.IsCloserToYAxis(oldPosition))
                {
                    continue;
                }
                if (robot.HasntMoved(oldPosition))
                {
                    continue;
                }
                if (robot.Position.Y != oldPosition.Y)
                {
                    PositionYChanged(streamMessage, robot);
                    continue;
                }
                TurnAbout(streamMessage, robot);
            }

            Console.WriteLine(robot.Position);
        }
Ejemplo n.º 5
0
        public static void SendSyntaxError(StreamMessage streamMessage, ClientRobot robot)
        {
            string message = ServerMessagesCodes.SERVER_SYNTAX_ERROR.GetEnumDescription();

            streamMessage.WriteMessage(message);
            streamMessage.CloseClient(robot);
        }
Ejemplo n.º 6
0
        public static void SendLogicError(StreamMessage streamMessage, ClientRobot robot)
        {
            string message = ServerMessagesCodes.SERVER_LOGIC_ERROR.GetEnumDescription();

            streamMessage.WriteMessage(message);
            robot.Close();
        }
Ejemplo n.º 7
0
        public static void Main(string[] args)
        {
            Tank tank = new Tank("Rabbit", ClientRobot.TEAM_NAME);

            ClientRobot.Connect(args);
            Console.WriteLine("Rabbit is ready for action.");
            while (true)
            {
                double toX = RANDOM.Next(0, 800) + 100;
                double toY = RANDOM.Next(0, 800) + 100;

                while (Math.Abs(toX - tank.X) > 50 && Math.Abs(toY - tank.Y) > 50)
                {
                    double angle = Utils.AngleDegree(tank.X, tank.Y, toX, toY);
                    if (Math.Abs(tank.AngleDrive - angle) > 3)
                    {
                        tank.Drive(angle, tank.Motor.ROTATE_IN);
                    }
                    else
                    {
                        tank.Wait();
                    }
                }
            }
        }
Ejemplo n.º 8
0
        private void HandleClient(object obj)
        {
            TcpClient     client        = (TcpClient)obj;
            StreamMessage streamMessage = new StreamMessage(client);
            ClientRobot   robot         = new ClientRobot {
                TcpClient = client
            };

            try
            {
                Communicate(streamMessage, robot);
            }
            catch (IOException ioe)
            {
                Console.WriteLine(ioe.Message);
            }
            catch (ObjectDisposedException ide)
            {
                Console.WriteLine(ide.Message);
            }
            finally
            {
                streamMessage.CloseClient(robot);
            }
        }
Ejemplo n.º 9
0
        public static bool TryLogIn(StreamMessage streamMessage, ClientRobot robot)
        {
            try
            {
                GetUserName(streamMessage, robot);

                string expectedPassword = GetExpectedPassword(streamMessage);

                GetPassword(streamMessage, robot);

                if (LoginIsValid(expectedPassword, streamMessage))
                {
                    MessageService.SendOk(streamMessage);
                    return(true);
                }

                MessageService.SendLoginFailed(streamMessage);

                return(false);
            }
            catch (IOException ioe)
            {
                return(false);
            }
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            ClientRobot.Connect(args);

            FlagPlace toFlagPlace  = null;
            FlagPlace ownFlagPlace = null;

            foreach (var flagPlace in flagPlaces)
            {
                if (flagPlace.TEAM_ID == tank.TEAM_ID)
                {
                    ownFlagPlace = flagPlace;
                }
                else
                {
                    toFlagPlace = flagPlace;
                }
            }


            while (true)
            {
                if (tank.carryFlag)
                {
                    driveTo(ownFlagPlace);
                }
                else
                {
                    driveTo(toFlagPlace);
                }
            }
        }
Ejemplo n.º 11
0
        private static void SetOrientationToRobot(StreamMessage streamMessage, ClientRobot robot)
        {
            Move(streamMessage, robot);
            var positionA = GetPositionSetToRobot(streamMessage, robot);

            Move(streamMessage, robot);
            var positionB = GetPositionSetToRobot(streamMessage, robot);

            robot.Orientation = OrientationRecogrition.Recognize(positionA, positionB);
        }
Ejemplo n.º 12
0
 public static void WaitForRecharging(StreamMessage streamMessage, ClientRobot robot)
 {
     robot.TcpClient.ReceiveTimeout = Constants.TIMEOUT_RECHARGING;
     streamMessage.ReadMessage("Accepted Full charged", MaxLenths.FullPower);
     if (!MessageValidator.ValidFullPower(streamMessage))
     {
         MessageService.SendLogicError(streamMessage, robot);
     }
     robot.TcpClient.ReceiveTimeout = Constants.TIMEOUT;
 }
Ejemplo n.º 13
0
 public static void TryMoveToGoal(StreamMessage streamMessage, ClientRobot robot)
 {
     SetOrientationToRobot(streamMessage, robot);
     while (robot.Position.X != 0 || robot.Position.Y != 0)
     {
         TurnToXAxis(streamMessage, robot);
         GetToXAxis(streamMessage, robot);
         TurnToYAxis(streamMessage, robot);
         GetToYAxis(streamMessage, robot);
     }
 }
Ejemplo n.º 14
0
        public void CloseClient(ClientRobot robot)
        {
            if (robot.IsClosed)
            {
                return;
            }

            robot.Close();
            StreamReader.Dispose();
            StreamWriter.Dispose();
            ClientClosed = true;
        }
Ejemplo n.º 15
0
        private static void Move(StreamMessage streamMessage, ClientRobot robot)
        {
            MessageService.SendMoveChallenge(streamMessage);
            GetConfirm(streamMessage, robot);
            var oldPosition = robot.Position;

            GetPositionSetToRobot(streamMessage, robot);
            if (robot.HasntMoved(oldPosition))
            {
                Move(streamMessage, robot);
            }
        }
Ejemplo n.º 16
0
        private static DriveAnswerCommand robotDriveToBase(ClientRobot robot)
        {
            double angle = AngleUtils.AngleDegree(robot.X, robot.Y, capturedBase.X, capturedBase.Y);

            if (robot.Power > robot.Motor.ROTATE_IN && Math.Abs(angle - robot.AngleDrive) > 1)
            {
                return(robot.Drive(robot.AngleDrive, robot.Motor.ROTATE_IN));
            }
            else
            {
                return(robot.Drive(angle, 100));
            }
        }
Ejemplo n.º 17
0
        public static void TryPickUp(StreamMessage streamMessage, ClientRobot robot)
        {
            GetKey(streamMessage);

            if (IsKeyValid(streamMessage, robot))
            {
                MessageService.SendOk(streamMessage);
            }
            else
            {
                MessageService.SendSyntaxError(streamMessage, robot);
            }
        }
Ejemplo n.º 18
0
        private static void ValidUserName(StreamMessage streamMessage, ClientRobot robot)
        {
            if (MessageValidator.IsRecharging(streamMessage))
            {
                return;
            }

            if (UserNameIsValid(streamMessage))
            {
                return;
            }

            MessageService.SendSyntaxError(streamMessage, robot);
        }
Ejemplo n.º 19
0
 private static bool IsKeyValid(StreamMessage streamMessage, ClientRobot robot)
 {
     if (MessageValidator.IsRecharging(streamMessage))
     {
         RechargingService.WaitForRecharging(streamMessage, robot);
         streamMessage.ReadMessage("Accepted key", MaxLenths.Message);
         IsKeyValid(streamMessage, robot);
     }
     if (MessageValidator.IsTooLong(streamMessage, MaxLenths.Login))
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 20
0
        private static void ValidPassword(StreamMessage streamMessage, ClientRobot robot)
        {
            if (MessageValidator.IsRecharging(streamMessage))
            {
                RechargingService.WaitForRecharging(streamMessage, robot);
                streamMessage.ReadMessage("Accepted password", MaxLenths.Password);
                ValidPassword(streamMessage, robot);
            }
            if (PasswordIsValid(streamMessage))
            {
                return;
            }

            MessageService.SendSyntaxError(streamMessage, robot);
        }
Ejemplo n.º 21
0
        private static Position GetPositionSetToRobot(StreamMessage streamMessage, ClientRobot robot)
        {
            var data = streamMessage.AcceptedMessage.DecodedData.Split(' ');

            if (!HasRightFormat(streamMessage))
            {
                return(robot.Position);
            }

            int x = GetPositionInt(data[1]);
            int y = GetPositionInt(data[2]);

            robot.Position = new Position(x, y);

            return(robot.Position);
        }
Ejemplo n.º 22
0
        private static void GetConfirm(StreamMessage streamMessage, ClientRobot robot)
        {
            if (MessageValidator.IsRecharging(streamMessage))
            {
                RechargingService.WaitForRecharging(streamMessage, robot);
                streamMessage.ReadMessage("Accepted confirm", MaxLenths.Confirm);
                GetConfirm(streamMessage, robot);
            }

            if (IsConfirmMessageValid(streamMessage))
            {
                return;
            }

            MessageService.SendSyntaxError(streamMessage, robot);
        }
Ejemplo n.º 23
0
        static void Main(string[] args)
        {
            GameTypeCommand gameType = ClientRobot.Connect(args);

            Spot[] spots = new Spot[gameType.ROBOTS_IN_ONE_TEAM];
            for (int i = 0; i < spots.Length; i++)
            {
                spots[i] = new Spot("spot_" + i);
            }
            while (true)
            {
                foreach (Spot spot in spots)
                {
                    process(spot, getWhatToDo(spot));
                }
            }
        }
Ejemplo n.º 24
0
        private static void TurnToXAxis(StreamMessage streamMessage, ClientRobot robot)
        {
            if (robot.IsClosed || robot.Position.Y == 0)
            {
                return;
            }

            switch (robot.Orientation)
            {
            case Orientation.East:
                if (robot.Position.Y > 0)
                {
                    TurnRight(streamMessage, robot);
                }
                else
                {
                    TurnLeft(streamMessage, robot);
                }
                break;

            case Orientation.South:
                if (robot.Position.Y < 0)
                {
                    TurnAbout(streamMessage, robot);
                }
                break;

            case Orientation.West:
                if (robot.Position.Y > 0)
                {
                    TurnLeft(streamMessage, robot);
                }
                else
                {
                    TurnRight(streamMessage, robot);
                }
                break;

            case Orientation.North:
                if (robot.Position.Y > 0)
                {
                    TurnAbout(streamMessage, robot);
                }
                break;
            }
        }
Ejemplo n.º 25
0
        private static void TurnToYAxis(StreamMessage streamMessage, ClientRobot robot)
        {
            switch (robot.Orientation)
            {
            case Orientation.East:
                if (robot.Position.X > 0)
                {
                    TurnAbout(streamMessage, robot);
                }
                break;

            case Orientation.South:
                if (robot.Position.X < 0)
                {
                    TurnLeft(streamMessage, robot);
                }
                else
                {
                    TurnRight(streamMessage, robot);
                }
                break;

            case Orientation.West:
                if (robot.Position.X > 0)
                {
                    TurnAbout(streamMessage, robot);
                }
                break;

            case Orientation.North:
                if (robot.Position.X < 0)
                {
                    TurnRight(streamMessage, robot);
                }
                else
                {
                    TurnLeft(streamMessage, robot);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 26
0
        private void Communicate(StreamMessage streamMessage, ClientRobot robot)
        {
            if (robot.IsClosed)
            {
                return;
            }
            if (!LoginService.TryLogIn(streamMessage, robot))
            {
                return;
            }

            MoveService.TryMoveToGoal(streamMessage, robot);
            if (robot.IsClosed)
            {
                return;
            }

            PickUpService.TryPickUp(streamMessage, robot);
        }
Ejemplo n.º 27
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Spot start.");
            string name = "Spot";
            Tank   spot = new Tank(name, ClientRobot.TEAM_NAME);

            ClientRobot.Connect(args);
            Console.WriteLine("Spot is ready for action.");

            int direction = 90;

            while (true)
            {
                if (spot.Power.Equals(0))
                {
                    spot.Drive(direction, 100);
                }
                else if ((direction == 90 && spot.Y > 575) ||
                         (direction == 270 && spot.Y < 425) ||
                         (direction == 180 && spot.X < 150) ||
                         (direction == 0 && spot.X > 850))
                {
                    spot.Drive(direction, 0);
                    direction = (direction + 90) % 360;
                }
                else
                {
                    for (int angle = 0; angle < 360; angle += 30)
                    {
                        ScanAnswerCommand scanAnswer;
                        if ((scanAnswer = spot.Scan(angle, 10)).ENEMY_ID != spot.ID)
                        {
                            spot.Shoot(angle, scanAnswer.RANGE);
                        }
                    }
                }
            }
        }
Ejemplo n.º 28
0
 private static void TurnLeft(StreamMessage streamMessage, ClientRobot robot)
 {
     MessageService.SendTurnLeftChallenge(streamMessage);
     GetConfirm(streamMessage, robot);
     robot.TurnLeft();
 }
Ejemplo n.º 29
0
 private static void TurnAbout(StreamMessage streamMessage, ClientRobot robot)
 {
     TurnRight(streamMessage, robot);
     TurnRight(streamMessage, robot);
 }
Ejemplo n.º 30
0
        public static void Main(string[] args)
        {
            ClientRobot.Connect(args);
            while (true)
            {
                RepairmenState repairmenState = getRepairmenState();
                MinerState     minerState     = getMinerState();


                switch (minerState)
                {
                case MinerState.DETONATE:
                    if (mineLayer.PutMinesList.Count > 0)
                    {
                        mineLayer.DetonateMine(mineLayer.PutMinesList[0].ID);
                    }
                    break;

                case MinerState.PUT_MINE:
                    mineLayer.PutMine();
                    break;

                case MinerState.GO_TO_REPAIRMAN:
                    mineLayer.Drive(AngleUtils.AngleDegree(mineLayer.X, mineLayer.Y, repairman.X, repairman.Y),
                                    mineLayer.Motor.ROTATE_IN);
                    break;

                case MinerState.GO_TO_BASE:
                    robotDriveToBase(mineLayer);
                    break;

                case MinerState.GO_AROUND:
                    if (EuclideanSpaceUtils.Distance(mineLayer.X, mineLayer.Y, capturedBase.X, capturedBase.Y) < BaseCapture.BASE_SIZE * 3.0 / 4.0)
                    {
                        scan1 = mineLayer.Scan(AngleUtils.AngleDegree(mineLayer.X, mineLayer.Y, capturedBase.X, capturedBase.Y), 10);
                    }
                    else
                    {
                        robotDriveAround(mineLayer);
                    }
                    break;
                }

                switch (repairmenState)
                {
                case RepairmenState.GO_AROUND:
                    if (EuclideanSpaceUtils.Distance(mineLayer.X, mineLayer.Y, capturedBase.X, capturedBase.Y) <
                        BaseCapture.BASE_SIZE * 3.0 / 4.0)
                    {
                        scan2 = repairman.Scan(AngleUtils.AngleDegree(mineLayer.X, mineLayer.Y, capturedBase.X, capturedBase.Y), 10);
                    }
                    else
                    {
                        robotDriveAround(repairman);
                    }
                    break;

                case RepairmenState.GO_TO_BASE:
                    robotDriveToBase(repairman);
                    break;

                case RepairmenState.REPAIR:
                    repairman.Repair((int)EuclideanSpaceUtils.Distance(repairman.X, repairman.Y, mineLayer.X, mineLayer.Y) + 1);
                    break;
                }

                if (repairmenState == RepairmenState.REPAIR)
                {
                    mineLayer.previousHitpoints = mineLayer.HitPoints;
                }
            }
        }