예제 #1
0
        // prints a visual display of the lander's height
        public void PrintLocation(MarsLander ml)
        {
            int height = ml.GetHeight();

            // used the modulus operator to detect if the height was evenly divisible by 100
            // if not, it subtracts the remainder
            if ((height % 100) != 0)
            {
                height -= (height % 100);
            }

            Console.WriteLine();
            for (int i = 1000; i > height; i -= 100)
            {
                Console.WriteLine("{0}m:", i);
            }

            // manually building the line with the asterisk
            Console.WriteLine("{0}m: *", height);

            // using a for loop to build the rest
            for (int i = (height - 100); i >= 0; i -= 100)
            {
                Console.WriteLine("{0}m:", i);
            }

            Console.WriteLine();
        }
 /// <summary>
 /// This prints out the info about the lander
 /// For example:
 ///      Exact height: 1350 meters
 ///      Current (downward) speed: -350 meters/second
 ///      Fuel points left: 0
 //// </summary>
 /// <param name="ml">MarsLander</param>
 public void PrintLanderInfo(MarsLander ml)
 {
     Console.WriteLine("Exact height: {0} meters", ml.GetCurrent().GetHeight());
     Console.WriteLine("Current (downward)speed: {0} meters / second", ml.GetCurrent().GetSpeed());
     Console.WriteLine("Fuel points left: {0}", ml.GetFuel());
     Console.WriteLine();
 }
예제 #3
0
        public void PrintLocation(MarsLander lander)
        {
            int height;

            if (lander.getHeight() <= 1000)
            {
                height = 1000;
            }
            else
            {
                height = lander.getHeight();
            }

            for (int i = 0; i < lander.getHeight(); i++)
            {
                if (height < 0)
                {
                    break;
                }
                else if (height / 100 == lander.getHeight() / 100)
                {
                    Console.WriteLine("{0}m: *", height);
                    height -= 100;
                }
                else
                {
                    Console.WriteLine("{0}m: ", height);
                    height -= 100;
                }
            }
            Console.WriteLine();
        }
예제 #4
0
 /// <summary>
 /// This will ask the user for how much fuel he/she wants to burn,
 /// verify that he/she's typed in an acceptable integer,
 ///  (repeatedly asking the user for input if needed),
 /// and will then return that number back to the main method
 /// </summary>
 /// <param name="ml">MarsLander</param>
 /// <returns>Amount of fuel to burn</returns>
 public int GetFuelToBurn(MarsLander ml)
 {
     int avail = ml.GetFuel();
     int fuel = 0;
     while (true)
     {
         Console.WriteLine("How many points of fuel would you like to burn?");
         string line = Console.ReadLine();
         if (!int.TryParse(line, out fuel))
         {
             Console.WriteLine("You need to type a whole number of fuel points to burn!");
         }
         else if (fuel < 0)
         {
             Console.WriteLine("You can't burn less than 0 points of fuel!");
         }
         else if (fuel > avail)
         {
             Console.WriteLine("You don't have {0} points of fuel!", fuel);
         }
         else
         {
             break;
         }
         Console.WriteLine();
         Console.WriteLine("Just as a reminder, here's where the lander is: ");
         PrintLanderInfo(ml);
     }
     return fuel;
 }
