Advertisement
Guest User

Untitled

a guest
May 10th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.53 KB | None | 0 0
  1. //http://stackoverflow.com/questions/5956194/why-isnt-my-if-else-block-ever-getting-hit-even-though-it-should-be-just-need/5956308#5956308
  2. //Help me out!
  3.  
  4. //And you know...
  5. //      don't steal my code, thanks!
  6.  
  7. import java.applet.*;
  8. import java.awt.*;
  9. import java.util.Random;
  10.  
  11. public class GameMain extends Applet implements Runnable {
  12.    
  13.     private int countLeft, countRight, countCenter;
  14.    
  15.     //Frame Settings
  16.     private int width;
  17.     private int height;
  18.     private int left, top;
  19.     private int playTop, playRight, playFloor, playLeft;
  20.     private int worldWidth, worldHeight;
  21.    
  22.     //Game Constants
  23.     private final int BASE_GENERATOR_WIDTH = 5;
  24.     private final Color EMPTY = null;
  25.     private final Color PARTICLE_SAND = new Color(255, 236, 139);
  26.     private final Color PARTICLE_WATER = Color.BLUE;
  27.     private final Color PARTICLE_SALT = new Color(252, 252, 252);
  28.     private final Color PARTICLE_OIL = new Color(133, 99, 99);
  29.    
  30.     //User Variables
  31.     private int GENERATOR_SAND = 5;
  32.     private int GENERATOR_WATER = 5;
  33.     private int GENERATOR_SALT = 5;
  34.     private int GENERATOR_OIL = 5;
  35.     private boolean HAS_FLOOR = true;
  36.    
  37.     //World
  38.     private Color[][] world;
  39.    
  40.     private Random rand;
  41.    
  42.     public static Toolkit k = Toolkit.getDefaultToolkit();
  43.     private Thread gameThread = null;
  44.    
  45.     private Graphics bufferGraphics;
  46.     private Image offscreen;
  47.    
  48.     public void init() {
  49.         width = 700;
  50.         height = 600;
  51.         //left = (k.getScreenSize().width - width) / 2;
  52.         //top = 20;
  53.        
  54.         top=0;
  55.         left=0;
  56.        
  57.         playTop = top + 3;
  58.         playFloor= (top+height) - (height/7);
  59.         playLeft = left + 3;
  60.         playRight = width - 6;
  61.        
  62.         worldWidth = playRight - playLeft;
  63.         worldHeight = playFloor - playTop;
  64.        
  65.         world = new Color[worldWidth][worldHeight+1];
  66.        
  67.         rand = new Random();
  68.        
  69.         for(int i = 0; i < world.length; i++) {
  70.             for(int j = 0; j < world[i].length; j++) {
  71.                 world[i][j] = EMPTY;
  72.             }
  73.         }
  74.        
  75.         offscreen = createImage(width, height);
  76.         bufferGraphics = offscreen.getGraphics();
  77.        
  78.         countCenter = countLeft = countRight = 0;
  79.        
  80.         gameThread = new Thread(this, "Game");
  81.         gameThread.start();
  82.     }
  83.    
  84.     public void run() {
  85.         //System.out.println("run() reached");
  86.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  87.        
  88.         while(true) {
  89.             //System.out.println("Reached repaint");
  90.             repaint();
  91.             try{
  92.                 Thread.sleep(1);
  93.             }
  94.             catch (InterruptedException ex) {
  95.                 System.out.println("Caught!");
  96.             }
  97.             Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  98.         }
  99.        
  100.     }
  101.    
  102.     public void stop() {
  103.        
  104.     }
  105.    
  106.     public void paint(Graphics g) {
  107.        
  108.         bufferGraphics.clearRect(playLeft, playTop, worldWidth, worldHeight);
  109.        
  110.         bufferGraphics.setColor(Color.GRAY);
  111.         bufferGraphics.fillRect(left, top, width, height);
  112.         bufferGraphics.setColor(Color.BLACK);
  113.         bufferGraphics.fillRect(left+3, top+3, width-6, height-6);
  114.         bufferGraphics.setColor(Color.GRAY);
  115.         bufferGraphics.fillRect(left, (top+height)-(height/7), width, (height/7));
  116.        
  117.         generateStreams();
  118.         doGravity();
  119.         drawWorld(bufferGraphics);
  120.        
  121.         g.drawImage(offscreen, 0, 0, this);
  122.        
  123.         //repaint();
  124.     }
  125.    
  126.     public void update(Graphics g) {
  127.         paint(g);
  128.     }
  129.    
  130.     private void generateStreams() {
  131.         generateSand();
  132.         generateWater();
  133.         generateSalt();
  134.         generateOil();
  135.     }
  136.    
  137.     private void generateSand() {
  138.         int center = worldWidth / 5;
  139.         int left = center - ((BASE_GENERATOR_WIDTH * GENERATOR_SAND) / 2);
  140.         int right = center + ((BASE_GENERATOR_WIDTH * GENERATOR_SAND) / 2);
  141.         int generatorWidth = (BASE_GENERATOR_WIDTH * GENERATOR_SAND);
  142.        
  143.         for(int i = 0; i < generatorWidth; i++) {
  144.             if(rand.nextInt(2) == 0) {
  145.                 world[left + i][0] = PARTICLE_SAND;
  146.             }
  147.         }
  148.     }
  149.    
  150.     private void generateWater() {
  151.         int center = worldWidth / 5 * 2;
  152.         int left = center - ((BASE_GENERATOR_WIDTH * GENERATOR_WATER) / 2);
  153.         int right = center + ((BASE_GENERATOR_WIDTH * GENERATOR_WATER) / 2);
  154.         int generatorWidth = (BASE_GENERATOR_WIDTH * GENERATOR_WATER);
  155.        
  156.         for(int i = 0; i < generatorWidth; i++) {
  157.             if(rand.nextInt(2) == 0) {
  158.                 world[left + i][0] = PARTICLE_WATER;
  159.             }
  160.         }
  161.     }
  162.    
  163.     private void generateSalt() {
  164.         int center = worldWidth / 5 * 3;
  165.         int left = center - ((BASE_GENERATOR_WIDTH * GENERATOR_SALT) / 2);
  166.         int right = center + ((BASE_GENERATOR_WIDTH * GENERATOR_SALT) / 2);
  167.         int generatorWidth = (BASE_GENERATOR_WIDTH * GENERATOR_SALT);
  168.        
  169.         for(int i = 0; i < generatorWidth; i++) {
  170.             if(rand.nextInt(2) == 0) {
  171.                 world[left + i][0] = PARTICLE_SALT;
  172.             }
  173.         }
  174.     }
  175.    
  176.     private void generateOil() {
  177.         int center = worldWidth / 5 * 4;
  178.         int left = center - ((BASE_GENERATOR_WIDTH * GENERATOR_OIL) / 2);
  179.         int right = center + ((BASE_GENERATOR_WIDTH * GENERATOR_OIL) / 2);
  180.         int generatorWidth = (BASE_GENERATOR_WIDTH * GENERATOR_OIL);
  181.        
  182.         for(int i = 0; i < generatorWidth; i++) {
  183.             if(rand.nextInt(2) == 0) {
  184.                 world[left + i][0] = PARTICLE_OIL;
  185.             }
  186.         }
  187.     }
  188.    
  189.       private void doGravity() {
  190.         for (int i = worldHeight - 1; i >= 0; i--) {
  191.             ArrayList<Integer> jS = new ArrayList<Integer>();
  192.             jS.ensureCapacity(worldWidth);
  193.             for (int j = 0; j < worldWidth; j++) {
  194.                 jS.add(j);
  195.             }
  196.             Collections.shuffle(jS);
  197.             for (int j = 0; j < worldWidth; j++) {
  198.                 int newJ = jS.get(j);
  199.                 if (world[newJ][i] != EMPTY) {
  200.                     if (hasGravity(world[newJ][i])) {
  201.                         dropParticle(newJ, i);
  202.                     }
  203.                 }
  204.             }
  205.         }
  206.       }
  207.    
  208.     private boolean hasGravity(Color c) {
  209.         if(c.equals(PARTICLE_SAND))
  210.             return true;
  211.         if(c.equals(PARTICLE_WATER))
  212.             return true;
  213.         if(c.equals(PARTICLE_SALT))
  214.             return true;
  215.         if(c.equals(PARTICLE_OIL))
  216.             return true;
  217.         return false;
  218.     }
  219.    
  220.     private void dropParticle(int x, int y) {
  221.             if(y == worldHeight-1) {
  222.                 if(!HAS_FLOOR) {
  223.                     world[x][y] = EMPTY;
  224.                 }
  225.             }
  226.             else if(world[x][y+1]==EMPTY  && (x+1 < world.length) && world[x+1][y+1]==EMPTY && (x-1 >= 0) && world[x-1][y+1] == EMPTY) {
  227.                 int r = rand.nextInt(50);
  228.                 if(r == 0) {
  229.                     world[x-1][y+1] = world[x][y];
  230.                     //System.out.println("GO: right");
  231.                     //countRight++;
  232.                 }
  233.                 else if(r == 1) {
  234.                     world[x+1][y+1] = world[x][y];
  235.                     //System.out.println("GO: left");
  236.                     //countLeft++;
  237.                 }
  238.                 else {
  239.                     world[x][y+1] = world[x][y];
  240.                     //countCenter++;
  241.                 }
  242.                 world[x][y] = EMPTY;
  243.             }
  244.             else if(world[x][y+1]==EMPTY) {
  245.                 world[x][y+1] = world[x][y];
  246.                 world[x][y] = EMPTY;
  247.                 //countCenter++;
  248.             }
  249.             else if((x+1 < world.length) && world[x+1][y+1]==EMPTY && (x-1 >= 0) && world[x-1][y+1] == EMPTY) {
  250.                 if(rand.nextBoolean()) {
  251.                     world[x-1][y+1] = world[x][y];
  252.                     //countLeft++;
  253.                 }
  254.                 else {
  255.                     world[x+1][y+1] = world[x][y];
  256.                     //countRight++;
  257.                 }
  258.                 world[x][y] = EMPTY;
  259.             }
  260.             else if((x-1 >= 0) && world[x-1][y+1] == EMPTY) {
  261.                 world[x-1][y+1] = world[x][y];
  262.                 world[x][y] = EMPTY;
  263.                 countLeft++;
  264.                 //System.out.println("Hit Left");
  265.             }
  266.             else if((x+1 < world.length) && world[x+1][y+1] == EMPTY) {
  267.                 world[x+1][y+1] = world[x][y];
  268.                 world[x][y] = EMPTY;
  269.                 countRight++;
  270.                 //System.out.println("Hit Right");
  271.             }
  272.            
  273.             System.out.println("Left: " + countLeft + " Center: " + countCenter + " Right: " + countRight);
  274.     }
  275.    
  276.     private void drawWorld(Graphics g) {
  277.         for(int i = 0; i < world.length; i++) {
  278.             for(int j = 0; j < world[i].length; j++) {
  279.                 if(world[i][j] != EMPTY) {
  280.                     g.setColor(world[i][j]);
  281.                     g.fillRect(playLeft + i, playTop + j, 1, 1);
  282.                 }
  283.             }
  284.         }
  285.     }
  286.  
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement