gpt4 book ai didi

c# - 检查 pictureBox 数组中的所有 pictureBoxes - 跳棋游戏?

转载 作者:行者123 更新时间:2023-11-30 20:44:13 30 4
gpt4 key购买 nike

大家好,我正在开发跳棋游戏。我有这种宣布获胜者的方法,我希望是否有办法使用循环来做到这一点。目前它工作得很好 :),但我不得不对其进行硬编码而不是使用逻辑。

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 只抓取不是nullPictureBox,然后测试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!");

关于c# - 检查 pictureBox 数组中的所有 pictureBoxes - 跳棋游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29690623/

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