예제 #1
0
        // Plays the game based on the contents of the settings and moves files.
        static void PlayGame()
        {
            MineBoard board = new MineBoard();

            if (!board.LoadSettings())
            {
                return;
            }

            if (!board.LoadMoves())
            {
                return;
            }

            int       turtleStartX = board.Settings.startPosition.Item1;
            int       turtleStartY = board.Settings.startPosition.Item2;
            Direction startDirection;

            if (!Enum.TryParse(board.Settings.startDirection, out startDirection))
            {
                Console.WriteLine("Error parsing start direction!");
                return;
            }

            // Create the turtle.
            Turtle turtle = new Turtle(turtleStartX, turtleStartY, startDirection);

            //turtle.Direction = startDirection;

            Console.WriteLine("Start Game - Turtle Position: " + turtle.GetTurtleUpdate());

            // Loop through the moves.
            foreach (char move in board.Moves.moveString.ToLower())
            {
                switch (move)
                {
                case 'm':
                {
                    turtle.Move();
                    Console.WriteLine("Move");
                    break;
                }

                case 'r':
                {
                    turtle.Rotate();
                    Console.WriteLine("Rotate");
                    break;
                }

                default:
                {
                    Console.WriteLine("Error - Illegal move!");
                    return;
                }
                }

                Console.WriteLine("Turtle Position: " + turtle.GetTurtleUpdate() + System.Environment.NewLine);

                // If NOT Still_In_Danger the game must be over.
                if (board.CheckStatus(turtle) != Still_In_Danger)
                {
                    break;
                }
            }

            Console.WriteLine("Game Over - Turtle's Status: " + board.CheckStatus(turtle).ToString());
        }
예제 #2
0
        // Execute a number of tests consisting of move strings and expected results.
        static void TestGame()
        {
            /*
             * BOARD LAYOUT (T (Turtle) facing North)
             *
             * 00000
             * TX000
             * 00XXE
             * 00000
             */

            Settings testSettings = new Settings();

            testSettings.columns        = 5;
            testSettings.rows           = 4;
            testSettings.startPosition  = new Tuple <int, int>(0, 1);
            testSettings.startDirection = North.ToString();
            testSettings.exitPosition   = new Tuple <int, int>(4, 2);
            testSettings.mineLocations  = new List <Tuple <int, int> > {
                new Tuple <int, int>(1, 1), new Tuple <int, int>(2, 2), new Tuple <int, int>(3, 2)
            };

            Tuple <Status, string>[] testValues =
            {
                Tuple.Create(Safe, "mrmmmmrmm"),            // Turtle successfully makes it to the Exit.
                Tuple.Create(Safe, "rrmmrrrmmmmrrrm"),      // Turtle successfully makes it to the Exit.
                Tuple.Create(Out_Of_Bounds, "mrmmmmm"),     // Turtle wanders out of bounds.
                Tuple.Create(Still_In_Danger, "mrmm"),      // Turtle still in danger.
                Tuple.Create(Mine_Hit, "mrmrm"),            // Turtle hits a mine.
                Tuple.Create(Mine_Hit, "rrmrrrmm"),         // Turtle hits a mine.
                Tuple.Create(Safe, "mrmmrmrrrmmrmmrmrmm")   // Turtle successfully makes it to the Exit.
                                                            //Note: A number of moves are skipped at the end as the Turtle has already escaped.
            };

            MineBoard board = new MineBoard();

            board.Settings = testSettings;

            int       turtleStartX = board.Settings.startPosition.Item1;
            int       turtleStartY = board.Settings.startPosition.Item2;
            Direction startDirection;

            if (!Enum.TryParse(board.Settings.startDirection, out startDirection))
            {
                Console.WriteLine("Error parsing start direction!");
                return;
            }

            Console.WriteLine("Unit Tests:");

            // Loop through unit tests
            for (int iii = 0; iii < testValues.Length; iii++)
            {
                Status expectedResult = testValues[iii].Item1;
                board.Moves = new Moves(testValues[iii].Item2);

                // Create the turtle
                Turtle turtle = new Turtle(turtleStartX, turtleStartY, startDirection);
                //turtle.Direction = startDirection;

                // Loop through the moves
                foreach (char move in board.Moves.moveString.ToLower())
                {
                    switch (move)
                    {
                    case 'm':
                    {
                        turtle.Move();
                        break;
                    }

                    case 'r':
                    {
                        turtle.Rotate();
                        break;
                    }

                    default:
                    {
                        Console.WriteLine("Error - Illegal move!");
                        return;
                    }
                    }

                    if (board.CheckStatus(turtle) != Still_In_Danger)
                    {
                        break;
                    }
                }

                Status endStatus = board.CheckStatus(turtle);

                Console.Write("Unit Test " + (iii + 1).ToString());

                if (expectedResult == endStatus)
                {
                    Console.Write(" passed!");
                }
                else
                {
                    Console.Write(" failed!");
                }

                Console.Write(System.Environment.NewLine);
            }

            Console.WriteLine("----------------------" + System.Environment.NewLine + System.Environment.NewLine);
        }