No description
bytes.c | ||
bytes.h | ||
cat.c | ||
cmpsize.c | ||
less | ||
LICENSE | ||
makefile | ||
meow | ||
readme.md | ||
stdin.c | ||
stdin.h | ||
test | ||
utils.h |
using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.IO;
namespace PokemonMUD { class Program { static void Main(string[] args) { Game game = new Game(); game.Run(); } }
class Game
{
private List<Player> players = new List<Player>();
private Dictionary<string, Area> areas = new Dictionary<string, Area>();
private Random random = new Random();
private const string SaveFile = "playerdata.json";
public Game()
{
InitializeAreas();
InitializePlayer();
}
private void InitializeAreas()
{
// Cities with healing centers
Area cerulean = new Area("Cerulean City", "A bustling city with a gym and healing center.", hasHealingCenter: true);
Area vermilion = new Area("Vermilion City", "A port city with a gym and healing center.", hasHealingCenter: true);
// Adjacent areas with unique creatures
Area ceruleanFields = new Area("Cerulean Fields", "Grassy fields with unique creatures.", creatures: new List<Creature>
{
new Creature("Pidgey", 50, 10),
new Creature("Oddish", 45, 8)
});
Area vermilionCoast = new Area("Vermilion Coast", "Sandy shores with unique creatures.", creatures: new List<Creature>
{
new Creature("Krabby", 55, 12),
new Creature("Tentacool", 50, 9)
});
// Connect areas
cerulean.Connections.Add(ceruleanFields);
ceruleanFields.Connections.Add(cerulean);
vermilion.Connections.Add(vermilionCoast);
vermilionCoast.Connections.Add(vermilion);
areas.Add("Cerulean City", cerulean);
areas.Add("Vermilion City", vermilion);
areas.Add("Cerulean Fields", ceruleanFields);
areas.Add("Vermilion Coast", vermilionCoast);
// Add masters
cerulean.HasMaster = true;
vermilion.HasMaster = true;
// Add store items directly to the lists
cerulean.StoreItems.AddRange(new List<Item> { new Item("Potion", 10, 20), new Item("Super Potion", 20, 50) });
vermilion.StoreItems.AddRange(new List<Item> { new Item("Potion", 10, 20), new Item("Antidote", 5, 10) });
}
private void InitializePlayer()
{
if (File.Exists(SaveFile))
{
string json = File.ReadAllText(SaveFile);
var savedPlayers = JsonSerializer.Deserialize<List<PlayerData>>(json);
foreach (var data in savedPlayers)
{
Player player = new Player(data.Name, areas[data.CurrentArea]);
player.Keys.AddRange(data.Keys);
player.Money = data.Money;
player.Inventory.AddRange(data.Inventory.Select(i => new Item(i.Name, i.Effect, i.Price)));
player.OwnedCreatures.AddRange(data.OwnedCreatures.Select(c => new Creature(c.Name, c.MaxHealth, c.Attack) { Health = c.Health }));
players.Add(player);
}
Console.WriteLine("Loaded saved player data.");
}
else
{
Console.Write("Enter your player name: ");
string name = Console.ReadLine();
Player player = new Player(name, areas["Cerulean City"]);
players.Add(player);
}
}
private void SavePlayerData()
{
var playerData = players.Select(p => new PlayerData
{
Name = p.Name,
CurrentArea = p.CurrentArea.Name,
Keys = p.Keys,
Money = p.Money,
Inventory = p.Inventory.Select(i => new ItemData { Name = i.Name, Effect = i.Effect, Price = i.Price }).ToList(),
OwnedCreatures = p.OwnedCreatures.Select(c => new CreatureData { Name = c.Name, Health = c.Health, MaxHealth = c.MaxHealth, Attack = c.Attack }).ToList()
}).ToList();
string json = JsonSerializer.Serialize(playerData, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(SaveFile, json);
Console.WriteLine("Player data saved.");
}
public void Run()
{
while (true)
{
Player currentPlayer = players[0]; // Simplified: single player for console
Console.WriteLine($"\nYou are in {currentPlayer.CurrentArea.Name}: {currentPlayer.CurrentArea.Description}");
Console.WriteLine("Commands: move [area], talk, heal, challenge [player], buy [item], who, quit");
Console.Write("Enter command: ");
string[] input = Console.ReadLine().ToLower().Split(' ', 2);
string command = input[0];
string argument = input.Length > 1 ? input[1] : "";
try
{
switch (command)
{
case "move":
MovePlayer(currentPlayer, argument);
break;
case "talk":
TalkToMaster(currentPlayer);
break;
case "heal":
HealCreatures(currentPlayer);
break;
case "challenge":
ChallengePlayer(currentPlayer, argument);
break;
case "buy":
BuyItem(currentPlayer, argument);
break;
case "who":
ShowWhoList();
break;
case "quit":
SavePlayerData();
return;
default:
Console.WriteLine("Unknown command.");
break;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
private void MovePlayer(Player player, string areaName)
{
Area targetArea = player.CurrentArea.Connections.Find(a => a.Name.ToLower() == areaName.ToLower());
if (targetArea == null)
throw new Exception("Cannot move there.");
player.CurrentArea = targetArea;
EncounterWildCreature(player);
}
private void EncounterWildCreature(Player player)
{
if (player.CurrentArea.Creatures.Count > 0 && random.Next(0, 100) < 30) // 30% chance
{
Creature wild = player.CurrentArea.Creatures[random.Next(player.CurrentArea.Creatures.Count)];
Console.WriteLine($"A wild {wild.Name} appears!");
Battle(player, wild);
}
}
private void TalkToMaster(Player player)
{
if (!player.CurrentArea.HasMaster)
throw new Exception("No master here.");
if (!player.Keys.Contains(player.CurrentArea.Name))
{
player.Keys.Add(player.CurrentArea.Name);
Console.WriteLine($"You received the {player.CurrentArea.Name} key!");
}
else
{
Console.WriteLine("You already have this city's key.");
}
}
private void HealCreatures(Player player)
{
if (!player.CurrentArea.HasHealingCenter)
throw new Exception("No healing center here.");
foreach (var creature in player.OwnedCreatures)
{
creature.Health = creature.MaxHealth;
Console.WriteLine($"{creature.Name} was fully healed!");
}
}
private void ChallengePlayer(Player player, string targetName)
{
Player target = players.Find(p => p.Name.ToLower() == targetName.ToLower() && p.CurrentArea == player.CurrentArea);
if (target == null)
throw new Exception("Player not found or not in this area.");
if (target == player)
throw new Exception("Cannot challenge yourself.");
if (!player.OwnedCreatures.Any(c => c.Health > 0))
throw new Exception("Your creatures are too weak to battle.");
Console.WriteLine($"Challenging {target.Name}!");
Battle(player, target.OwnedCreatures.First(c => c.Health > 0)); // Battle with first healthy creature
}
private void Battle(Player player, Creature opponent)
{
Creature playerCreature = player.OwnedCreatures.FirstOrDefault(c => c.Health > 0);
if (playerCreature == null)
throw new Exception("All your creatures have fainted!");
Console.WriteLine($"{playerCreature.Name} vs {opponent.Name}!");
while (playerCreature.Health > 0 && opponent.Health > 0)
{
opponent.Health -= playerCreature.Attack;
Console.WriteLine($"{playerCreature.Name} deals {playerCreature.Attack} damage to {opponent.Name}. {opponent.Name} HP: {opponent.Health}");
if (opponent.Health <= 0)
break;
playerCreature.Health -= opponent.Attack;
Console.WriteLine($"{opponent.Name} deals {opponent.Attack} damage to {playerCreature.Name}. {playerCreature.Name} HP: {playerCreature.Health}");
}
if (playerCreature.Health <= 0)
Console.WriteLine($"{playerCreature.Name} fainted! You lose the battle.");
else
Console.WriteLine($"{opponent.Name} fainted! You win the battle.");
}
private void BuyItem(Player player, string itemName)
{
if (player.CurrentArea.StoreItems.Count == 0)
throw new Exception("No store here.");
Item item = player.CurrentArea.StoreItems.Find(i => i.Name.ToLower() == itemName.ToLower());
if (item == null)
throw new Exception("Item not found.");
if (player.Money < item.Price)
throw new Exception("Not enough money.");
player.Money -= item.Price;
player.Inventory.Add(item);
Console.WriteLine($"Bought {item.Name} for {item.Price} coins. Money left: {player.Money}");
}
private void ShowWhoList()
{
Console.WriteLine("Players online:");
foreach (Player p in players)
{
Console.WriteLine($"{p.Name} in {p.CurrentArea.Name} with {p.Keys.Count} key(s): {string.Join(", ", p.Keys)}");
}
}
}
class Player
{
public string Name { get; }
public Area CurrentArea { get; set; }
public List<string> Keys { get; } = new List<string>();
public List<Creature> OwnedCreatures { get; } = new List<Creature>();
public List<Item> Inventory { get; } = new List<Item>();
public int Money { get; set; } = 100;
public Player(string name, Area startingArea)
{
Name = name;
CurrentArea = startingArea;
OwnedCreatures.Add(new Creature("Bulbasaur", 100, 15));
}
}
class Area
{
public string Name { get; }
public string Description { get; }
public List<Area> Connections { get; } = new List<Area>();
public bool HasMaster { get; set; }
public bool HasHealingCenter { get; set; }
public List<Creature> Creatures { get; } = new List<Creature>();
public List<Item> StoreItems { get; set; } = new List<Item>(); // Changed to writable property
public Area(string name, string description, bool hasHealingCenter = false, List<Creature> creatures = null)
{
Name = name;
Description = description;
HasHealingCenter = hasHealingCenter;
if (creatures != null)
Creatures.AddRange(creatures);
}
}
class Creature
{
public string Name { get; }
public int Health { get; set; }
public int MaxHealth { get; }
public int Attack { get; }
public Creature(string name, int health, int attack)
{
Name = name;
Health = health;
MaxHealth = health;
Attack = attack;
}
}
class Item
{
public string Name { get; }
public int Price { get; }
public int Effect { get; }
public Item(string name, int effect, int price)
{
Name = name;
Effect = effect;
Price = price;
}
}
// Data classes for JSON serialization
class PlayerData
{
public string Name { get; set; }
public string CurrentArea { get; set; }
public List<string> Keys { get; set; }
public int Money { get; set; }
public List<ItemData> Inventory { get; set; }
public List<CreatureData> OwnedCreatures { get; set; }
}
class ItemData
{
public string Name { get; set; }
public int Effect { get; set; }
public int Price { get; set; }
}
class CreatureData
{
public string Name { get; set; }
public int Health { get; set; }
public int MaxHealth { get; set; }
public int Attack { get; set; }
}
}