private void Recursive(Cell c, ref List <Cell> visitedCells) { List <Direction> _allDirections = new List <Direction> { Direction.Up, Direction.Down, Direction.Right, Direction.Left }; foreach (Direction d in _allDirections) { RobotMove move = this.MoveRobot(0, d).FirstOrDefault(); if (move == null) { continue; } if (visitedCells.Contains(move.EndingCell)) { //We've already moved to this exact cell, no recursion necessary. this.UndoMove(); } else { //Recursively move in each direction at this new cell. visitedCells.Add(move.EndingCell); Recursive(move.EndingCell, ref visitedCells); this.UndoMove(); } } }
public void UndoMove() { int index = this.WinningDestinations.IndexOf(this.CurrentWinningDestination); //Start of game, nothing to undo. if (index == 0 && this.CurrentWinningDestination.MoveHistory.Count == 0) { return; } if (this.CurrentWinningDestination.MoveHistory.Count == 0) { this.CurrentWinningDestination.CurrentWinningCell = false; this.CurrentWinningDestination.RobotID = -1; this.CurrentWinningDestination = this.WinningDestinations[index - 1]; this.CurrentWinningDestination.CurrentWinningCell = true; } //Pop a move from the history RobotMove move = this.CurrentWinningDestination.MoveHistory.Pop(); this.CurrentWinningDestination.PoppedHistory.Push(move); //Update the Robot position this.UpdateRobotPosition(move.RobotId, move.EndingCell, move.StartingCell); }
public Map DoMove(RobotMove robotMove, Map map) { if (map.State != CheckResult.Nothing) return map; var resMap = map.Move(robotMove); AddMove(robotMove); UpdateMap(resMap); return resMap; }
public void MoveRobot(int idRobot, RobotMove move) { var robotToMove = _battleArena.GetRobotById(idRobot); if (robotToMove != null) { robotToMove.PerformBattleMove(move); } }
// Use this for initialization void Start() { Robot = this.gameObject; RobotRb = this.GetComponent <Rigidbody>(); DefaultPos = Robot.transform.position; Move = gameObject.AddComponent <RobotMove>(); Move.Propaty(Robot, scale, rotateSpeed, MoveDeadTime, true, SpeedCurve); RobotLevel = PlayerPrefs.GetInt("Robot_Level", 2); Debug.Log("Robot_Level=" + RobotLevel); colliderDistance = 0.6f * 5f * Robot.transform.localScale.x; }
private RobotMove MoveRobotHelper(int robot, Direction d) { RobotMove move = null; Cell initialLoc = this.RobotCurrentLocations[robot]; Cell currentLoc = initialLoc; Cell nextLoc = null; if (initialLoc.Walls.HasFlag(GetCellWallFromDirection(d)) || OnEdgeOfBoard(initialLoc, d)) { return(null); } Direction temp = d; while (move == null) { if (OnEdgeOfBoard(currentLoc, temp)) { move = new RobotMove(robot, initialLoc, currentLoc, d); continue; } nextLoc = GetAdjacentCell(currentLoc, temp); if (nextLoc.Walls.HasFlag(GetCellWallFromDirection(GetOppositeDirection(temp))) || nextLoc.RobotID != -1) { move = new RobotMove(robot, initialLoc, currentLoc, d); continue; } SetRobotPath(robot, currentLoc, nextLoc, temp); if (nextLoc.Deflector != null && nextLoc.Deflector.RobotID != robot) { temp = nextLoc.Deflector.GetNewDirection(temp); } if (nextLoc.Portal != null && nextLoc.Portal.RobotID != robot) { nextLoc = nextLoc.Portal.Exit; } currentLoc = nextLoc; continue; } //Update the Robot position this.UpdateRobotPosition(robot, initialLoc, currentLoc); this.CurrentWinningDestination.MoveHistory.Push(move); return(move); }
public void PerformBattleMove(RobotMove move) { switch (move) { case RobotMove.L: Position.Heading = _compass.GetNextLeftPoint(Position.Heading); break; case RobotMove.R: Position.Heading = _compass.GetNextRightPoint(Position.Heading); break; case RobotMove.M: UpdateLocationAfterForwardMove(); break; } }
public List <RobotMove> MoveRobot(int robot, params Direction[] directions) { List <RobotMove> moves = new List <RobotMove>(); foreach (Direction d in directions) { RobotMove move = MoveRobotHelper(robot, d); if (move != null) { moves.Add(move); } } if (AutoSetNextWinningDestination) { SetNextWinningDestination(); } return(moves); }
public void TEST2() { Position position = new Position() { X = 3, Y = 3, Direction = DirectionEnum.E }; var maksimumRobotAxis = new List <int>() { 5, 5 }; var RobotMoves = "LMLMLMLMM"; RobotMove robotMove = new RobotMove(); robotMove.RobotMoving(maksimumRobotAxis, RobotMoves, position); var Output = $"{position.X} {position.Y} {position.Direction.ToString()}"; var expectedOutput = "5 1 E"; Assert.AreEqual(expectedOutput, Output); }
public WaveCell(Vector pos, int stepNumber, WaveCell prevCell, RobotMove move, int waterproofLeft, int razorsLeft) { Pos = pos; StepNumber = stepNumber; PrevCell = prevCell; Move = move; WaterproofLeft = waterproofLeft; RazorsLeft = razorsLeft; }
public ReferenceTestItem(string mapName, string filename, RobotMove[] moves, int score, CheckResult result, string finalMapState) { MapName = mapName; Filename = filename; Moves = moves; Score = score; Result = result; FinalMapState = finalMapState; }
static void readUserInput() { while (true) { ConsoleKeyInfo pressedKey = Console.ReadKey(); if (pressedKey.Key == ConsoleKey.Spacebar) { Console.Clear(); foreach (Replica replica in replicas) { MovingRobotIntoMatrixStateMachine.DisplayMatrix(replica.RoleState.MessageSender.UniqueId, (replica.RoleState as ReplicaState).StateMachine as MovingRobotIntoMatrixStateMachine); } } //We will use two different client/threads to simulate simultaneous command send //use LEFT/RIGHT/UP/DOWN for thread 2, and "1234" for thread 1 else if (pressedKey.KeyChar >= '1' && pressedKey.KeyChar <= '4') { RobotMove move = RobotMove.Down; if (pressedKey.KeyChar == '1') { move = RobotMove.Left; } else if (pressedKey.KeyChar == '2') { move = RobotMove.Up; } else if (pressedKey.KeyChar == '3') { move = RobotMove.Right; } MoveRobotCommand command = new MoveRobotCommand(); command.Move = move; Thread thread = new Thread(() => { sendPaxosRequest(command, 0); }); thread.Start(); } else { RobotMove move = RobotMove.Down; if (pressedKey.Key == ConsoleKey.LeftArrow) { move = RobotMove.Left; } else if (pressedKey.Key == ConsoleKey.UpArrow) { move = RobotMove.Up; } else if (pressedKey.Key == ConsoleKey.RightArrow) { move = RobotMove.Right; } MoveRobotCommand command = new MoveRobotCommand(); command.Move = move; Thread thread = new Thread(() => { sendPaxosRequest(command, 1); }); thread.Start(); } } }
public Map Move(RobotMove move) { var newMap = Clone(); if (move == RobotMove.Abort) { newMap.State = CheckResult.Abort; return newMap; } newMap.MovesCount++; if (move == RobotMove.CutBeard) CutBeard(newMap); else if (move != RobotMove.Wait) { var newRobot = Robot.Add(move.ToVector()); if (CheckValid(newRobot.X, newRobot.Y)) DoMove(newRobot.X, newRobot.Y, newMap); } if (newMap.State != CheckResult.Win) Update(newMap); return newMap; }
private void Recursive(Node prev) { //Don't attempt to find any paths that are longer than the fastest path discovered so far. if (_fastestWin != -1 && prev.Depth >= _fastestWin || prev.Depth > 30) { return; } Direction prevDirection = Direction.Up; if (prev.Data.Move != null) { prevDirection = prev.Data.Move.Direction; } foreach (int i in _robotsByPriority) { foreach (Direction d in _allDirections) { _numberOfNodesEvaluated++; RobotMove move = _model.MoveRobot(i, d).FirstOrDefault(); if (move == null) { continue; //an Invalid move not worth saving } NodeData nodeData = new NodeData(move, new Dictionary <int, Cell>(_model.RobotCurrentLocations)); Node next = new Node(nodeData, prev.Depth + 1, prev); prev.Next.Add(next); //Check for a repeating position in the tree. Repeated positions do not need to do any more recursive calls. //If the current depth is quicker than the repeated positions depth, // we should use this path to the position. Rebalance the tree + update depths. Node repeatedNode = null; bool shouldRecurse = false; if (_tree.ContainsKey(next.GetIndex())) { repeatedNode = _tree[next.GetIndex()]; } if (repeatedNode != null) { if (repeatedNode.Depth > next.Depth) { SwapRepeatingNode(repeatedNode, next); } } else { //Eww refactor this shouldRecurse = true; _tree.Add(next.GetIndex(), next); } //This move found a solution if (_model.CurrentWinningDestination.X != _winningDestination.X || _model.CurrentWinningDestination.Y != _winningDestination.Y) { if (_fastestWin == -1) { _fastestWin = next.Depth; } if (_fastestWin > next.Depth) { _fastestWin = next.Depth; } _winningNodes.Add(next); _model.UndoMove(); return; } if (shouldRecurse) { Recursive(next); } //Before trying other paths from this node, Undo the current move. _model.UndoMove(); } } }
public bool IsSafe(string mapName, string from, RobotMove move, int movesDone, int waterproofLeft) { Map map = WellKnownMaps.LoadMap(mapName); return map.IsSafeMove(from, ((Vector)from).Add(move.ToVector()), movesDone, waterproofLeft); }
private void Start() { Trans = gameObject.AddComponent <RobotMove>(); Trans.Propaty(View, scale, rotateSpeed, MoveDeadTime, false, SpeedCurve); }
private void AddMove(RobotMove move) { if (OnMoveAdded != null) OnMoveAdded(move); }
private bool MoveChangeMapSignificantly(Map map, RobotMove move) { // return true; return map.HasActiveObjects || map.RocksFallAfterMoveTo(map.Robot.Add(move.ToVector())); }
private void Recursive(int depth) { Console.WriteLine(String.Format("Beginning Depth: {0}", depth)); Dictionary <int, Node> nodesToAdd = new Dictionary <int, Node>(); //Process all nodes at this depth foreach (Node prev in _tree.Where(t => t.Value.Depth == depth).Select(t => t.Value)) { //Console.WriteLine(String.Format("Starting Node: {0}", prev.ToString())); SetRobotCurrentLocations(prev); foreach (int i in _robotsByPriority) { foreach (Direction d in _allDirections) { _numberOfNodesEvaluated++; RobotMove move = _model.MoveRobot(i, d).FirstOrDefault(); if (move == null) { continue; //an Invalid move not worth saving } Console.WriteLine(move.ToString()); NodeData nodeData = new NodeData(move, new Dictionary <int, Cell>(_model.RobotCurrentLocations)); Node next = new Node(nodeData, depth + 1, prev); prev.Next.Add(next); //This move found a solution, we're done processing. if (_model.CurrentWinningDestination.X != _winningDestination.X || _model.CurrentWinningDestination.Y != _winningDestination.Y) { Console.WriteLine("Solution Found with this move."); _winningNode = next; _model.UndoMove(); return; } //Check for a repeating position in the tree. Any repeated position does not need to be evaluated. //It either occurs in the same number of moves, or fewer, since this is a breadth first algorithm. if (!_tree.ContainsKey(next.GetIndex()) && !nodesToAdd.ContainsKey(next.GetIndex())) { nodesToAdd.Add(next.GetIndex(), next); } _model.UndoMove(); } } } //If we didn't add any new nodes, we've searched everything. if (nodesToAdd.Count == 0) { return; } _tree = _tree.Concat(nodesToAdd).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); if (_winningNode == null && depth < 12) //depths >13 will not be explored by BreadthFirst. { Recursive(depth + 1); } }