/** Returns true if at least one space on the Board is empty. * Empty spaces are stored as null. **/ publicstaticbooleanemptySpaceExists(Board b) { // TODO: Fill in this function. returnfalse; }
如何获取板子的长度和宽度?
观察 Board 类,我们发现函数:
1 2 3 4
/** Returns the size of the board. */ publicintsize() { return values.length; }
所以,调用 b.size() 即可获得板子的长度。 由此,我们可以写出大体框架。
1 2 3 4 5 6 7 8 9 10
publicstaticbooleanemptySpaceExists(Board b) { // TODO: Fill in this function. for(introw=0; row < b.size(); row ++) { for(intcol=0; col < b.size(); col ++) { if(...) returntrue; } } returnfalse; }
可见,if 判断框里面的条件就是 格子为空。观察 Board 类和 Tile 类
b.tile(col, row) 返回 Null 如果这个格子为空
因此,最终的代码为:
1 2 3 4 5 6 7 8 9 10
publicstaticbooleanemptySpaceExists(Board b) { // TODO: Fill in this function. for(introw=0; row < b.size(); row ++) { for(intcol=0; col < b.size(); col ++) { if(b.tile(col, row) == null) returntrue; } } returnfalse; }
maxTileExists
1 2 3 4 5 6
/** * Returns true if any tile is equal to the maximum valid value. * Maximum valid value is given by MAX_PIECE. Note that * given a Tile object t, we get its value with t.value(). */ publicstaticbooleanmaxTileExists(Board b) { // TODO: Fill in this function. returnfalse; }
/** Return the value supplied to my constructor. */ publicintvalue() { return value; }
所以能写出
1 2 3 4 5 6 7 8 9 10
publicstaticbooleanmaxTileExists(Board b) { // TODO: Fill in this function. for(intcol=0; col < b.size(); col ++) { for(introw=0; row < b.size(); row ++) { if(b.tile(col, row).value() == MAX_PIECE) returntrue; } } returnfalse; }
然而,测试出错。
因为没有考虑到空格子的问题。 所以我们加上
1 2
if(b.tile(col, row) == null) continue;
最终的代码为:
1 2 3 4 5 6 7 8 9 10 11 12
publicstaticbooleanmaxTileExists(Board b) { // TODO: Fill in this function. for(intcol=0; col < b.size(); col ++) { for(introw=0; row < b.size(); row ++) { if(b.tile(col, row) == null) continue; if(b.tile(col, row).value() == MAX_PIECE) returntrue; } } returnfalse; }
atLeastOneMoveExists
1 2 3 4 5 6
/** * Returns true if there are any valid moves on the board. * There are two ways that there can be valid moves: * 1. There is at least one empty space on the board. * 2. There are two adjacent tiles with the same value. */ publicstaticbooleanatLeastOneMoveExists(Board b) { // TODO: Fill in this function. returnfalse; }
返回 true 的情况之一:
至少存在一个空格子
至少有两个相邻格子的值相同 其中条件一也就是 emptySpaceExists 返回 true
1 2 3 4 5 6 7
publicstaticbooleanatLeastOneMoveExists(Board b) { // TODO: Fill in this function. if(emptySpaceExists(b)) returntrue; returnfalse; }
publicstaticbooleanatLeastOneMoveExists(Board b) { // TODO: Fill in this function. if(emptySpaceExists(b)) returntrue; booleantwo=false; int[] dx = {-1, 0, 1, 0}; int[] dy = {0, 1, 0, -1}; intsize= b.size(); for(intcol=0; col < size; col ++) { for(introw=0; row < size; row ++){ intvalue_cur= b.tile(col, row).value(); for(intk=0; k < 4; k ++){ intcur_col= col + dy[k]; intcur_row= row + dx[k]; if(cur_col > 0 && cur_col < size && cur_row > 0 && cur_row < size && b.tile(cur_col, cur_row).value() == value_cur) returntrue; } } } returnfalse; }
Building the Game Logic
tilt
1 2 3 4 5 6 7 8 9 10 11 12 13
publicbooleantilt(Side side) { boolean changed; changed = false; // TODO: Modify this.board (and perhaps this.score) to account // for the tilt to the Side SIDE. If the board changed, set the // changed local variable to true. checkGameOver(); if (changed) { setChanged(); } return changed; }
// for the tilt to the Side SIDE. If the board changed, set the // changed local variable to true. checkGameOver(); if (changed) { setChanged(); } return changed; }
但是上述代码只针对了方向向上。 官方提供了函数 $setViewingPerspective$,
For this problem, we’ve given away a clean solution. This will allow you to handle the other three directions with only two additional lines of code! Specifically, the Board class has a setViewingPerspective(Side s) function that will change the behavior of the tile and move classes so that they _behave as if the given side was NORTH_.
Important: Make sure to use board.setViewingPerpsective to set the perspective back to Side.NORTH before you finish your call to tilt, otherwise weird stuff will happen.
// for the tilt to the Side SIDE. If the board changed, set the // changed local variable to true. checkGameOver(); if (changed) { setChanged(); } return changed; }