Newbie here:

I'm working on a small console game. Few days ago I came up with some code that was doing exactly what I wanted during character creation - I wanted the user to be able to type in anything BUT if the name string contains a number or is empty then force them to try again. IF the name was a normal string I'd take the first letter and swap it with the same letter in upper case so the name would look nice later on.

I came up with something like this:

Player player = new Player();
do
{
    char x; char y;
    Console.Write("Enter your name: ");
    player.playerName = Console.ReadLine().ToLower();
    x = player.playerName[0]; y = Convert.ToChar(player.playerName[0].ToString().ToUpper());
    player.playerName = player.playerName.Replace(x, y).ToString();
}
while (player.playerName == "" || !(player.playerName).All(char.IsLetter));

For a couple of days, it's been working perfectly, just like other things, I had my friend try to "break" the game and he didn't manage to. I moved on with the code, doing other things.

Yesterday I was working on other stuff and I had to ask a question here on the site: How to call a method after each line of code or something like that. I got a great answer and I used it, everything works. However, because of that, I had to put some changes in my Player() class. Basically, it now looks like this:

public class Player
{
    public static bool isDead;
    public string playerName;
    public string playerSex;
    public string playerVocation;
    public string playerRace;
    public int playerLevel;
    public int playerExperience;
    protected int _playerHealth = 0;
    public int playerHealth
    {
        set
        {
            _playerHealth = value;
            if (_playerHealth == 0)
            {
                isDead = true;
                Console.WriteLine("You are dead!");
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.Write("Press any key to continue...");
                Console.ReadKey();
                Console.Clear();
                Environment.Exit(0);
            }
        }
        get
        {
            return _playerHealth;
        }
    }
    public int playerMana;
    public int playerMagicResistance;
    public int playerPhysicalResistance;
}

Now the line:x = player.playerName[0] in the main "Program" throws an error whenever I press enter while creating a character name. Before the changes I'm talking about when the user input was "empty" it would just ask them to do it again. Now it simply throws IndexOutOfRange error.

I tried making variables static in the Player() class, then removing the static keyword and just declaring a new player object - now nothing seems to work and I haven't found a solution despite trying. It's really killing me that it was working before and now it's not.

#edit I read the suggested answer but it's mostly about arrays and it's not the case.

0

There are 0 answers