C#tic tac toe帮助!初学者

时间:2016-02-21 17:36:39

标签: c#

我是c#的初学者,我正在制作一个tic tac toe游戏。我无法理解这个代码对于每个图片框的点击属性有什么问题我有9,当我在视觉学习上运行时它会中断:

 private void pbxSquare0_Click(object sender, EventArgs e)
 {
 PictureBox pct = (PictureBox)sender;
 int pic = Convert.ToInt32((pct.Name).Substring(10,1));
        count++;

 //switching the condition for each picture box
  switch (pic)
  {
              case 1:
               {
                    if (pict1 == 0) { pict1++; pbxSquare0.BackgroundImage = pictr; }
                    else if (pict1 == 2) { pbxSquare0.Enabled = false; } break;
                }
            case 2:
                {
                    if (pict2 == 0) { pict2++; pbxSquare1.BackgroundImage = pictr; }
                    else if (pict2 == 2) { pbxSquare1.Enabled = false; } break;
                }
            case 3:
                {
                    if (pict3 == 0) { pict3++; pbxSquare2.BackgroundImage = pictr; }
                    else if (pict3 == 2) { pbxSquare2.Enabled = false; } break;
                }
            case 4:
                {
                    if (pict4 == 0) { pict4++; pbxSquare3.BackgroundImage = pictr; }
                    else if (pict4 == 2) { pbxSquare3.Enabled = false; } break;
                }
            case 5:
                {
                    if (pict5 == 0) { pict5++; pbxSquare4.BackgroundImage = pictr; }
                    else if (pict5 == 2) { pbxSquare4.Enabled = false; } break;
                }
            case 6:
                {
                    if (pict6 == 0) { pict6++; pbxSquare5.BackgroundImage = pictr; }
                    else if (pict6 == 2) { pbxSquare5.Enabled = false; } break;
                }
            case 7:
                {
                    if (pict7 == 0) { pict7++; pbxSquare6.BackgroundImage = pictr; }
                    else if (pict7 == 2) { pbxSquare7.Enabled = false; } break;
                }
            case 8:
                {
                    if (pict8 == 0) { pict8++; pbxSquare7.BackgroundImage = pictr; }
                    else if (pict8 == 2) { pbxSquare7.Enabled = false; } break;
                }
            case 9:
                {
                    if (pict9 == 0) { pict9++; pbxSquare8.BackgroundImage = pictr; }
                    else if (pict9 == 2) { pbxSquare8.Enabled = false; } break;
                }
            default: break;

1 个答案:

答案 0 :(得分:1)

这不是完整的代码,但我确实看到了一个问题:

您正在使用组件名称来确定单击了哪个框。 (不好的想法 - 您应该使用Tag属性!)但是,该开关语句中的框标记为0到8 - 但从名称中提取的框号在1到9范围内。

这是否导致我不知道的崩溃 - 你没有完整的代码来看看会发生什么。

另外,查看列表或数组来保存你的盒子 - 这里没有理由使用switch语句。