Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
269 changes: 269 additions & 0 deletions codemast.java
Original file line number Diff line number Diff line change
@@ -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<Rectangle> rectz;
private List<Shape> 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<MouseEvent>() {
@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<MouseEvent>() {

@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<Shape> 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<Bounds>() {
@Override
public void changed(ObservableValue<? extends Bounds> observable,
Bounds oldValue, Bounds newValue) {
for (final Shape target : new ArrayList<Shape>(targets)) {
if (((Path)Shape.intersect(bullet, target)).getElements().size() > 0) {
targets.remove(target);
root.getChildren().remove(target);
}
}
}
});
bulletAnimation.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
root.getChildren().remove(bullet);
}
});
bulletAnimation.play();
}

//cheat key
private void cheat(){
theScene.setOnKeyPressed(new EventHandler<KeyEvent>() {
@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<Rectangle> rectangles(int numberOf) {
ArrayList<Rectangle> theRectangles = new ArrayList<Rectangle>();
for (int a = 0; a<numberOf; a++) {
object = new Rectangle(300*Math.random()+100, 300*Math.random()+100, 15, 15);
object.setFill(Color.LIGHTSKYBLUE);
theRectangles.add(object);
}
return theRectangles;
}

//removing blox and checking if they're all gone
public void movement(double elapsedTime) {
if (start) {
for (Rectangle recta: rectz) {
recta.setRotate(recta.getRotate()-Math.random());
}
for (Rectangle rectan: rectz) {
Shape intersector = Shape.intersect(catcher, rectan);
if (intersector.getBoundsInLocal().getWidth()!=-1) {
rectan.setFill(Color.BLACK);
root.getChildren().remove(rectan);
rectz.remove(rectan);
break;
}
}
if (rectz.isEmpty()) {
gc2.clearRect(0, 0, 512, 512);
winningScreen(canvas);
}
}
}

//reading key input
private void keyInput (KeyCode code) {
switch (code) {
case RIGHT:
catcher.setX(catcher.getX() + KEY_INPUT_SPEED);
break;
case LEFT:
catcher.setX(catcher.getX() - KEY_INPUT_SPEED);
break;
case UP:
catcher.setY(catcher.getY() - KEY_INPUT_SPEED);
break;
case DOWN:
catcher.setY(catcher.getY() + KEY_INPUT_SPEED);
break;
default:
}
}

//screen after level one passed
public Canvas winningScreen(Canvas canvas){
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, 500, 500);
Font myFont = Font.font("Times New Roman", FontWeight.BOLD, 24);
gc.setFont(myFont);
gc.setFill(Color.DARKBLUE);
gc.fillText("CONGRATULATIONS!!!!!!", 100, 60);
Font myFont2 = Font.font("Times New Roman", FontWeight.BOLD, 17);
gc.setFont(myFont2);
gc.fillText("You've beaten all of the pesky UNC blox.", 100, 90);
Font myFont3 = Font.font("Times New Roman", FontWeight.NORMAL, 15);
gc.setFont(myFont3);
gc.fillText("But you're not done just yet... looks like UNC fans are back for more.", 40, 120);
gc.setFont(myFont2);
gc.fillText("Drag your mouse to start the next level!", 115, 160);
Font myFont4 = Font.font("Times New Roman", FontWeight.NORMAL, 13);
gc.setFont(myFont4);
gc.fillText("(ok to be honest this level pretty much doesn't work at all)", 100, 180);
gc.drawImage(new Image("/images/gthc.jpg"), 165, 200);
return canvas;
}
}
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions game_mmc56
Submodule game_mmc56 added at 2cf8f8
14 changes: 14 additions & 0 deletions recitation
Original file line number Diff line number Diff line change
@@ -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.
64 changes: 45 additions & 19 deletions src/Bins.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -25,11 +24,26 @@ public List<Integer> readData (Scanner input) {
}
return results;
}

/**
* The main program.
*/
public static void main (String args[]) {

public void printInfo(int total, PriorityQueue<Disk> 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<Disk> pq) {
disky.add(size);
pq.add(disky);

}

public void heuristicOne(List<Integer> datax) {
Bins b = new Bins();
Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE));
List<Integer> data = b.readData(input);
Expand All @@ -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<Integer> fitDisksAndPrint(List<Integer> myList, Function<List<Integer>,List<Integer>> 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<Integer> 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<Disk> pq = new PriorityQueue<Disk>();
pq.add(new Disk(0));

int diskId = 1;
int total = 0;


Collections.sort(data, Collections.reverseOrder());
pq.add(new Disk(0));
Expand Down
Loading