Created
January 9, 2020 08:41
-
-
Save JerryNixon/6117096bbafe259370525d8ec005f625 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| namespace Adventure | |
| { | |
| internal class Program | |
| { | |
| private static Game _game; | |
| private static void Main(string[] args) | |
| { | |
| Console.Title = $"Welcome to Camelot."; | |
| _game = new Game(); | |
| Start(); | |
| } | |
| public static void Start() | |
| { | |
| _game.Reset(); | |
| Room01(); | |
| Console.Clear(); | |
| _game.Say("Your game has ended."); | |
| Console.ReadKey(); | |
| Start(); | |
| } | |
| private static void Room01() { } | |
| private static void Room02() { } | |
| private static void Room03() { } | |
| private static void Room04() { } | |
| private static void Room05() { } | |
| private static void Room06() { } | |
| private static void Room07() { } | |
| private static void Room08() { } | |
| private static void Room09() { } | |
| private static void Room10() { } | |
| private static void Room11() { } | |
| private static void Room12() { } | |
| private static void Room13() { } | |
| private static void Room14() { } | |
| private static void Room15() { } | |
| private static void Room16() { } | |
| private static void Room17() { } | |
| private static void Room18() { } | |
| private static void Room19() { } | |
| private static void Room20() { } | |
| } | |
| internal class Game | |
| { | |
| private readonly Random _random = new Random(); | |
| public bool GameOver => Life == 0; | |
| public int Life; | |
| public int Score; | |
| public void Reset() | |
| { | |
| Life = 100; | |
| Score = 0; | |
| } | |
| public void Treasure(string text, int value, ConsoleColor foreground = ConsoleColor.Green, ConsoleColor background = ConsoleColor.DarkGray) | |
| { | |
| Say(text, foreground, background); | |
| Say($"Your life has increased to {value}.", foreground, background); | |
| Life = Life + value; | |
| PrintInformation(); | |
| } | |
| public void PrintInformation(ConsoleColor foreground = ConsoleColor.White, ConsoleColor background = ConsoleColor.DarkGray) | |
| { | |
| Say($"Your life {Life}. Your score {Score}", foreground, background); | |
| } | |
| public void Doors(params Action[] actions) | |
| { | |
| Say($"{Environment.NewLine}There are {actions.Count()} doors."); | |
| var doors = Enumerable.Range(1, actions.Count()).Select(x => $"Door number {x}").ToArray(); | |
| var door = Choose("Which do you choose?", doors); | |
| Say($"You have chosen door number {(int)door}{Environment.NewLine}"); | |
| actions[(int)door - 1].Invoke(); | |
| } | |
| public bool Monster(int monster, string kind) | |
| { | |
| Console.Clear(); | |
| Say($"There is an angry {kind} in the room.", ConsoleColor.Red, ConsoleColor.White); | |
| Say($"You have {Life} life. The {kind} has {monster}.", ConsoleColor.Black, ConsoleColor.White); | |
| var answer = Choose($"Choose your weapon to attach the {kind}.", | |
| "Your dangerous fist (2 point at risk)", | |
| "Your enchanged hammer (4 points at risk)", | |
| "Your magical sword (6 points at risk)"); | |
| var victory = _random.NextDouble() >= 0.5; | |
| if (victory) | |
| { | |
| monster = Math.Max(monster - (int)answer * 2, 0); | |
| Say($"You strike the {kind}. His life is now {monster}.", ConsoleColor.Green); | |
| } | |
| else | |
| { | |
| Life = Math.Max(Life - (int)answer * 2, 0); | |
| Say($"The {kind} hits you. Your life is now {Life}.", ConsoleColor.Red); | |
| } | |
| if (monster == 0) | |
| { | |
| Say($"You have been victorious against the {kind}.", ConsoleColor.Green); | |
| Console.ReadKey(true); | |
| return true; | |
| } | |
| else if (Life == 0) | |
| { | |
| Say($"You have been defeated by the {kind}.", ConsoleColor.Red); | |
| Console.ReadKey(true); | |
| return false; | |
| } | |
| else | |
| { | |
| Console.ReadKey(true); | |
| return Monster(monster, kind); | |
| } | |
| } | |
| public bool GetRandomTrueFalse() | |
| { | |
| return _random.NextDouble() >= 0.5; | |
| } | |
| public void Say(string text, ConsoleColor foreground = ConsoleColor.White, ConsoleColor background = ConsoleColor.Black) | |
| { | |
| Console.ForegroundColor = foreground; | |
| Console.BackgroundColor = background; | |
| Console.WriteLine(text); | |
| Console.ResetColor(); | |
| } | |
| public Answer Choose(string text, params string[] options) | |
| { | |
| return Choose(text, ConsoleColor.White, ConsoleColor.Black, options); | |
| } | |
| public Answer Choose(string text, ConsoleColor foreground = ConsoleColor.White, ConsoleColor background = ConsoleColor.Black, params string[] options) | |
| { | |
| Console.ForegroundColor = foreground; | |
| Console.BackgroundColor = background; | |
| Console.WriteLine(text); | |
| Console.ResetColor(); | |
| foreach (var item in options.Select((x, i) => new { Index = (Answer)(i + 1), Text = x })) | |
| { | |
| Console.WriteLine($"{item.Index}. {item.Text}"); | |
| } | |
| var character = Console.ReadKey(true).KeyChar.ToString().ToUpper(); | |
| // what if they typed a number that could work? | |
| if (int.TryParse(character, out var number) && number <= options.Count()) | |
| { | |
| return (Answer)number; | |
| } | |
| // can we parse they character to the enum? | |
| if (!Enum.TryParse<Answer>(character, out var result)) | |
| { | |
| return TryAgain(); | |
| } | |
| // is the enum inside the possible options? | |
| if ((int)result > options.Count()) | |
| { | |
| return TryAgain(); | |
| } | |
| // looks good | |
| return result; | |
| Answer TryAgain() | |
| { | |
| while (Console.KeyAvailable) | |
| { | |
| Console.ReadKey(true); | |
| } | |
| Say("Please try again.", ConsoleColor.Yellow, ConsoleColor.Blue); | |
| return Choose(text, foreground, background, options); | |
| } | |
| } | |
| public string Ask(string text, ConsoleColor foreground = ConsoleColor.White, ConsoleColor background = ConsoleColor.Black) | |
| { | |
| Console.ForegroundColor = foreground; | |
| Console.BackgroundColor = background; | |
| Console.WriteLine(); | |
| Console.ResetColor(); | |
| return Console.ReadLine(); | |
| } | |
| public ConsoleKey ReadKey() | |
| { | |
| if (Console.KeyAvailable) | |
| { | |
| return Console.ReadKey(true).Key; | |
| } | |
| else | |
| { | |
| return default(ConsoleKey); | |
| } | |
| } | |
| public char ReadChar() | |
| { | |
| if (Console.KeyAvailable) | |
| { | |
| return Console.ReadKey(true).KeyChar; | |
| } | |
| else | |
| { | |
| return default(char); | |
| } | |
| } | |
| } | |
| public enum Answer : int { None = 0, A = 1, B = 2, C = 3, D = 4, E = 5, F = 6, G = 7, H = 8, I = 9 } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment