Skip to content

Instantly share code, notes, and snippets.

@JerryNixon
Created January 9, 2020 08:36
Show Gist options
  • Select an option

  • Save JerryNixon/115dda07a74788d3d169a286bd907f8a to your computer and use it in GitHub Desktop.

Select an option

Save JerryNixon/115dda07a74788d3d169a286bd907f8a to your computer and use it in GitHub Desktop.
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();
Room1();
Console.Clear();
_game.Say("Your game has ended.");
Console.ReadKey();
Start();
}
private static void Room1()
{
_game.Say("You have entered a green room.", ConsoleColor.White, ConsoleColor.Green);
_game.Doors(Room2, Room3, Room4);
}
private static void Room2()
{
_game.Say("You have entered a red room.", ConsoleColor.White, ConsoleColor.Red);
if (_game.Monster(10, "Monster"))
{
_game.Doors(Room3, Room4, Room5);
}
}
private static void Room3()
{
_game.Say("You have entered a brown room.", ConsoleColor.White, ConsoleColor.DarkYellow);
_game.Treasure("You have discovered a refreshing fountain.", 10);
_game.Doors(Room4, Room5, Room1);
}
private static void Room4()
{
_game.Say("You have entered a blue room.", ConsoleColor.White, ConsoleColor.Blue);
if (_game.Monster(10, "Dragon"))
{
_game.Treasure("The dragon's mouth contains treasure.", 20);
_game.Doors(Room3, Room5);
}
}
private static void Room5()
{
_game.Say("You have entered a blue room.", ConsoleColor.White, ConsoleColor.Blue);
if (_game.Monster(10, "Dragon"))
{
_game.Doors(Room3, Room5);
}
}
}
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