Prev | Current Page 763 | Next

Frank Zammetti

"Practical DWR 2 Projects"


Speaking of picking a tile, we skipped over the pickTile() method earlier, so let??™s take a
look at that now:
private int pickTile(final boolean inSecondTile) {
Random generator = new Random();
if (inSecondTile) {
// When it's the second tile, there's initially a 10% chance the opponent
// will simply choose the right matching tile; otherwise fall through to
// a random choice. However, as the opponent finds more matches, the
// percentage increases slightly to simulate a player with a memory
// (albeit not a great memory!).
int choosePercent = generator.nextInt(100);
if (choosePercent < (10 + matches)) {
log.debug("pickTile() - Choosing matching tile");
for (int i = 0; i < 42; i++) {
if (tileValues[i] == tileValues[flippedTile1] && i != flippedTile1) {
if (log.isDebugEnabled()) {
log.debug("pickTile() - Matching tile = " + i);
}
return i;
}
}
}
}
int tile = generator.nextInt(42);
// Keep picking until we pick one that isn't already matched and that
// isn't currently flipped.
while (tileStates[tile] == 1 || tile == flippedTile1 ||
tile == flippedTile2) {
tile = generator.nextInt(42);
}
if (log.isDebugEnabled()) {
log.debug("pickTile() - tile = " + tile);
}
return tile;
} // End pickTile().


Pages:
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775