Goats and Tigers now available on iPhone |
Back to Life for iPhone Main Page
Back to 1-Dimensional Pattern Matching
Back to 2-Dimensional Pattern Matching
Back to 2-Dimensional Pattern Rotation
Matching Multiple Patterns on a 2-D Cellular Automata Board
The applet below generates random patterns with random widths and heights of between 2 and 4. It displays these patterns at the bottom of the applet, each in a different colour. It then searches for each of these patterns in a 2-dimensional cellular automata board, highlighting matches using the same colour that is used to display the pattern at the bottom of the applet.
Generating the Patterns
Unlike the previous applet in which all patterns were the same width and height, this applet uses a private class to store the random pattern's bitboard and the width and height of that bitboard, as well as providing a constructor which encapsulates the generation of a random pattern.private class RandomPattern { Long bitBoard[]; int width, height; RandomPattern(int maxWidth, int maxHeight) { width = (int)(Math.random() * (maxWidth - 1)) + 2; height = (int)(Math.random() * (maxHeight - 1)) + 2; bitBoard = new Long[height]; for(int i = 0; i < height; i++) bitBoard[i] = new Long((long)(Math.random() * (1L << width))); } }
Storing Pattern Matches
A private class is used to store the co-ordinate at which each match is found, the size of its bounding box and the colour to be used to display it.private class Match { Point topLeftCoord; int width, height; Color color; public Match(Point topLeftCoord, int width, int height, Color color) { this.topLeftCoord = topLeftCoord; this.width = width; this.height = height; this.color = color; } }