diff --git a/ codemast.java b/ codemast.java new file mode 100644 index 0000000..659465b --- /dev/null +++ b/ codemast.java @@ -0,0 +1,269 @@ +// This entire file is part of my masterpiece. +// Michelle Chen +package game_mmc56; + import java.util.ArrayList; + import java.util.Iterator; +import java.util.List; +import java.util.Random; + +import javafx.animation.KeyFrame; + import javafx.animation.Timeline; +import javafx.animation.TranslateTransition; +import javafx.application.Application; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.geometry.Bounds; +import javafx.scene.Group; + import javafx.scene.Scene; + import javafx.scene.canvas.Canvas; + import javafx.scene.canvas.GraphicsContext; + import javafx.scene.paint.Color; +import javafx.scene.shape.Arc; +import javafx.scene.shape.ArcType; +import javafx.scene.shape.Circle; +import javafx.scene.shape.Path; +import javafx.scene.shape.Rectangle; + import javafx.scene.shape.Shape; + import javafx.scene.text.Font; + import javafx.scene.text.FontWeight; + import javafx.stage.Stage; + import javafx.util.Duration; + import javafx.stage.Stage; + import javafx.scene.text.FontPosture; + import javafx.scene.image.Image; + import javafx.scene.image.ImageView; + import javafx.scene.input.KeyCode; + import javafx.scene.input.KeyEvent; +import javafx.scene.input.MouseEvent; + + class Game { + public static final String gameName = "B T B"; + private static final int numberOf = 30; + public static final int KEY_INPUT_SPEED = 13; + + private Scene theScene; + private Canvas canvas; + private GraphicsContext gc2; + + private Group root; + private Rectangle catcher; + private Rectangle object; + + private ArrayList rectz; + private List targets; + + private boolean start = false; + + //name of the game + public String getName() { + return gameName; + } + + //setting up scene + public Scene myScene(int width, int height) { + //scene + root = new Group(); + theScene = new Scene(root, width, height, Color.LAVENDERBLUSH); + //splash screen + canvas = new Canvas(width, height); + gc2 = splashScreen(width, height, canvas); + root.getChildren().add(canvas); + //starting game + startGame(width, height); + //level two.. not working + levelTwo(width, height, gc2); + //cheat code + cheat(); + return theScene; + } + + //starting game + private void startGame(int width, int height) { + theScene.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler() { + @Override + public void handle(MouseEvent mouseEvent) { + if (mouseEvent.getClickCount() ==2) { + start = true; + gc2.clearRect(0, 0, 500, 500); +// square + catcher = new Rectangle(width /9, height /9, 30, 30); + catcher.setFill(Color.DARKBLUE); + root.getChildren().add(catcher); +// other squares + rectz= rectangles(numberOf); + for(Rectangle rect: rectz) { + root.getChildren().add(rect); + } + //respond to key presses + theScene.setOnKeyPressed(e -> keyInput(e.getCode())); + //winning game + canvas = new Canvas(width, height); + GraphicsContext gc = canvas.getGraphicsContext2D(); + root.getChildren().add(canvas); + } + } + }); + + cheat(); + levelTwo(width, height, gc2); + + } + + private void levelTwo(int width, int height, GraphicsContext gcc) { + theScene.addEventFilter(MouseEvent.MOUSE_DRAGGED, new EventHandler() { + + @Override + public void handle(MouseEvent event) { + gcc.clearRect(0, 0, 500, 500); + gcc.setFill(Color.AQUA); + final Arc gun = new Arc(width/2, height, 50, 50, 0, 180); + gun.setFill(Color.DARKBLUE); + gun.setType(ArcType.ROUND); + root.getChildren().add(gun); + //square + root.getChildren().add(new Rectangle(215, 25, 75, 75)); + fire(targets); + } + + }); + + } + + //this doesn't work + private void fire(List targets) { + gc2.clearRect(0, 0, 500, 500); + final Shape bullet = new Circle(4, Color.BLACK); + root.getChildren().add(bullet); + final TranslateTransition bulletAnimation = new TranslateTransition(Duration.seconds(1), bullet); + final int bulletTargetX = new Random().nextInt(500); + + bullet.boundsInParentProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, + Bounds oldValue, Bounds newValue) { + for (final Shape target : new ArrayList(targets)) { + if (((Path)Shape.intersect(bullet, target)).getElements().size() > 0) { + targets.remove(target); + root.getChildren().remove(target); + } + } + } + }); + bulletAnimation.setOnFinished(new EventHandler() { + @Override + public void handle(ActionEvent event) { + root.getChildren().remove(bullet); + } + }); + bulletAnimation.play(); + } + + //cheat key + private void cheat(){ + theScene.setOnKeyPressed(new EventHandler() { + @Override + public void handle(KeyEvent event) { + if (event.getCode() == KeyCode.ENTER){ + gc2.clearRect(0, 0, 500, 500); + winningScreen(canvas); + } + } + }); + } + + //splash screen + public GraphicsContext splashScreen(int width, int height, Canvas canvas) { + GraphicsContext gc2 = canvas.getGraphicsContext2D(); + Font myFont = Font.font("Times New Roman", FontWeight.BOLD, 30); + gc2.setFont(myFont); + gc2.setFill(Color.DARKBLUE); + gc2.fillText("BTB : BEAT THE BLOX", 80, 36); + Font myFont2 = Font.font("Times New Roman", FontWeight.BOLD, 15); + gc2.setFont(myFont2); + gc2.fillText("GTHC! You're a loyal Duke fan on a mission to shut down pesky UNC", 20, 80); + gc2.fillText("fans. Use your left and right keys to move your Duke Blue block around", 20, 98); + gc2.fillText("the screen, and touch the spinning UNC blox to make them dissapear.", 20, 116); + Font myFont3 = Font.font("Times New Roman", FontWeight.BOLD, 18); + gc2.setFont(myFont3); + gc2.fillText("double tap to start the game!", 125, 160); + gc2.drawImage(new Image("/images/ddmf.png"), 100, 200); + return gc2; + } + + //arraylist of blox + private ArrayList rectangles(int numberOf) { + ArrayList theRectangles = new ArrayList(); + for (int a = 0; a + + + + + diff --git a/game_mmc56 b/game_mmc56 new file mode 160000 index 0000000..2cf8f8e --- /dev/null +++ b/game_mmc56 @@ -0,0 +1 @@ +Subproject commit 2cf8f8e9e766326fd558e8d10036a3f4b7076a83 diff --git a/recitation b/recitation new file mode 100644 index 0000000..6c7fda7 --- /dev/null +++ b/recitation @@ -0,0 +1,14 @@ +Readability +1) Some pieces of code that obscure our understanding of the algorithm are the two chunks of code in the main method of the Bins class that look very similar and have a lot of repetitive code, as it makes it hard to distinguish the algorithms.  In addition, having lots of print statements amidst the algorithm code is distracting. On the other hand, it would be helpful to understanding the algorithm to have pieces of code with comments that explain what the code does, like in the Disk class where there are clear comments. +2) It would be useful to have comments delineating where each heuristic exists in the code.  It would also be useful to have comments clarifying what chunks of code, like in the for loops, for example, do. +3) There could be clearer and more concise code in the Bins class under the main method, as there are a lot of lengthy for loops.   + +Testability +1) To test this code for bugs, you could run on different files. +2) An example of a “test case” could be one with file sizes that produce very different results based on the sorting order to ensure each algorithm works differently.  For example, 750000 100000 800000. +3) It would be helpful to have a function for each heuristic, one for the worst-fit decreasing method and one for the worst-fit method.  It may also be useful to have a function that adds and removes from the priority queue as the for loop conditions require, since that code is repeptitive. +4) Test cases for this would be the same because you jsut need to ensure the heuristic work. + +Extensibility + +There is duplicate code here for each of the for loops in dealing with adding files to disks according to each heuristic.  The two for loops are almost identical except for the if condition, so these chunks of codes may benefit from being placed into a function that is simply reused for each heuristic.  The print statements could also be put into a function since they print the same things, except for one tweak specifying the method being printed which can just be specified via a parameter.  The Bins class also has lazy code because the main function contains all of the code instead of creating separate functions to do all of it. \ No newline at end of file diff --git a/src/Bins.java b/src/Bins.java index b313a3d..cbf3434 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -1,10 +1,9 @@ -import java.io.File; -import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; +import java.util.function.Function; /** * Runs a number of algorithms that try to fit files onto disks. @@ -25,11 +24,26 @@ public List readData (Scanner input) { } return results; } - - /** - * The main program. - */ - public static void main (String args[]) { + + public void printInfo(int total, PriorityQueue pq) { + System.out.println("total size = " + total / 1000000.0 + "GB"); + System.out.println(); + System.out.println("worst-fit method"); + System.out.println("number of pq used: " + pq.size()); + while (!pq.isEmpty()) { + System.out.println(pq.poll()); + } + System.out.println(); + + } + + public void consolidation(Disk disky, int size, PriorityQueue pq) { + disky.add(size); + pq.add(disky); + + } + + public void heuristicOne(List datax) { Bins b = new Bins(); Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE)); List data = b.readData(input); @@ -43,25 +57,37 @@ public static void main (String args[]) { Disk d = pq.peek(); if (d.freeSpace() > size) { pq.poll(); - d.add(size); - pq.add(d); + consolidation(d, size, pq); } else { Disk d2 = new Disk(diskId); diskId++; - d2.add(size); - pq.add(d2); + consolidation(d2, size, pq); } total += size; } + + printInfo(total, pq); + } + + public List fitDisksAndPrint(List myList, Function,List> func){ + List list = func.apply(myList); + return list; + } + + /** + * The main program. + */ + public static void main (String args[]) { + Bins b = new Bins(); + Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE)); + List data = b.readData(input); - System.out.println("total size = " + total / 1000000.0 + "GB"); - System.out.println(); - System.out.println("worst-fit method"); - System.out.println("number of pq used: " + pq.size()); - while (!pq.isEmpty()) { - System.out.println(pq.poll()); - } - System.out.println(); + PriorityQueue pq = new PriorityQueue(); + pq.add(new Disk(0)); + + int diskId = 1; + int total = 0; + Collections.sort(data, Collections.reverseOrder()); pq.add(new Disk(0)); diff --git a/src/Disk.java b/src/Disk.java index e3a3f31..eae7b9e 100644 --- a/src/Disk.java +++ b/src/Disk.java @@ -71,14 +71,9 @@ public String toString () { @Override public boolean equals (Object other) { if (other != null && other instanceof Disk) { - if (myId == ((Disk) other).myId) { - return true; - } else { - return false; - } - } else { - return false; + return (myId == ((Disk) other).myId); } + return false; } /** diff --git a/src/lab_bins/dsf.java b/src/lab_bins/dsf.java new file mode 100644 index 0000000..e404623 --- /dev/null +++ b/src/lab_bins/dsf.java @@ -0,0 +1,46 @@ +package lab_bins; + +public class dsf { + +} + +//import javafx.scene.Scene; +// +//public class Fire extends Simulation { +// private double probCatch = .2; +// private int gridSize; +// private Scene myScene; +// private GridCell[][] myGrid; +// //getcardinalneighbors +// +// public Fire() { +// // TODO Auto-generated constructor stub +// gridSize = getGridSize(); +// myGrid = getCells(); +// } +// +// public Scene init(){ +// myScene = super.init(gridSize,gridSize); +// //fill +// for (int a = 0; a