gpt4 book ai didi

java - For 循环和 if 语句的行为很奇怪

转载 作者:行者123 更新时间:2023-12-01 18:02:50 25 4
gpt4 key购买 nike

我正在制作一个国际象棋游戏,到目前为止一切都很好,我现在正在编写每个棋子的规则。问题是 for 循环的行为很奇怪。它是针对主教的,所以 x 下降,y 上升。此时,每当我尝试将点添加到可能的移动中时,它都会表现得很奇怪

for (int i = 0; i < 8; i++) {
Point newLoc = new Point(x-i, y+i);

if(team.equals("white")) {
if(containsPiece(newLoc)) {
if(ChessBoard.black.containsKey(newLoc)) {
possibilities.put(newLoc, rating);
break;
}
else {
break;
}
} else
possibilities.put(newLoc, rating);
}

containsPiece() 工作得很好,possibilities 是我存储可能的移动的 HashMap

在我看来,它应该工作得很好,因为如果 newLoc 处的图 block 是白色的,它不应该将其添加到可能的移动中,并且停止在该方向上进行任何移动。有谁明白为什么它似乎放弃了之前添加到可能性

中的所有可能的 Action

What it should look like

What it is looking like with the code above

最佳答案

i 应该从 1 开始,而不是 0,因为当 i==0 时, newLoc 是象的位置 ((x-0,y+0)),因此您可以从循环中中断,因为象是白色瓷砖。

关于java - For 循环和 if 语句的行为很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39367020/

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