C# 控制台应用国际象棋 table
C# console app chess table
我正在尝试创建国际象棋 table 并将其输出到屏幕上我还希望能够在 table 中添加 x。例如,如果我想显示一个图形的当前位置在 A 上,2 那里会出现一个 x。我目前只显示 table,它甚至不包含在数组中:
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();
}
但是我完全无法访问或控制刚刚绘制的 table。我希望能够将 "x" 置于此处的免费 space 之间:|x| 如何将此 table 置于某种形式锯齿状 array/2d 数组或嵌套列表 ?
看我的回答:Tic-tac-toe code help improve
我想这正是您要找的。
在您的情况下,使用 bool 数组可能就足够了,因为您只想存储两个状态(空或 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();
}
创建一个二维字符数组:char[x][y] boxes
其中 x 是棋盘的宽度,y 是高度。
将每个字符初始化为白色 space。
将所需位置设置为所需字符:boxes[2][2] = 'x'
做一个循环:
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();
}
我正在尝试创建国际象棋 table 并将其输出到屏幕上我还希望能够在 table 中添加 x。例如,如果我想显示一个图形的当前位置在 A 上,2 那里会出现一个 x。我目前只显示 table,它甚至不包含在数组中:
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();
}
但是我完全无法访问或控制刚刚绘制的 table。我希望能够将 "x" 置于此处的免费 space 之间:|x| 如何将此 table 置于某种形式锯齿状 array/2d 数组或嵌套列表 ?
看我的回答:Tic-tac-toe code help improve
我想这正是您要找的。 在您的情况下,使用 bool 数组可能就足够了,因为您只想存储两个状态(空或 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();
}
创建一个二维字符数组:char[x][y] boxes
其中 x 是棋盘的宽度,y 是高度。
将每个字符初始化为白色 space。
将所需位置设置为所需字符:boxes[2][2] = 'x'
做一个循环:
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();
}