The TileGame problem from the 2009 AP Computer Science Exam is typical of free response problems that test Lists. The problem requires you to manipulate a List and the objects inside.
Review the TileGame free response problem with AP CS Tutor Brandon Horn.
TileGame Part (a): getIndexForFit
private int getIndexForFit(NumberTile tile)
{
if (board.size() == 0 || tile.getRight() == board.get(0).getLeft())
return 0;
if (tile.getLeft() == board.get(board.size() - 1).getRight())
return board.size();
for(int i = 1; i < board.size(); i++)
if(board.get(i - 1).getRight() == tile.getLeft() &&
board.get(i).getLeft() == tile.getRight())
return i;
return -1;
}
TileGame Part (b): insertTile
public boolean insertTile(NumberTile tile)
{
for (int i = 1; i <= 4; i++)
{
int index = getIndexForFit(tile);
if (index != -1)
{
board.add(index, tile);
return true;
}
tile.rotate();
}
return false;
}
2009 AP CS Exam Solutions & Explanations |
Recommended Practice Problems |