Exemple #1
0
        private string[] translateMoves(mazePos[] moves)
        {
            mazePos temp = new mazePos();
            bool changed = true;

            while (changed)
            {
                changed = false;

                for (int x = 0; x < moves.Length - 1; x++)
                {
                    if (moves[x].time > moves[x + 1].time)
                    {
                        temp = moves[x + 1];
                        moves[x + 1] = moves[x];
                        moves[x] = temp;

                        changed = true;
                    }
                }
            }

            // Convert the "moves" into "commands"
            //string currentHeading = "west";     // IDEA: Have it assume that the first move is already the direction it is heading?  Can use this to get its bearings.
            string newHeading = "";
            List<string> commands = new List<string>();

            for(int x = 0; x < moves.Length - 1; x++)
            {

                // Find in which direction it is moving
                if (moves[x].x == moves[x+1].x)
                {
                    // Moving horizontally (x is constant --> y is changing)
                    if (moves[x + 1].y < moves[x].y)
                        newHeading = "north";
                    else
                        newHeading = "south";

                }
                else
                {
                    // Moving vertically (y is constant --> x is changing)
                    if (moves[x + 1].x < moves[x].x)
                        newHeading = "west";
                    else
                        newHeading = "east";

                }

                // Find what command(s) is needed to carry out this movement
                if(newHeading == currentHeading)
                    commands.Add("forward");
                    // currentHeading = "forward";
                else
                {
                    int currentH = headingToInt(currentHeading);
                    int newH = headingToInt(newHeading);

                    if(currentH == 0 && newH == 3)
                    {
                        commands.Add("turnLeft90");
                        commands.Add("forward");
                        currentHeading = intHeadingToString(3);
                    }
                    else if(currentH == 3 && newH == 0)
                    {
                        commands.Add("turnRight90");
                        commands.Add("forward");
                        currentHeading = intHeadingToString(0);
                    }
                    else if(currentH < newH)
                    {
                        commands.Add("turnRight90");
                        commands.Add("forward");
                        currentHeading = intHeadingToString(newH);
                    }
                    else // newH < currentH
                    {
                        commands.Add("turnLeft90");
                        commands.Add("forward");
                        currentHeading = intHeadingToString(newH);
                    }
                }

            }

            string[] results = new string[commands.Count];
            for(int x = 0; x < commands.Count; x++)
            {
                results[x] = commands[x];
            }

            return results;
        }
Exemple #2
0
        private mazePos[] parseResults(string moves)
        {
            string[] parsedMoves;

            parsedMoves = moves.Split('\r', '\n');

            string listOfMoves = "";
            for (int x = 0; x < parsedMoves.Length; x++)
            {
                if (parsedMoves[x].IndexOf("move") != -1)
                    listOfMoves = parsedMoves[x].Trim();
            }

            string[] allMoves = listOfMoves.Split(' ');

            //mazePos[] eachMove = new mazePos[(listOfMoves.Length - 4)/11];  // "\r\n" is 4 chars and each "move(x,y,z)" is 11 chars.
            //mazePos[] eachMove = new mazePos[(listOfMoves.Length + 1) / 12];  // each "move(x,y,z)" is 12 chars.  Added 1 more because there is no space at the end for the last move(...).
            int numOfMoves = 1;  // Start at 1 because the last move does not have a space after it.
            for(int x = 0; x < listOfMoves.Length; x++)
            {
                if(listOfMoves[x] == ' ')
                    numOfMoves++;
            }
            mazePos[] eachMove = new mazePos[numOfMoves];
            int numProcessed = 0;
            foreach (string moveString in allMoves)
            {
                string parsedMove = moveString.Substring(5);
                parsedMove = parsedMove.TrimEnd(')');

                string[] moveComponents = parsedMove.Split(',');
                eachMove[numProcessed] = new mazePos();
                eachMove[numProcessed].x = Int32.Parse(moveComponents[0]);
                eachMove[numProcessed].y = Int32.Parse(moveComponents[1]);
                eachMove[numProcessed].time = Int32.Parse(moveComponents[2]);
                numProcessed++;
            }

            return eachMove;
        }