gpt4 book ai didi

c# - 用于缩短代码的动态图像名称

转载 作者:行者123 更新时间:2023-11-30 14:06:35 24 4
gpt4 key购买 nike

我有一个很长的代码,我调用它来检查棋盘上的图 block ,并向具有相同图 block 名称的图片框显示正确的图像。

    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

如果图 block 有阴影,我会使用以“S”开头(如果不是“U”的话)开头的图像名称。它还会检查该片的值与图像名称几乎相同的片。

我想以这样的方式(如果可能的话)缩短代码,使用名为的图像字典

tiles[xtile].Image = ?????."S"+piece;

或类似的东西。

这是 image names 的示例我用过。

最佳答案

可以按名称检索资源:

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);

关于c# - 用于缩短代码的动态图像名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46946609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com