Skip to content

Development Coursework#112

Open
JamesBowman-06 wants to merge 2 commits into
cfrantzidis:masterfrom
JamesBowman-06:master
Open

Development Coursework#112
JamesBowman-06 wants to merge 2 commits into
cfrantzidis:masterfrom
JamesBowman-06:master

Conversation

@JamesBowman-06

@JamesBowman-06 JamesBowman-06 commented Mar 13, 2025

Copy link
Copy Markdown

Player.cs:

using System;
using System.Collections.Generic;

namespace DungeonExplorer
{
    public class Player
    {
        public string Name { get; private set; }
        public int Health { get; private set; }
        private List<string> inventory = new List<string>();

        public Player(string name, int health)
        {
            Name = name;
            Health = health;
        }
        public void PickUpItem(string item)
        {
            inventory.Add(item.ToLower());
            Console.WriteLine($"{item} added to inventory.");
        }

        public void UseItem(string item)
        {
            if (!inventory.Contains(item))
            {
                Console.WriteLine("You don't have that item right now!");
                return;
            }

            switch (item.ToLower())
            {
                case "potion":
                    if (Health == 100)
                    {
                        Console.WriteLine("Your health is already 100 - no need for the potion.");
                        return;
                    }
                    Heal(10);
                    Console.WriteLine("The potion healed 10 health.");
                    inventory.Remove(item);
                    break;

                case "wooden sword":
                    Console.WriteLine("There are no enemies to fight right now.");
                    break;

                default:
                    Console.WriteLine("You can't use that right now.");
                    break;
            }
        }
        public string InventoryContents()
        {
            return inventory.Count > 0 ? string.Join(", ", inventory) : "Your inventory has nothing in it!";
        }

        public void TakeDamage(int damage)
        {
            Health -= damage;
            if (Health < 0) Health = 0;
            Console.WriteLine($"You took {damage} damage. Current health = {Health}");
        }

        public void Heal(int amount)
        {
            Health += amount;
            if (Health > 100) Health = 100;
        }
    }
}

Game.cs:

using System;

namespace DungeonExplorer
{
    internal class Game
    {
        private Player player;
        private Room currentRoom;
        private Random random = new Random();

        public Game()
        {
            // Initialize the game with one room and one player
            player = new Player("James", 100);

            if (random.Next(1, 3) == 1)
            {
                new Enemy("Small Ogre", 30, 10);
            }
            currentRoom = new Room("You are in a dark cold dungeon with cobwebs everywhere. There are some items laying around. You spot an old wooden sword in one corner. There is a red potion on a table to your left");
            currentRoom.AddItem("Potion");
            currentRoom.AddItem("Wooden Sword");
        }

        public void Start()
        {
            // Change the playing logic into true and populate the while loop
            bool playing = true;
            while (playing)
            {
                // Code your playing logic here


                Console.Clear();
                Console.WriteLine("Current Room: " + currentRoom.GetDescription());
                Console.WriteLine("Items in this room: " + currentRoom.GetItems());
                Console.WriteLine("Your inventory: " + player.InventoryContents());
                Console.WriteLine("Your health: " + player.Health);

                if (currentRoom.HasEnemy())
                {
                    Console.WriteLine($"An enemy appears! It's a {currentRoom.Enemy.Name} with {currentRoom.Enemy.Health} HP!");
                    Console.WriteLine("What will you do?");
                    Console.WriteLine("1. Attack");
                    Console.WriteLine("2. Run");

                    string choice = Console.ReadLine().ToLower();
                    if (choice == "1")
                    {
                        Fight();
                        if (player.Health <= 0)
                        {
                            Console.WriteLine("You died...");
                            playing = false;
                            continue;
                        }
                    }
                    else if (choice == "2")
                    {
                        Console.WriteLine("You fled the enemy!");
                        playing = false;
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("Invalid choice!");
                    }
                }
                else
                {
                    Console.WriteLine("\nWhat would you like to do?");
                    Console.WriteLine("1. Pick up an item");
                    Console.WriteLine("2. Use an item");
                    Console.WriteLine("3. Exit the dungeon");

                    string choice = Console.ReadLine().ToLower();
                    switch (choice)
                    {
                        case "1":
                            Console.WriteLine("Which item would you like to pick up?");
                            string itemToPick = Console.ReadLine().ToLower();

                            if (currentRoom.HasItem(itemToPick))
                            {
                                player.PickUpItem(itemToPick);
                                currentRoom.RemoveItem(itemToPick);
                            }
                            else
                            {
                                Console.WriteLine("That item is not in the room!");
                            }
                            break;

                        case "2":
                            Console.WriteLine("Which item would you like to use?");
                            string itemToUse = Console.ReadLine();
                            player.UseItem(itemToUse);
                            break;

                        case "3":
                            playing = false;
                            Console.WriteLine("Exiting the dungeon...");
                            break;

                        default:
                            Console.WriteLine("Invalid option.");
                            break;
                    }
                }

                Console.WriteLine("\nPress any key to continue...");
                Console.ReadKey();
            }
        }

