Page 1 of 1

Storing color in a string c#

Posted: Wed Mar 21, 2012 10:35 pm
by mattykins
Is it at all possible to store color inside of a string variable? so lets say for instance i have a game board that looks like this

Code: Select all

#################
#                          
#  P                      
#                          
#################
And all the spaces are normal color except for the P, say i want the P to be blue. Is there anyway to store this inside of a string variable? Because right now my board update looks like:

Code: Select all

static public void boardWrite()
        {
            Console.Clear();
            board.Remove(0, board.Length);
            for (int y = 0; y < 20; y++)
            {
                int i = 0;
                for (int x = 0; x < 80; x++)
                {
                    i++;
                    
                    if (i < 80)
                    {
                        board.Append(tiles[x, y]);
                    }
                    else
                    {
                        board.Append("\n");
                        i = 0;
                    }
                    
                }

            }
            Console.Write(board);
            

        }
I use stringbuilder to create one long string variable with a \n for a new line every 80 characters, however this method does not let me assign color to individual characters in the string. Any suggestions to either a new board writing method or a way to store color inside the string method?

Re: Storing color in a string c#

Posted: Wed Mar 21, 2012 11:22 pm
by Callan S.
How are you constructing the strings?

I'd think that if instead of recording how many characters there are, as you build the string you have the code just record characters and spaces in an integer and when it gets to 80 make a new row. That way you can just stick in the colour changes.

If you're building it manually, you could have it be that every two spaces is one character, the first space giving the colour (coded to a single character), the second is the character to show. Then have the program go through and convert the colour codes to html colour codes before printing it.

Re: Storing color in a string c#

Posted: Wed Mar 21, 2012 11:39 pm
by mattykins
Hmm, I'm a bit confused, how would i store the color codes?

Re: Storing color in a string c#

Posted: Thu Mar 22, 2012 12:29 am
by Jackolantern
I believe you could still use the Color structure in a console application. It is part of System.Drawing, which is usually for GUI building, but I don't see why you couldn't import it (which may require an assembly reference; not sure since the only time I have used it VS added it for me) and use it since it is just a regular struct and doesn't actually depend on any GUI frameworks.

Re: Storing color in a string c#

Posted: Thu Mar 22, 2012 12:36 am
by Callan S.
I guess it's not spitting it onto a web page, is it? I was kinda thinking that for some reason.

Well, I'd go with the prior character designating the colour, then the program goes through each character and figures the right colour to use to print the next character. A string with

"r#g$b@"

would come out as

#$@

where r=red, g=green, b=blue

C# has functions for getting a sub string from a string, right? You'd use a while loop, each time grabbing character at X for the colour, then X+1 for the character to show, then incrementing X by 2 until you reach the end of the string.

I'm just guessing that I understand the problem. I don't, for example, know the commands in #C to change the font colour used in outputting a string.

Re: Storing color in a string c#

Posted: Thu Mar 22, 2012 12:56 am
by mattykins
For using system.drawing how would i use that inside a console? I modified the example on MDSN in the link you gave me but still can't figure out how to use that to set different console colors.

FYI Console Colors are normally changed by

Code: Select all

Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("Hello World!"); //This would output Hello World! in blue font on a console.
Console.ResetColor();

Console.BackgroundColor = ConsoleColor.Blue;
Console.Write("Hello World!"); //This would output Hello World in the default font color with a blue background
Console.ResetColor();
//And you can use any combination of background and foreground color for different font colors w/ different backgrounds.
If that helps.

Thanks though everyone :)

EDIT:Oh and its not spitting it out on to a webpage. And i'm not sure if C# has that function...It probably does but idk.