// Move the hero and set the map with new info, also pick treasure if the hero move to a treasure public void move(int oldPosY, int oldPosX, int newPosY, int newPosX, Adventurer adv) { if (newPosX >= map.Matrice.GetLength(1) || newPosY >= map.Matrice.GetLength(0) || newPosY < 0 || newPosX < 0) { return; } if (map.Matrice[newPosY, newPosX].IsTraversable == true) { map.Matrice[oldPosY, oldPosX].IsTraversable = true; if (map.Matrice[oldPosY, oldPosX].Treasure <= 0) { map.Matrice[oldPosY, oldPosX].Draw = "."; } else { map.Matrice[oldPosY, oldPosX].Draw = "T(" + map.Matrice[oldPosY, oldPosX].Treasure + ")"; } map.Hero[adv.Id].PosX = newPosX; map.Hero[adv.Id].PosY = newPosY; map.Matrice[newPosY, newPosX].IsTraversable = false; map.Matrice[newPosY, newPosX].Draw = "A(" + utils.adjustNameLenght(map.Hero[adv.Id].Name) + ")"; if (map.Matrice[newPosY, newPosX].Treasure > 0) { map.Hero[adv.Id].Treasure += 1; map.Matrice[newPosY, newPosX].Treasure -= 1; } } else { return; } }
public void changeOrientation(string newOrientation, Adventurer adv) { if (newOrientation == "G") { if (map.Hero[adv.Id].Orientation == 1) { map.Hero[adv.Id].Orientation = 4; } else { map.Hero[adv.Id].Orientation -= 1; } } if (newOrientation == "D") { if (map.Hero[adv.Id].Orientation == 4) { map.Hero[adv.Id].Orientation = 1; } else { map.Hero[adv.Id].Orientation += 1; } } }
//calcul juste l'orientation et l'arrivée du point private void ComputeAdventurerMove(Adventurer adventurer) { // on mets en place une boucle au cas ou la consigne est de dépiler les moves tant qu'ils sont illégaux // sinon ignorer la boucle bool keepmoving = true; while (keepmoving) { if (adventurer.MoveQueue.Count == 0) { return; } Move move = adventurer.MoveQueue.Dequeue(); switch (move) { case Move.Front: if (TryMoveFront(adventurer)) { ; } keepmoving = false; break; case Move.Left: case Move.Right: Rotate(adventurer, move); keepmoving = false; break; default: throw new FormatException(); } } }
public void setAdventurers(string[] mapInfo) { if (mapInfo.Length == 6) { int posX = int.Parse(mapInfo[2]); int posY = int.Parse(mapInfo[3]); if (posX >= map.GetLength(1) || posY >= map.GetLength(0) || posY < 0 || posX < 0) { Console.WriteLine("Your Adventurer is out of the map.\nThe new Hero " + mapInfo[1] + " was not created."); return; } if (map[posY, posX].IsTraversable == true) { hero[nbHero] = new Adventurer(nbHero, posX, posY, mapInfo[1], mapInfo[5], mapInfo[4]); map[posY, posX].Draw = "A(" + utils.adjustNameLenght(hero[nbHero].Name) + ")"; map[posY, posX].IsTraversable = false; nbHero++; } else { Console.WriteLine("Your Hero can't spwan on another hero or mountain."); return; } } else { Console.WriteLine("Adventurers has to follow this model (6 inputs) : A-Name-X-Y-Orientation-Movement Sequence\nLine ignored."); return; } }
/// <summary> /// Determines if the adventurer can move or not /// </summary> /// <param name="adventurer"></param> /// <param name="myMap"></param> /// <param name="MountainList"></param> /// <param name="AdventurerList"></param> /// <param name="nextBoxCoordinates"></param> /// <returns>Returns true if the adventurer can move</returns> private static bool AllowToMove(Adventurer adventurer, Map myMap, IEnumerable <Mountain> MountainList, IEnumerable <Adventurer> AdventurerList, int[] nextBoxCoordinates) { bool Moving = true; //1. When an adventurer will go out of the map, he can't move if (nextBoxCoordinates[0] < 0 || nextBoxCoordinates[0] > myMap.WidthBoxesNumber - 1 || nextBoxCoordinates[1] < 0 || nextBoxCoordinates[1] > myMap.HeightBoxesNumber - 1) { Moving = false; } //2. when a mountain is in the box in which the adventurer has to come if (CheckMountain(MountainList, nextBoxCoordinates[0], nextBoxCoordinates[1]) == true) { Moving = false; } //3. when an adventurer is in the box in which another adventurer has to come if (CheckAdventurer(AdventurerList, nextBoxCoordinates[0], nextBoxCoordinates[1]) == true) { Moving = false; } return(Moving); }
/// <summary> /// Method to know the coordinates of the next box where the adventurer will go /// </summary> /// <param name="adventurer"></param> /// <returns>The return is an array of the coordinates of the next box where the adventurer will go</returns> private static int[] NextBox(Adventurer adventurer) { int[] xy = new int[] { adventurer.AdventurerHorizontalAxis, adventurer.AdventurerVerticalAxis }; if (adventurer.Orientation == "E") { xy[0] = adventurer.AdventurerHorizontalAxis + 1; } else if (adventurer.Orientation == "O") { xy[0] = adventurer.AdventurerHorizontalAxis - 1; } else if (adventurer.Orientation == "S") { xy[1] = adventurer.AdventurerVerticalAxis + 1; } else if (adventurer.Orientation == "N") { xy[1] = adventurer.AdventurerVerticalAxis - 1; } else { throw new MessageException($"La valeur {adventurer.Orientation} n'est pas valide.\n" + $"L'orientation doit être N pour Nord, E pour Est, O pour Ouest et S pour Sud."); } return(xy); }
//effectue l'action de déplacement et récupére les trésors potentiel private void ResolveMovement(Point destination, Adventurer adventurer) { adventurer.Position = destination; for (int j = 0; j < map.Treasures.Count; j++) { if (map.Treasures[j].Position.Equals(destination) && map.Treasures[j].Number > 0) { var temptrez = map.Treasures[j]; temptrez.Number--; map.Treasures[j] = temptrez; adventurer.Treasure++; } } map.Treasures.RemoveAll(i => i.Number <= 0); }
// Here you check which way the hero will move (North, East, South, Ouest) and set the movement public void preMove(Adventurer adv) { switch (map.Hero[adv.Id].Orientation) { case 1: // North move(map.Hero[adv.Id].PosY, map.Hero[adv.Id].PosX, map.Hero[adv.Id].PosY - 1, map.Hero[adv.Id].PosX, adv); break; case 2: // East move(map.Hero[adv.Id].PosY, map.Hero[adv.Id].PosX, map.Hero[adv.Id].PosY, map.Hero[adv.Id].PosX + 1, adv); break; case 3: // South move(map.Hero[adv.Id].PosY, map.Hero[adv.Id].PosX, map.Hero[adv.Id].PosY + 1, map.Hero[adv.Id].PosX, adv); break; case 4: // West move(map.Hero[adv.Id].PosY, map.Hero[adv.Id].PosX, map.Hero[adv.Id].PosY, map.Hero[adv.Id].PosX - 1, adv); break; } }
private bool TryMoveFront(Adventurer adventurer) { var orientation = adventurer.Orientation; //on récupére la position de l'aventurier et on essaye de voir si la destination est valide Point destination = adventurer.Position; switch (orientation) { case Orientation.Nord: destination.Y = destination.Y - 1; break; case Orientation.Sud: destination.Y = destination.Y + 1; break; case Orientation.Ouest: destination.X = destination.X - 1; break; case Orientation.Est: destination.X = destination.X + 1; break; } //Si impossible de bouger, rien ne se passe ce tour ci if (!IsMoveValid(destination)) { return(false); } //Sinon, on move et on récupère un trésor si il y en a un ResolveMovement(destination, adventurer); //ON nettoie la liste des trésors si un trésor a atteint 0 return(true); }
public static List <Adventurer> Get(string[] fileContent) { var data = fileContent.Select(x => x.Replace("-", "").Split(' ').Where(y => y != string.Empty).ToArray()).ToArray(); var adventurers = new List <Adventurer>(); foreach (var mapLine in data) { switch (mapLine[0]) { case CellType.Adventurer: var adventurer = new Adventurer { Name = mapLine[1], Coordinates = new Coordinates(int.Parse(mapLine[2]), int.Parse(mapLine[3])), Moves = new Queue <string>(mapLine[5].Select(x => x.ToString())), Orientation = mapLine[4] }; adventurers.Add(adventurer); break; } } return(adventurers); }
private void Rotate(Adventurer adventurer, Move move) { //Pour la rotation on va exploiter les valeurs de l'énumération ce sera moins lourd if (move == Move.Right) { adventurer.Orientation = (Orientation)((((int)adventurer.Orientation) + 1) % 4); } else if (move == Move.Left) { if (adventurer.Orientation == Orientation.Nord) { adventurer.Orientation = Orientation.Ouest; } else { adventurer.Orientation = (Orientation)Math.Abs((((int)adventurer.Orientation) - 1) % 4); } } else { throw new FormatException(); } }
private void ComputeMove(World world, Adventurer adventurer, string move) { if (move == MoveType.TurnRight) { switch (adventurer.Orientation) { case Orientation.North: adventurer.Orientation = Orientation.East; break; case Orientation.East: adventurer.Orientation = Orientation.South; break; case Orientation.South: adventurer.Orientation = Orientation.West; break; case Orientation.West: adventurer.Orientation = Orientation.North; break; } } else if (move == MoveType.TurnLeft) { switch (adventurer.Orientation) { case Orientation.North: adventurer.Orientation = Orientation.West; break; case Orientation.East: adventurer.Orientation = Orientation.North; break; case Orientation.South: adventurer.Orientation = Orientation.East; break; case Orientation.West: adventurer.Orientation = Orientation.South; break; } } else if (move == MoveType.GoForward) { var nextCell = new Cell(); switch (adventurer.Orientation) { case Orientation.North: nextCell = world.Map.Content[adventurer.Coordinates.X, adventurer.Coordinates.Y - 1]; if (nextCell.IsAccessible) { --adventurer.Coordinates.Y; } break; case Orientation.East: nextCell = world.Map.Content[adventurer.Coordinates.X + 1, adventurer.Coordinates.Y]; if (nextCell.IsAccessible) { ++adventurer.Coordinates.X; } break; case Orientation.South: nextCell = world.Map.Content[adventurer.Coordinates.X, adventurer.Coordinates.Y + 1]; if (nextCell.IsAccessible) { ++adventurer.Coordinates.Y; } break; case Orientation.West: nextCell = world.Map.Content[adventurer.Coordinates.X - 1, adventurer.Coordinates.Y]; if (nextCell.IsAccessible) { --adventurer.Coordinates.X; } break; } var treasureCell = nextCell as TreasureCell; if (treasureCell?.TreasuresCount > 0) { ++adventurer.TreasuresCount; --treasureCell.TreasuresCount; } } }