Skip to content

Instantly share code, notes, and snippets.

@janell-baxter
Created July 9, 2020 16:06
Show Gist options
  • Save janell-baxter/555f973ebfecb3a4da21b175cbc8f601 to your computer and use it in GitHub Desktop.
Save janell-baxter/555f973ebfecb3a4da21b175cbc8f601 to your computer and use it in GitHub Desktop.
Game-like application with explorable areas
namespace ExplorableAreasDemo
{
class Item
{
public string Name;
public string Description;
public Item(string name, string description)
{
Name = name;
Description = description;
}
}
}
using System;
using System.Collections.Generic;
namespace ExplorableAreasDemo
{
class Location
{
public string Name;
public string Description;
public List<Item> Items = new List<Item>();
public ConsoleColor LocationColor = new ConsoleColor();
//TODO: add array or list of people that player can meet
//use random names, dialogue, and items in the NPC inventories
public Location(string name, string description, List<Item> items)
{
Name = name;
Description = description;
Items = items;
LocationColor = ConsoleColor.DarkGreen;
}
public Location(string name, string description, List<Item> items, ConsoleColor color)
{
Name = name;
Description = description;
Items = items;
LocationColor = color;
}
public string About()
{
return $"{Name}: {Description}";
}
}
}
using System.Collections.Generic;
namespace ExplorableAreasDemo
{
class Person
{
public string Name;
public List<Item> Inventory;
public Person(string name, List<Item> inventory)
{
if (name != "")
{ Name = name; }
else
{ Name = "Anonymous Explorer"; }
Inventory = inventory;
}
}
}
/*
* Explorable Areas
* by Janell Baxter
* 7/6/2020
* ITP Summer 2020
*/
namespace ExplorableAreasDemo
{
class Program
{
static void Main(string[] args)
{
new World();
}
}
}
using System;
namespace ExplorableAreasDemo
{
class Utility
{
public static Random random = new Random();
public static int GetRandomNumber(int max)
{
return random.Next(max);
}
}
}
using System;
using System.Collections.Generic;
using static System.Console;
namespace ExplorableAreasDemo
{
class World
{
string Name = "My Awesome World";
List<Location> Locations;
Person player;
public World()
{
SetUpWorld();
SetUpPlayer();
LocationMenu();
}
private void SetUpWorld()
{
Locations = new List<Location>();
Title = Name + " by Janell";
WriteLine($"Welcome to {Name}");
//mountains
//Example of creating a temporary list to pass into the constructor
List<Item> temp = new List<Item>();
temp.Add(new Item("leaf", "generic leaf"));
temp.Add(new Item("stick", "basic stick"));
temp.Add(new Item("rock", "a hefty rock"));
temp.Add(new Item("coin", "a shiny coin"));
temp.Add(new Item("snorkel mask", "important for breathing under water"));
Locations.Add(new Location("Tall Mountains", "Very tall mountains. Steep. Intriquing.", temp, ConsoleColor.Gray));
//Forest
//instead of creating a temporary list,
//you can create a new list and add items when you make the location instance
Locations.Add(new Location("Forest", "Dark scary forest", new List<Item> { new Item("leaf", "generic leaf"), new Item("stick", "basic stick"), new Item("snorkel breathing tube", "important for snorkeling"), new Item("fin set", "good for underwater snorkeling") }));
//Lake
//make list and add items
Locations.Add(new Location("Lake", "A great place for snorkeling", new List<Item> {new Item("treasure", "lots of great treasure items")}, ConsoleColor.DarkBlue));
}
private void SetUpPlayer()
{
WriteLine("Hello explorer! What is your name?");
string input = ReadLine();
player = new Person(input, new List<Item>());
WriteLine("Welcome " + player.Name);
}
private void LocationMenu()
{
Clear();
WriteLine("These are the places you can visit... enter a number to travel:");
int choice = 1;
foreach (Location location in Locations)
{
WriteLine(choice + ") " + location.About());
choice++;
}
string input = ReadLine();
switch(input)
{
case "1":
//0
Travel(0);
break;
case "2":
//1
Travel(1);
break;
case "3":
//2
bool mask = false;
bool tube = false;
bool fins = false;
foreach (Item item in player.Inventory)
{
if (item.Name.Contains("mask"))
{
mask = true;
}
else if (item.Name.Contains("tube"))
{
tube = true;
}
else if (item.Name.Contains("fin"))
{
fins = true;
}
}
if (mask && tube && fins)
{ Travel(2); }
else
{
WriteLine("You are missing essential snorkeling gear. Make sure you have a mask, a breathing tube, and fins.");
WriteLine("Press any key to continue...");
ReadKey();
}
break;
}
//TODO:
//convert input to a number
//find that number inside of the Locations list and use that as target location
//See the Travel method for example of how to do this : )
LocationMenu();
}
private void Travel(int choice)
{
BackgroundColor = Locations[choice].LocationColor;
ForegroundColor = ConsoleColor.White;
Clear();
WriteLine($"Welcome {player.Name} to {Locations[choice].Name}");
WriteLine(Locations[choice].Description);
//you could build up with three lines...
//int listSize = Locations[choice].Items.Count;
//int randomNumber = Utility.GetRandomNumber(listSize);
//Item randomLocationItem = Locations[choice].Items[randomNumber];
//or just have one line...
Item randomItem = Locations[choice].Items[Utility.GetRandomNumber(Locations[choice].Items.Count)];
WriteLine($"You see a {randomItem.Name} ({randomItem.Description}) lying on the ground. Do you pick it up and take it with you?");
string input = ReadLine();
if (input.ToLower() == "yes")
{
player.Inventory.Add(randomItem);
}
WriteLine("Here is what is in your inventory:");
foreach (Item item in player.Inventory)
{
WriteLine(item.Name);
}
WriteLine("Press any key to continue...");
ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment