import java.awt.*; // Needed for window and graphics (explained in COMP1406) import javax.swing.*; // Needed for window and graphics (explained in COMP1406) // This application displays an image public class FireSpreadSimulation { // This variable stores the panel that displays the fire // (This is a topic discussed in COMP1406 course) public static TerrainPanel terrainPanel; // These are the codes that indicate what is at each position in the array public static final byte GRASS = 1; public static final byte TREES = 2; public static final byte WATER = 3; public static final byte FIRE = 4; public static final byte BURNED = 0; // This is the 170 x 102 cell terrain where each cell corresponds to // a terrain type as is shown with the above 5 possible byte codes. public static String[] terrainData = { "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111112211111122222112222222222222221111111111111221121221112211111111111111111111111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112221111222222222222221111111111111221121111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111222222222222221111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111222222221222221111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111222222221122211111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111122211111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111112111111111111111111111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111113331111111111111111111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111311333331111111111111111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111113111333333333333331111111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111113333333333333333111111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111112211111111111111111111111111111131111333333333333333333111111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111121111111111111111111111111111331133333333333333333333311111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111112211111111111111111111111111331333333333333333333333311111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111113333333333333333333333333311111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111133333113333333333333333333331111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111112111333111113333333333333333333333111111111111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111111133331113333333333333333333333333331113333111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111111113333331133333333333333333333333333333333331111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111111113333333333333333333333333333333333333333333331111111111111111111111111111111111111111111111211111111111111111111111111111111111111", "11111111111111111111111111111111111113333333333333333333333333333333333333333333333331111111111111111111111111111111111111111111112221111111111111111111111111111111111111", "11111111111111111111111111111111111133333333333333333333333111333333333333333333333331111111111111111111111111111111111111111111112221111111111111111111111111111111111111", "11111111111111111111111111111111113333333333333333333333111111333333333333333333333333311111111111111111111111111111111111111111111221111111111111111111111111111111111111", "11111111111111111111111111111111133333333333333333333331111133333333333333333333333333331111111111111111111111111111111111111111111221111111111111111111111111111111111111", "11111111111111111111111111111111333333333333333333333111211333333333333333333333333333331111111111111111111111111111111111111111111121111111111111111111111111111111111111", "11111111111111111111111111111113333333333333333333331111113333333333333333333333333333311111111111111111111111111111111111111111111121111111111111111111111111111111111111", "11111111111111111111111111111333333333333333333333111111133333333333333333333333333333311111111111111111111111111111111111111111111112111111111111111111111111111111111111", "11111111111111111111111111113333331111333333333331111111311133333333333333333333333333311111111111111111111111111111111111111111111112111111111111111111111111111111111111", "11111111111111111111111111333331111113333333111111111111111111133333333333333333333333333111111111111111111111111111111111111111111111111111111111111111111111111111111111", "11111111111111111111111111311111111133333311111111111111111111113333333333333333311113333111111111111111111111111111111111111111111111121111111111111111111111111111111111", "11111111111111111111111111111111111111331111111111111111111111111333333333111111112213333111111111111111111111111111111111111111111111121111111111111111111111111111111111", "11111111111111111111111111111111111111112111111111111111111111111131133111111111111213331111111111111111111111111111111111111111111111111111111111111111111111111111111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111211111111111111111111111121111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111222111221221111111121111111111111111111111111111122111111122211111111111111111111121111111", "11111111111111111111111111111111111111111111111111111111111111111111111111121111111111111111133311111111111112221111111111111111122211111111211222211111111111111121111111", "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111122133333333333333111111111111111111112211111111111111111222211111111111121111111", "11111111111111111111112111111111111111111111111111111111111111111111212112222211333111211122111111333333333333333111112222222221111111111111111111112222111111111211111111", "11111111111111111111111111111111111111111111111111111111111111111111221121111111333331113313111111333333333333333331111111221111111111111111111111112222222111112211111111", "11111111111111111112111111111111111111111111111111111111112111111121111111333333333333313333333333333333333333333333333331121111111111111111111111111122222221111111211111", "11111111111111111122111111111111111111111111111111111111111122111111111111333333133331111113333333333333333333333333333333122111111111111111111111111112222221111211111112", "11111111111111111121111111111111111111111111111111111111111122111113333133333333133311121111333333333333333333333333333333111211111111111111111111111122222222112211222222", "11111111111111111121111111111111111111111111111111111111111122112133333333333333133311212221133333333333333333333333333333311211111111111111111111111112222222222222222222", "11111111111111111111111111111111111111111111111111111111111122112133333333333333333311121111111133333333333333333333333333331111111111111111111111111111122222222222222222", "11111111111111111112111111111111111111111111111111111111111122221333113333333333333111111111111111333333333333333333333333331111111111111111111111111111111122222222222222", "11111111111111111112111111111111111111111111111111111111111122213331133333333333331221111111111112133333333333333111333333333311111111111111111111111111111122222222222222", "11111111111111111112111111111111111111111111111111111111111112113311333333333331331221111111111121113333333333333311133333333331211111111111111111111111111112222222222222", "11111111111111111112111111111111111111111111111111111111111222133111333333333311311211111111111121113333333333333331133333333331111111111111111111111111111112222222222222", "11111111111111111112111111111111111111111111111111111111111221133113333333331111111111111111111111133333333333333331111333333333111111111111111111111111111112222222222222", "11111111111111111122111111111111111111111111111111111111111213311133333333331111111111111111111112113333333333333333121133333331311111111111111111111111111112222222222222", "11111111111111111122111111111111111111111111111111111111111133112133333333311111111111111111111111113333333333333333111133333311111111111111111111111111111222222222222222", "11111111111111111112111111111111111111111111111111111111111131121333333333111211111111111111111111113333333333333333311111133331111111111111111111111111112222222222222222", "11111111111111111111221111111111111111111111111111111111122111211333333333111111111111111111111112213333333333333333111112111331211111111111111111111122222222222222222222", "11111111111111111111111111111111111111111111111111111111112121113333333333111111111111111111111111213333333333333331111111111111211111111111111111211221122222222211222211", "11111111111111111111111122111111111111111111111111111111111111113333333333111111111111111111111111113333333333333311211111111111211111111111111111222222222222221111222211", "11111111111111111111111112211111111111111111111111111111111111113333333331121111111111111111111111133333333333333311111111111112111111111111111112222222222222131112222111", "11111111111111111111111111211111111111111111111111111111111111113333333331121111111111111111111121333333333333333111111111111111211121111111111112221111121113312222221111", "11111111111111111111111111112211111111111111111111111111111111113333333311211111111111111111122211333333333333333111111111111111111111111111122122212222113333312222211111", "11111111111111111111111111111122111111111111111111111111111111133333333311111111111111111111121133331111133333333111111111111111111111112222221111331112113333331111111111", "11111111111111111111111111111112111111111111111111111111111111333333333311211111111111111111211333112212113333333111111111111111111112221111133333333311133333311211111111", "11111111111111111111111111111112111111111111111111111111111111333333333332211111111111112111211333122111113333333111111111111111111222113333333333333333333333312111111111", "11111111111111111111111111111112111111111111111111111111111111333333333311111111111111111222111331122211213333333111111111111111112211333333333333333333333333332111111111", "11111111111111111111111111111112211111111111111111111111111111333333333311111111111111111222221111211111213333333111111111111111122133333333333333333333333333332111111111", "22222222222112222221122211122222211111111111111111111111111111333333333311111111111111111222222222111111213333333111111111111111121333333333333333333333333331112211111111", "11111111111111111111111111111111211111111111111111111111111113333333333331111111111111111122222221111111113333331111111111111111211333333331111111333333333112222221111111", "11111111111111111111111111111111211111111111111111111111111113333333333331111111111111111111122221111111113333331111111111111111213333311112222222111111111122222222211111", "11111111111111111111111111111111211111111111111111111111111113333333333331112111111111112211111111111112211333111111111111111111221111222222222222222222222222222222222211", "11111111111111111111111111111111211111111111111111111111111113333333333333111111111111111111111111111111111331121111111111111111122222221122211111222222222222222222221211", "11111111111111111111111111111111211111111111111111111111111213333333333333111211111111111111111211111111121112211111111111111111111122111222111111111112122222111111111111", "11111111111111111111111111111111111111111111111111111111111113333333333333122111111111111111111111111111222211121111111111111112211111121111111112111111122211111111111111", "11111111111111111111111111111111111111111111111111111111111221333333333333112112111111111111111111111111111221121111111122111111311333331111111111111111122211111111111111", "11111111111111111111111111111111112221111111111111111111111121333333333333112112111111111111111111111122112222111221111121111333333333331111111111111111122111111111111111", "11111111111111111111111111111111111111111111111111111111111121333333333333112111111111111111111111111121311112212111333331111333333333111111111111111111111111111111111111", "11111111111111111111111111111111111122221122222222222111222221333333333333121111111111111111111111122111311122221113333333333333333331111111111111111111111111111111111111", "11111111111111111111111111111111111112221111111111111111122221333333333333121111111111111111111111122113331122111333333333333333333111111111111111111111111111111111111111", "11111111111111111111111111111111111111121111111111111111111111333333333331111111111111111111111112111111111221113333333333333333331111111111111111111111111111111111111111", "11111111111111111111111111111111111111222111111111111111111121333333333331211111111111111111111112111122222211333333333333333333111111111111111111111111111111111111111111", "11111111111111111111111111111111111111112211111111111111111111333333333311111111111111111111111112221122221133333333333333333111111111111111111111111111111111111111111111", "11111111111111111111111111111111111111112211111111111111111112133333333311111111111111111111111112211111111333333333333333311111111111111111111111111111111111111111111111", "11111111111111111111111111111111111111112211111111111111111112133333333122111111111111111111111211113333333333333333333311111111111111111111111111111111111111111111111111", "11111111111111111111111111111111111111122211111111111111111112113333331122111111111111111111111122133333333333333333311111221111111111111111111111111111111111111111111122", "11111111111111111111111111111111111111122221111111111111121112213333112222222222221112221222222222133333333333333333111111111111111111111111111111111111111111111111111112", "11111111111111111111111111111111211111222122221111111122122112221111111211111111111111111111111122211133333333333311111111111111111111111111111111111111111111111111111112", "11111111111111111111111111111112111122222122121111111222212111122211111211111111111111111111111111222111133333333111111111111111111111111111111111111111111111111111111111", "11111111111111111111111111111122222222212122111111112211212211121121111111111111111111111111111111212211111111211111111111121111111111111111111111111111111111111111111111", "11111111111111111111111111111112222111111111111222222222222211121111111111111111111111111111111111111122111111111111111111111111111111111111111111111111111111111111111111", "11111111111111111111111111111111222111111111111222222211222211121111111111111111111111111111111111111111111111111111111111111111111111111111111111111112112111111111111111", "11111111111111111111111111111111122111111111111222211111211211221111111111111111111111121111111111111111111111111111111111121111111111111111111111111111122111111111111111", "11111111111111111111111111111111122211111111112211111111111221221111111111111111111111121111111111111111111111111111111111111111111111111111111111121111122111111111111122", "11111111111111111111111111111111122111111111112211111111111221221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111222111111111111222", "11111111111111111111111111111111122111111111222211111111111221121111111111111111111111111111111111111111111111111111111111111111111111111111111111111112222111111111211222", "11111111111111111111111111211112221111112111222111111111111221121111111111111111111111111111111111111111111111111111111111121111111111111111111111111111111111111111222222", "11111111111111111111122222222222221111122111221111111111111221121111111111111111111111111111111111111111111111111111111111221111111111111111111111122212211111121111222222", "11111111112211112222222222222222111111222222222111111111111111121111111211111111111111111111111111111111111111111111111111221111111111111111111111112212221111112222222222", "11111111111111111111111111122222122111222222222111111111111111121111121111111111111111111111111111111111111111111111111111221111111111111111111111111111221122222222222222", "11111111111111111111111111112222122211122222222211111111111111221111211111111111111111111111111111111111111111111111111111221111111111111111111111111111222222222222222222", "11111111111111111111111111212222122221222222222222111111111111222111111111111111111111111111111111111111111111111111111111221111111111111111111111111222222222122222222222", "11111111111111111111111111212222112222222222222222111111111111222211111111111111111111111111111111111111111111111111111111211111111111111111111111111112222222222222222222", "11111111111111111111111111112222111122222222222222111111111111222111111111111111111111111111111111111111111111111111111111211111111111111111111111112111222222222222222222" }; // These variables store the width and height of the terrain public static int terrainWidth = terrainData[0].length(); public static int terrainHeight = terrainData.length; // This is the terrain itself, which is made from the above String array. public static byte[][] terrain = new byte[terrainHeight][terrainWidth]; // This code represets the active border of all fires public static int[] borderX; // points along the border of the fire public static int[] borderY; // points along the border of the fire public static float[] borderCost; // points along the border of the fire public static int borderSize; // # points along the border of the fire public static void startSimulation() { // Fill up the terrain array for (int i=0; i 0) { terrainPanel.repaint(); // This code redraws the window spreadFire(); // This code simulates the fire spreading try{ Thread.sleep(1); } catch(Exception e){}; } } // Function that determines if a cell can be burned public static boolean isBurnable(int y, int x) { return (terrain[y][x] == GRASS) || (terrain[y][x] == TREES); } // Simulate the spreading of the fire in each direction from the oldest fire point public static void spreadFire() { // Grab the last border point (after sorted). It should be the oldest // border of the fires ... which should be processed first int pX = borderX[borderSize-1]; int pY = borderY[borderSize-1]; float pCost = borderCost[borderSize-1]; // Extingush the fire at this location by marking the terrain as burned terrain[pY][pX] = BURNED; borderSize--; // Now we will check all around this extinguished pixel to spread // the fire outwards in all 8 directions, if possible // Check left if ((pX > 0) && isBurnable(pY,pX-1)) { terrain[pY][pX-1] = FIRE; borderX[borderSize] = pX-1; borderY[borderSize] = pY; borderCost[borderSize] = pCost + 1 + (int)(Math.random()*3); borderSize++; } // Check right if ((pX < terrainWidth-1) && isBurnable(pY,pX+1)) { terrain[pY][pX+1] = FIRE; borderX[borderSize] = pX+1; borderY[borderSize] = pY; borderCost[borderSize] = pCost + 1 + (int)(Math.random()*3); borderSize++; } // Check up if ((pY > 0) && isBurnable(pY-1,pX)) { terrain[pY-1][pX] = FIRE; borderX[borderSize] = pX; borderY[borderSize] = pY-1; borderCost[borderSize] = pCost + 1 + (int)(Math.random()*3); borderSize++; } // Check down if ((pY < terrainHeight-1) && isBurnable(pY+1,pX)) { terrain[pY+1][pX] = FIRE; borderX[borderSize] = pX; borderY[borderSize] = pY+1; borderCost[borderSize] = pCost + 1 + (int)(Math.random()*3); borderSize++; } // Check left/up diagonal if ((pX > 0) && (pY > 0) && isBurnable(pY-1,pX-1)) { terrain[pY-1][pX-1] = FIRE; borderX[borderSize] = pX-1; borderY[borderSize] = pY-1; borderCost[borderSize] = pCost + 1.414f + (int)(Math.random()*3); borderSize++; } // Check right/up diagonal if ((pX < terrainWidth-1) && (pY > 0) && isBurnable(pY-1,pX+1)) { terrain[pY-1][pX+1] = FIRE; borderX[borderSize] = pX+1; borderY[borderSize] = pY-1; borderCost[borderSize] = pCost + 1.414f + (int)(Math.random()*3); borderSize++; } // Check left/down diagonal if ((pX > 0) && (pY < terrainHeight-1) && isBurnable(pY+1,pX-1)) { terrain[pY+1][pX-1] = FIRE; borderX[borderSize] = pX-1; borderY[borderSize] = pY+1; borderCost[borderSize] = pCost + 1.414f + (int)(Math.random()*3); borderSize++; } // Check right/down diagonal if ((pX < terrainWidth-1) && (pY=0; p--) { boolean madeSwap = false; for (int i=0; i<=p-1; i++) { if (borderCost[i] < borderCost[i+1]) { int tempX = borderX[i+1]; int tempY = borderY[i+1]; float tempCost = borderCost[i+1]; borderX[i+1] = borderX[i]; borderY[i+1] = borderY[i]; borderCost[i+1] = borderCost[i]; borderX[i] = tempX; borderY[i] = tempY; borderCost[i] = tempCost; madeSwap = true; } } if (!madeSwap) return; } } // The main method simply creates a window with a TerrainPanel and then starts the simulation public static void main(String args[]) { JFrame frame = new JFrame("Fire Spread Simulation"); frame.add(terrainPanel = new TerrainPanel(terrain)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); // Makes size according to panel's preference frame.setVisible(true); startSimulation(); } }