I am creating a simple dungeon crawler game. My program runs and does what I want it to do so far, but I am starting to run into difficulty with adding new functionality, and I think one of the main issues is my class design.
I want to refactor my classes according to proper OOP principles, of which I am attempting to understand. I would like to have a better understanding of this now so that I will have an easier time going forward.
I am thinking about restructuring my classes according to the following:
Character
:
id
name
currenthp
maximumhp
level
experience
Room
(Character
should know its location, correct?)Monster
(Character
should also know its target, correct?)
Monster
:
id
name
currenthp
maximumhp
level
Room
(likeCharacter
,Monster
should know its location?)Character
(likeCharacter
,Monster
should know its target?)
Room
:
id
location
- The location of its doors
Dungeon
? (shouldRoom
know what Dungeon it is in?)Monster
(s)? (shouldRoom
know what monsters are in it?)Character
? (shouldRoom
know what character is in it?)
Dungeon:
id
name
Rooms
Does the above seem like a reasonable design for my classes? My main question is: to what extent should each class know about the other classes?
Character
class:
private class Character
{
public string name { get; set; }
public int level { get; set; }
public Tuple<int, int> currentLocation { get; set; }
public int currentHP { get; set; }
public int maximumHP { get; set; }
public Dungeon dungeon { get; set; }
public Monster target { get; set; }
public Character(string name, Dungeon dungeon)
{
this.name = name;
level = 1;
maximumHP = 100;
currentHP = 100;
this.dungeon = dungeon;
switch (dungeon.Value)
{
case 1: //Dungeon 1
currentLocation = new Tuple<int, int>(5, 0);
break;
case 2: //Dungeon 2
currentLocation = new Tuple<int, int>(0, 0);
break;
case 3: //Dungeon 3
currentLocation = new Tuple<int, int>(0, 0);
break;
}
}
public List<string> lookAroundRoom()
{
Room room = dungeon.DungeonLayout[currentLocation];
List<string> targets = new List<string>();
foreach (Monster monster in room.monster)
{
targets.Add(monster.Name);
}
return targets;
}
public Monster targetMonster(ListBox lst)
{
//I think this should be split into two methods
//currently it does two things - sets the monster as the target
//and then returns monster data
Room room = dungeon.DungeonLayout[currentLocation];
target = room.monster[lst.SelectedIndex];
return target;
}
}
Monster
class:
public class Monster
{
private static int autoIncrementID = 1;
public int ID
{
get { return autoIncrementID; }
}
public string Name { get; set; }
public int level { get; set; }
public int currentHP { get; set; }
public int maximumHP { get; set; }
public Monster()
{
Name = $"Monster {autoIncrementID++}";
currentHP = 20;
maximumHP = 20;
level = 1;
}
}
public void MonsterDie(Room room, Monster monster)
{
room.monster = null;
}
Room
class:
public class Room
{
public bool MoveNorth { get; set; }
public bool MoveEast { get; set; }
public bool MoveSouth { get; set; }
public bool MoveWest { get; set; }
public List<Monster> monster { get; set; }
public Room(bool moveNorth = false, bool moveEast = false, bool moveSouth = false, bool moveWest = false)
{
MoveNorth = moveNorth;
MoveEast = moveEast;
MoveSouth = moveSouth;
MoveWest = moveWest;
monster = new List<Monster>();
monster.Add(new Monster());
}
}
Dungeon
class:
public class Dungeon
{
public string Name { get; set; }
public int Value { get; set; }
public Dictionary<Tuple<int, int>, Room> DungeonLayout { get; set; }
public Dungeon(int value)
{
DungeonLayout = new Dictionary<Tuple<int, int>, Room>();
switch (value)
{
case 1:
Name = "Dungeon 1";
Value = value;
DungeonLayout.Add(new Tuple<int, int>(5, 0), new Room(false, false, false, true));
DungeonLayout.Add(new Tuple<int, int>(4, 0), new Room(false, true, true, false));
//Code truncated. The rest of this code contains
//more lines like the above two
//to generate the layout of the dungeon.
}
}
}
LivingBeing
orActor
orCombatant
which reflects the fact that they both have HPs, do damage, have locations within rooms and move around. \$\endgroup\$