Search code examples
c#consoleconsole-applicationchess

C# console app chess table


I'm trying to create a chess table and output it on the screen also I want to be able to add x's in the table. For example if I want to show that the current position of a figure is on A,2 a x appears there.. I currently have only the table displayed and it's not even contained in array :

    private static readonly string[] letters = { "A", "B", "C", "D", "E", "F", "G", "H" };
    private const int size = 8;
    private static void Main()
    {
        const string top = " -----------------";
        const string line = "| | | | | | | | |";
        for (int i = 0; i < size; i++)
        {
            Console.WriteLine(" {0}", top);
            Console.WriteLine("{0} {1}", size - i, line);
        }
        Console.WriteLine(" {0}", top);
        Console.Write("   ");
        for (int i = 0; i < size; i++)
        {
            Console.Write("{0} ",letters[i]);
        }
        Console.ReadKey();
    }

However I have absolutely no access or control over the table it's just drawn. I want to be able to put the "x" in between the free space here : |x| how can I put this table in some sort of jagged array/2d array or a nested list ?


Solution

  • create a 2D array of chars : char[x][y] boxes where x is the width of the board and y is the height.

    Initialize every char to a white space.

    Set desired spot to desired char : boxes[2][2] = 'x'

    Make a loop:

    for(int y = 0; y < boxes.length; y++)
    {
        //print line
        Console.Write("|")
        for(int x = 0; x < boxes[0].length; x++)
        {
            //Show char at that spot
            Console.Write("{0}|", boxes[y][x]);
        }
        //procede to next line
        Console.WriteLine();
    }