检查pictureBox数组中的所有图片盒 - Checkers Game?

时间:2015-04-17 04:53:44

标签: c# loops picturebox

嘿伙计们我正在制作跳棋游戏。我有这个声明获胜者的方法,我希望是否有办法使用循环来做到这一点。目前它工作正常:),但我不得不硬编码而不是使用逻辑。

public void checkWinner() {
  if (pictureBoxs[0].Image != bc && pictureBoxs[2].Image != bc && pictureBoxs[4].Image != bc && pictureBoxs[6].Image != bc && pictureBoxs[9].Image != bc && pictureBoxs[11].Image != bc &&
    pictureBoxs[13].Image != bc && pictureBoxs[15].Image != bc && pictureBoxs[16].Image != bc && pictureBoxs[18].Image != bc && pictureBoxs[20].Image != bc && pictureBoxs[22].Image != bc &&
    pictureBoxs[25].Image != bc && pictureBoxs[27].Image != bc && pictureBoxs[29].Image != bc && pictureBoxs[31].Image != bc && pictureBoxs[32].Image != bc && pictureBoxs[34].Image != bc &&
    pictureBoxs[36].Image != bc && pictureBoxs[38].Image != bc && pictureBoxs[41].Image != bc && pictureBoxs[43].Image != bc && pictureBoxs[45].Image != bc && pictureBoxs[47].Image != bc &&
    pictureBoxs[48].Image != bc && pictureBoxs[50].Image != bc && pictureBoxs[52].Image != bc && pictureBoxs[54].Image != bc && pictureBoxs[57].Image != bc && pictureBoxs[59].Image != bc &&
    pictureBoxs[61].Image != bc && pictureBoxs[63].Image != bc) {
    MessageBox.Show("White is the winner!");

  } else if (pictureBoxs[0].Image != wc && pictureBoxs[2].Image != wc && pictureBoxs[4].Image != wc && pictureBoxs[6].Image != wc && pictureBoxs[9].Image != wc && pictureBoxs[11].Image != wc &&
    pictureBoxs[13].Image != wc && pictureBoxs[15].Image != wc && pictureBoxs[16].Image != wc && pictureBoxs[18].Image != wc && pictureBoxs[20].Image != wc && pictureBoxs[22].Image != wc &&
    pictureBoxs[25].Image != wc && pictureBoxs[27].Image != wc && pictureBoxs[29].Image != wc && pictureBoxs[31].Image != wc && pictureBoxs[32].Image != wc && pictureBoxs[34].Image != wc &&
    pictureBoxs[36].Image != wc && pictureBoxs[38].Image != wc && pictureBoxs[41].Image != wc && pictureBoxs[43].Image != wc && pictureBoxs[45].Image != wc && pictureBoxs[47].Image != wc &&
    pictureBoxs[48].Image != wc && pictureBoxs[50].Image != wc && pictureBoxs[52].Image != wc && pictureBoxs[54].Image != wc && pictureBoxs[57].Image != wc && pictureBoxs[59].Image != wc &&
    pictureBoxs[61].Image != wc && pictureBoxs[63].Image != wc) {
    MessageBox.Show("Black is the winner!");
  }
}

我不能使用for循环从0到63,因为pictureBoxs[1]pictureBoxs[3]设置为空,它会产生错误!

2 个答案:

答案 0 :(得分:3)

使用LINQ仅抓取 PictureBox的{​​{1}},然后测试null的值:

Image

答案 1 :(得分:1)

你可以创建一个' IsWinner'功能如下:

    private bool IsWinner(Image opponent)
    {
        foreach (PictureBox box in pictureBoxes)
        {
            if (
                (box.Image != null)
                && (box.Image == opponent)
                )
            {
                return false;
            }
        }

        return true;
    }

然后这样称呼它:

        if (IsWinner(wc))
        {
            MessageBox.Show("Black is the winner!");
        }
        else if (IsWinner(bc))
        {
             MessageBox.Show("White is the winner!");
        }