检查 pictureBox 数组中的所有 pictureBoxes - 跳棋游戏?
Check all pictureBoxes in pictureBox Array - Checkers Game?
大家好,我正在制作跳棋游戏。我有这种宣布获胜者的方法,我希望是否有办法使用循环来做到这一点。目前它工作得很好 :),但我不得不对其进行硬编码而不是使用逻辑。
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!");
}
}
我无法使用从 0 到 63 的循环,因为 pictureBoxs[1]
或 pictureBoxs[3]
设置为空并且会产生错误!
使用 LINQ 仅抓取 不是 null
的 PictureBox
,然后测试 Image
的值:
if (pictureBoxs.Where(p => p != null).All(p => p.Image != bc))
MessageBox.Show("White is the winner!");
else if (pictureBoxs.Where(p => p != null).All(p => p.Image != wc))
MessageBox.Show("Black is the winner!");
您可以像这样创建一个 '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!");
}
大家好,我正在制作跳棋游戏。我有这种宣布获胜者的方法,我希望是否有办法使用循环来做到这一点。目前它工作得很好 :),但我不得不对其进行硬编码而不是使用逻辑。
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!");
}
}
我无法使用从 0 到 63 的循环,因为 pictureBoxs[1]
或 pictureBoxs[3]
设置为空并且会产生错误!
使用 LINQ 仅抓取 不是 null
的 PictureBox
,然后测试 Image
的值:
if (pictureBoxs.Where(p => p != null).All(p => p.Image != bc))
MessageBox.Show("White is the winner!");
else if (pictureBoxs.Where(p => p != null).All(p => p.Image != wc))
MessageBox.Show("Black is the winner!");
您可以像这样创建一个 '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!");
}