Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: haavakno/INF3110_Mandatory1
base: f356369ccc
...
head fork: haavakno/INF3110_Mandatory1
compare: 2b2795c24b
  • 1 commit
  • 10 files changed
  • 0 commit comments
  • 1 contributor
Commits on Oct 14, 2012
haavakno Commit med fuckup 2b2795c
28 INF3110_Mandatory1/src/Other/Direction.java
View
@@ -13,17 +13,16 @@
*/
public enum Direction {
- LEFT("-x", '<'),
- RIGHT("x", '>'),
- UP("y", '^'),
- DOWN("-y", 'v');
+ LEFT("-x"),
+ RIGHT("x"),
+ UP("y"),
+ DOWN("-y");
private String value;
private char charValue;
- private Direction(String value, char charValue) {
+ private Direction(String value) {
this.value = value;
- this.charValue = charValue;
}
public static Direction getDirection(String value) {
@@ -36,11 +35,18 @@ public static Direction getDirection(String value) {
}
public static char getCharValue(Direction d) {
- for (Direction dir : Direction.values()) {
- if (dir == d) {
- return dir.charValue;
- }
+ switch (d) {
+ case LEFT:
+ return '<';
+ case RIGHT:
+ return '>';
+ case UP:
+ return '^';
+ case DOWN:
+ return 'x';
+ default:
+ throw new RuntimeException("Unknown direction: " + d);
}
- throw new RuntimeException("Unknown direction: " + d);
+
}
}
3  INF3110_Mandatory1/src/Other/VarDecl.java
View
@@ -55,7 +55,8 @@ public int getValue() {
}
public void setExpression(IExpression exp) {
- identifier.setEvaluated(true);
+ identifier.setEvaluated(false);
+ exp.interpret();
this.exp = exp;
}
16 INF3110_Mandatory1/src/expression/BooleanExpression.java
View
@@ -44,13 +44,13 @@ public void interpret() {
@Override
public int getValue() {
-
- if (isEvaluated()) {
- return this.value;
- } else {
- super.setEvaluated(true);
- this.interpret();
- return getValue();
- }
+ super.setEvaluated(true);
+ this.interpret();
+ return this.value;
}
+
+ @Override
+ public String toString() {
+ return exp1 + " " + rel + " " + exp2;
+ }
}
2  INF3110_Mandatory1/src/expression/PlusExpression.java
View
@@ -23,7 +23,7 @@ public PlusExpression(Operator op, IExpression exp1, IExpression exp2) {
@Override
public void interpret() {
super.interpret();;
-
+
int value1 = exp1.getValue();
int value2 = exp2.getValue();
19 INF3110_Mandatory1/src/inf3110_mandatory1/INF3110_Mandatory1.java
View
@@ -8,6 +8,7 @@
import Other.Operator;
import Other.Size;
import Other.VarDecl;
+import expression.BooleanExpression;
import expression.Identifier;
import interfaces.IGrid;
import interfaces.IRobot;
@@ -24,6 +25,8 @@
import statement.PenUp;
import statement.Right;
import statement.Start;
+import statement.StatementList;
+import statement.While;
/**
*
@@ -47,9 +50,10 @@ public static void main(String[] args) {
Identifier ii = new Identifier("ii");
Identifier jj = new Identifier("jj");
Identifier kk = new Identifier("kk");
- p.addStatement(new Start(new Number(3), new Number(3), Direction.getDirection("-x")));
+ p.addStatement(new Start(new Number(0), new Number(11), Direction.getDirection("y")));
p.addStatement(new PenDown());
- p.addStatement(new Forward(new Number(1)));
+
+/* p.addStatement(new Forward(new Number(1)));
p.addStatement(new PenUp());
p.addStatement(new Right(new Number(2)));
p.addStatement(new PenDown());
@@ -68,8 +72,17 @@ public static void main(String[] args) {
p.addStatement(new Forward(kk));
p.addStatement(new Assignment(jj, new Number(3)));
p.addStatement(new Left(kk));
- p.addStatement(new Forward(new PlusExpression(Operator.getOperator("+"), jj, kk)));
+ p.addStatement(new Forward(new PlusExpression(Operator.getOperator("+"), jj, kk)));*/
+ p.addVarDecl(new VarDecl(jj, new Number(3)));
+ p.addStatement(new Assignment(jj, new Number(11)));
+ p.addStatement(new Right(jj));
+ p.addStatement(new Right(jj));
+// p.addStatement(new Right(jj));
+ StatementList st = new StatementList();
+ st.addStatement(new Right(jj));
+ st.addStatement(new Assignment(jj, new PlusExpression(Operator.getOperator("-"), jj, new Number(1))));
+// p.addStatement(new While(new BooleanExpression(Operator.getOperator(">"), jj, new Number(0)), st));
p.interpret();
}
}
4 INF3110_Mandatory1/src/log/Log.java
View
@@ -10,10 +10,10 @@
*/
public class Log {
public static void logln(String msg) {
- System.out.println(msg);
+ // System.out.println(msg);
}
public static void log(String msg) {
- System.out.print(msg);
+ // System.out.print(msg);
}
}
13 INF3110_Mandatory1/src/program/Grid.java
View
@@ -70,7 +70,10 @@ public String toString() {
Arrays.fill(grid, '.');
for (IPosition p: this.robot.getPositions()) {
- int index = (p.getXPosition().getValue() - 1) + (yMax - p.getYPosition().getValue()) * xMax;
+ int index = p.getXPosition().getValue() + (yMax - p.getYPosition().getValue() - 1) * xMax;
+ System.out.println(index + " " + p);
+// System.out.println(p + " " + index);
+// System.out.println(p.getXPosition().getValue() + " " + yMax + " " + p.getYPosition().getValue());
grid[index] = Direction.getCharValue(p.getDirection());
}
@@ -100,13 +103,13 @@ public String toString() {
public boolean legalMove(IPosition position) {
switch (position.getDirection()) {
case LEFT:
- return 0 < position.getXPosition().getValue();
+ return 0 <= position.getXPosition().getValue();
case RIGHT:
- return position.getXPosition().getValue() <= size.getXBounds().getValue();
+ return position.getXPosition().getValue() < size.getXBounds().getValue();
case UP:
- return position.getYPosition().getValue() <= size.getYBounds().getValue();
+ return position.getYPosition().getValue() < size.getYBounds().getValue();
case DOWN:
- return 0 < position.getYPosition().getValue();
+ return 0 <= position.getYPosition().getValue();
default:
throw new RuntimeException("Unknown direction: " + position.getDirection());
}
9 INF3110_Mandatory1/src/program/Robot.java
View
@@ -53,8 +53,6 @@ public void moveBackward(Number number) {
public void moveForward(Number number) {
Log.log("Robot: Moving from (" + this.position + ") ");
- // Draw current position if penDown
- setPosition(this.position);
IPositionActions position;
for (int i = 1; i <= number.getValue(); ++i) {
@@ -78,10 +76,14 @@ public void moveForward(Number number) {
throw new RuntimeException("Unknown direction: " + this.position.getDirection());
}
- position = Position.updatePosition((IPosition)this.position, x, y);
+ position = new Position(new Number(this.position.getXPosition().getValue() + x),
+ new Number(this.position.getYPosition().getValue() + y),
+ Direction.getDirection("x"));// ))Position.updatePosition((IPosition)this.position, x, y);
+ System.out.println("Log this: " + position);
if (grid.legalMove(position)) {
this.setPosition(position);
} else {
+ System.out.println("hei hoi");
break;
}
@@ -93,6 +95,7 @@ public void moveForward(Number number) {
private void setPosition(IPositionActions position) {
this.position = position;
if (penDown) {
+ System.out.println("Adding to positions: " + position);
this.positions.add(position);
}
}
61 INF3110_Mandatory1/src/statement/StatementList.java
View
@@ -0,0 +1,61 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package statement;
+
+import interfaces.Handler;
+import interfaces.IStatement;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+
+/**
+ *
+ * @author haavakno
+ */
+public class StatementList implements Handler, Iterable<IStatement> {
+ private final Collection<IStatement> statementList = new LinkedList<IStatement>();
+ private boolean printed = false;
+
+ public void addStatement(IStatement statement) {
+ this.statementList.add(statement);
+ }
+
+ @Override
+ public void interpret() {
+ prettyPrint();
+ for (IStatement statement : statementList) {
+ statement.interpret();
+ }
+ }
+
+ @Override
+ public void prettyPrint() {
+ if (!printed) {
+ setPrinted();
+ System.out.println(this);
+ }
+ }
+
+ @Override
+ public void setPrinted() {
+ this.printed = true;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ for (IStatement statement : statementList) {
+ statement.setPrinted();
+ sb.append(statement);
+ sb.append("\n");
+ }
+ return sb.toString();
+ }
+
+ @Override
+ public Iterator iterator() {
+ return statementList.iterator();
+ }
+}
27 INF3110_Mandatory1/src/statement/While.java
View
@@ -4,9 +4,34 @@
*/
package statement;
+import expression.BooleanExpression;
+import interfaces.IStatement;
+
public class While extends Statement {
- //private BooleanExpression boolExp;
+ private BooleanExpression boolExp;
+ private StatementList statementList;
+
+ public While(BooleanExpression boolExp, StatementList statementList) {
+ this.boolExp = boolExp;
+ this.statementList = statementList;
+ }
+
+ @Override
+ public void interpret() {
+ super.interpret();
+
+ while (boolExp.getValue() == 1) {
+ for (IStatement statement : statementList) {
+ statement.interpret();
+ }
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "while(" + boolExp + ") {\n" + statementList + "\n}";
+ }
}

No commit comments for this range

Something went wrong with that request. Please try again.