Problem 6 5 - Firestarter (Bonus) PDF

Title Problem 6 5 - Firestarter (Bonus)
Author Vansh Darji
Course Programming Fundamentals
Institution Western Sydney University
Pages 4
File Size 34.9 KB
File Type PDF
Total Downloads 58
Total Views 138

Summary

This is the Code to Practical Problem 6.5 in Practical 6...


Description

/* PERMITTED COMMANDS move(); turnLeft(); turnRight(); treeLeft(); treeRight(); treeFront(); onLeaf(); putLeaf(); removeLeaf(); mushroomFront(); onLeaf(x, y); putLeaf(x,y); removeLeaf(x,y); for sensing, adding or removing a leaf at coordinate (x,y) onFire(x, y); putFire(x,y); removeFire(x,y); for sensing, adding or removing a leaf at coordinate (x,y) getX(); getY(); for obtaining the current coordinates of Clara JAVA if, while, for, do, &&, ||, ! variables, arrays */ class MyClara extends Clara { // constants final int WORLD_HEIGHT = 30; final int WORLD_WIDTH = 40; // variables boolean[][] currentWorldState; boolean[][] nextWorldState; /** * Place leaves at random and then apply the game of life rules */ void run() { // obtain the currentWorldState array by checking for leaves in the world currentWorldState = worldToArray(); // play game of life if not running for the first time nextWorldState = applyGameOfLifeRules(currentWorldState); updateWorld(nextWorldState); currentWorldState = nextWorldState; } /** * Scan the world for leaves and put true as the value of the * corresponding array element if a leaf is found */ boolean[][] worldToArray() { boolean[][] retVal = new boolean[WORLD_HEIGHT][WORLD_WIDTH]; for (int i = 0; i < WORLD_HEIGHT; i++) { for (int k = 0; k < WORLD_WIDTH; k++) {

if (onLeaf(i, k)) retVal[i][k] = true; else retVal[i][k] = false; } } return retVal; } /** * Go through the every element of inInitialArray and apply game of life rules to it * The results will be stored in the array that this method returns */ boolean[][] applyGameOfLifeRules(boolean[][] inInitialArray) { boolean[][] resultingArray = new boolean[inInitialArray.length][inInitialArray[0].length]; // go through all elements of the initial array and count neighbours // apply game of life rules based on the number of each cell's neighbours for (int i = 0; i < inInitialArray.length; i++) { for (int k = 0; k < inInitialArray[i].length; k++) { int neighbours = countNeighbours(inInitialArray, i, k); // Any live cell with fewer than two live neighbours dies, as if caused by under-population if (neighbours < 2) resultingArray[i][k] = false; // Any live cell with two or three live neighbours lives on to the next generation if ((inInitialArray[i][k] == true) && (neighbours == 2 || neighbours == 3)) resultingArray[i][k] = true; // Any live cell with more than three live neighbours dies, as if by overcrowding if (neighbours > 3) resultingArray[i][k] = false; // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction if (neighbours == 3 && inInitialArray[i][k] == false) resultingArray[i][k] = true; }

} return resultingArray; } /** * Remove all the leaves in Clara's world */ void cleanWorld() { for (int i = 0; i < WORLD_HEIGHT; i++) { for (int k = 0; k < WORLD_WIDTH; k++) { removeLeaf(k, i); } } }

/** * Given the array and the coordinates of a particular element of this array * count how many neighbors that are not false does this array have. */ int countNeighbours(boolean[][] inArray, int x, int y) { int retVal = 0; // we need to check all 8 surrounding cells // including those on the diagonal for (int i = -1; i...


Similar Free PDFs