Skip to content

Commit

Permalink
Starting code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredlwong committed Sep 3, 2013
0 parents commit bb1dc77
Show file tree
Hide file tree
Showing 13 changed files with 716 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
bin
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ps0</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
53 changes: 53 additions & 0 deletions src/rules/RulesOf6005.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package rules;

/**
* RulesOf6005 represents collaboration policy of 6.005 as described by the
* general information on Stellar.
*
* the Policy is described by the mayUseCodeInAssignment method.
*
*/
public class RulesOf6005 {

/**
* Judge whether a given piece of code may be used in an assignment (problem
* set or team project) or not, according to the 6.005 collaboration policy.
*
* @param writtenByYourself true if the code in question was written by
* yourself or, in the case of a team project, your teammates,
* otherwise false.
* @param availableToOthers if not writtenByYourself, whether or not the
* code in question is available to all other students in the class.
* Otherwise ignored.
* @param writtenAsCourseWork if not writtenByYourself, whether or not the
* code in question was written specifically as part of a solution to
* a 6.005 assignment, in the current or past semesters. Otherwise
* ignored.
* @param citingYourSource if not writtenByYourself, whether or not you
* properly cite your source. Otherwise ignored.
* @param implementationRequired whether the assignment specifically asks
* you to implement the feature in question.
* @return Whether or not, based on the information provided in the
* arguments, you are likely to be allowed to use the code in
* question in your assignment, according to the 6.005 collaboration
* policy for the current semester.
*/
public static boolean mayUseCodeInAssignment(boolean writtenByYourself,
boolean availableToOthers, boolean writtenAsCourseWork,
boolean citingYourSource, boolean implementationRequired) {
// TODO: Fill in this method, then remove the exception
throw new UnsupportedOperationException();
}

/**
* Main method of the class.
*
* Runs the mayUseCodeInAssignment method.
* @param args
*/
public static void main(String[] args){
System.out.println("You may certainly use code you wrote yourself: " +
RulesOf6005.mayUseCodeInAssignment(true, false, true, true, true));
}
}

18 changes: 18 additions & 0 deletions src/rules/RulesOf6005Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package rules;

import static org.junit.Assert.*;
import org.junit.Test;

/**
* JUnit tests for RulesOf6005.
*/
public class RulesOf6005Test {
/**
* Tests the mayUseCodeInAssignment method.
*/
@Test
public void testMayUseCodeInAssignment() {
assertEquals(false, RulesOf6005.mayUseCodeInAssignment(false, true, false, false, false));
assertEquals(true, RulesOf6005.mayUseCodeInAssignment(true, false, true, true, true));
}
}
33 changes: 33 additions & 0 deletions src/turtle/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package turtle;

/**
* Command list entries
*/
enum ActionType {
FORWARD, TURN
}

public class Action {
ActionType type;
int intParam;
double doubleParam;
String displayString;
LineSegment lineSeg;

public Action(ActionType type, int intParam, double doubleParam,
String displayString, LineSegment lineSeg) {
this.type = type;
this.intParam = intParam;
this.doubleParam = doubleParam;
this.displayString = displayString;
this.lineSeg = lineSeg;
}

public String toString() {
if (displayString == null) {
return "";
} else {
return displayString;
}
}
}
71 changes: 71 additions & 0 deletions src/turtle/DrawableTurtle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package turtle;

import java.util.ArrayList;
import java.util.List;
import java.lang.Math;

import javax.swing.SwingUtilities;

/**
* Turtle for drawing.
*/
public class DrawableTurtle implements Turtle {

List<Action> actionList;
List<LineSegment> lines;

Point currentPosition;
double currentHeading;

private static final int canvasWidth = 512;
private static final int canvasHeight = 512;

public DrawableTurtle() {
this.currentPosition = new Point(0, 0);
this.currentHeading = 0.0;
this.lines = new ArrayList<LineSegment>();
this.actionList = new ArrayList<Action>();
}

/**
* Command to send the turtle forward a number of units.
*
* @param units number of pixels to go in currentHeading's direction; must be positive.
*/
public void forward(int units) {
double newX = this.currentPosition.x + Math.cos(Math.toRadians(90.0 - currentHeading)) * (double)units;
double newY = this.currentPosition.y + Math.sin(Math.toRadians(90.0 - currentHeading)) * (double)units;

LineSegment lineSeg = new LineSegment(this.currentPosition.x, this.currentPosition.y, newX, newY);
this.lines.add(lineSeg);
this.currentPosition = new Point(newX, newY);

this.actionList.add(new Action(ActionType.FORWARD, units, 0.0, "forward " + units + " units", lineSeg));
}

/**
* Change the heading by some degrees clockwise.
*
* @param degrees amount of change in angle, in degrees, with positive being clockwise.
*/
public void turn(double degrees) {
degrees = (degrees % 360 + 360) % 360;
this.currentHeading += degrees;
if (this.currentHeading >= 360.0)
this.currentHeading -= 360.0;
this.actionList.add(new Action(ActionType.TURN, 0, degrees, "turn " + degrees + " degrees", null));
}

/**
* Draw to the screen.
*/
public void draw() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
(new TurtleGUI(actionList, canvasWidth, canvasHeight)).setVisible(true);
}
});
return;
}

}
48 changes: 48 additions & 0 deletions src/turtle/LineSegment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package turtle;

/**
* Class for a line segment in pixel space.
*/
public class LineSegment {
public final Point start;
public final Point end;

/*
* Constructor that takes in 4 integers for the coordinates.
*
* @param startx x-coordinate of start point
*
* @param starty y-coordinate of start point
*
* @param endx x-coordinate of end point
*
* @param endy y-coordinate of end point
*/
public LineSegment(double startx, double starty, double endx, double endy) {
this.start = new Point(startx, starty);
this.end = new Point(endx, endy);
}

/*
* Constructor that takes in the start point and end point.
*
* @param start one end of the line segment
*
* @param end the other end of the line segment
*/
public LineSegment(Point start, Point end) {
this.start = start;
this.end = end;
}

/*
* Calculate the length of this segment.
*
* @return the length of the line segment
*/
public double length() {
return Math.sqrt(Math.pow(this.start.x - this.end.x, 2.0)
+ Math.pow(this.start.y - this.end.y, 2.0));
}

}
14 changes: 14 additions & 0 deletions src/turtle/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package turtle;

/**
* Class for a point in floating-point pixel space.
*/
public class Point {
public final double x;
public final double y;

public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
21 changes: 21 additions & 0 deletions src/turtle/Turtle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package turtle;

/**
* Turtle interface
*
* Defines the interface shared for FakeTurtle (for testing) and the
* real turtle (for displaying things on screen). Note that the
* standard directions/rotations use 'logo' semantics: initial heading
* of zero is 'up', and positive angles rotate the turtle clockwise.
*
* We implement: forward, turn right, and draw
*/
public interface Turtle {

public void forward(int units);

public void turn(double angle);

public void draw();

}
Loading

0 comments on commit bb1dc77

Please sign in to comment.