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 ax 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 ?
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();
}
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.