0

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 ?

KOPEUE
  • 260
  • 4
  • 15

2 Answers2

1

Look at my answer here: Tic-tac-toe code help improve

I think this is exactly what you are looking for. In your case it might be sufficient to use an array of bool, because you only want to store two states (empty or X).

private static readonly string[] letters = { "A", "B", "C", "D", "E", "F", "G", "H" };
    private const int size = 8;

    private static bool[,] chessboard;

    private static void Main()
    {
        const string top = " -----------------";

        //init chessboard
        chessboard = new bool[size, size];

        //place a figure on field 4/6 for demonstration
        chessboard[4, 6] = true;

        for (int y = 0; y < size; y++)
        {
            Console.WriteLine(" {0}", top);
            Console.Write("{0} ", size - y);
            for (int x = 0; x < size; x++)
            {
                Console.Write("|{0}", chessboard[x, y] ? 'X' : ' ');
            }
            Console.WriteLine("|");
        }

        Console.Write("   ");
        for (int i = 0; i < size; i++)
        {
            Console.Write("{0} ", letters[i]);
        }
        Console.ReadKey();
    }
Community
  • 1
  • 1
Mario Dekena
  • 843
  • 6
  • 20
  • This doesn't help's me at all can you provide a sample code ? – KOPEUE Mar 17 '16 at 22:42
  • I think using char/string is a lot better than bool like shown in the FreshWaterTaffy's answer and simply put the value of the char in there not checking and than putting – KOPEUE Mar 17 '16 at 22:59
  • Better really depends on the usecase. I had real games in mind where the data structure is most likely an array of enum or even object. But for your textbased console application i agree. Its also easier to add another symbol. If you know for sure that you will only use X and empty, i would still go with bool though ;) – Mario Dekena Mar 17 '16 at 23:03
  • The code should always be as flexible as possible for example adding third symbol will break your logic using boolean variable however it will work fine with char/string array.. – KOPEUE Mar 17 '16 at 23:06
  • 1
    The reason this is a bad idea for a real game is, that you have far more states then you need. While this doesnt sound bad at all at first, it gets problematic if you have a complicated game logic. Your have to ensure your algorithms can operate on a defined gamestate. With the possibility to add a figure that doesnt exist in the game you create error prone code. But as i said, for your case it is probably fine. – Mario Dekena Mar 17 '16 at 23:10
0

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();
}
FreshWaterTaffy
  • 270
  • 1
  • 2
  • 18