c# writing to the console help [SOLVED]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

c# writing to the console help [SOLVED]

Post by mattykins »

So in a little console game i'm working on there is a draw board method that i use to draw all the tiles. it currently looks like this:

Code: Select all

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

Each time the initial for loop iterates the second for loop iterates 80 times, or for each y value there are 80 x values making a 17 x 80 board of tiles. The glaring problem with this code is that it has to do 1,370 Console.Write's every time the board updates(which is everytime the player moves). My question is would there be a more efficient way to handle the writing of the board.

if it helps here is a link to where you can download the project, as of now the only thing that works is collision detection and movement(use WASD to move):
http://www.mediafire.com/?g4queiudjsrvaab
Last edited by mattykins on Fri Mar 16, 2012 9:51 pm, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: c# writing to the console help

Post by Jackolantern »

Situations like this can be helped by the C# StringBuilder class. You could build up the string using StringBuilder in memory, and then output the whole constructed string at once, or maybe once for each line. Here is a tutorial about it as well. 8-)
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: c# writing to the console help

Post by mattykins »

That helped so much thanks! :)

The revised code looks like:

Code: Select all


        static public void boardWrite()
        {
            Console.Clear();
            board.Remove(0, board.Length);
            for (int y = 0; y < 17; 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);
        }
It is extremely smooth now there is no screen flashing or anything like that :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: c# writing to the console help [SOLVED]

Post by Jackolantern »

Awesome! Glad it helped out! :D
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”