Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 10th, 2013  |  syntax: Java  |  size: 3.79 KB  |  views: 1,196  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. private void initTiles()
  2. {
  3.         // Forgive me Gang of Four wherever you are.
  4.         /* 4 fields, 4 forest, 4 pasture, 3 mountains, 3 hills, 1 desert
  5.            19 total
  6.            + water */
  7.         /* These 18+1 tiles represent the positions (hungarian-notated as p)
  8.            on the board. Their roll numbers and terrain types will be assigned
  9.            randomly, from the contents of a game box. */
  10.         Tile.UnfinishedTile p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
  11.                 p12, p13, p14, p15, p16, p17, p18, water;
  12.         /* This line initializes all the ts to null so that I can refer to them
  13.            later, because otherwise it thinks the try block can fail and leave
  14.            them uninitialized, even though the exception is never actually
  15.            thrown. */
  16.         p0=p1=p2=p3=p4=p5=p6=p7=p8=p9=p10=p11=p12=p13=p14=p15=p16=p17=p18=null;
  17.         water = null;
  18.         /* These numbers are the tokens that come in the box: */
  19.         Integer[] ns = shuffle(
  20.                 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12
  21.         );
  22.         /* And these are how many of each terrain type comes in the box: */
  23.         Tile.Terrain[] ts = shuffle(
  24.                 FIELD, FIELD, FIELD, FIELD,
  25.                 FOREST, FOREST, FOREST, FOREST,
  26.                 PASTURE, PASTURE, PASTURE, PASTURE,
  27.                 MOUNTAIN, MOUNTAIN, MOUNTAIN,
  28.                 HILL, HILL, HILL,
  29.                 DESERT
  30.         );
  31.         try { /* Here we're assigning the terrains (ts) and the numbers (ns) */
  32.                 int i=0;
  33.                 p0 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  34.                 p1 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  35.                 p2 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  36.                 p3 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  37.                 p4 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  38.                 p5 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  39.                 p6 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  40.                 p7 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  41.                 p8 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  42.                 p9 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  43.                 p10 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  44.                 p11 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  45.                 p12 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  46.                 p13 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  47.                 p14 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  48.                 p15 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  49.                 p16 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  50.                 p17 = new Tile.UnfinishedTile(ts[i], ns[i++]);
  51.                 p18 = new Tile.UnfinishedTile(ts[i], ns[i]);
  52.                 water = new Tile.UnfinishedTile(WATER, 0);
  53.         } catch (Tile.NeedsNeighborsException e) {
  54.                 // Duly noted, and addressed below.
  55.                 /* This exception is never thrown by any code, but it exists as
  56.                    a checked exception to ensure that everyone who uses an
  57.                    UnfinishedTile is reminded and forced to acknowledge that the
  58.                    tile is unfinished and its neighbors must be set */
  59.         }
  60.         /* We know about magic numbers and magic strings, here's magic code.
  61.            I came up with this unreadable abomination by taking a hex grid
  62.            and labelling it in an organized manner with its positions.
  63.            Then I transcribed the neighbors of each hex into this code.
  64.          */
  65.         p0.setNeighbors(water, water, p2, p4, p1, water);
  66.         p1.setNeighbors(water, p0, p4, p6, p3, water);
  67.         p2.setNeighbors(water, water, p5, p7, p4, p0);
  68.         p3.setNeighbors(water, p1, p6, p8, water, water);
  69.         p4.setNeighbors(p0, p2, p7, p9, p6, p1);
  70.         p5.setNeighbors(water, water, water, p10, p7, p2);
  71.         p6.setNeighbors(p1, p4, p9, p11, p8, p3);
  72.         p7.setNeighbors(p2, p5, p10, p12, p9, p4);
  73.         p8.setNeighbors(p3, p6, p11, p13, water, water);
  74.         p9.setNeighbors(p4, p7, p12, p14, p11, p6);
  75.         p10.setNeighbors(p5, water, water, p15, p12, p7);
  76.         p11.setNeighbors(p6, p9, p14, p16, p13, p8);
  77.         p12.setNeighbors(p7, p10, p15, p17, p14, p9);
  78.         p13.setNeighbors(p8, p11, p16, water, water, water);
  79.         p14.setNeighbors(p9, p12, p17, p18, p16, p11);
  80.         p15.setNeighbors(p10, water, water, water, p17, p12);
  81.         p16.setNeighbors(p11, p14, p18, water, water, p13);
  82.         p17.setNeighbors(p12, p15, water, water, p18, p14);
  83.         p18.setNeighbors(p14, p17, water, water, water, p16);
  84. }
clone this paste RAW Paste Data