예제 #5
0
        //  In a nutshell, the program is made up of four major classes:
        //
        //  1. This Program class
        //      It houses main; main's job is to drive the overall logic of the game
        //
        //  2. MarsLander
        //      This stores the current height, speed, and fuel of the lander (properly encapsulated)
        //      This also has an instance of the 'MarsLanderHistory' object, and the lander is
        //      responsible for adding to the history whenever it calculates the lander's new speed
        //
        //  3. UserInterface
        //      This handles ALL interactions with the user.
        //      NO OTHER CLASS IS ALLOWED TO INTERACT WITH THE USER!!
        //
        //  3. MarsLanderHistory
        //      This manages an array of RoundInfo objects.  It will provide a method to add
        //      another round to the history of the lander (and will resize the array, if needed)
        //      It provides a Clone method so that the lander can return a copy of the
        //      history (which will prevent other classes from changing it's history)
        //      And it provides a way to get the number of rounds, and (for each round) the
        //      height and speed of that round (the UserInterface.PrintHistory method will use these)
        //          (This class uses the provided, minor RoundInfo class)
        //
        //  Substantial amounts of this program have been commented out, so that the program will compile
        //
        static void Main(string[] args)
        {
            UserInterface ui        = new UserInterface();
            MarsLander    lander    = new MarsLander();
            const int     MAX_SPEED = 10; // 10 m/s

            // const means that we can't change the value after this line

            ui.PrintGreeting();

            while (lander.GetHeight() > 0)
            {
                ui.PrintLocation(lander);
                ui.PrintLanderInfo(lander);


                int fuelToBurn = ui.GetFuelToBurn(lander);

                lander.CalculateNewSpeed(fuelToBurn);
            }


            ui.PrintLocation(lander);
            ui.PrintLanderInfo(lander);
            ui.PrintEndOfGameResult(lander, MAX_SPEED);
        }
        /// <summary>
        /// This will ask the user for how much fuel he/she wants to burn,
        /// verify that he/she's typed in an acceptable integer,
        /// (repeatedly asking the user for input if needed),
        /// and will then return that number back to the main method
        /// </summary>
        /// <param name="ml">MarsLander</param>
        /// <returns>Amount of fuel to burn</returns>
        public int GetFuelToBurn(MarsLander ml)
        {
            int avail = ml.GetFuel(); //available fuel
            int fuel  = 0;

            while (true)
            {
                Console.WriteLine("How many points of fuel would you like to burn?");
                string line = Console.ReadLine();
                if (!int.TryParse(line, out fuel))
                {
                    Console.WriteLine("You need to type a whole number of fuel points to burn!");
                }
                else if (fuel < 0)
                {
                    Console.WriteLine("You can't burn less than 0 points of fuel!");
                }
                else if (fuel > avail)
                {
                    Console.WriteLine("You don't have {0} points of fuel!", fuel);
                }
                else
                {
                    break;
                }
                Console.WriteLine();
                Console.WriteLine("Just as a reminder, here's where the lander is: ");
                PrintLanderInfo(ml);
            }
            return(fuel);
        }
        /// <summary>
        /// This will print the 'picture' of the lander
        /// for example:
        ///      1000m: *
        ///
        ///      900m:
        ///      800m:
        /// etc, etc
        /// </summary>
        ///  /// <param name="ml">MarsLander</param>
        public void PrintLocation(MarsLander ml)
        {
            // use integer math to determine which 100's block the current height is in.
            int alt = (ml.GetCurrent().GetHeight()) / 100;

            Console.WriteLine("{0}00m: *", alt);

            while (alt > 0)
            {
                alt--;
                Console.WriteLine("{0}00m:", alt);
            }
            Console.WriteLine();
        }
예제 #8
0
 /// <summary>
 /// This will only be called once the lander is on the surface of Mars, 
 ///  and will tell the player if they successly landed or if they crashed
 /// </summary>
 /// <param name="ml">MarsLander</param>
 /// <param name="maxSpeed">Maximum speed at landing</param>
 public void PrintEndOfGameResult(MarsLander ml, int maxSpeed)
 {
     if (ml.GetCurrent().GetSpeed() > maxSpeed)
     {
         Console.WriteLine("The maximum speed for a safe landing is {0}; your lander's current speed is {1}", maxSpeed, ml.GetCurrent().GetSpeed());
         Console.WriteLine("You have crashed the lander into the surface of Mars, killing everyone on board,");
         Console.WriteLine("costing NASA millions of dollars, and setting the space program back by decades!");
     }
     else
     {
         Console.WriteLine("Congratulations!! You've successfully landed your Mars Lander, without crashing!!!");
     }
     Console.WriteLine("Here's the height/speed info for you:");
     PrintHistory(ml.GetHistory());
 }
