Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# OOP Text Adventure Update | 2/18 #2

Merged
merged 8 commits into from
Feb 18, 2025
Merged

C# OOP Text Adventure Update | 2/18 #2

merged 8 commits into from
Feb 18, 2025

Conversation

Nicole-Scalera
Copy link
Owner

Overview

  • Pulling the latest version of oop-C# into dev.

In-depth Details

  • This pull request introduces several new changes regarding the C# text adventure game that I am building.
  • As mentioned in another repository, I am looking to utilize the stacking workflow features by Graphite.
  • I am testing some things out, like which branch should be the trunk branch (probably main or dev).
  • For the safety and consistency of the project, I'm merging all of my current work into dev.
  • This includes C# files, notes, and more.

Nicole-Scalera and others added 8 commits February 1, 2025 18:58
Overview
- Created a new C# Console App Project in Visual Studio 2022.
- Runs on .NET 8.0 (Long Term Support).

In-depth Details
- I will break down the details of this commit below.

A. Purposes of This Project
- I created this project in an attempt to teach myself more about object-oriented programming in C#.
- I learned and practiced OOP concepts through several Java classes.
- I believe this helped me teach C# to myself.
- However, as much as I love documentation, I really like to go follow a lesson plan through textbooks, videos, and any other learning materials I can get my hands on.
- Likewise, I am going through a lesson on OOP in C# now to brush up on my skills.
- Hopefully this will allow me to optimize and improve my other projects.

B. Commit Contents
- In this commit, I created a new solution and project named oop-text-adventure.
- Set up the Program.cs file.
     - This file prompts the user for their name.
     - It grabs the contents of their response and sets it to a variable called name.
     - If their response is empty, it will just set the name to "No Name"
          - It uses {0} as a placeholder to grab the first variable created.
     - The system then welcomes them to the program.
Overview
- Updated Program.cs.
- Added Character.cs and Player.cs.

In-depth Details
- I will breakdown the details below.

A. New Classes
- Created abstract Character class.
     - Abstract means it can't be instantiated by other objects.
     - Instead, it acts like a base class for other objects to draw their logic from.
- Likewise, Player class as an extension of the Character class.
- Note that both files use the namespace "OOPAdventure".

B. Constructors
- Character.cs contains a Name constructor.
     - Added a public name property with automatic getter and setter methods.
          - "{ get; set; }"
- The Player class contains a constructor that passes in a single argument called name.
- Its keyword "base(name)" passes the variable back to Character.cs.
	- This works because Character.cs is the base class.

C. Program.cs
- Back in Program.cs, I referenced the OOPAdventure namespace.
     - "using OOPAdventure;"
- I also created a variable named player, which is an instance of the Player class.
- The console now prints out the inputted name via the variable, like so:
     - Console.WriteLine("Welcome {0} to your OOP adventure!", player.Name);
- Note that the word "Name" is capital.
- In C# we use capital letters to define the name of any public field or property.
Overview
- Moved Player.cs and Character.cs to new folder named Character.

In-depth Details
- This is to keep our scripts organized, as more scripts will be created in different folders.
Overview
- Updated Program.cs.
- Added Language.cs and English.cs.
- Added a protected scope to new files.

In-depth Details
- I will breakdown the details below.

A. New Classes
- Created abstract Language class.
- This file contains all the properties for all the text present in the game.
- Created English, which is an extension of Language.
     - Same concept applied in Character and Player classes (94a5e3b).
- Both files use OOPAdventure namespace.

B. Properties
- Added the following properties to Language.cs:
     - Welcome
     - ChooseYourName
     - DefaultName
- All properties are public, but the get-and-set methods are tweaked:
     - { get; protected set; }
- The keyword "protected" allows only subclasses to access properties.
- In this case, that would be the English class.

C. Constructors
- English.cs inherits the ChooseYourName property from Language.cs
- ChooseYourName is set within new constructor.
     - ChooseYourName = "Hello, what's your name?";

D. Program.cs
- Back in Program.cs, the prompt message has had its string replaced with ChooseYourName.
     - Console.WriteLine(language.ChooseYourName);

E. Conclusion
- The "protected" scope, which is defined in Language.cs and utilized in English.cs, protects data and allows for easy language changes.
Overview
- Created Text.cs class inside of Text folder.
- Implemented singleton pattern for accessing a single instance of the Language class.

In-depth Details
- I will breakdown the details below.

A. Creation of Text.cs
- Text.cs is a public class that is static.
     - Static ensures there is only one instance of the class throughout the entire system.
     - More importantly, this provides a global point of access to that instance.
- Inside of Text.cs, I created a private static field called _language.
     - This is to store the instance of the language class.
- Below that is a public static property named Language.
     - This property has a custom getter method that verifies there is only one instance.
     - If it's empty (null), throw an exception.
     - Otherwise, return the instance (_language).
- After the constructor, I implemented a method called LoadLanguage that globally registers the single instance of the Language class.

B. Modifying Program.cs
- I updated Program.cs to now use the new Text class and its loadLanguage method.
- Rather than defining the language, I created a reference to the class itself.
- However, I don't create a new instance of Text, I just call the static method, LoadLanguage, directly.
- Specifically, the program passes a new instance of English into the LoadLanguage.
- This can then be used throughout the script, such as in the Console.WriteLine() pieces.
     - Before: Console.WriteLine("Hello, what's your name?");
     - After: Console.WriteLine(Text.Language.ChooseYourName);
- This ensures consistent access to the language instance across scenes.
Overview
- Added Notes2.txt with regards to the oop-text-adventure project.

In-depth Details
- I am taking notes on the course I am following as I go along.
- Not everything I code is identical to the course (I often like to experiment with my code between commits).
- However, I actively take notes for my own reference.
- I am uploading them here as well.
Overview
- Created .gitignore.

In-depth Details
- .git ignore has been created in oop-text-adventure project.
- I love using both VS Code and Rider, as they both have their own unique abilities and layouts.
- It looks like Rider created a .gititnore by default, which I can edit later as needed.
Overview
- indexLayout.xml and vcs.xml added in the oop-text-adventure project.

In-depth Details
- As mentioned in 811667e, I love using JetBrains Rider.
- In addition to a .gitignore, Rider also added some project settings files.
- Copilot informed me that these files were not needed unless I was collaborating on work, but being that I'm often switching between computers, this might help.
- I use Rider and VSC on both and have been really enjoying the different tools and layout.
@Nicole-Scalera Nicole-Scalera self-assigned this Feb 18, 2025
@Nicole-Scalera Nicole-Scalera merged commit b20460f into dev Feb 18, 2025
@Nicole-Scalera Nicole-Scalera deleted the oop-c# branch February 18, 2025 18:48
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.

1 participant