        private void Fight()
        {
            Enemy enemy = currentRoom.Enemy;

            while (player.Health > 0 && enemy.IsAlive())
            {
                Console.WriteLine($"You attack the {enemy.Name}!");

                int damage = player.InventoryContents().Contains("wooden sword") ? 15 : 5;
                enemy.TakeDamage(damage);
                Console.WriteLine($"You dealt {damage} damage! Enemy HP: {enemy.Health}");

                if (!enemy.IsAlive())
                {
                    Console.WriteLine($"You defeated the {enemy.Name}!");
                    currentRoom.RemoveEnemy();
                    break;
                }

                Console.WriteLine($"{enemy.Name} attacks you!");
                player.TakeDamage(enemy.Attack());
                Console.WriteLine($"You took {enemy.Power} damage! Your HP: {player.Health}");

                if (player.Health <= 0)
                {
                    Console.WriteLine("You have been defeated...");
                    break;
                }

                Console.WriteLine("Press any key for the next round...");
                Console.ReadKey();
            }
        }
    }
}

Enemy.cs:

using System;
using System.Collections.Generic;


namespace DungeonExplorer
{
    public class Enemy
    {
        public string Name { get; private set; }
        public int Health { get; private set; }
        public int Power { get; private set; }

        public Enemy(string name, int health, int power)
        {
            Name = name;
            Health = health;
            Power = power;
        }

        public void TakeDamage(int damage)
        {
            Health -= damage;
            if (Health < 0) Health = 0;
        }

        public bool IsAlive()
        {
            return Health > 0;
        }

        public int Attack()
        {
            return Power;
        }
    }
}

Room.cs

using System;
using System.Collections.Generic;
using DungeonExplorer;

namespace DungeonExplorer
{
    public class Room
    {
        private string description;
        private List<string> items;
        public Enemy Enemy { get; private set; }

        public Room(string description, Enemy enemy = null)
        {
            this.description = description;
            items = new List<string>();
            Enemy = enemy;
        }

        public string GetDescription()
        {
            return description;
        }

        public void AddItem(string item)
        {
            items.Add(item.ToLower());
        }

        public string GetItems()
        {
            return items.Count > 0 ? string.Join(", ", items) : "No items.";
        }

        public bool HasItem(string item)
        {
            return items.Contains(item.ToLower());
        }

        public bool RemoveItem(string item)
        {
            return items.Remove(item.ToLower());
        }

        public bool HasEnemy()
        {
            return Enemy != null && Enemy.IsAlive();
        }

        public void RemoveEnemy()
        {
            Enemy = null;
        }
    }
}

Program.cs:

using System;

namespace DungeonExplorer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Game game = new Game();
            game.Start();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

@JamesBowman-06 JamesBowman-06 changed the title Update README.md Development Coursework Mar 13, 2025
@samwhite-git

Copy link
Copy Markdown

Overall good display of C#, however the enemies class does not function as clearly intended.

@ryanbecksafc

Copy link
Copy Markdown

Code criteria has been outlined well, however there should be more comments used to display what is going on with the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants