Guest User

The Game of Life

a guest
Nov 16th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.24 KB | None | 0 0
  1. package pl.jacadev.lifegame;
  2.  
  3. import static org.lwjgl.opengl.GL11.*;
  4.  
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7.  
  8. import org.lwjgl.LWJGLException;
  9. import org.lwjgl.opengl.Display;
  10. import org.lwjgl.opengl.DisplayMode;
  11. import org.lwjgl.input.Mouse;
  12. import org.lwjgl.input.Keyboard;
  13.  
  14. import pl.jacadev.lifegame.Entity.State;
  15.         /**
  16.          *  @author JacaDev
  17.          */
  18. public class Main {
  19.     /**
  20.      * Default frame dimensions declaration
  21.      */
  22.     public static final int FRAME_WIDTH = 800;
  23.     public static final int FRAME_HEIGHT = 800;
  24.     public static final int SIZE = 100;
  25.     /**
  26.      * Main method with ogl setup and main loop.
  27.      *
  28.      */
  29.     public static void main(String[] args) throws FileNotFoundException,
  30.             IOException {
  31.         try {
  32.             Display.setDisplayMode(new DisplayMode(FRAME_WIDTH, FRAME_HEIGHT));
  33.             Display.setTitle("The Game of Life");
  34.             Display.create();
  35.  
  36.         } catch (LWJGLException e) {
  37.             e.printStackTrace();
  38.         }
  39.  
  40.         glMatrixMode(GL_PROJECTION);
  41.         glLoadIdentity();
  42.         glOrtho(0, FRAME_WIDTH, FRAME_WIDTH, 0, 1, -1);
  43.         glMatrixMode(GL_MODELVIEW);
  44.         glEnable(GL_TEXTURE_2D);
  45.  
  46.         EntityMap.spawnEntities();
  47.        
  48.         //The control by console.
  49.        
  50.         /*new Thread() {
  51.  
  52.             @Override
  53.             public void run() {
  54.                 Scanner sc = new Scanner(System.in);
  55.                 while (true) {
  56.                     String s = sc.nextLine();
  57.                     if (s.split(" ")[0].equals("create")) {
  58.                         Entity en = EntityMap.getEntityAt(Integer.parseInt(s.split(" ")[1]),
  59.                                         Integer.parseInt(s.split(" ")[2]));
  60.                         if(en == null){
  61. ;
  62.                         EntityMap.getEntities().add(
  63.                                 new Entity(Integer.parseInt(s.split(" ")[1]),
  64.                                         Integer.parseInt(s.split(" ")[2])));
  65.                         EntityMap.getEntityAt(Integer.parseInt(s.split(" ")[1]),
  66.                                 Integer.parseInt(s.split(" ")[2])).makeExisting(true);
  67.                         }else{
  68.                             en.makeExisting(true);
  69.                         }
  70.                     }
  71.                     if(s.equals("next")){
  72.                         EntityMap.nextGen();
  73.                         System.out.println(EntityMap.getMapInString());
  74.                     }
  75.                 }
  76.             }
  77.  
  78.         }.start();*/
  79.  
  80.         new Thread(){
  81.             @Override
  82.             public void run() {
  83.                 try {
  84.                     sleep(30);
  85.                 } catch (InterruptedException e) {
  86.                     // TODO Auto-generated catch block
  87.                     e.printStackTrace();
  88.                 }
  89.                 if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
  90.                     EntityMap.nextGen();
  91.                 }
  92.                 run();
  93.             };
  94.         }.start();
  95.         //Main loop
  96.         while (!Display.isCloseRequested()) {
  97.             glClear(GL_COLOR_BUFFER_BIT);
  98.             EntityMap.draw();
  99.            
  100.             if(Mouse.isButtonDown(0)){
  101.                 for(int x = 0; x < EntityMap.getEntities().size(); x++){
  102.                     Entity en = EntityMap.getEntities().get(x);
  103.                     if(en.getRect().contains((Mouse.getX()), FRAME_HEIGHT  - (Mouse.getY()))){
  104.                         en.setExisting(true);
  105.                         en.setState(State.NEW);
  106.                     }
  107.                 }
  108.             }
  109.             if(Mouse.isButtonDown(1)){
  110.                 for(int x = 0; x < EntityMap.getEntities().size(); x++){
  111.                     Entity en = EntityMap.getEntities().get(x);
  112.                     if(en.getRect().contains((Mouse.getX()), FRAME_HEIGHT  - (Mouse.getY()))){
  113.                         en.setExisting(false);
  114.                     }
  115.                 }
  116.             }
  117.             Display.update();
  118.             Display.sync(40);
  119.         }
  120.         Display.destroy();
  121.         System.exit(0);
  122.     }
  123. }
  124.  
  125.  
  126. package pl.jacadev.lifegame;
  127.  
  128. import static org.lwjgl.opengl.GL11.*;
  129.  
  130. import java.util.ArrayList;
  131. import java.util.Arrays;
  132. import java.util.List;
  133.  
  134. public class EntityMap {
  135.     /**
  136.      * @param entities
  137.      *            - The list of all entities at game.
  138.      */
  139.     private static ArrayList<Entity> entities = new ArrayList<Entity>();
  140.  
  141.     public static ArrayList<Entity> getEntities() {
  142.         return entities;
  143.     }
  144.  
  145.     /**
  146.      *
  147.      * @return amount of neighbours of entity.
  148.      */
  149.     public static int countNeighbours(Entity entity) {
  150.         int counter = 0;
  151.         for (int x = 0; x < entities.size(); x++) {
  152.             Entity en = entities.get(x);
  153.             if ((entity.getX() - 1 == en.getX() && entity.getY() == en.getY() && en
  154.                     .isExisting())
  155.                     || (entity.getX() + 1 == en.getX()
  156.                             && entity.getY() == en.getY() && en.isExisting())
  157.                     || (entity.getX() == en.getX()
  158.                             && entity.getY() - 1 == en.getY() && en
  159.                                 .isExisting())
  160.                     || (entity.getX() == en.getX()
  161.                             && entity.getY() + 1 == en.getY() && en
  162.                                 .isExisting())
  163.                     || (entity.getX() + 1 == en.getX()
  164.                             && entity.getY() - 1 == en.getY() && en
  165.                                 .isExisting())
  166.                     || (entity.getX() + 1 == en.getX()
  167.                             && entity.getY() + 1 == en.getY() && en
  168.                                 .isExisting())
  169.                     || (entity.getX() - 1 == en.getX()
  170.                             && entity.getY() + 1 == en.getY() && en
  171.                                 .isExisting())
  172.                     || (entity.getX() - 1 == en.getX() && entity.getY() - 1 == en
  173.                             .getY()) && en.isExisting()) {
  174.                 counter++;
  175.             }
  176.         }
  177.         return counter;
  178.  
  179.     }
  180.     public static List<Entity> getNeighbours(Entity en){
  181.         ArrayList<Entity> toReturn =   new ArrayList<Entity>();
  182.         toReturn.addAll(Arrays.asList(new Entity[]{
  183.                  getEntityAt(en.getX(), en.getY() + 1),
  184.                  getEntityAt(en.getX(), en.getY() - 1),
  185.                  getEntityAt(en.getX() + 1, en.getY()),
  186.                  getEntityAt(en.getX() - 1, en.getY()),
  187.                  getEntityAt(en.getX() + 1, en.getY() + 1),
  188.                  getEntityAt(en.getX() - 1, en.getY() - 1),
  189.                  getEntityAt(en.getX() + 1, en.getY() - 1),
  190.                  getEntityAt(en.getX() - 1, en.getY() + 1)
  191.         }));
  192.         for(int i = 0; i < toReturn.size(); i++){
  193.             if(toReturn.get(i) == null){
  194.                 toReturn.remove(toReturn.get(i));
  195.             }
  196.         }
  197.         return toReturn;
  198. }
  199.  
  200.     /**
  201.      * Start another generation.
  202.      */
  203.     public static void nextGen() {
  204.         for (int x = 0; x < entities.size(); x++) {
  205.             Entity en = entities.get(x);
  206.             if(en.isExisting()){
  207.             en.logic();
  208.             for(Entity entity : getNeighbours(en)){
  209.                 entity.logic();
  210.             }
  211.             }
  212.         }
  213.         for (int x = 0; x < entities.size(); x++) {
  214.             Entity en = entities.get(x);
  215.             if(en.isExisting()){
  216.             en.completeLogic();;
  217.             for(Entity entity : getNeighbours(en)){
  218.                 entity.completeLogic();;
  219.             }
  220.             }
  221.         }
  222.     }
  223.  
  224.     /**
  225.      * Spawn default entities.
  226.      */
  227.     public static void spawnEntities() {
  228.         for (int x = 0; x < Main.SIZE + 6; x++) {
  229.             for (int y = 0; y < Main.SIZE + 2; y++) {
  230.                 entities.add(new Entity(x - 3, y - 3));
  231.             }
  232.         }
  233.     }
  234.  
  235.     /**
  236.      *
  237.      * Get entity with the same x and y as in the arguments.
  238.      */
  239.     public static Entity getEntityAt(int x, int y) {
  240.         for (int b = 0; b < entities.size(); b++) {
  241.             Entity en = entities.get(b);
  242.             if (en.getX() == x && en.getY() == y) {
  243.                 return en;
  244.             }
  245.         }
  246.         return null;
  247.     }
  248.  
  249.     /**
  250.      *
  251.      * For the console form of control.
  252.      *
  253.      * @return Pretty, formated string with list of living entities.
  254.      */
  255.     public static String getMapInString() {
  256.         String toReturn = "";
  257.         for (int x = 0; x < entities.size(); x++) {
  258.             Entity en = entities.get(x);
  259.             if (en.isExisting()) {
  260.                 toReturn += "Entity at: x - " + en.getX() + " y - " + en.getY()
  261.                         + " " + "\n";
  262.             }
  263.         }
  264.         return toReturn;
  265.     }
  266.  
  267.     /**
  268.      * Draw
  269.      */
  270.     public static void draw() {
  271.         glColor3d(1, 1, 1);
  272.         glRectd(0, 0, Main.FRAME_WIDTH, Main.FRAME_HEIGHT);
  273.         for (int x = 0; x < Main.SIZE; x++) {
  274.             glColor3d(0, 0, 0);
  275.             glBegin(GL_LINES);
  276.             glVertex2d(x * ((int)Main.FRAME_WIDTH / Main.SIZE), 0);
  277.             glVertex2d(x * (int)(Main.FRAME_WIDTH / Main.SIZE), Main.FRAME_HEIGHT);
  278.             glEnd();
  279.             glBegin(GL_LINES);
  280.             glVertex2d(0, x * (int)(Main.FRAME_HEIGHT / Main.SIZE));
  281.             glVertex2d(Main.FRAME_WIDTH, x * (int)(Main.FRAME_HEIGHT / Main.SIZE));
  282.             glEnd();
  283.         }
  284.         for (Entity e : entities) {
  285.             e.draw();
  286.         }
  287.     }
  288.  
  289.  
  290. }
  291.  
  292.  
  293.  
  294. package pl.jacadev.lifegame;
  295.  
  296. import static org.lwjgl.opengl.GL11.*;
  297. import org.lwjgl.util.Rectangle;
  298.  
  299. public class Entity {
  300.     private int x;
  301.     private int y;
  302.     private Rectangle rect;
  303.     private boolean exist;
  304.     private State state;
  305.     public static enum State{
  306.         NEW, NORMAL;
  307.     }
  308.    
  309.     public void setExisting(boolean canItLive) {
  310.         exist = canItLive;
  311.     }
  312.  
  313.     public boolean isExisting() {
  314.         return exist;
  315.     }
  316.  
  317.     public int getX() {
  318.         return x;
  319.     }
  320.  
  321.     public void setX(int x) {
  322.         this.x = x;
  323.     }
  324.     public void setState(State state){
  325.         this.state = state;
  326.     }
  327.     public State getState(){
  328.         return state;
  329.     }
  330.  
  331.     public int getY() {
  332.         return y;
  333.     }
  334.  
  335.     public void setY(int y) {
  336.         this.y = y;
  337.     }
  338.  
  339.     public Entity(int x, int y) {
  340.         super();
  341.  
  342.         this.x = x;
  343.         this.y = y;
  344.         rect = new Rectangle(x * (Main.FRAME_WIDTH / (int)(Main.SIZE)), y * (Main.FRAME_HEIGHT / (int)(Main.SIZE)),
  345.                 Main.FRAME_WIDTH / (int)(Main.SIZE), Main.FRAME_HEIGHT / (int)(Main.SIZE));
  346.     }
  347.  
  348.  
  349.     private boolean willexist;
  350.     private boolean willntexist;
  351.     public void logic() {
  352.         this.state = State.NORMAL;
  353.         int neighbours = EntityMap.countNeighbours(this);
  354.         if (this.exist) {
  355.             if (neighbours <= 1) {
  356.                 this.willntexist = true;
  357.             }
  358.             if (neighbours >= 4) {
  359.                 this.willntexist = true;
  360.             }
  361.         } else {
  362.             if (neighbours == 3) {
  363.                 this.willexist = true;
  364.             }
  365.         }
  366.  
  367.     }
  368.     public void completeLogic(){
  369.         if(willexist){
  370.             exist = true;
  371.             state = State.NEW;
  372.             willexist = false;
  373.         }
  374.         if(willntexist){
  375.             exist = false;
  376.             willntexist = false;
  377.         }
  378.     }
  379.  
  380.     public void draw() {
  381.         if (this.exist) {
  382.             switch(state){
  383.             case NEW:
  384.                 glColor3d(0, 1, 0);
  385.                 break;
  386.             case NORMAL:
  387.                 glColor3d(0, 0, 1);
  388.                 break;
  389.             }
  390.             glRectd(rect.getX() + 1, rect.getY(), rect.getX() + rect.getWidth(),
  391.                     rect.getY() + rect.getHeight() - 1);
  392.         }
  393.  
  394.     }
  395.  
  396.     public Rectangle getRect() {
  397.         return rect;
  398.     }
  399.     public void setRect(Rectangle rect){
  400.         this.rect = rect;
  401.     }
  402. }
Advertisement
Add Comment
Please, Sign In to add comment