Game-of-Life-WSU-CEG4180  v0.1
RefactoringtheGameofLife
ShapeCollection.java
Go to the documentation of this file.
1 /* This page is part of the Game of Life source code */
2 
3 /**
4  * Copyright 1996-2004 Edwin Martin <edwin@bitstorm.nl>
5  * @author Edwin Martin
6  */
7 
8 package org.bitstorm.gameoflife;
9 
10 /**
11  * Contains some nice Game Of Life shapes.
12  * @author Edwin Martin
13  */
14 public class ShapeCollection {
15  private static final Shape CLEAR;
16  private static final Shape GLIDER;
17  private static final Shape SMALLEXPL;
18  private static final Shape EXPLODER;
19  private static final Shape CELL10;
20  private static final Shape FISH;
21  private static final Shape PUMP;
22  private static final Shape SHOOTER;
23  private static final Shape[] COLLECTION;
24 
25  static {
26  CLEAR = new Shape("Clear", new int[][] {} );
27  GLIDER = new Shape("Glider", new int[][] {{1,0}, {2,1}, {2,2}, {1,2}, {0,2}});
28  SMALLEXPL = new Shape("Small Exploder", new int[][] {{0,1}, {0,2}, {1,0}, {1,1}, {1,3}, {2,1}, {2,2}});
29  EXPLODER = new Shape("Exploder", new int[][] {{0,0}, {0,1}, {0,2}, {0,3}, {0,4}, {2,0}, {2,4}, {4,0}, {4,1}, {4,2}, {4,3}, {4,4}});
30  CELL10 = new Shape("10 Cell Row", new int[][] {{0,0}, {1,0}, {2,0}, {3,0}, {4,0}, {5,0}, {6,0}, {7,0}, {8,0}, {9,0}});
31  FISH = new Shape("Lightweight spaceship", new int[][] {{0,1}, {0,3}, {1,0}, {2,0}, {3,0}, {3,3}, {4,0}, {4,1}, {4,2}});
32  PUMP = new Shape("Tumbler", new int[][] {{0,3}, {0,4}, {0,5}, {1,0}, {1,1}, {1,5}, {2,0}, {2,1}, {2,2}, {2,3}, {2,4}, {4,0}, {4,1}, {4,2}, {4,3}, {4,4}, {5,0}, {5,1}, {5,5}, {6,3}, {6,4}, {6,5}});
33  SHOOTER = new Shape("Gosper Glider Gun", new int[][] {{0,2}, {0,3}, {1,2}, {1,3}, {8,3}, {8,4}, {9,2}, {9,4}, {10,2}, {10,3}, {16,4}, {16,5}, {16,6}, {17,4}, {18,5}, {22,1}, {22,2}, {23,0}, {23,2}, {24,0}, {24,1}, {24,12}, {24,13}, {25,12}, {25,14}, {26,12}, {34,0}, {34,1}, {35,0}, {35,1}, {35,7}, {35,8}, {35,9}, {36,7}, {37,8}});
34  COLLECTION = new Shape[] {CLEAR, GLIDER, SMALLEXPL, EXPLODER, CELL10, FISH, PUMP, SHOOTER};
35  }
36 
37  /**
38  * Get array of shapes.
39  *
40  * It's not tamper-proof, but that's okay.
41  * @return collection of shapes
42  */
43  public static Shape[] getShapes() {
44  return COLLECTION;
45  }
46 
47  /**
48  * Get shape by its name.
49  * @param name name of shape
50  * @return shape object
51  * @throws ShapeException if no shape with this name exist
52  */
53  public static Shape getShapeByName( String name ) throws ShapeException {
54  Shape[] shapes = getShapes();
55  for ( int i = 0; i < shapes.length; i++ ) {
56  if ( shapes[i].getName().equals( name ) )
57  return shapes[i];
58  }
59  throw ( new ShapeException("Unknown shape: "+name) );
60  }
61 }
static Shape getShapeByName(String name)