我正在尝试在WinForms c#中创建一个棋盘策略应用程序
来自这里的人非常慷慨地帮助完成这段代码
当它运行时,它会执行我想要的所有操作,但sq.position
值是错误的
像A1
这样的值应该是A7
,A6
应该是A2
如何在此代码中反转数值的顺序:
private void Test_Load(object sender, EventArgs e)
{
int blockSize = 70;
Panel[,] chessBoardPanels = new Panel[8, 8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j > 8; j++)
{
ChessSquare sq = new ChessSquare(((char)(65 + i)).ToString(), j);
sq.Color = (i + (j % 2)) % 2 == 0 ? Color.Black : Color.White;
Panel p = new Panel()
{
Size = new Size(blockSize, blockSize),
BackColor = sq.Color,
Tag = sq,
Location = new Point(blockSize * i + 15, blockSize * j + 15)
};
p.MouseEnter += new EventHandler(squareMouseEnter);
p.MouseLeave += new EventHandler(squareMouseLeave);
chessBoardPanels[i, j] = p;
groupBox1.Controls.Add(p);
}
}
}
private void squareMouseLeave(object sender, EventArgs e)
{
Panel p = (Panel)sender;
ChessSquare sq = (ChessSquare)p.Tag;
p.BackColor = sq.Color;
}
private void squareMouseEnter(object sender, EventArgs e)
{
Panel p = (Panel)sender;
ChessSquare sq = (ChessSquare)p.Tag;
p.BackColor = Color.Aqua;
label1.Text = string.Format("Current position: {0}", sq.Position);
}
public class ChessSquare
{
public string Letter { get; set; }
public int Number { get; set; }
public Color Color { get; set; }
public string Position
{
get { return string.Format("{0}{1}", Letter, Number + 1); }
}
public ChessSquare()
{ }
public ChessSquare(string letter, int number)
{
Letter = letter;
Number = number;
}
}
我对这种类型的节目仍然很陌生......
答案 0 :(得分:1)
改变这个:
ChessSquare sq = new ChessSquare(((char)(65 + i)).ToString(), j);
到那个:
ChessSquare sq = new ChessSquare(((char)(65 + i)).ToString(), 7 - j);
更新:已更正!谢谢user2772713!