public ActionsQueue(EntityCollection gameCollection, GameField field) { _field = field; _queue = new List<Action>(); _size = _queue.Count; _gameCollection = gameCollection; }
public Team(int teamId, AI ai, GameField field) : base() { _teamId = teamId; _ai = ai; _field = field; }
internal ReplayAction ReadAction(GameField field) { String str = sr.ReadLine(); String type = ""; int subNumb = 0; List<Cell> path = new List<Cell>(); while (!str.Contains("End action")) { if (str.Contains("Type")) { str = sr.ReadLine(); type = str; str = sr.ReadLine(); } if (str.Contains("Path")) { str = sr.ReadLine(); while (!str.Contains("End path")) { path.Add(field.Field[Int32.Parse(str.Split(' ').ElementAt(0)), Int32.Parse(str.Split(' ').ElementAt(1))]); str = sr.ReadLine(); } } if (str.Contains("Submarine")) { str = sr.ReadLine(); subNumb = Int32.Parse(str); str = sr.ReadLine(); } str = sr.ReadLine(); } AIAction action = null; switch (type) { case "Move": action = new Move(path); break; case "LaunchTorpedo": action = new LaunchTorpedo(path); break; case "PlaceMine": action = new PlaceMine(path); break; } return new ReplayAction(action, subNumb); }
private Collection<PathNode> GetNeighbours(PathNode pathNode, Cell goal, GameField field, Func<Cell, Cell, float> weightFunc = null) { var result = new Collection<PathNode>(); List<Cell> neighboursCell = pathNode.Position.Neighbours; foreach (var point in neighboursCell) { if (point.Type == CellType.LAND) continue; var neighbourNode = new PathNode() { Position = point, CameFrom = pathNode, PathLengthFromStart = pathNode.PathLengthFromStart + GetDistanceBetweenNeighbours(pathNode.Position, point, weightFunc), HeuristicEstimatePathLength = GetHeuristicPathLength(point, goal) }; result.Add(neighbourNode); } return result; }
public List<Cell> FindPath(GameField field, Cell start, Cell goal, Func<Cell, Cell, float> weightFunc = null) { var closedSet = new Collection<PathNode>(); var openSet = new Collection<PathNode>(); PathNode startNode = new PathNode() { Position = start, CameFrom = null, PathLengthFromStart = 0, HeuristicEstimatePathLength = GetHeuristicPathLength(start, goal) }; openSet.Add(startNode); while (openSet.Count > 0) { var currentNode = openSet.OrderBy(node => node.EstimateFullPathLength).First(); if (currentNode.Position == goal) return GetPathForNode(currentNode); openSet.Remove(currentNode); closedSet.Add(currentNode); foreach (var neighbourNode in GetNeighbours(currentNode, goal, field, weightFunc)) { if (closedSet.Count(node => node.Position == neighbourNode.Position) > 0) continue; var openNode = openSet.FirstOrDefault(node => node.Position == neighbourNode.Position); if (openNode == null) openSet.Add(neighbourNode); else if (openNode.PathLengthFromStart > neighbourNode.PathLengthFromStart) { openNode.CameFrom = currentNode; openNode.PathLengthFromStart = neighbourNode.PathLengthFromStart; } } } return null; }
public virtual AIAction NextAction(Submarine sub, GameField field) { for (int i = 0; i < 5; i++) switch (rnd.Next(5)) { case 0: addMarker(new Aim(field.Field[rnd.Next(field.Height), rnd.Next(field.Width)])); break; case 1: addMarker(new Circle(field.Field[rnd.Next(field.Height), rnd.Next(field.Width)])); break; case 2: addMarker(new Check(field.Field[rnd.Next(field.Height), rnd.Next(field.Width)])); break; case 3: addMarker(new Flag(field.Field[rnd.Next(field.Height), rnd.Next(field.Width)])); break; case 4: addMarker(new XMark(field.Field[rnd.Next(field.Height), rnd.Next(field.Width)])); break; } List<Cell> path; switch (rnd.Next(3)) { case 0 : int count = rnd.Next(3) + 1; int moveX = rnd.Next(Config.FIELD_HEIGHT); int moveY = rnd.Next(Config.FIELD_WIDTH); while (field.Field[moveX, moveY].Type == CellType.LAND) { moveX = rnd.Next(Config.FIELD_HEIGHT); moveY = rnd.Next(Config.FIELD_WIDTH); } path = field.getPath(sub.Cell, field.Field[moveX, moveY]); if (path == null) path = new List<Cell>(); return new Move(path); case 1 : count = rnd.Next(3) + 1; moveX = rnd.Next(Config.FIELD_HEIGHT); moveY = rnd.Next(Config.FIELD_WIDTH); while (field.Field[moveX, moveY].Type == CellType.LAND) { moveX = rnd.Next(Config.FIELD_HEIGHT); moveY = rnd.Next(Config.FIELD_WIDTH); } path = field.getPath(sub.Cell, field.Field[moveX, moveY]); if (path == null) path = new List<Cell>(); return new PlaceMine(path); case 2 : int x = rnd.Next(Config.FIELD_HEIGHT); int y = rnd.Next(Config.FIELD_WIDTH); while ((field.Field[x, y].Type == CellType.LAND) || ((x == sub.Cell.I) && (y == sub.Cell.J))) { x = rnd.Next(Config.FIELD_HEIGHT); y = rnd.Next(Config.FIELD_WIDTH); } path = field.getPath(sub.Cell, field.Field[x, y]); return new LaunchTorpedo(path); default: return null; } }
internal Team readTeam(int teamId, GameField field) { Team team = new Team(teamId, new AI(), field); String str = sr.ReadLine(); while (!str.Contains("End team")) { if (str.Equals("Base cells")) { str = sr.ReadLine(); while (!str.Equals("End base cells")) { team.addToCollection(new BaseC(field.Field[Int32.Parse(str.Split(' ').ElementAt(0)), Int32.Parse(str.Split(' ').ElementAt(1))], team)); str = sr.ReadLine(); } } if (str.Equals("Submarines")) { str = sr.ReadLine(); while (!str.Equals("End submarines")) { team.addToCollection(new Submarine(field.Field[Int32.Parse(str.Split(' ').ElementAt(0)), Int32.Parse(str.Split(' ').ElementAt(1))], team, team.TeamId == 0 ? LogicService.submarineL : LogicService.submarineR, Int32.Parse(str.Split(' ').ElementAt(2)), Int32.Parse(str.Split(' ').ElementAt(3)), Int32.Parse(str.Split(' ').ElementAt(4)), Int32.Parse(str.Split(' ').ElementAt(5)))); str = sr.ReadLine(); } } if (str.Equals("Mines")) { str = sr.ReadLine(); while (!str.Equals("End mines")) { team.addToCollection(new Mine(field.Field[Int32.Parse(str.Split(' ').ElementAt(0)), Int32.Parse(str.Split(' ').ElementAt(1))], team)); str = sr.ReadLine(); } } str = sr.ReadLine(); } return team; }
internal SunkSubmarine ReadSunkSubmarine(GameField field) { String str = sr.ReadLine(); SunkSubmarine ss = null; while (!str.Contains("End sunk submarine")) { ss = new SunkSubmarine(field.Field[Int32.Parse(str.Split(' ').ElementAt(0)), Int32.Parse(str.Split(' ').ElementAt(1))], new Color(Int32.Parse(str.Split(' ').ElementAt(2)))); str = sr.ReadLine(); } return ss; }
internal ReplayStep ReadStep(GameField field, int stepNumb) { String str = sr.ReadLine(); EntityCollection collection = null; ReplayAction action = null; while (!str.Contains("End step")) { if (str.Contains("Collection")) collection = ReadCollection(field); if (str.Contains("Action")) action = ReadAction(field); str = sr.ReadLine(); } return new ReplayStep(stepNumb, collection, action); }
internal Marker ReadMarker(GameField field) { String str = sr.ReadLine(); Marker marker = null; while (!str.Contains("End marker")) { String type = str; str = sr.ReadLine(); switch (type) { case "Aim": marker = new Aim(field.Field[Int32.Parse(str.Split(' ').ElementAt(0)), Int32.Parse(str.Split(' ').ElementAt(1))]); break; case "Circle": marker = new Circle(field.Field[Int32.Parse(str.Split(' ').ElementAt(0)), Int32.Parse(str.Split(' ').ElementAt(1))]); break; case "Check": marker = new Check(field.Field[Int32.Parse(str.Split(' ').ElementAt(0)), Int32.Parse(str.Split(' ').ElementAt(1))]); break; case "Flag": marker = new Flag(field.Field[Int32.Parse(str.Split(' ').ElementAt(0)), Int32.Parse(str.Split(' ').ElementAt(1))]); break; case "XMark": marker = new XMark(field.Field[Int32.Parse(str.Split(' ').ElementAt(0)), Int32.Parse(str.Split(' ').ElementAt(1))]); break; } str = sr.ReadLine(); } return marker; }
internal EntityCollection ReadInitPos(GameField field) { EntityCollection gameCollection = new EntityCollection(); string str = sr.ReadLine(); while (!str.Equals("End init position")) { if (str.Contains("Team")) { Team team = readTeam(Int32.Parse(str.Split(' ').ElementAt(1)), field); team.InitForReplay(gameCollection); gameCollection.addToCollection(team); } str = sr.ReadLine(); } return gameCollection; }
internal List<ReplayStep> ReadGame(GameField field) { List<ReplayStep> replaySteps = new List<ReplayStep>(); String str = sr.ReadLine(); int k = 1; while (!str.Contains("End game")) { replaySteps.Add(ReadStep(field, k++)); str = sr.ReadLine(); } return replaySteps; }
internal EntityCollection ReadCollection(GameField field) { String str = sr.ReadLine(); EntityCollection collection = new EntityCollection(); while (!str.Contains("End collection")) { if (str.Contains("Sunk submarine")) collection.addToCollection(ReadSunkSubmarine(field)); if (str.Contains("Marker")) { Marker marker = ReadMarker(field); marker.Parent = collection; collection.addToCollection(marker); } if (str.Contains("Team")) { Team team = readTeam(Int32.Parse(str.Split(' ').ElementAt(1)), field); team.InitForReplay(collection); collection.addToCollection(team); } str = sr.ReadLine(); } return collection; }
public void reset() { gameField = new GameField(); }