본문 바로가기

OM

생명게임


GameOfLife.java======================================================================================

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GameOfLife extends JApplet
    implements Runnable, ActionListener, MouseListener
{
    static final int maxX = 40;
    static final int maxY = 20;
    boolean map[][];
    int mapState[][];
    int gen;
    boolean nextGen;

    JPanel controlPanel;
    JLabel genLabel;
    JButton startButton;

    Thread clock;

    public void init()
    {
        map = new boolean[maxX][maxY];
        mapState = new int[maxX][maxY];
        gen = 1;
        nextGen = false;

        for(int x=15; x<25; x++)
        {
            for(int y=10; y<11; y++)
            {
                map[x][y] = true;
            }
        }

        Container contentPane = getContentPane();
        controlPanel = new JPanel();

        genLabel = new JLabel(gen + " 세대");
        controlPanel.add(genLabel);

        startButton = new JButton("START");
        startButton.addActionListener(this);
        controlPanel.add(startButton);

        contentPane.add(controlPanel, BorderLayout.SOUTH);

        addMouseListener(this);
    }

    public void start()
    {
        if(clock==null)
        {
            clock = new Thread(this);
            clock.start();
        }
    }

    public void paint(Graphics g)
    {
        super.paint(g);

        for(int x=0; x<maxX; x++)
        {
            for(int y=0; y<maxY; y++)
            {
                if(map[x][y])
                {
                    g.fillRect(x*10, y*10, 10, 10);
                }
                else{
                    g.drawRect(x*10, y*10, 10, 10);
                }
            }
        }
    }

    public void stop()
    {
        if(clock!=null && clock.isAlive())
        {
            clock = null;
        }
    }

    public void run()
    {
        while(true)
        {
            try
            {
                clock.sleep(1000);
            }
            catch(InterruptedException ie){}

            if(nextGen)
            {
                makeNextGen();
                gen++;
                genLabel.setText(gen + " 세대");
                repaint();
            }
        }
    }

    public void makeNextGen()
    {
        for(int x=0; x<maxX; x++)
        {
            for(int y=0; y<maxY; y++)
            {
                if(map[x][y])
                {
                    mapState[x][y] = 100;
                }
                else{
                    mapState[x][y] = 0;
                }
            }
        }

        for(int x=0; x<maxX; x++)
        {
            for(int y=0; y<maxY; y++)
            {
                countLife(x, y);
            }
        }

        for(int x=0; x<maxX; x++)
        {
            for(int y=0; y<maxY; y++)
            {
                switch(mapState[x][y])
                {
                    case 3 :
                    case 102 :
                    case 103 :
                        map[x][y] = true;
                        break;
                    default :
                        map[x][y] = false;
                        break;
                }
            }
        }

    }

    public void countLife(int x, int y)
    {
        for(int i=-1; i<=1; i++)
        {
            for(int j=-1; j<=1; j++)
            {
                if(i!=0 || j!=0)
                {
                    if(x+i>=0 && x+i<maxX && y+j>=0 && y+j<maxY)
                    {
                        if(map[x+i][y+j])
                        {
                            mapState[x][y]++;
                        }
                    }
                }
            }
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        if(nextGen)
        {
            nextGen = false;
            startButton.setText("start");
        }
        else{
            nextGen = true;
            startButton.setText("stop");
        }
    }

    public void mousePressed(MouseEvent e)
    {
        int mouseX = e.getX();
        int mouseY = e.getY();

        if(mouseX<maxX*10 && mouseY<maxY*10)
        {
            map[mouseX/10][mouseY/10] = !map[mouseX/10][mouseY/10];
            repaint();
        }
    }

    public void mouseReleased(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
}

=====================================================================================================

'OM' 카테고리의 다른 글

Yahoo WebAPI를 이용한 날씨 위젯 2  (0) 2008.05.11
Yahoo WebAPI를 이용한 날씨 위젯 1  (1) 2008.05.10
Flash Javascript 연동 (ExternalInterface)  (0) 2008.05.07
애니메이션  (0) 2008.05.07
Card Match 2  (0) 2008.05.04
원 그리기 (MouseListener)  (0) 2008.05.02
디지털 시계  (0) 2008.05.02
숫자 야구 게임  (0) 2008.05.02
가위바위보 게임  (2) 2008.04.28