package Life;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
* Conway's game of life
*
* 2009-Dec-17 added generations text and count
* 2009-Dec-17 added ability to change square size
*/
public class Life7 extends JFrame implements KeyListener
{
static final long serialVersionUID=0;
Image im;
int xCoordinate=0;
int yCoordinate=0;
int gridSize=30;
int cursorCount=0;
int generation=0;
int squareSize=20;
String tempSize="";
int lifeSize=squareSize/2;
int selectItem=0;
boolean goLife = false;
int[][] board = new int[gridSize][gridSize];
boolean patternMenu = false;
boolean sizeBox = false;
boolean gridBox = false;
Life7()
{
super("Conway's Game of Life");
JPanel p = new JPanel();
this.setSize(1280, 1024);
this.setVisible(true);
p.requestFocus();
addKeyListener(this);
newGame();
}
public void newGame()
{
goLife = false;
board = new int[gridSize][gridSize];
for(int x=0;x<gridSize;x=x+1)
{
for(int y=0;y<gridSize;y=y+1)
{
board[x][y]=0;
}
}
generation=0;
}
public void makeProgramWait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch(Exception e)
{
System.out.println("An error in sleep process.");
}
}
public int countDots(int x, int y)
{
int d=0;
// count the dots before our dot
for(int s=x-1;s<x+2;s=s+1)
{
for(int t=y-1;t<y+2;t=t+1)
{
if(s != -1 && s != gridSize && t != -1 && t != gridSize && board[s][t] == 1 && (s != x || t != y))
{
d=d+1;
}
}
}
return d;
}
public void printBoard()
{
for(int x=0;x<gridSize;x=x+1)
{
for(int y=0;y<gridSize;y=y+1)
{
System.out.print(board[y][x]);
}
System.out.println();
}
}
public void lifeStep()
{
int b[][] = new int[gridSize][gridSize];
for(int x=0;x<gridSize;x=x+1)
{
for(int y=0;y<gridSize;y=y+1)
{
//b[x][y]=0;
int a = countDots(x, y);
if(a < 2 || a > 3)
{
b[x][y] = 0;
}
if(a == 2 && board[x][y] == 1)
{
b[x][y] = 1;
}
if(a == 3)
{
b[x][y] = 1;
}
}
}
board=b;
}
public void playGame()
{
while(true)
{
if(goLife == true)
{
lifeStep();
generation++;
}
makeProgramWait(100);
repaint();
}
}
public void paint(Graphics g)
{
if(im == null)
{
im = createImage(this.getWidth(), this.getHeight());
}
Graphics tempG = im.getGraphics();
paintScreen(tempG);
g.drawImage(im, 0,0, this);
}
public void drawScore(Graphics g)
{
g.setFont(new Font("Arial Bold", Font.BOLD, 40));
g.setColor(Color.yellow);
g.drawString("Generation: "+generation, 30, 70);
g.setFont(new Font("Arial", Font.PLAIN, 10));
}
public void paintScreen(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0, this.getWidth(), this.getHeight());
g.setColor(Color.orange);
drawScore(g);
g.setColor(Color.yellow);
for(int x=0;x<gridSize;x=x+1)
{
for(int y=0;y<gridSize;y=y+1)
{
if(board[x][y] == 1)
{
g.setColor(Color.yellow);
g.fillOval(x*squareSize+100, y*squareSize+100, squareSize, squareSize);
}
g.setColor(new Color(128,128, 0));
g.drawRect(x*squareSize+100, y*squareSize+100, squareSize, squareSize);
}
}
g.setColor(Color.green);
g.drawOval(xCoordinate*squareSize+100, yCoordinate*squareSize+100, squareSize, squareSize);
if(patternMenu == true && goLife == false)
{
g.setColor(new Color(200, 200, 255));
g.fillRect(xCoordinate*squareSize+120, yCoordinate*squareSize+100, 100, 260);
g.setColor(new Color(160, 160, 200));
g.fillRect(xCoordinate*squareSize+120, selectItem*16+yCoordinate*squareSize+100, 100, 16);
g.setColor(Color.yellow);
Patterns.drawShape(g, xCoordinate*squareSize+225, selectItem*16+yCoordinate*squareSize+85, 4, selectItem);
g.setColor(Color.red);
g.drawRect(xCoordinate*squareSize+120, yCoordinate*squareSize+100, 100, 260);
int len = Patterns.getPatternNameListLength();
g.setColor(Color.black);
for(int t=0;t<len;t=t+1)
{
g.drawString(Patterns.getPatternName(t), xCoordinate*squareSize+130, yCoordinate*squareSize+t*16+112);
}
}
if(sizeBox == true && goLife == false)
{
g.setColor(new Color(200, 255, 200));
g.fillRoundRect(700, 700, 50, 50, 5, 5);
g.setColor(new Color(0, 50, 0));
g.drawRoundRect(700, 700, 50, 50, 5, 5);
g.setColor(Color.white);
g.fillRect(710, 710, 30, 20);
g.setColor(new Color(0, 50, 0));
g.drawString(tempSize, 712, 720);
if(cursorCount < 5)
{
g.drawLine(712+tempSize.length()*7, 712, 712+tempSize.length()*7, 720);
}
if(cursorCount > 10)
{
cursorCount = 0;
}
cursorCount++;
}
if(gridBox == true && goLife == false)
{
g.setColor(new Color(200, 255, 200));
g.fillRoundRect(700, 700, 50, 50, 5, 5);
g.setColor(new Color(0, 50, 0));
g.drawRoundRect(700, 700, 50, 50, 5, 5);
g.setColor(Color.white);
g.fillRect(710, 710, 30, 20);
g.setColor(new Color(0, 50, 0));
g.drawString(tempSize, 712, 720);
if(cursorCount < 5)
{
g.drawLine(712+tempSize.length()*7, 712, 712+tempSize.length()*7, 720);
}
if(cursorCount > 10)
{
cursorCount = 0;
}
cursorCount++;
}
}
public void keyPressed(KeyEvent e)
{
int inKey;
inKey = e.getKeyCode();
if(gridBox == true)
{
if(KeyEvent.VK_ENTER == inKey)
{
gridSize = Integer.parseInt(tempSize);
gridBox = false;
newGame();
}
if(KeyEvent.VK_BACK_SPACE == inKey && tempSize.length()> 0)
{
tempSize = tempSize.substring(0, tempSize.length()-1);
}
System.out.println(inKey);
if(((char) inKey) >= '0' && ((char) inKey) <= '9')
{
tempSize = tempSize + (char) inKey;
}
return;
}
if(KeyEvent.VK_ENTER == inKey)
{
squareSize = Integer.parseInt(tempSize);
sizeBox = false;
newGame();
}
if(KeyEvent.VK_BACK_SPACE == inKey && tempSize.length()> 0)
{
tempSize = tempSize.substring(0, tempSize.length()-1);
}
if(((char) inKey) >= '0' && ((char) inKey) <= '9')
{
tempSize = tempSize + (char) inKey;
}
if(gridBox == true || sizeBox == true)
{
return;
}
if(KeyEvent.VK_Q == inKey)
{
System.exit(0);
}
else if(KeyEvent.VK_S == inKey)
{
newGame();
}
else if(KeyEvent.VK_LEFT== inKey)
{
xCoordinate = xCoordinate - 1;
}
else if(KeyEvent.VK_RIGHT== inKey)
{
xCoordinate = xCoordinate + 1;
}
else if(KeyEvent.VK_UP== inKey)
{
if(patternMenu == false)
{
yCoordinate = yCoordinate - 1;
}
else
{
if(selectItem > 0)
{
selectItem--;
}
}
}
else if(KeyEvent.VK_G == inKey)
{
goLife = true;
}
else if(KeyEvent.VK_N == inKey)
{
lifeStep();
}
else if(KeyEvent.VK_P == inKey)
{
printBoard();
}
else if(KeyEvent.VK_DOWN== inKey)
{
if(patternMenu == false)
{
yCoordinate = yCoordinate + 1;
}
else
{
if(selectItem < 16)
{
selectItem++;
}
}
}
else if(KeyEvent.VK_SPACE == inKey)
{
board[xCoordinate][yCoordinate]=1;
}
else if(KeyEvent.VK_CONTROL == inKey && goLife == false)
{
patternMenu = true;
}
else if(KeyEvent.VK_SHIFT == inKey && goLife == false)
{
tempSize = ""+squareSize;
sizeBox = true;
}
else if(KeyEvent.VK_CAPS_LOCK == inKey && goLife == false)
{
tempSize = ""+gridSize;
gridBox = true;
}
repaint();
}
public void keyReleased(KeyEvent e)
{
int inKey;
inKey = e.getKeyCode();
if(KeyEvent.VK_CONTROL == inKey)
{
Patterns.placePattern(board, xCoordinate, yCoordinate, selectItem+1);
selectItem=0;
patternMenu = false;
}
}
public void keyTyped(KeyEvent e)
{
}
public static void main(String args[])
{
Life7 app = new
Life7();
app.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
app.playGame();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
* Conway's game of life
*
* 2009-Dec-17 added generations text and count
* 2009-Dec-17 added ability to change square size
*/
public class Life7 extends JFrame implements KeyListener
{
static final long serialVersionUID=0;
Image im;
int xCoordinate=0;
int yCoordinate=0;
int gridSize=30;
int cursorCount=0;
int generation=0;
int squareSize=20;
String tempSize="";
int lifeSize=squareSize/2;
int selectItem=0;
boolean goLife = false;
int[][] board = new int[gridSize][gridSize];
boolean patternMenu = false;
boolean sizeBox = false;
boolean gridBox = false;
Life7()
{
super("Conway's Game of Life");
JPanel p = new JPanel();
this.setSize(1280, 1024);
this.setVisible(true);
p.requestFocus();
addKeyListener(this);
newGame();
}
public void newGame()
{
goLife = false;
board = new int[gridSize][gridSize];
for(int x=0;x<gridSize;x=x+1)
{
for(int y=0;y<gridSize;y=y+1)
{
board[x][y]=0;
}
}
generation=0;
}
public void makeProgramWait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch(Exception e)
{
System.out.println("An error in sleep process.");
}
}
public int countDots(int x, int y)
{
int d=0;
// count the dots before our dot
for(int s=x-1;s<x+2;s=s+1)
{
for(int t=y-1;t<y+2;t=t+1)
{
if(s != -1 && s != gridSize && t != -1 && t != gridSize && board[s][t] == 1 && (s != x || t != y))
{
d=d+1;
}
}
}
return d;
}
public void printBoard()
{
for(int x=0;x<gridSize;x=x+1)
{
for(int y=0;y<gridSize;y=y+1)
{
System.out.print(board[y][x]);
}
System.out.println();
}
}
public void lifeStep()
{
int b[][] = new int[gridSize][gridSize];
for(int x=0;x<gridSize;x=x+1)
{
for(int y=0;y<gridSize;y=y+1)
{
//b[x][y]=0;
int a = countDots(x, y);
if(a < 2 || a > 3)
{
b[x][y] = 0;
}
if(a == 2 && board[x][y] == 1)
{
b[x][y] = 1;
}
if(a == 3)
{
b[x][y] = 1;
}
}
}
board=b;
}
public void playGame()
{
while(true)
{
if(goLife == true)
{
lifeStep();
generation++;
}
makeProgramWait(100);
repaint();
}
}
public void paint(Graphics g)
{
if(im == null)
{
im = createImage(this.getWidth(), this.getHeight());
}
Graphics tempG = im.getGraphics();
paintScreen(tempG);
g.drawImage(im, 0,0, this);
}
public void drawScore(Graphics g)
{
g.setFont(new Font("Arial Bold", Font.BOLD, 40));
g.setColor(Color.yellow);
g.drawString("Generation: "+generation, 30, 70);
g.setFont(new Font("Arial", Font.PLAIN, 10));
}
public void paintScreen(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0, this.getWidth(), this.getHeight());
g.setColor(Color.orange);
drawScore(g);
g.setColor(Color.yellow);
for(int x=0;x<gridSize;x=x+1)
{
for(int y=0;y<gridSize;y=y+1)
{
if(board[x][y] == 1)
{
g.setColor(Color.yellow);
g.fillOval(x*squareSize+100, y*squareSize+100, squareSize, squareSize);
}
g.setColor(new Color(128,128, 0));
g.drawRect(x*squareSize+100, y*squareSize+100, squareSize, squareSize);
}
}
g.setColor(Color.green);
g.drawOval(xCoordinate*squareSize+100, yCoordinate*squareSize+100, squareSize, squareSize);
if(patternMenu == true && goLife == false)
{
g.setColor(new Color(200, 200, 255));
g.fillRect(xCoordinate*squareSize+120, yCoordinate*squareSize+100, 100, 260);
g.setColor(new Color(160, 160, 200));
g.fillRect(xCoordinate*squareSize+120, selectItem*16+yCoordinate*squareSize+100, 100, 16);
g.setColor(Color.yellow);
Patterns.drawShape(g, xCoordinate*squareSize+225, selectItem*16+yCoordinate*squareSize+85, 4, selectItem);
g.setColor(Color.red);
g.drawRect(xCoordinate*squareSize+120, yCoordinate*squareSize+100, 100, 260);
int len = Patterns.getPatternNameListLength();
g.setColor(Color.black);
for(int t=0;t<len;t=t+1)
{
g.drawString(Patterns.getPatternName(t), xCoordinate*squareSize+130, yCoordinate*squareSize+t*16+112);
}
}
if(sizeBox == true && goLife == false)
{
g.setColor(new Color(200, 255, 200));
g.fillRoundRect(700, 700, 50, 50, 5, 5);
g.setColor(new Color(0, 50, 0));
g.drawRoundRect(700, 700, 50, 50, 5, 5);
g.setColor(Color.white);
g.fillRect(710, 710, 30, 20);
g.setColor(new Color(0, 50, 0));
g.drawString(tempSize, 712, 720);
if(cursorCount < 5)
{
g.drawLine(712+tempSize.length()*7, 712, 712+tempSize.length()*7, 720);
}
if(cursorCount > 10)
{
cursorCount = 0;
}
cursorCount++;
}
if(gridBox == true && goLife == false)
{
g.setColor(new Color(200, 255, 200));
g.fillRoundRect(700, 700, 50, 50, 5, 5);
g.setColor(new Color(0, 50, 0));
g.drawRoundRect(700, 700, 50, 50, 5, 5);
g.setColor(Color.white);
g.fillRect(710, 710, 30, 20);
g.setColor(new Color(0, 50, 0));
g.drawString(tempSize, 712, 720);
if(cursorCount < 5)
{
g.drawLine(712+tempSize.length()*7, 712, 712+tempSize.length()*7, 720);
}
if(cursorCount > 10)
{
cursorCount = 0;
}
cursorCount++;
}
}
public void keyPressed(KeyEvent e)
{
int inKey;
inKey = e.getKeyCode();
if(gridBox == true)
{
if(KeyEvent.VK_ENTER == inKey)
{
gridSize = Integer.parseInt(tempSize);
gridBox = false;
newGame();
}
if(KeyEvent.VK_BACK_SPACE == inKey && tempSize.length()> 0)
{
tempSize = tempSize.substring(0, tempSize.length()-1);
}
System.out.println(inKey);
if(((char) inKey) >= '0' && ((char) inKey) <= '9')
{
tempSize = tempSize + (char) inKey;
}
return;
}
if(KeyEvent.VK_ENTER == inKey)
{
squareSize = Integer.parseInt(tempSize);
sizeBox = false;
newGame();
}
if(KeyEvent.VK_BACK_SPACE == inKey && tempSize.length()> 0)
{
tempSize = tempSize.substring(0, tempSize.length()-1);
}
if(((char) inKey) >= '0' && ((char) inKey) <= '9')
{
tempSize = tempSize + (char) inKey;
}
if(gridBox == true || sizeBox == true)
{
return;
}
if(KeyEvent.VK_Q == inKey)
{
System.exit(0);
}
else if(KeyEvent.VK_S == inKey)
{
newGame();
}
else if(KeyEvent.VK_LEFT== inKey)
{
xCoordinate = xCoordinate - 1;
}
else if(KeyEvent.VK_RIGHT== inKey)
{
xCoordinate = xCoordinate + 1;
}
else if(KeyEvent.VK_UP== inKey)
{
if(patternMenu == false)
{
yCoordinate = yCoordinate - 1;
}
else
{
if(selectItem > 0)
{
selectItem--;
}
}
}
else if(KeyEvent.VK_G == inKey)
{
goLife = true;
}
else if(KeyEvent.VK_N == inKey)
{
lifeStep();
}
else if(KeyEvent.VK_P == inKey)
{
printBoard();
}
else if(KeyEvent.VK_DOWN== inKey)
{
if(patternMenu == false)
{
yCoordinate = yCoordinate + 1;
}
else
{
if(selectItem < 16)
{
selectItem++;
}
}
}
else if(KeyEvent.VK_SPACE == inKey)
{
board[xCoordinate][yCoordinate]=1;
}
else if(KeyEvent.VK_CONTROL == inKey && goLife == false)
{
patternMenu = true;
}
else if(KeyEvent.VK_SHIFT == inKey && goLife == false)
{
tempSize = ""+squareSize;
sizeBox = true;
}
else if(KeyEvent.VK_CAPS_LOCK == inKey && goLife == false)
{
tempSize = ""+gridSize;
gridBox = true;
}
repaint();
}
public void keyReleased(KeyEvent e)
{
int inKey;
inKey = e.getKeyCode();
if(KeyEvent.VK_CONTROL == inKey)
{
Patterns.placePattern(board, xCoordinate, yCoordinate, selectItem+1);
selectItem=0;
patternMenu = false;
}
}
public void keyTyped(KeyEvent e)
{
}
public static void main(String args[])
{
Life7 app = new
Life7();
app.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
app.playGame();
}
}