我有一个非常长的代码,我打电话来检查棋盘上的瓷砖,并将相应的图像显示到具有相同图块名称的图片框。
private void DisplayBoardDisplayTile(string xtile, string piece)
{
if (xtile == "a1")
{
if (piece == "0000") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0000; } //empty
if (piece == "0001") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0001; } //white pawn
if (piece == "0010") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0010; } //white rook
if (piece == "0011") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0011; } //white knight
if (piece == "0100") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0100; } //white bishop
if (piece == "0101") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0101; } //white queen
if (piece == "0110") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0110; } //white king
if (piece == "0111") { a1.Image = WindowsFormsApplication5.Properties.Resources.S0111; } //black pawn
if (piece == "1000") { a1.Image = WindowsFormsApplication5.Properties.Resources.S1000; } //black rook
if (piece == "1001") { a1.Image = WindowsFormsApplication5.Properties.Resources.S1001; } //black knight
if (piece == "1010") { a1.Image = WindowsFormsApplication5.Properties.Resources.S1010; } //black bishop
if (piece == "1011") { a1.Image = WindowsFormsApplication5.Properties.Resources.S1011; } //black queen
if (piece == "1100") { a1.Image = WindowsFormsApplication5.Properties.Resources.S1100; } //black king
}
完整代码位于以下链接:https://pastebin.com/XLbEeZZH
如果图块是阴影的,我使用的图像名称以" S"如果不是" U"。 它还会检查作品的值与图像名称几乎相同的作品。
我想以这种方式缩短代码(如果可能的话)使用名为
的图像词典tiles[xtile].Image = ?????."S"+piece;
或类似的东西。
以下是我使用的image names的示例。
答案 0 :(得分:4)
可以按名称检索资源:
Image img = (Bitmap)WindowsFormsApplication5.Properties.Resources.ResourceManager.GetObject("S0000");
并且可以使用少量位运算符(未测试)计算S或U:
(this.Controls[xtile] as PictureBox).Image = (Bitmap)WindowsFormsApplication5
.Properties.Resources.ResourceManager.GetObject("SU"[xtile[0] + xtile[1] & 1] + piece);
答案 1 :(得分:3)
您需要设置一些字典,以便从xtile
和piece
进行映射,以便将代码缩小到您刚才去的一行_tiles[xtile].Image = _shadings[xtile][piece];
。
试试这个:
private static Dictionary<string, System.Drawing.Image> _shaded = new Dictionary<string, System.Drawing.Image>()
{
{ "0000", WindowsFormsApplication5.Properties.Resources.S0000 },
{ "0001", WindowsFormsApplication5.Properties.Resources.S0001 },
//etc
{ "1100", WindowsFormsApplication5.Properties.Resources.S1101 },
};
private static Dictionary<string, System.Drawing.Image> _unshaded = new Dictionary<string, System.Drawing.Image>()
{
{ "0000", WindowsFormsApplication5.Properties.Resources.U0000 },
{ "0001", WindowsFormsApplication5.Properties.Resources.U0001 },
// etc
{ "1100", WindowsFormsApplication5.Properties.Resources.U1101 },
};
private static Dictionary<string, PictureBox> _tiles = new Dictionary<string, PictureBox>()
{
{ "a1", a1 },
{ "b1", b1 },
// etc
{ "h8", h8 },
};
private static Dictionary<string, Dictionary<string, System.Drawing.Image>> _shadings = new Dictionary<string, Dictionary<string, System.Drawing.Image>>()
{
{ "a1", _shaded },
{ "b1", _unshaded },
// etc
{ "h8", _shaded },
};
private void DisplayBoardDisplayTile(string xtile, string piece)
{
_tiles[xtile].Image = _shadings[xtile][piece];
}