Prev | Current Page 746 | Next

Frank Zammetti

"Practical DWR 2 Projects"


int tile1 = generator.nextInt(42);
while (tilesSetAlready.get(new Integer(tile1)) != null) {
tile1 = generator.nextInt(42);
}
// Pick another tile, and keep doing it until one that isn't set is found.
tilesSetAlready.put(new Integer(tile1), new Object());
int tile2 = generator.nextInt(42);
while (tilesSetAlready.get(new Integer(tile2)) != null) {
tile2 = generator.nextInt(42);
}
tilesSetAlready.put(new Integer(tile2), new Object());
// Set the tiles to the next tile type.
grid[tile1] = i;
grid[tile2] = i;
}
log.trace("generateGrid() - Exit");
return grid;
} // End generateGrid().
Since the goal is to place two copies of each of the 21 different tile types (because, remember,
there are 42 tiles in a grid), we begin a loop from 1 to 21. For each, we randomly choose a
CHAPTER 8 n DWR FOR FUN AND PROFIT (A DWR GAME!) 443
tile number from 0 to 41. Then, we see whether that tile has already been chosen by looking it
up in the tilesSetAlready Map. If it??™s already been used, we pick another tile and try again. If
it??™s not used, we record it in that Map and select another, and again do the same check and
repeat the choice if it??™s already used. Finally, when we have two tiles that haven??™t been previously
set, we go ahead and set their values to the current value of the loop, and then continue
the loop.


Pages:
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758