forked from cfrantzidis/DungeonExplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cs
More file actions
107 lines (94 loc) · 3.02 KB
/
Copy pathPlayer.cs
File metadata and controls
107 lines (94 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
namespace DungeonExplorer
{
public class Player
{
public string Name { get; private set; }
public int Health { get; private set; }
private string _name;
private int _health;
private List<string> inventory = new List<string>();
public Player(string name, int health)
public string Name
{
Name = name;
Health = health;
get { return _name; }
set
{
if (string.IsNullOrWhiteSpace(value))
{
Console.WriteLine("Invalid input, player name defaulted to 'Player1'");
_name = "Player1";
}
else
{
_name = value;
}
}
}
public void PickUpItem(string item)
public int Health
{
get { return _health; }
set { _health = Math.Max(0, value); } // Ensures health is never below 0
}
public Room CurrentRoom { get; private set; } // externally this is read only however can be set internally
public Player(string name, int health)
{
Name = string.IsNullOrWhiteSpace(name) ? "Player1" : name;
Health = Math.Max(0, health);
}
public void EnterRoom(Room room)
{
CurrentRoom = room;
Console.WriteLine($"You have entered: {room.GetDescription()}");
}
public void Move(string direction)
{
Room nextRoom = null;
switch (direction.ToLower())
{
case "north": nextRoom = CurrentRoom?.North; break;
case "south": nextRoom = CurrentRoom?.South; break;
case "east": nextRoom = CurrentRoom?.East; break;
case "west": nextRoom = CurrentRoom?.West; break;
default:
Console.WriteLine("Invalid direction.");
return;
}
if (nextRoom != null)
{
EnterRoom(nextRoom);
}
else
{
Console.WriteLine("You can't go that way!");
}
}
public void PickUpItem(string item)
{
if (!string.IsNullOrEmpty(item))
{
inventory.Add(item);
}
}
public string InventoryContents()
{
return string.Join(", ", inventory);
if (inventory.Count == 0)
return "Inventory is empty.";
return string.Join(Environment.NewLine, inventory.Select((x, n) => $"{n + 1}. {x}"));
}
public void DisplayStatus()
{
Console.WriteLine($"Player: {Name}");
Console.WriteLine($"Health: {Health}");
Console.WriteLine("Inventory:");
Console.WriteLine(InventoryContents());
}
}
}