Example #1
0
        public RepairBot(string inData, string outFile)
        {
            string curMapFile = "BotMap.txt";
            string rootPath   = Path.GetDirectoryName(outFile);

            curMapFile         = Path.Combine(rootPath, curMapFile);
            sw                 = new StreamWriter(outFile);
            xSize              = 42;
            ySize              = 42;
            startNode          = new ExploreNode();
            startNode.nodeType = RobotResultCode.MOVE_SUCCESFUL;
            curNode            = startNode;
            curMap             = new ExploreNode[xSize * ySize];
            curPos.x           = xSize / 2;
            curPos.y           = ySize / 2;
            int startIndex = Helpers.GetIndexFromCoordinate(curPos, xSize);

            curMap[startIndex] = startNode;
            curComputer        = new IntComputer();
            curComputer.InitializeMemory(inData);
            curComputer.StartComputer(true);
            RunRobot();

            //DrawMap();
            int endIndex = Helpers.GetIndexFromCoordinate(curPos, xSize);
            int length   = SearchTree(startIndex, endIndex);

            sw.Close();
        }
Example #2
0
        public void InitializeExploreNode(RobotResultCode curType, ExploreNode parentNode, MovementCommandEnum enterDir)
        {
            parentNode.AssignMovementNode(this, enterDir);
            MovementCommandEnum exitDir = RepairBot.GetOppositeCommand(enterDir);

            AssignMovementNode(parentNode, exitDir);
            nodeType = curType;
        }
Example #3
0
 bool IsNodeOpen(ExploreNode curNode)
 {
     if (curNode != null)
     {
         if (curNode.nodeType != RobotResultCode.WALL)
         {
             if (curNode.nodeType != RobotResultCode.OXYGENATED)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Example #4
0
        void ProcessRobotResult(RobotResultCode resultCode)
        {
            Vector2 posToUpdate = GetNewPosition(curPos, curDir);

            if ((resultCode == RobotResultCode.MOVE_SUCCESFUL) || (resultCode == RobotResultCode.MOVE_SUCCESFUL_OXYGEN))
            {
                // do nothing
                // found a new node
                int curIndex = Helpers.GetIndexFromCoordinate(posToUpdate, xSize);
                if (curMap[curIndex] == null)
                {
                    // ALL NEW NEW NEW!
                    ExploreNode newNode = new ExploreNode();

                    curMap[curIndex] = newNode;
                }
                curMap[curIndex].InitializeExploreNode(resultCode, curNode, curDir);
                // otherwise we've already been here.
                curNode = curMap[curIndex];
                if (!isBackTracking)
                {
                    fromStartCommands.Push(curDir);
                }
                curPos = posToUpdate;
                return;
            }
            else if (resultCode == RobotResultCode.WALL)
            {
                if (isBackTracking)
                {
                    // errro
                    int t = 9;
                }
                // make a new node, but don't move. Go backwards one.
                int curIndex = Helpers.GetIndexFromCoordinate(posToUpdate, xSize);
                if (curMap[curIndex] == null)
                {
                    // ALL NEW NEW NEW!
                    ExploreNode newNode = new ExploreNode();
                    curMap[curIndex] = newNode;
                }
                curMap[curIndex].InitializeExploreNode(RobotResultCode.WALL, curNode, curDir);
                // otherwise we've already been here.
                // curpos does not change
                return;
            }
        }
Example #5
0
        public int SearchTree(int startIndex, int endIndex)
        {
            ExploreNode startNode = curMap[oxygenIndex];


            HashSet <ExploreNode> oxygenatedNodes = new HashSet <ExploreNode>();


            Queue <ExploreNode> nextNodes        = new Queue <ExploreNode>();
            Queue <ExploreNode> nodesToOxygenate = new Queue <ExploreNode>();

            bool isComplete = false;
            int  minutes    = 0;

            startNode.nodeType = RobotResultCode.OXYGENATED;
            nodesToOxygenate.Enqueue(startNode);

            while (!isComplete)
            {
                minutes++;
                while (nodesToOxygenate.Count > 0)
                {
                    ExploreNode        curNode   = nodesToOxygenate.Dequeue();
                    List <ExploreNode> openNodes = curNode.GetOpenNodes();
                    foreach (ExploreNode openNode in openNodes)
                    {
                        if (!oxygenatedNodes.Contains(openNode))
                        {
                            nextNodes.Enqueue(openNode);
                            oxygenatedNodes.Add(openNode);
                            openNode.nodeType = RobotResultCode.OXYGENATED;
                        }
                    }
                }
                nodesToOxygenate = new Queue <ExploreNode>(nextNodes); // DURR
                if (nextNodes.Count == 0)
                {
                    int t = 90;
                    return(-1);
                }
                nextNodes.Clear();
                sw.WriteLine("-----------------MINUTE " + minutes + "-----------------------");
                IsOxygenated();
                DrawMap();
            }
            return(-1);
        }
Example #6
0
        void AssignMovementNode(ExploreNode newNode, MovementCommandEnum curDir)
        {
            if (curDir == MovementCommandEnum.NORTH)
            {
                northNode = newNode;
            }
            if (curDir == MovementCommandEnum.SOUTH)
            {
                southNode = newNode;
            }

            if (curDir == MovementCommandEnum.EAST)
            {
                eastNode = newNode;
            }
            if (curDir == MovementCommandEnum.WEST)
            {
                westNode = newNode;
            }
        }