-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
111 lines (102 loc) · 3.94 KB
/
Program.cs
File metadata and controls
111 lines (102 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Runtime.InteropServices.Marshalling;
public class CodeNameGenerator
{
private static readonly string[] Adjectives;
private static readonly string[] Nouns;
private static readonly string[] Animals;
private static readonly Random Random = new Random();
static CodeNameGenerator()
{
try
{
Adjectives = LoadEmbeddedResource("en-adj.txt");
Animals = LoadEmbeddedResource("en-animal.txt");
Nouns = LoadEmbeddedResource("en-noun.txt");
if (Adjectives.Length == 0 || Nouns.Length == 0)
{
throw new InvalidOperationException("Word files are empty");
}
}
catch (Exception ex)
{
throw new InvalidOperationException(
"Failed to load word files. Make sure en-adj.txt and en-noun.txt " +
"are set as Embedded Resources in your project.", ex);
}
}
private static string[] LoadEmbeddedResource(string fileName)
{
var assembly = Assembly.GetExecutingAssembly();
var allResources = assembly.GetManifestResourceNames();
foreach (var res in allResources)
{
// Console.WriteLine($" {res}");
}
// Look for resources that contain the filename (more flexible)
var resourceName = allResources
.FirstOrDefault(name => name.Contains(fileName));
if (resourceName == null)
{
throw new FileNotFoundException($"Embedded resource containing '{fileName}' not found. Available resources: {string.Join(", ", allResources)}");
}
using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
{
throw new FileNotFoundException($"Could not load stream for resource: {resourceName}");
}
using var reader = new StreamReader(stream);
return reader.ReadToEnd()
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Trim())
.Where(line => !string.IsNullOrEmpty(line))
.ToArray();
}
/*
public static string GenerateCodeName()
{
var adjective = Adjectives[Random.Next(Adjectives.Length)];
var noun = Nouns[Random.Next(Nouns.Length)];
return $"{adjective}-{noun}";
}
*/
public static void Main(string[] args)
{
try
{
Console.WriteLine("Animal Or Noun (1 for Animal, 2 for Noun):");
var input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input))
{
throw new ArgumentException("Input cannot be empty");
}
if (input.Equals("1", StringComparison.OrdinalIgnoreCase))
{
// Generate an animal-themed code name
var adjective = Adjectives[Random.Next(Adjectives.Length)];
var animalNoun = Animals[Random.Next(Animals.Length)];
Console.WriteLine($"{adjective}-{animalNoun}");
}
else if (input.Equals("2", StringComparison.OrdinalIgnoreCase))
{
// Generate a noun-themed code name
var nounAdjective = Adjectives[Random.Next(Adjectives.Length)];
var nounNoun = Nouns[Random.Next(Nouns.Length)];
Console.WriteLine($"{nounAdjective}-{nounNoun}");
}
else
{
throw new ArgumentException("Input must be 'animal' or 'noun'");
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine($"Error generating code name: {ex.Message}");
}
}
}