diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..6ba2ac2 --- /dev/null +++ b/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/lib/guava-bootstrap-jdk5-16.0.jar b/lib/guava-bootstrap-jdk5-16.0.jar new file mode 100644 index 0000000..f75b3a2 Binary files /dev/null and b/lib/guava-bootstrap-jdk5-16.0.jar differ diff --git a/lib/guava-jdk5-16.0-javadoc.jar b/lib/guava-jdk5-16.0-javadoc.jar new file mode 100644 index 0000000..5d6e368 Binary files /dev/null and b/lib/guava-jdk5-16.0-javadoc.jar differ diff --git a/lib/guava-jdk5-16.0-sources.jar b/lib/guava-jdk5-16.0-sources.jar new file mode 100644 index 0000000..6e9963b Binary files /dev/null and b/lib/guava-jdk5-16.0-sources.jar differ diff --git a/lib/guava-jdk5-16.0.jar b/lib/guava-jdk5-16.0.jar new file mode 100644 index 0000000..945b236 Binary files /dev/null and b/lib/guava-jdk5-16.0.jar differ diff --git a/lib/hamcrest-core-1.3.jar b/lib/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/lib/hamcrest-core-1.3.jar differ diff --git a/lib/junit-4.12-javadoc.jar b/lib/junit-4.12-javadoc.jar new file mode 100644 index 0000000..f7bdb82 Binary files /dev/null and b/lib/junit-4.12-javadoc.jar differ diff --git a/lib/junit-4.12-sources.jar b/lib/junit-4.12-sources.jar new file mode 100644 index 0000000..884f92f Binary files /dev/null and b/lib/junit-4.12-sources.jar differ diff --git a/lib/junit-4.12.jar b/lib/junit-4.12.jar new file mode 100644 index 0000000..3a7fc26 Binary files /dev/null and b/lib/junit-4.12.jar differ diff --git a/src/debug/ContainerArray.java b/src/debug/ContainerArray.java index 124ece6..f1cd9a2 100755 --- a/src/debug/ContainerArray.java +++ b/src/debug/ContainerArray.java @@ -23,6 +23,14 @@ public int size () { } public void remove (E objectToRemove) { + for (int i = 0; i < internalArray.length; i++) { + if (internalArray[i] == objectToRemove) { + for (int j = i; j < internalArray.length - 1; j++) { + internalArray[j] = internalArray[j+1]; + } + break; + } + } currentSize--; } diff --git a/src/debug/ContainerArrayTest.java b/src/debug/ContainerArrayTest.java index c566d50..a4d9b91 100755 --- a/src/debug/ContainerArrayTest.java +++ b/src/debug/ContainerArrayTest.java @@ -41,7 +41,11 @@ public void testObjectIsRemoved () { String alligator = "Alligator"; myContainer.add("Alligator"); myContainer.add("Bear"); + myContainer.add("Tiger"); myContainer.remove("Bear"); assertEquals("Remove should be same reference", alligator, myContainer.get(0)); + assertEquals("Objects shoulld be the same", "Tiger", myContainer.get(1)); + } + } diff --git a/src/stephen_vooga/TestTextAreaGameDescriptionEditor.java b/src/stephen_vooga/TestTextAreaGameDescriptionEditor.java new file mode 100644 index 0000000..461d84d --- /dev/null +++ b/src/stephen_vooga/TestTextAreaGameDescriptionEditor.java @@ -0,0 +1,25 @@ +package stephen_vooga; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +public class TestTextAreaGameDescriptionEditor { + + private TextAreaGameDescriptionEditor editor = null; + + @Before + public void setUp () { + editor = new TextAreaGameDescriptionEditor("Hello", "Go", 5, e -> printStuff()); + } + + public void printStuff() { + System.out.println("Hello"); + } + + @Test + public void testPromptText() { + assertEquals("Paddings should match", "Hello", editor.getPromptText()); + } +} diff --git a/src/stephen_vooga/TextAreaGameDescriptionEditor.java b/src/stephen_vooga/TextAreaGameDescriptionEditor.java new file mode 100644 index 0000000..c38f930 --- /dev/null +++ b/src/stephen_vooga/TextAreaGameDescriptionEditor.java @@ -0,0 +1,31 @@ +package stephen_vooga; + +import java.util.ResourceBundle; + +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.geometry.Insets; + +/** + * Creates a VBox containing a Label prompting author to edit the game description, a TextArea in which the author + * can write the game description, and a Button allowing the author to save the text within the TextArea as the + * game's description + * + * @author Stephen + * + */ + +public class TextAreaGameDescriptionEditor extends TextAreaParent { + + private static final double VBOX_PADDING = 10.0; + private static final String RESOURCE_BUNDLE_KEY = "mainScreenGui"; + private ResourceBundle myResources; + + public TextAreaGameDescriptionEditor(String promptText, String buttonText, int prefRows, EventHandler handler) { + super(promptText, buttonText, prefRows, handler); + myResources = ResourceBundle.getBundle(RESOURCE_BUNDLE_KEY); + setContainerPadding(new Insets(VBOX_PADDING)); + setTextAreaPromptText(myResources.getString("enterGameDescription")); + } + +} diff --git a/src/stephen_vooga/TextAreaParent.java b/src/stephen_vooga/TextAreaParent.java new file mode 100644 index 0000000..c6b6bae --- /dev/null +++ b/src/stephen_vooga/TextAreaParent.java @@ -0,0 +1,56 @@ +package stephen_vooga; + +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.geometry.Insets; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.TextArea; +import javafx.scene.layout.VBox; + +/** + * + * Abstract class generating a VBox containing a Label prompting user to do something, a TextArea in which the author + * can enter text, and a Button that can perform any defined action + * game's description + * + * @author Stephen + * + */ + +public abstract class TextAreaParent { + + private VBox myContainer; + private Label myPrompt; + private TextArea myTextArea; + private Button myButton; + + public TextAreaParent(String promptText, String buttonText, int prefRows, EventHandler handler) { + myContainer = new VBox(); + myPrompt = new Label(promptText); + myPrompt.setWrapText(true); + myTextArea = new TextArea(); + myTextArea.setPrefRowCount(prefRows); + myButton = new Button(buttonText); + myButton.setOnAction(handler); + myButton.prefWidthProperty().bind(myContainer.widthProperty()); + myContainer.getChildren().addAll(myPrompt, myTextArea, myButton); + } + + public void setContainerPadding(Insets insets) { + myContainer.setPadding(insets); + } + + public String getPromptText() { + return myPrompt.getText(); + } + + protected void setTextAreaPromptText(String prompt) { + myTextArea.setPromptText(prompt); + } + + public VBox getCoupledNodes() { + return myContainer; + } + +} diff --git a/src/tdd/ChallengePart1.java b/src/tdd/ChallengePart1.java new file mode 100644 index 0000000..d82c932 --- /dev/null +++ b/src/tdd/ChallengePart1.java @@ -0,0 +1,102 @@ +package tdd; + +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + +/** + * Created by rhondusmithwick on 4/7/16. + * + * @author Rhondu Smithwick + */ +public class ChallengePart1 { + + @Test + public void testThatCellsAreEmptyByDefault() { + Sheet sheet = new Sheet(); + String[] testStrings = new String[]{"A1", "ZX347"}; + for (String s: testStrings) { + assertEquals("", sheet.get(s)); + } + } + +// Implement each test before going to the next one. + + @Test + public void testThatTextCellsAreStored() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "A string"); + assertEquals("A string", sheet.get(theCell)); + + sheet.put(theCell, "A different string"); + assertEquals("A different string", sheet.get(theCell)); + + sheet.put(theCell, ""); + assertEquals("", sheet.get(theCell)); + } + +// Implement each test before going to the next one; then refactor. + + @Test + public void testThatManyCellsExist() { + Sheet sheet = new Sheet(); + sheet.put("A1", "First"); + sheet.put("X27", "Second"); + sheet.put("ZX901", "Third"); + + assertEquals("A1", "First", sheet.get("A1")); + assertEquals("X27", "Second", sheet.get("X27")); + assertEquals("ZX901", "Third", sheet.get("ZX901")); + + sheet.put("A1", "Fourth"); + assertEquals("A1 after", "Fourth", sheet.get("A1")); + assertEquals("X27 same", "Second", sheet.get("X27")); + assertEquals("ZX901 same", "Third", sheet.get("ZX901")); + } + + +// Implement each test before going to the next one. +// You can split this test case if it helps. + + @Test + public void testThatNumericCellsAreIdentifiedAndStored() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "X99"); // "Obvious" string + assertEquals("X99", sheet.get(theCell)); + + sheet.put(theCell, "14"); // "Obvious" number + assertEquals("14", sheet.get(theCell)); + + sheet.put(theCell, " 99 X"); // Whole string must be numeric + assertEquals(" 99 X", sheet.get(theCell)); + + sheet.put(theCell, " 1234 "); // Blanks ignored + assertEquals("1234", sheet.get(theCell)); + + sheet.put(theCell, " "); // Just a blank + assertEquals(" ", sheet.get(theCell)); + } + +// Refactor before going to each succeeding test. + + public void testThatWeHaveAccessToCellLiteralValuesForEditing() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "Some string"); + assertEquals("Some string", sheet.getLiteral(theCell)); + + sheet.put(theCell, " 1234 "); + assertEquals(" 1234 ", sheet.getLiteral(theCell)); + + sheet.put(theCell, "=7"); // Foreshadowing formulas:) + assertEquals("=7", sheet.getLiteral(theCell)); + } + +} diff --git a/src/tdd/Sheet.java b/src/tdd/Sheet.java new file mode 100644 index 0000000..d99ebb3 --- /dev/null +++ b/src/tdd/Sheet.java @@ -0,0 +1,39 @@ +package tdd; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by rhondusmithwick on 4/7/16. + * + * @author Rhondu Smithwick + */ +public class Sheet { + + private final Map sheetMap = new HashMap<>(); + + public String get(String cell) { + String value = getLiteral(cell); + try { + Double.parseDouble(value); + return value.trim(); + } catch (NumberFormatException n) { + return value; + } + } + + public void put(String cell, String cellValue) { + sheetMap.put(cell, cellValue); + } + + public String getLiteral(String cell) { + if (!containsCell(cell)) { + sheetMap.put(cell, ""); + } + return sheetMap.get(cell); + } + + private boolean containsCell(String cell) { + return sheetMap.containsKey(cell); + } +} diff --git a/src/voogasalad/rhondu/IComponent.java b/src/voogasalad/rhondu/IComponent.java new file mode 100644 index 0000000..95742cd --- /dev/null +++ b/src/voogasalad/rhondu/IComponent.java @@ -0,0 +1,85 @@ +package voogasalad.rhondu; + +import com.google.common.base.Preconditions; +import javafx.beans.property.SimpleObjectProperty; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Predicate; + + +/** + * This is the interface for all components, which hold data. + * + * @author Rhondu Smithwick, Tom Wu + */ +public interface IComponent { + + /** + * Returns if only one of this component is allowed. + * + * @return whether only one of these components is allowed for an Entity + */ + @Deprecated + default boolean unique() { + return false; + } + + /** + * Gets any properties this component holds. + * + * @return all the properties this coompoennt holds + */ + default List> getProperties() { + return Collections.emptyList(); + } + + /** + * Gets a specific property with specified value class and with specified name. + *

+ * Example call for a position: + * SimpleObjectProperty$Double$ x = position.getProperty(Double, "X"); + * + * @param propertyClass the class of the property's held value + * @param name the name of property + * @param the type of the property's held value + * @return specific property of specified class and with name + * @throws IllegalArgumentException if incorrect propertyClass or no property with this name is present + */ + @SuppressWarnings({"unchecked", "OptionalGetWithoutIsPresent"}) + default SimpleObjectProperty getProperty(Class propertyClass, String name) throws IllegalArgumentException { + Predicate> isRight = (s) -> (Objects.equals(s.getName(), name)); + Optional> rightProperty = getProperties().stream().filter(isRight).findFirst(); + boolean hasProperty = rightProperty.isPresent(); + Preconditions.checkArgument(hasProperty, "No such property with this name is present"); + boolean rightClass = propertyClass.isInstance(rightProperty.get().get()); + Preconditions.checkArgument(rightClass, "Incorrect value class"); + return (SimpleObjectProperty) rightProperty.get(); + } + + /** + * Get a map, where each entry is a component name to it's value class. + * + * @return a map, where each entry is a component name to it's value class + */ + default Map> getPropertyNamesAndClasses() { + Map> nameCLassMap = new HashMap<>(); + for (SimpleObjectProperty property : getProperties()) { + nameCLassMap.put(property.getName(), property.get().getClass()); + } + return nameCLassMap; + } + + /** + * Return the class to put into an entity map. + * + * @return the class to be put into an Entity map + */ + default Class getClassForComponentMap() { + return getClass(); + } +} diff --git a/src/voogasalad/rhondu/SingleProperty.java b/src/voogasalad/rhondu/SingleProperty.java new file mode 100644 index 0000000..ec76aa3 --- /dev/null +++ b/src/voogasalad/rhondu/SingleProperty.java @@ -0,0 +1,45 @@ +package voogasalad.rhondu; + +import javafx.beans.property.SimpleObjectProperty; + +import java.util.Collections; +import java.util.List; + +/** + * Holds one property of type A. + * + * @param the type of the first property + * @author Rhondu Smithwick + */ +public class SingleProperty { + + private final SimpleObjectProperty property1; + + /** + * Constructor. + * + * @param name1 of the property + * @param value1 initial value of the property + */ + public SingleProperty(String name1, A value1) { + property1 = new SimpleObjectProperty<>(this, name1, value1); + } + + /** + * Get first property. + * + * @return the first property + */ + public SimpleObjectProperty property1() { + return property1; + } + + /** + * Get the property as a list. + * + * @return the property as a list + */ + public List> getProperties() { + return Collections.singletonList(property1()); + } +} diff --git a/src/voogasalad/rhondu/TwoProperty.java b/src/voogasalad/rhondu/TwoProperty.java new file mode 100644 index 0000000..25de709 --- /dev/null +++ b/src/voogasalad/rhondu/TwoProperty.java @@ -0,0 +1,49 @@ +package voogasalad.rhondu; + +import javafx.beans.property.SimpleObjectProperty; + +import java.util.Arrays; +import java.util.List; + +/** + * Holds Two Properties and allows access to them. + * + * @param type of first property + * @param type of second property + * @author Rhondu Smithwick + */ +public class TwoProperty extends SingleProperty { + + private final SimpleObjectProperty property2; + + /** + * Constructor. + * + * @param name1 of the first property + * @param value1 starting value of the frist property + * @param name2 of the second property + * @param value2 starting value of the second property + */ + public TwoProperty(String name1, A value1, String name2, B value2) { + super(name1, value1); + property2 = new SimpleObjectProperty<>(this, name2, value2); + } + + /** + * Get the second property. + * + * @return the second property + */ + public SimpleObjectProperty property2() { + return property2; + } + + /** + * Get the properties as a list. + * + * @return the properties as a list + */ + public List> getProperties() { + return Arrays.asList(property1(), property2()); + } +} diff --git a/src/voogasalad/rhondu/Velocity.java b/src/voogasalad/rhondu/Velocity.java new file mode 100644 index 0000000..1b74c6e --- /dev/null +++ b/src/voogasalad/rhondu/Velocity.java @@ -0,0 +1,88 @@ +package voogasalad.rhondu; + +import javafx.beans.property.SimpleObjectProperty; + +import java.util.List; +import java.util.function.DoubleUnaryOperator; + + +/** + * Created by rhondusmithwick on 4/1/16. + * + * @author Rhondu Smithwick + */ +public class Velocity implements IComponent { + + private final TwoProperty twoProperty; + + public Velocity() { + twoProperty = new TwoProperty<>("Speed", 0.0, "Direction", 0.0); + } + + public Velocity(double speed, double direction) { + this(); + setSpeed(speed); + setDirection(direction); + } + + public Velocity(double vx, double vy, boolean flag) { + this(); + setVXY(vx, vy); + } + + public double getSpeed() { + return speedProperty().get(); + } + + public void setSpeed(double speed) { + speedProperty().set(speed); + } + + public SimpleObjectProperty speedProperty() { + return twoProperty.property1(); + } + + public double getDirection() { + return directionProperty().get(); + } + + public void setDirection(double direction) { + directionProperty().set(direction); + } + + public SimpleObjectProperty directionProperty() { + return twoProperty.property2(); + } + + private double getVHelp(DoubleUnaryOperator func) { + double directionRadians = Math.toRadians(getDirection()); + return getSpeed() * func.applyAsDouble(directionRadians); + } + + public double getVX() { + return getVHelp(Math::cos); + } + + public double getVY() { + return getVHelp(Math::sin); + } + + public void setVXY(double vx, double vy) { + setSpeed(Math.sqrt(vx * vx + vy * vy)); + setDirection(Math.toDegrees(Math.atan2(vy, vx))); + } + + public void add(double dvx, double dvy) { + setVXY(getVX() + dvx, getVY() + dvy); + } + + @Override + public String toString() { + return String.format("Velocity: [Speed: %s, Direction: %s]", getSpeed(), getDirection()); + } + + @Override + public List> getProperties() { + return twoProperty.getProperties(); + } +} \ No newline at end of file diff --git a/src/voogasalad/rhondu/VelocityTest.java b/src/voogasalad/rhondu/VelocityTest.java new file mode 100644 index 0000000..26b9b49 --- /dev/null +++ b/src/voogasalad/rhondu/VelocityTest.java @@ -0,0 +1,61 @@ +package voogasalad.rhondu; + +import javafx.beans.property.SimpleObjectProperty; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +/** + * Created by rhondusmithwick on 4/7/16. + * + * @author Rhondu Smithwick + */ +public class VelocityTest { + + @Test + public void testInput() { + double speed = 800; + double direction = 20; + final Velocity velocity = new Velocity(speed, direction); + assertEquals(velocity.getSpeed(), speed, .001); + assertEquals(velocity.getDirection(), direction, .001); + } + + @Test + public void testSet() { + double inSpeed = 800; + double inDirection = 20; + final Velocity velocity = new Velocity(inSpeed, inDirection); + double newSpeed = 100000; + velocity.setSpeed(newSpeed); + assertEquals(velocity.getSpeed(), newSpeed, .001); + double newDirection = 4000; + velocity.setDirection(newDirection); + assertEquals(velocity.getDirection(), newDirection, .001); + } + + @Test + public void testProperties() { + double inSpeed = 800; + double inDirection = 20; + final Velocity velocity = new Velocity(inSpeed, inDirection); + SimpleObjectProperty speed = velocity.getProperty(Double.class, "Speed"); + SimpleObjectProperty speedCheck = velocity.speedProperty(); + assertEquals(speed, speedCheck); + } + + @Test + public void testPropertiesMap() { + Map> testMap = new HashMap<>(); + testMap.put("Speed", Double.class); + testMap.put("Direction", Double.class); + double inSpeed = 800; + double inDirection = 20; + final Velocity velocity = new Velocity(inSpeed, inDirection); + Map> velocityMap = velocity.getPropertyNamesAndClasses(); + assertEquals(testMap, velocityMap); + } +}