From 2a24fd0da14b2fd9d3c5b54eac875ba7a971fc19 Mon Sep 17 00:00:00 2001 From: Amyoletsgo <182237604+Amyoletsgo@users.noreply.github.com> Date: Tue, 11 Mar 2025 17:10:47 +0000 Subject: [PATCH 1/3] Version 1 First Version: will need errors corrected but runs --- .vscode/settings.json | 3 ++ Game.cs | 15 +++++++--- Player.cs | 65 +++++++++++++++++++++++++++++++++++++------ Program.cs | 1 - Room.cs | 42 ++++++++++++++++++++++++---- 5 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..013007bb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "dotnet.preferCSharpExtension": true +} \ No newline at end of file diff --git a/Game.cs b/Game.cs index dacdc422..5866e3c8 100644 --- a/Game.cs +++ b/Game.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Media; namespace DungeonExplorer @@ -7,19 +8,25 @@ internal class Game { private Player player; private Room currentRoom; + private Room nextRoom; public Game() { // Initialize the game with one room and one player - + currentRoom = new Room("room 1: entrance", "a torch", 2, 2, 1); + nextRoom = new Room("room 2: passage", "a key", 5, 2, 4); + List playerInventory = new List(); + player = new Player("player", 100, playerInventory); } public void Start() { - // Change the playing logic into true and populate the while loop - bool playing = false; + bool playing = true; while (playing) { - // Code your playing logic here + player.setPlayerName(); + currentRoom.WriteDescription(); + player.PickUpItem(currentRoom.RoomItem); + playing = false; } } } diff --git a/Player.cs b/Player.cs index 21cc773f..d6048611 100644 --- a/Player.cs +++ b/Player.cs @@ -1,25 +1,72 @@ -using System.Collections.Generic; +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 inventory = new List(); + private string PlayerName = "player"; + private int PlayerHealth; + private List PlayerInventory = new List(); - public Player(string name, int health) + public Player(string PlayerName, int PlayerHealth, List PlayerInventory) { - Name = name; - Health = health; + Name = PlayerName; + Health = PlayerHealth; + Inventory = PlayerInventory; } - public void PickUpItem(string item) + public string Name { + get { return PlayerName; } + set { PlayerName = string.IsNullOrWhiteSpace(value) ? "player" : value; } + } + public int Health + { + get { return PlayerHealth; } + set { PlayerHealth = value; } + } + public List Inventory + { + get { return PlayerInventory; } + set { PlayerInventory = value ?? new List(); } + } + public void setPlayerName() + { + string playerNameInput; + do + { + Console.WriteLine("What name do you wish to play under: "); + playerNameInput = Console.ReadLine(); + } + while (string.IsNullOrEmpty(playerNameInput)); + + Name = playerNameInput; + Console.WriteLine($"Hello, {Name}! You are starting with {Health} health points"); + } + public void PickUpItem(string roomItem) + { + string pickUpItem; + do + { + Console.WriteLine($"There is {roomItem} in this room. Do you wish to pick it up? (Y/N): "); + pickUpItem = Console.ReadLine().ToUpper(); + } + while (pickUpItem != "Y" && pickUpItem != "N"); + if (pickUpItem == "Y") + { + PlayerInventory.Add(roomItem); + Console.WriteLine($"Item {roomItem} picked up!"); + Console.WriteLine($"{Name}, your inventory contains: {InventoryContents()}, and your hp is {Health}"); + } + else if (pickUpItem == "N") + { + Console.WriteLine($"{roomItem} not picked up"); + } } public string InventoryContents() { - return string.Join(", ", inventory); + return string.Join(", ", Inventory); } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 452cda8c..c56c1611 100644 --- a/Program.cs +++ b/Program.cs @@ -12,7 +12,6 @@ static void Main(string[] args) { Game game = new Game(); game.Start(); - Console.WriteLine("Waiting for your Implementation"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } diff --git a/Room.cs b/Room.cs index cb092efb..55e9ca1a 100644 --- a/Room.cs +++ b/Room.cs @@ -1,17 +1,47 @@ -namespace DungeonExplorer +using System; +using System.Dynamic; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace DungeonExplorer { public class Room { - private string description; + private string roomName; + private string roomItem; + private int roomLength; + private int roomWidth; + private int roomDoors; + + private Player player; - public Room(string description) + + public Room(string roomName, string roomItem, int roomLength, int roomWidth, int roomDoors) { - this.description = description; + this.roomName = roomName; + this.roomItem = roomItem; + this.roomLength = roomLength; + this.roomWidth = roomWidth; + this.roomDoors = roomDoors; } - public string GetDescription() + public string RoomName { - return description; + get { return roomName; } + set { roomName = value; } } + public string RoomItem + { + get { return roomItem; } + set { roomItem = value; } + } + public void WriteDescription() + { + Console.WriteLine( + $"You enter {roomName}. This room goes back {roomLength} metres. \n" + + $"It is {roomWidth} metres wide. The room has {roomDoors} Doors \n"); + } + } } \ No newline at end of file From fd49c4dcbf49c2508d6d6d2dbfe7de050ba26737 Mon Sep 17 00:00:00 2001 From: Amyoletsgo <182237604+Amyoletsgo@users.noreply.github.com> Date: Wed, 12 Mar 2025 22:34:32 +0000 Subject: [PATCH 2/3] Version 2 Updated after reviews: comments added cases corrected added telling the user about empty inventory --- Game.cs | 24 ++++++++++++++----- Player.cs | 70 ++++++++++++++++++++++++++++++++++++++++++------------ Program.cs | 8 +++---- Room.cs | 11 ++++++--- 4 files changed, 85 insertions(+), 28 deletions(-) diff --git a/Game.cs b/Game.cs index 5866e3c8..5f7b3e54 100644 --- a/Game.cs +++ b/Game.cs @@ -5,27 +5,39 @@ namespace DungeonExplorer { internal class Game - { - private Player player; - private Room currentRoom; - private Room nextRoom; + { + // defining all of the objects to be used in the game class + private Player player;// sets up an instance of "player" for the user + private Room currentRoom;// sets up an instance of "room" for the player to interact with + private Room nextRoom;// sets up the room to move on to + + public Game() { - // Initialize the game with one room and one player + // setting up the argyuments for the objects currentRoom = new Room("room 1: entrance", "a torch", 2, 2, 1); - nextRoom = new Room("room 2: passage", "a key", 5, 2, 4); + nextRoom = new Room("room 2: passage", "a key", 5, 2, 4); + // sets up an empty list of strings for the player inventory List playerInventory = new List(); player = new Player("player", 100, playerInventory); } + public void Start() { bool playing = true; while (playing) { + //gets the player to input a username player.setPlayerName(); + //writes the description for the current room currentRoom.WriteDescription(); + //gives the user an option to pick up an item from the room + player.PickUpItem(currentRoom.RoomItem); + //set next room to current room and run the next room description + currentRoom = nextRoom; player.PickUpItem(currentRoom.RoomItem); + //sets playing to false to finish the game playing = false; } } diff --git a/Player.cs b/Player.cs index d6048611..e073a63b 100644 --- a/Player.cs +++ b/Player.cs @@ -5,44 +5,71 @@ namespace DungeonExplorer { public class Player { - private string PlayerName = "player"; - private int PlayerHealth; - private List PlayerInventory = new List(); + private string playerName = "player"; // sets a default player name + private int playerHealth; //sets up integer value for health + private List playerInventory = new List(); //sets inventory as list of strings. - public Player(string PlayerName, int PlayerHealth, List PlayerInventory) + public Player(string playerName, int playerHealth, List playerInventory) { - Name = PlayerName; - Health = PlayerHealth; - Inventory = PlayerInventory; + //takes the inputs from when the class is called to set up the variables for the class + Name = playerName; + Health = playerHealth; + Inventory = playerInventory; } + public string Name { - get { return PlayerName; } - set { PlayerName = string.IsNullOrWhiteSpace(value) ? "player" : value; } + //gets and sets player name + //sets as "player" if the value is empty + get { return playerName; } + set { playerName = string.IsNullOrWhiteSpace(value) ? "player" : value; } } + public int Health { - get { return PlayerHealth; } - set { PlayerHealth = value; } + //get and set for health + get { return playerHealth; } + set { playerHealth = value; } } + public List Inventory { - get { return PlayerInventory; } - set { PlayerInventory = value ?? new List(); } + // gets and sets an inventory + // returns a new list if the value is not there + get { return playerInventory; } + set { playerInventory = value ?? new List(); } } + + + //method for setting the player name public void setPlayerName() { - string playerNameInput; + string playerNameInput; // empty string to start do { + //asks user to input a name Console.WriteLine("What name do you wish to play under: "); playerNameInput = Console.ReadLine(); } + //loops again if the input is null or empty while (string.IsNullOrEmpty(playerNameInput)); + //sets the player as their username Name = playerNameInput; Console.WriteLine($"Hello, {Name}! You are starting with {Health} health points"); + + //message displaying inventory (or that it is empty) + if (playerInventory.Count == 0) + { + Console.WriteLine($"Your inventory contains is empty"); + } + else + { + Console.WriteLine($"Your inventory contains: {InventoryContents()}"); + } } + + //method to give the option to pick up an item and add it to the inventory. public void PickUpItem(string roomItem) { string pickUpItem; @@ -55,15 +82,28 @@ public void PickUpItem(string roomItem) if (pickUpItem == "Y") { - PlayerInventory.Add(roomItem); + //adds item to inventory + playerInventory.Add(roomItem); + //tells user what they have picked up and their current stats. Console.WriteLine($"Item {roomItem} picked up!"); Console.WriteLine($"{Name}, your inventory contains: {InventoryContents()}, and your hp is {Health}"); } else if (pickUpItem == "N") { Console.WriteLine($"{roomItem} not picked up"); + //message displaying inventory (or that it is empty) + if (playerInventory.Count == 0) + { + Console.WriteLine($"{Name}, your inventory contains is empty and your hp is {Health}"); + } + else + { + Console.WriteLine($"{Name}, your inventory contains: {InventoryContents()}, and your hp is {Health}"); + } } } + + // turns the list of inventory into a string public string InventoryContents() { return string.Join(", ", Inventory); diff --git a/Program.cs b/Program.cs index c56c1611..d8d39602 100644 --- a/Program.cs +++ b/Program.cs @@ -10,10 +10,10 @@ internal class Program { static void Main(string[] args) { - Game game = new Game(); - game.Start(); - Console.WriteLine("Press any key to exit..."); - Console.ReadKey(); + Game game = new Game(); //calls an object of the game class (where the main program is) + game.Start(); //starts the game using start method + Console.WriteLine("Press any key to exit..."); + Console.ReadKey(); // reads key before it closes the program } } } diff --git a/Room.cs b/Room.cs index 55e9ca1a..a7386398 100644 --- a/Room.cs +++ b/Room.cs @@ -8,17 +8,17 @@ namespace DungeonExplorer { public class Room { + //sets up the parameters for the class private string roomName; private string roomItem; private int roomLength; private int roomWidth; private int roomDoors; - private Player player; - public Room(string roomName, string roomItem, int roomLength, int roomWidth, int roomDoors) { + //takes the inputs from when the class is called to set up the variables for the class this.roomName = roomName; this.roomItem = roomItem; this.roomLength = roomLength; @@ -28,16 +28,21 @@ public Room(string roomName, string roomItem, int roomLength, int roomWidth, int public string RoomName { + //gets and sets the name of the room get { return roomName; } set { roomName = value; } } + public string RoomItem { + //gets and sets the name of the item in the room get { return roomItem; } set { roomItem = value; } } + public void WriteDescription() - { + { + //writes a description of the room including all of the variables passed in Console.WriteLine( $"You enter {roomName}. This room goes back {roomLength} metres. \n" + $"It is {roomWidth} metres wide. The room has {roomDoors} Doors \n"); From 08f46ba42bf1e7f1918cc40eafa5b1b2adafff16 Mon Sep 17 00:00:00 2001 From: Amyoletsgo <182237604+Amyoletsgo@users.noreply.github.com> Date: Thu, 13 Mar 2025 09:13:56 +0000 Subject: [PATCH 3/3] commit updates code --- Game.cs | 2 +- Player.cs | 2 +- Program.cs | 2 +- Room.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Game.cs b/Game.cs index 5f7b3e54..5b8ec5da 100644 --- a/Game.cs +++ b/Game.cs @@ -28,7 +28,7 @@ public void Start() bool playing = true; while (playing) { - //gets the player to input a username + //gets the player to input a username player.setPlayerName(); //writes the description for the current room currentRoom.WriteDescription(); diff --git a/Player.cs b/Player.cs index e073a63b..f720f3fc 100644 --- a/Player.cs +++ b/Player.cs @@ -103,7 +103,7 @@ public void PickUpItem(string roomItem) } } - // turns the list of inventory into a string + // turns the list of inventory into a string public string InventoryContents() { return string.Join(", ", Inventory); diff --git a/Program.cs b/Program.cs index d8d39602..c8d389f0 100644 --- a/Program.cs +++ b/Program.cs @@ -13,7 +13,7 @@ static void Main(string[] args) Game game = new Game(); //calls an object of the game class (where the main program is) game.Start(); //starts the game using start method Console.WriteLine("Press any key to exit..."); - Console.ReadKey(); // reads key before it closes the program + Console.ReadKey(); // reads key before it closes the program } } } diff --git a/Room.cs b/Room.cs index a7386398..6829e26c 100644 --- a/Room.cs +++ b/Room.cs @@ -28,7 +28,7 @@ public Room(string roomName, string roomItem, int roomLength, int roomWidth, int public string RoomName { - //gets and sets the name of the room + //gets and sets the name of the room get { return roomName; } set { roomName = value; } }