proj0
proj0
emptySpaceExists(Board b)
我的实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/** Returns true if at least one space on the Board is empty.
* Empty spaces are stored as null.
* */
public static boolean emptySpaceExists(Board b) {
int n = b.size();
boolean is=false;
Tile tl;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
tl=b.tile(i,j);
if(tl.value()==0){
is=true;
}
}
}
return is;
}
Claude:
这个方法有几个问题:
注释说空格存储为
null
(Empty spaces are stored as null),但代码中却检查tl.value()==0
。如果空格真的是null
,那么调用tl.value()
会导致NullPointerException
。代码效率不高,即使找到一个空格,循环仍会继续检查所有格子。
变量名
is
不够描述性。
根据注释,正确的实现应该是:
1
2
3
4
5
6
7
8
9
10
11
public static boolean emptySpaceExists(Board b) {
int n = b.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (b.tile(i, j) == null) {
return true; // 找到空格立即返回
}
}
}
return false;
}
如果实际上空格是以值为0的瓦片表示(而不是null),那么注释需要修改,并且代码应该在找到第一个空格时立即返回:
1
2
3
4
5
6
7
8
9
10
11
12
public static boolean emptySpaceExists(Board b) {
int n = b.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Tile tl = b.tile(i, j);
if (tl != null && tl.value() == 0) {
return true;
}
}
}
return false;
}
This post is licensed under CC BY 4.0 by the author.