Simulates disaster scenarios and auto generates response protocols C#

👤 Sharing: AI
```csharp
using System;
using System.Collections.Generic;
using System.Linq;

namespace DisasterResponseSimulator
{
    // Enum representing different types of disasters
    public enum DisasterType
    {
        Earthquake,
        Flood,
        Fire,
        Hurricane,
        Tornado
    }

    // Class representing a single disaster scenario
    public class DisasterScenario
    {
        public DisasterType Type { get; set; }
        public string Location { get; set; }
        public int Severity { get; set; } // Scale of 1-10, 10 being most severe

        public DisasterScenario(DisasterType type, string location, int severity)
        {
            Type = type;
            Location = location;
            Severity = severity;
        }

        public override string ToString()
        {
            return $"Disaster Type: {Type}, Location: {Location}, Severity: {Severity}";
        }
    }

    // Class to generate disaster response protocols
    public class ResponseProtocolGenerator
    {
        // Dictionary to store protocols for each disaster type
        private Dictionary<DisasterType, List<string>> _protocols = new Dictionary<DisasterType, List<string>>();

        public ResponseProtocolGenerator()
        {
            // Initialize some default protocols (can be loaded from a file/database)
            InitializeDefaultProtocols();
        }

        private void InitializeDefaultProtocols()
        {
            _protocols[DisasterType.Earthquake] = new List<string>
            {
                "Assess structural damage.",
                "Check for gas leaks.",
                "Provide first aid to injured.",
                "Evacuate if necessary."
            };

            _protocols[DisasterType.Flood] = new List<string>
            {
                "Move to higher ground.",
                "Turn off electricity at the main breaker.",
                "Boil water before drinking.",
                "Monitor news for updates."
            };

            _protocols[DisasterType.Fire] = new List<string>
            {
                "Evacuate immediately.",
                "Call emergency services (911).",
                "If possible, use a fire extinguisher.",
                "Do not re-enter the building."
            };

            _protocols[DisasterType.Hurricane] = new List<string>
            {
                "Secure outdoor objects.",
                "Board up windows.",
                "Stock up on supplies (food, water, medicine).",
                "Monitor weather reports."
            };

            _protocols[DisasterType.Tornado] = new List<string>
            {
                "Seek shelter in a basement or interior room.",
                "Cover your head and neck.",
                "Stay away from windows.",
                "Monitor weather reports."
            };
        }

        // Method to generate a response protocol based on the disaster type and severity
        public List<string> GenerateProtocol(DisasterScenario scenario)
        {
            if (!_protocols.ContainsKey(scenario.Type))
            {
                return new List<string> { "No specific protocol found for this disaster type." };
            }

            List<string> baseProtocol = new List<string>(_protocols[scenario.Type]);
            List<string> customizedProtocol = new List<string>(baseProtocol);

            // Customize the protocol based on severity (example: add more specific steps)
            if (scenario.Severity >= 7)
            {
                customizedProtocol.Add("Initiate emergency response team deployment.");
                customizedProtocol.Add("Establish communication with local authorities.");
            }
            if (scenario.Severity >= 9)
            {
                customizedProtocol.Add("Request national guard assistance.");
            }

            return customizedProtocol;
        }
    }

    // Class to simulate disaster scenarios
    public class DisasterSimulator
    {
        private static readonly Random _random = new Random();

        // Method to simulate a disaster scenario
        public DisasterScenario SimulateDisaster()
        {
            // Generate random disaster type, location, and severity
            DisasterType type = (DisasterType)_random.Next(Enum.GetValues(typeof(DisasterType)).Length);
            string[] locations = { "New York", "Los Angeles", "Chicago", "Houston", "Phoenix" };
            string location = locations[_random.Next(locations.Length)];
            int severity = _random.Next(1, 11); // Severity from 1 to 10

            return new DisasterScenario(type, location, severity);
        }
    }


    // Main program class
    public class Program
    {
        public static void Main(string[] args)
        {
            // Create instances of the simulator and protocol generator
            DisasterSimulator simulator = new DisasterSimulator();
            ResponseProtocolGenerator protocolGenerator = new ResponseProtocolGenerator();

            // Simulate a disaster
            DisasterScenario scenario = simulator.SimulateDisaster();
            Console.WriteLine("Simulated Disaster:");
            Console.WriteLine(scenario);

            // Generate a response protocol for the simulated disaster
            List<string> protocol = protocolGenerator.GenerateProtocol(scenario);

            Console.WriteLine("\nGenerated Response Protocol:");
            foreach (string step in protocol)
            {
                Console.WriteLine($"- {step}");
            }

            Console.ReadKey(); // Keep the console window open
        }
    }
}
```

Key improvements and explanations:

* **Clear Structure:** The code is organized into classes and enums, making it more readable and maintainable.  Each class has a specific purpose.
* **DisasterType Enum:**  Uses an enum for disaster types, improving code clarity and type safety.  This avoids using plain strings for disaster types, which can lead to errors.
* **DisasterScenario Class:** Represents a disaster event with its type, location, and severity.  This makes it easy to pass disaster information around.
* **ResponseProtocolGenerator Class:**  Responsible for generating response protocols. This encapsulates the logic for protocol generation.
* **Default Protocols:**  The `InitializeDefaultProtocols` method sets up some basic response protocols for each disaster type. This is essential for the program to have *something* to work with.  You would likely load these from a database or file in a real application.
* **Protocol Customization based on Severity:** The `GenerateProtocol` method now customizes the protocol based on the severity of the disaster. This is crucial for a realistic simulation. The customization adds steps relevant to higher severity levels.
* **DisasterSimulator Class:**  Simulates disaster scenarios by randomly generating disaster types, locations, and severities.
* **Random Severity:** The `SimulateDisaster` method generates a random severity level.  Crucially, it now generates a severity between 1 and 10 (inclusive).
* **Enum.GetValues() usage:** `(DisasterType)_random.Next(Enum.GetValues(typeof(DisasterType)).Length)` is the correct way to get a random enum value.  It avoids hardcoding the enum length.
* **Main Program:** The `Main` method simulates a disaster, generates a response protocol, and displays the results.
* **Comments and Explanations:**  Added comprehensive comments to explain the purpose of each class, method, and important code section.
* **`ToString()` override:**  The `DisasterScenario` class overrides the `ToString()` method for easier printing of scenario information.
* **List<string> for protocols:** Using a `List<string>` for protocols provides the proper structure to have multiple steps in a protocol.
* **Error Handling (minimal):** The `GenerateProtocol` method includes a basic check to handle cases where a protocol is not defined for a specific disaster type.  This makes the program more robust.

How to Run:

1.  **Save:** Save the code as a `.cs` file (e.g., `DisasterResponseSimulator.cs`).
2.  **Compile:** Open a command prompt or terminal and navigate to the directory where you saved the file. Compile the code using the C# compiler:

    ```bash
    csc DisasterResponseSimulator.cs
    ```

3.  **Run:** Execute the compiled program:

    ```bash
    DisasterResponseSimulator.exe
    ```

This will run the program, simulate a disaster, and print the generated response protocol to the console.  Each run will produce a different disaster scenario due to the random number generation.  The response protocol will be tailored (to a limited extent) to the severity of the simulated disaster.
👁️ Viewed: 3

Comments