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); } } }
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); }
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*/ }
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); }
public static void SendSyntaxError(StreamMessage streamMessage, ClientRobot robot) { string message = ServerMessagesCodes.SERVER_SYNTAX_ERROR.GetEnumDescription(); streamMessage.WriteMessage(message); streamMessage.CloseClient(robot); }
public static void SendLogicError(StreamMessage streamMessage, ClientRobot robot) { string message = ServerMessagesCodes.SERVER_LOGIC_ERROR.GetEnumDescription(); streamMessage.WriteMessage(message); robot.Close(); }
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(); } } } }
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); } }
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); } }
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); } } }
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); }
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; }
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); } }
public void CloseClient(ClientRobot robot) { if (robot.IsClosed) { return; } robot.Close(); StreamReader.Dispose(); StreamWriter.Dispose(); ClientClosed = true; }
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); } }
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)); } }
public static void TryPickUp(StreamMessage streamMessage, ClientRobot robot) { GetKey(streamMessage); if (IsKeyValid(streamMessage, robot)) { MessageService.SendOk(streamMessage); } else { MessageService.SendSyntaxError(streamMessage, robot); } }
private static void ValidUserName(StreamMessage streamMessage, ClientRobot robot) { if (MessageValidator.IsRecharging(streamMessage)) { return; } if (UserNameIsValid(streamMessage)) { return; } MessageService.SendSyntaxError(streamMessage, robot); }
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); }
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); }
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); }
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); }
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)); } } }
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; } }
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(); } }
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); }
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); } } } } }
private static void TurnLeft(StreamMessage streamMessage, ClientRobot robot) { MessageService.SendTurnLeftChallenge(streamMessage); GetConfirm(streamMessage, robot); robot.TurnLeft(); }
private static void TurnAbout(StreamMessage streamMessage, ClientRobot robot) { TurnRight(streamMessage, robot); TurnRight(streamMessage, robot); }
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; } } }