예제 #9
0
 // This will only be called once the lander is on the surface of Mars,
 //  and will tell the player if they successly landed or if they crashed
 public void PrintEndOfGameResult(MarsLander ml, int maxSpeed)
 {
     if (ml.GetSpeed() <= maxSpeed && ml.GetHeight() == 0)
     {
         Console.WriteLine("Congratulations!! You've successfully landed your Mars Lander, without crashing!!!");
     }
     else
     {
         Console.WriteLine("The maximum speed for a safe landing is 10; " +
                           $"your lander's current speed is {ml.GetSpeed()}");
         Console.WriteLine("You have crashed the lander into the surface of Mars, killing everyone on board," +
                           "costing NASA millions of dollars, and setting the space program back by decades!");
     }
     PrintHistory(ml.GetHistory(maxSpeed));
 }
 /// <summary>
 /// This will only be called once the lander is on the surface of Mars,
 /// and will tell the player if they successly landed or if they crashed
 /// </summary>
 /// <param name="ml">MarsLander</param>
 /// <param name="maxSpeed">Maximum speed at landing</param>
 public void PrintEndOfGameResult(MarsLander ml, int maxSpeed)
 {
     if (ml.GetCurrent().GetSpeed() > maxSpeed)
     {
         Console.WriteLine("The maximum speed for a safe landing is {0}; your lander's current speed is {1}", maxSpeed, ml.GetCurrent().GetSpeed());
         Console.WriteLine("You have crashed the lander into the surface of Mars, killing everyone on board,");
         Console.WriteLine("costing NASA millions of dollars, and setting the space program back by decades!");
     }
     else
     {
         Console.WriteLine("Congratulations!! You've successfully landed your Mars Lander, without crashing!!!");
     }
     Console.WriteLine("Here's the height/speed info for you:");
     PrintHistory(ml.GetHistory());
 }
예제 #11
0
        // This will ask the user for how much fuel they want to burn,
        // verify that they've type in an acceptable integer,
        //  (repeatedly asking the user for input if needed),
        // and will then return that number back to the main method
        public int GetFuelToBurn(MarsLander ml)
        {
            int  nFuel    = 0;
            bool isNum    = false;
            int  attempts = 0;

            while (true)
            {
                Console.WriteLine("\nHow many points of fuel would you like to burn?");

                // attempts to convert the string of input into the integer nFuel
                isNum = Int32.TryParse(Console.ReadLine(), out nFuel);

                // to clearly show the end of the turn
                Console.WriteLine("\n=================================================");


                // once enough input has been wrong, the user is reminded where everything is
                if (attempts == 1)
                {
                    Console.WriteLine("\n");
                    Console.WriteLine("Just as a reminder, here's where the lander is: \n");
                    PrintLanderInfo(ml);
                    Console.WriteLine();
                    attempts = 0;
                }

                if (isNum == false)
                {
                    Console.WriteLine("You need to type a whole number of fuel points to burn!\n");
                    attempts++;
                }
                else if (nFuel < 0)
                {
                    Console.WriteLine("You can't burn less than 0 points of fuel!");
                    attempts++;
                }
                else if (nFuel > ml.GetFuel())
                {
                    Console.WriteLine("You don't have {0} points of fuel!", nFuel);
                    attempts++;
                }
                else
                {
                    return(nFuel);
                }
            }
        }
예제 #12
0
        public int GetFuelToBurn(MarsLander lander)
        {
            bool   errorDone = true;
            string szInput;
            int    userNum;

            while (errorDone == true)
            {
                Console.WriteLine("How many points of fuel would you like to burn?");
                szInput = Console.ReadLine();
                Int32.TryParse(szInput, out userNum);

                if (Int32.TryParse(szInput, out userNum) == true)
                {
                    if (userNum < 0)
                    {
                        Console.WriteLine("You can't burn less than 0 points of fuel!");
                        Console.WriteLine();
                        Console.WriteLine("Just as a reminder, here's where the lander is: ");
                        PrintLanderInfo(lander);
                        continue;
                    }
                    else if (userNum > lander.getFuel())
                    {
                        Console.WriteLine("You don't have {0} points of fuel!", userNum);
                        Console.WriteLine();
                        Console.WriteLine("Just as a reminder, here's where the lander is: ");
                        PrintLanderInfo(lander);
                        continue;
                    }
                    else
                    {
                        errorDone = false;
                        return(userNum);
                    }
                }
                else
                {
                    Console.WriteLine("You need to type a whole number of fuel points to burn!");
                    Console.WriteLine();
                    Console.WriteLine("Just as a reminder, here's where the lander is: ");
                    PrintLanderInfo(lander);
                    continue;
                }
            }
            return(-1);// yeah?
        }
