soso soso -3 years ago 81
C# Question

Trying to replace the two spaces after the matched pattern in c#

I am currently making a text based game. I am drawing a map using ASCII. I want to place an "@" sign in the room where the player is located. the four square boxes are the rooms. Here is the code:

string map = @"
+--------+
| |
|| ||
| |
+---||---+
||
+-||-+
|| ||
+-||-+
||
+-||-+
|| ||
+-||-+
||
+-||-+
|| ||
+- -+";
//this doesn't work lol
map = map.Replace("||","@");


It is for an assignment.

Answer Source

To answer your direct question, assuming that there is a part of your map string that has two spaces after two vertical bars and you want to replace the second space with an @ symbol, you could do this:

map.Replace("||  ", "|| @");

Which of course would replace every instance of the search string, scattering @ symbols throughout your map, and I strongly doubt that you want this to happen.

In fact I don't think it's a good idea to change the map string at all. Instead you would be better served by keeping the map data separate from the player position data and handling the output differently.

For example, if you are allowed to use the Console object's positioning commands you can do something like this:

int playerX;
int playerY;
string map;

public void Draw()
{
    Console.Clear();
    Console.Write(map);
    // save cursor position
    int l = Console.CursorLeft;
    int t = Console.CursorTop;
    // display player marker
    Console.SetCursorPosition(playerX, playerY);
    Console.Write("@");
    // restore cursor position
    Console.SetCursorPosition(l, t);
}

You can use cursor positioning to do all sorts of fun things, just like we used to in the good old days of ANSI art. Ah, nostalgia.

If you can't use - or for some reason can't rely on - cursor positioning you will need to get a bit more creative. One method is to break the map down into lines and figure out which one needs to have the player mark added right before you do the output:

public void Draw()
{
    string[] lines = map.Replace("\r", "").Split('\n');
    for(int y = 0; y < lines.Length; y++)
    {
        string curr = lines[y];
        if (y == playerY)
            curr = curr.Substring(0, playerX) + "@" + curr.Substring(playerX + 1);
        Console.WriteLine(curr);
    }
}

Of course this will blow up as soon as the player movement goes outside of the area covered by the map string, or hits the right-most character of the line.

Another option would be to create an array of map cells with information in each cell about what is there. The map string is used as input for generating the map array, which is used for all operations after that. The draw loop for that would then test against the player position to determine whether it will draw the player's marker or whatever character the map cell is represented by. You can also use this array for testing whether a move is valid (empty map cell), adding things like cell content (treasure!), etc.

The code for which is well outside the scope of this answer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download