[C#] Text-RPG: Efficiency

Started by
1 comment, last by nooblet 12 years, 9 months ago
Hello everybody,

I've been programming in C++ for quite some time, and decided to migrate to C# for simplicity's sake. Instead of following a bunch of tutorials and learning that way, I decided to just jump in with a game and see how easy it is to learn compared to C++. I'm quite astonished, it's very easy to learn how things work with past experience. Regardless, I need some help to improve the efficiency of my programming abilities, and to make sure my game operates accordingly. Directly below this is my source code, and under that is some questions I have. I left some comments in the code about questions I have, and please forgive the mess. I'm just trying to learn.

[heading]Program.cs[/heading]

[source lang="csharp"]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project1
{
class Program
{
static void Main()
{
Menu myMenu = new Menu();

myMenu.MainMenu();
}
}
}
[/source]

[heading]Menu.cs[/heading]
[source lang="csharp"]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project1
{
class Menu
{
public void MainMenu()
{
bool tryAgain = true;

while (tryAgain == true)
{
Console.Clear();
Console.WriteLine("=============================================================================");
Console.WriteLine("Welcome to Untitled!");
Console.WriteLine("=============================================================================");
Console.WriteLine("1. New Game");
Console.WriteLine("2. Load Game");
Console.WriteLine("3. Exit Game");
Console.WriteLine("=============================================================================");
Console.Write("INPUT: ");
string plyInput = Console.ReadLine();
// Below converts the string into an integer. Is there a better/more efficient way to do this, like C++?
// cin >> iPlyInput;
int iPlyInput = Int32.Parse(plyInput);

if (iPlyInput == 1)
{
NewGame();
tryAgain = false;
}
else if (iPlyInput == 2)
{
// LoadGame();
tryAgain = false;
}
else if (iPlyInput == 3)
{
// ExitGame();
tryAgain = false;
}
else
{
Console.Clear();
Console.WriteLine("Invalid input.");
tryAgain = true;
Console.ReadKey();
}
}
}

public void NewGame()
{
Console.Clear();
Console.WriteLine("=============================================================================");
Console.WriteLine("Please choose your character:");
Console.WriteLine("=============================================================================");
Console.WriteLine("1. Alien: This race is equivilent to a mage.");
Console.WriteLine("2. Robot: This race is equivilent to a warrior.");
Console.WriteLine("3. Ninja: This race is equivilent to a rogue.");
Console.WriteLine(" ");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("TIP: When asked for input, you can type /help for further assistance.");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("=============================================================================");
Console.Write("INPUT: ");
string plyInput = Console.ReadLine();

HelpMenu(plyInput);
}

public void HelpMenu(string plyInput)
{
bool tryAgain = true;

if (plyInput == "/help" || plyInput == "/HELP")
{
while (tryAgain == true)
{
Console.Clear();
Console.WriteLine("=============================================================================");
Console.WriteLine("What race would you like more information on?");
Console.WriteLine("=============================================================================");
Console.WriteLine("1. Alien");
Console.WriteLine("2. Robot");
Console.WriteLine("3. Ninja");
Console.WriteLine("=============================================================================");
Console.Write("INPUT: ");
string plyInput2 = Console.ReadLine();
int iPlyInput = Int32.Parse(plyInput2);

if (iPlyInput == 1)
{
Console.Clear();
Console.WriteLine("The Alien is like a mage.");
tryAgain = false;
Console.ReadKey();
}
else if (iPlyInput == 2)
{
Console.Clear();
Console.WriteLine("The Robot is like a warrior.");
tryAgain = false;
Console.ReadKey();
}
else if (iPlyInput == 3)
{
Console.Clear();
Console.WriteLine("The Ninja is like a rogue.");
tryAgain = false;
Console.ReadKey();
}
else
{
Console.Clear();
Console.WriteLine("Invalid input.");
tryAgain = true; // different way to error check?
Console.ReadKey();
}
}
}
}
}
}
[/source]

The only question I have outside of the comments is: what is the best way for checking which text to display for /help, and how do you handle save states? I was thinking of adding an additional string argument to my HelpMenu method and the text that is displayed changes according to the parameter passed. Any suggestions for that? Furthermore, how do I handle save states? I've never actually done it before, so what I've considered doing is the following: creating a text file that saves data such as the race, level, items, coordinates, etc. Based on that data, if the user chooses to load their game, the game will read all of that and display it. I'm not too worried about players altering the text document as this game is just a test of my abilities, and I want to play around with the console and language more before I jump into XNA.

Thanks for your suggestions, comments, and concerns in advanced.
Advertisement

Hello everybody,

I've been programming in C++ for quite some time, and decided to migrate to C# for simplicity's sake. Instead of following a bunch of tutorials and learning that way, I decided to just jump in with a game and see how easy it is to learn compared to C++. I'm quite astonished, it's very easy to learn how things work with past experience. Regardless, I need some help to improve the efficiency of my programming abilities, and to make sure my game operates accordingly. Directly below this is my source code, and under that is some questions I have. I left some comments in the code about questions I have, and please forgive the mess. I'm just trying to learn.

...Snip

The only question I have outside of the comments is: what is the best way for checking which text to display for /help, and how do you handle save states? I was thinking of adding an additional string argument to my HelpMenu method and the text that is displayed changes according to the parameter passed. Any suggestions for that? Furthermore, how do I handle save states? I've never actually done it before, so what I've considered doing is the following: creating a text file that saves data such as the race, level, items, coordinates, etc. Based on that data, if the user chooses to load their game, the game will read all of that and display it. I'm not too worried about players altering the text document as this game is just a test of my abilities, and I want to play around with the console and language more before I jump into XNA.

Thanks for your suggestions, comments, and concerns in advanced.


You could use a TryParse instead as that will throw an exception when the input can't be converted to an int. Error handling and checking in C# is usually done with exceptions. Abit of googling give you this for command line arguments.


Text is an idea but you could also use XML if you wanted to the .NET framework supports that out of the box.

I found that C# programming was more a matter of learning the .NET framework instead of the actual language, which isnt to different from C++, however remember that struct are not classes as in C++.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Thank you very much for your comments. I'll look into using XML instead; sounds like it could be much easier that way. I'll also look into TryParse to see if there's anything I can do with it. The main concern I had was creating a string and then parsing it into an integer -- just seemed like I was over-complicating things, but I guess not. Thanks again!

This topic is closed to new replies.

Advertisement