jaitools.demo.jiffle
Class GameOfLife
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
jaitools.demo.jiffle.GameOfLife
- All Implemented Interfaces:
- ImageObserver, MenuContainer, Serializable, Accessible, RootPaneContainer, WindowConstants
public class GameOfLife
- extends JFrame
John Conway's Game of Life implemented with Jiffle.
The Game of Life is a cellular automaton, ie. a grid based model where
the value of each grid cell at time t depends on its state and that
of its neighbours at time t-1.
See the Wikipedia article at: http://en.wikipedia.org/wiki/Conway's_Game_of_Life
This program is a basic implementation of the game to demonstrate using
Jiffle in a simulation setting. It also illustrates the following aspects
of the language:
- Use of the foreach loop with an integer sequence (start:end).
- Pixel neighbour references.
- The outside script option to set a value returned for
pixel locations beyond the bounds of a source image.
- Naked conditional expressions to return 0/1 values.
Jiffle scripts
The program uses two different Jiffle scripts: one which represents a
a world with hard edges and a second where the world is a toroid,
ie. opposite edges of the image are joined to form a continuous surface.
In both scripts the world is an image where an unoccupied location is
represented by pixel value 0 and an occupied location by pixel value 1.
World with hard edges
options { outside = 0; }
n = 0;
foreach (iy in -1:1) {
foreach (ix in -1:1) {
n += world[ix, iy];
}
}
n -= world;
nextworld = (n == 3) || (world && n==2);
The expression world[ix, iy] accesses a relative neighbour
location. For example world[-1, 1] would get the value of a pixel
at (x-1, y+1) where x and y are the ordinates of the current pixel.
The two foreach loops iterate use integer sequence syntax
(startValue:endValue) to iterate over the 3x3 neighbourhood centred
on the current pixel and count the number of occupied cells (value of 1).
The rules of Life are expressed in terms of the number of neighbouring cells
occupied, so we adjust the value of n b subtracting the value of
the current pixel.
The options block at the top of the script sets a value to be returned
for any neighbour positions that are beyond the bounds of the image. Without
this option, the runtime object would throw a
JiffleRuntimeException at the very first pixel
when trying to access the relative neighbour position world[-1, -1].
The final line of the script expresses all of the Game of Life rules in a
single statement ! It uses naked conditional statements which return
1 or 0.
Toroidal world
n = 0;
foreach (iy in -1:1) {
yy = y() + iy;
yy = if (yy < 0, height() - 1, yy);
yy = if (yy >= height(), 0, yy);
foreach (ix in -1:1) {
xx = x() + ix;
xx = if (xx < 0, width()-1, xx);
xx = if (xx >= width(), 0, xx);
n += world[$xx, $yy];
}
}
n -= world;
nextworld = (n == 3) || (world && n==2);
This script treats the source image, represented by the world
variable, as a toroid by calculating absolute neighbour positions.
These are indicated by the $ prefix. When a neighbour position is
beyond an edge, it is adjusted to the corresponding position from the
opposite edge. Note that we don't need the outside option in this
script.
Using the Jiffle runtime objects
The Game of Life is an iterative algorithm where the output for time t
becomes the input for time t+1. Here, we accomplish this by simply
caching the Jiffle runtime objects and using them repeatedly with two images
which are represented by the variables world and nextworld
in the scripts. The images are swapped between source and destination roles
at each time step as shown in this code fragment...
activeRuntime.setSourceImage(WORLD_NAME, curWorld);
activeRuntime.setDestinationImage(NEXT_WORLD_NAME, nextWorld);
activeRuntime.evaluateAll(null);
TiledImage temp = curWorld;
curWorld = nextWorld;
nextWorld = temp;
Acknowledgement
The patterns included with this program are a tiny sample of the pattern
collection at LifeWiki: http://www.conwaylife.com/wiki/
- Since:
- 1.1
- Version:
- $Id: GameOfLife.java 1592 2011-03-29 11:01:25Z michael.bedward $
- Author:
- Michael Bedward
- See Also:
- Serialized Form
| Fields inherited from class java.awt.Frame |
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR |
| Methods inherited from class javax.swing.JFrame |
addImpl, createRootPane, frameInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isDefaultLookAndFeelDecorated, isRootPaneCheckingEnabled, paramString, processWindowEvent, remove, repaint, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setIconImage, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, update |
| Methods inherited from class java.awt.Frame |
addNotify, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setCursor, setExtendedState, setMaximizedBounds, setMenuBar, setResizable, setState, setTitle, setUndecorated |
| Methods inherited from class java.awt.Window |
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOwnedWindows, getOwner, getOwnerlessWindows, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isShowing, pack, paint, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImages, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, setVisible, show, toBack, toFront |
| Methods inherited from class java.awt.Container |
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusBackward, transferFocusDownCycle, validate, validateTree |
| Methods inherited from class java.awt.Component |
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resize, resize, setBackground, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusUpCycle |
GameOfLife
public GameOfLife()
main
public static void main(String[] args)
Copyright © 2011. All Rights Reserved.