예제 #13
0
        // This should print the 'picture' of hte lander
        // for example:
        //      1000m: *
        //      900m:
        //      800m:
        // etc, etc
        public void PrintLocation(MarsLander m)
        {
            int i = m.GetHeight() / 100 * 100;

            if ((m.GetHeight() / 100) * 100 < 1000)
            {
                i = 1000;
            }

            for (; i >= 0; i -= 100)
            {
                if (i == (m.GetHeight() / 100) * 100)
                {
                    Console.WriteLine($"{i}m :  *  ");
                }
                else
                {
                    Console.WriteLine($"{i}m :  ");
                }
            }
        }
예제 #14
0
        // This will ask the user for how much fuel they want to burn,
        // verify that they've type in an acceptable integer,
        //  (repeatedly asking the user for input if needed),
        // and will then return that number back to the main method
        public int GetFuelToBurn(MarsLander m)
        {
            bool looper  = false;
            bool endLoop = false;

            while (endLoop == false)
            {
                if (looper == true)
                {
                    Console.WriteLine("Just as a reminder, here's where the lander is: ");
                    PrintLanderInfo(m);
                }
                looper = true;

                Console.WriteLine("How many points of fuel would you like to burn?");
                string tempString = Console.ReadLine();
                int    nFuel;

                if (!Int32.TryParse(tempString, out nFuel))
                {
                    Console.WriteLine("Please enter a valid integer");
                }
                else if (nFuel >= 0 && m.GetFuel() >= nFuel)
                {
                    return(nFuel);
                }
                else if (nFuel < 0)
                {
                    Console.WriteLine("You cannot burn less than 0 points of fuel!");
                }
                else if (m.GetFuel() < nFuel)
                {
                    Console.WriteLine($"You don't have {nFuel} points of fuel.");
                }
            }

            return(0);
        }
예제 #15
0
 public void PrintEndOfGameResult(MarsLander ml, int maxSpeed)
 {
     if (ml.GetSpeed() <= maxSpeed)
     {
         Console.WriteLine("\n=================================================");
         Console.WriteLine("                       SUCCESS");
         Console.WriteLine("=================================================\n");
         Console.WriteLine("Congratulations!! \n" +
                           "You've successfully landed your Mars Lander, without crashing!!!");
     }
     else
     {
         Console.WriteLine("\n=================================================");
         Console.WriteLine("                       FAILURE");
         Console.WriteLine("=================================================\n");
         Console.WriteLine("The maximum speed for a safe landing is {0}m/s; \n" +
                           "your lander's current speed is {1}m/s... \n" +
                           "\nYou have crashed the lander into the surface of Mars, \n" +
                           "killing everyone on board, costing NASA millions of dollars, \n" +
                           "and setting the space program back by decades! :(", maxSpeed, ml.GetSpeed());
     }
     PrintHistory(ml.GetHistory());
 }
예제 #16
0
 /// <summary>
 /// This prints out the info about the lander
 /// For example:
 ///      Exact height: 1350 meters
 ///      Current (downward) speed: -350 meters/second
 ///      Fuel points left: 0
 //// </summary>
 public void PrintLanderInfo(MarsLander ml)
 {
     Console.WriteLine("Exact height: {0} meters", ml.GetCurrent().GetHeight());
     Console.WriteLine("Current (downward)speed: {0} meters / second", ml.GetCurrent().GetSpeed());
     Console.WriteLine("Fuel points left: {0}", ml.GetFuel());
     Console.WriteLine();
 }
예제 #17
0
 // This prints out the info about the lander
 // For example:
 //      Exact height: 1350 meters
 //      Current (downward) speed: -350 meters/second
 //      Fuel points left: 0
 public void PrintLanderInfo(MarsLander m)
 {
     Console.WriteLine($"Exact height : {m.GetHeight()} meters\n" +
                       $"Current (downward) speed: {m.GetSpeed()} meters/second\n" +
                       $"Fuel points left : {m.GetFuel()}");
 }
예제 #18
0
        /// <summary>
        /// This will print the 'picture' of the lander
        /// for example:
        ///      1000m: *
        ///      900m: 
        ///      800m:
        /// etc, etc
        /// </summary>
        public void PrintLocation(MarsLander ml)
        {
            // use integer math to determine which 100's block the current height is in.
            int alt = (ml.GetCurrent().GetHeight()) / 100;
            Console.WriteLine("{0}00m: *", alt);

            while (alt > 0)
            {
                alt--;
                Console.WriteLine("{0}00m:", alt);
            }
            Console.WriteLine();
        }