Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 1 commit
  • 10 files changed
  • 0 comments
  • 1 contributor
Oct 14, 2012
Commit med fuckup 2b2795c
28  INF3110_Mandatory1/src/Other/Direction.java
@@ -13,17 +13,16 @@
13 13
  */
14 14
 public enum Direction {
15 15
     
16  
-    LEFT("-x", '<'), 
17  
-    RIGHT("x", '>'),
18  
-    UP("y", '^'),
19  
-    DOWN("-y", 'v');
  16
+    LEFT("-x"), 
  17
+    RIGHT("x"),
  18
+    UP("y"),
  19
+    DOWN("-y");
20 20
     
21 21
     private String value;
22 22
     private char charValue;
23 23
     
24  
-    private Direction(String value, char charValue) {
  24
+    private Direction(String value) {
25 25
         this.value = value;
26  
-        this.charValue = charValue;
27 26
     }
28 27
     
29 28
     public static Direction getDirection(String value) {
@@ -36,11 +35,18 @@ public static Direction getDirection(String value) {
36 35
     }
37 36
     
38 37
     public static char getCharValue(Direction d) {
39  
-        for (Direction dir : Direction.values()) {
40  
-            if (dir == d) {
41  
-                return dir.charValue;
42  
-            }
  38
+        switch (d) {
  39
+            case LEFT:
  40
+                return '<';
  41
+            case RIGHT:
  42
+                return '>';
  43
+            case UP:
  44
+                return '^';
  45
+            case DOWN:
  46
+                return 'x';
  47
+            default:
  48
+                throw new RuntimeException("Unknown direction: " + d);
43 49
         }
44  
-        throw new RuntimeException("Unknown direction: " + d);
  50
+        
45 51
     }
46 52
 }
3  INF3110_Mandatory1/src/Other/VarDecl.java
@@ -55,7 +55,8 @@ public int getValue() {
55 55
     }
56 56
 
57 57
     public void setExpression(IExpression exp) {
58  
-        identifier.setEvaluated(true);
  58
+        identifier.setEvaluated(false);
  59
+        exp.interpret();
59 60
         this.exp = exp;
60 61
     }
61 62
     
16  INF3110_Mandatory1/src/expression/BooleanExpression.java
@@ -44,13 +44,13 @@ public void interpret() {
44 44
     
45 45
     @Override
46 46
     public int getValue() {
47  
-        
48  
-        if (isEvaluated()) {
49  
-            return this.value;
50  
-        } else {
51  
-            super.setEvaluated(true);
52  
-            this.interpret();
53  
-            return getValue();
54  
-        }
  47
+        super.setEvaluated(true);
  48
+        this.interpret();
  49
+        return this.value;
55 50
     }  
  51
+    
  52
+    @Override
  53
+    public String toString() {
  54
+        return exp1 + " " + rel +  " " + exp2;
  55
+    }
56 56
 }
2  INF3110_Mandatory1/src/expression/PlusExpression.java
@@ -23,7 +23,7 @@ public PlusExpression(Operator op, IExpression exp1, IExpression exp2) {
23 23
     @Override 
24 24
     public void interpret() {      
25 25
         super.interpret();;
26  
-        
  26
+
27 27
         int value1 = exp1.getValue();
28 28
         int value2 = exp2.getValue();
29 29
 
19  INF3110_Mandatory1/src/inf3110_mandatory1/INF3110_Mandatory1.java
@@ -8,6 +8,7 @@
8 8
 import Other.Operator;
9 9
 import Other.Size;
10 10
 import Other.VarDecl;
  11
+import expression.BooleanExpression;
11 12
 import expression.Identifier;
12 13
 import interfaces.IGrid;
13 14
 import interfaces.IRobot;
@@ -24,6 +25,8 @@
24 25
 import statement.PenUp;
25 26
 import statement.Right;
26 27
 import statement.Start;
  28
+import statement.StatementList;
  29
+import statement.While;
27 30
 
28 31
 /**
29 32
  *
@@ -47,9 +50,10 @@ public static void main(String[] args) {
47 50
         Identifier ii = new Identifier("ii");
48 51
         Identifier jj = new Identifier("jj");
49 52
         Identifier kk = new Identifier("kk");
50  
-        p.addStatement(new Start(new Number(3), new Number(3), Direction.getDirection("-x")));
  53
+        p.addStatement(new Start(new Number(0), new Number(11), Direction.getDirection("y")));
51 54
         p.addStatement(new PenDown());
52  
-        p.addStatement(new Forward(new Number(1)));
  55
+        
  56
+/*        p.addStatement(new Forward(new Number(1)));
53 57
         p.addStatement(new PenUp());
54 58
         p.addStatement(new Right(new Number(2)));
55 59
         p.addStatement(new PenDown());
@@ -68,8 +72,17 @@ public static void main(String[] args) {
68 72
         p.addStatement(new Forward(kk));
69 73
         p.addStatement(new Assignment(jj, new Number(3)));
70 74
         p.addStatement(new Left(kk));
71  
-        p.addStatement(new Forward(new PlusExpression(Operator.getOperator("+"), jj, kk)));
  75
+        p.addStatement(new Forward(new PlusExpression(Operator.getOperator("+"), jj, kk)));*/
  76
+        p.addVarDecl(new VarDecl(jj, new Number(3)));
  77
+        p.addStatement(new Assignment(jj, new Number(11)));
  78
+        p.addStatement(new Right(jj));
  79
+        p.addStatement(new Right(jj));
  80
+//        p.addStatement(new Right(jj));
  81
+        StatementList st = new StatementList();
  82
+        st.addStatement(new Right(jj));
  83
+        st.addStatement(new Assignment(jj, new PlusExpression(Operator.getOperator("-"), jj, new Number(1))));
72 84
 
  85
+//        p.addStatement(new While(new BooleanExpression(Operator.getOperator(">"), jj, new Number(0)), st));
73 86
         p.interpret();
74 87
     }
75 88
 }
4  INF3110_Mandatory1/src/log/Log.java
@@ -10,10 +10,10 @@
10 10
  */
11 11
 public class Log {
12 12
     public static void logln(String msg) {
13  
-        System.out.println(msg);
  13
+    //    System.out.println(msg);
14 14
     }
15 15
     
16 16
     public static void log(String msg) {
17  
-        System.out.print(msg);
  17
+   //     System.out.print(msg);
18 18
     }
19 19
 }
13  INF3110_Mandatory1/src/program/Grid.java
@@ -70,7 +70,10 @@ public String toString() {
70 70
         Arrays.fill(grid, '.');
71 71
 
72 72
         for (IPosition p: this.robot.getPositions()) {
73  
-            int index = (p.getXPosition().getValue() - 1) + (yMax - p.getYPosition().getValue()) * xMax;
  73
+            int index = p.getXPosition().getValue() + (yMax - p.getYPosition().getValue() - 1) * xMax;
  74
+            System.out.println(index + " " + p);
  75
+//            System.out.println(p + " " + index);
  76
+//            System.out.println(p.getXPosition().getValue() + " " + yMax + " " + p.getYPosition().getValue());
74 77
             grid[index] = Direction.getCharValue(p.getDirection());
75 78
         }
76 79
         
@@ -100,13 +103,13 @@ public String toString() {
100 103
     public boolean legalMove(IPosition position) {
101 104
         switch (position.getDirection()) {
102 105
             case LEFT:
103  
-                return 0 < position.getXPosition().getValue();
  106
+                return 0 <= position.getXPosition().getValue();
104 107
             case RIGHT:
105  
-                return position.getXPosition().getValue() <= size.getXBounds().getValue();
  108
+                return position.getXPosition().getValue() < size.getXBounds().getValue();
106 109
             case UP:
107  
-                return position.getYPosition().getValue() <= size.getYBounds().getValue();
  110
+                return position.getYPosition().getValue() < size.getYBounds().getValue();
108 111
             case DOWN:
109  
-                return 0 < position.getYPosition().getValue();
  112
+                return 0 <= position.getYPosition().getValue();
110 113
             default:
111 114
                 throw new RuntimeException("Unknown direction: " + position.getDirection());
112 115
         }
9  INF3110_Mandatory1/src/program/Robot.java
@@ -53,8 +53,6 @@ public void moveBackward(Number number) {
53 53
     public void moveForward(Number number) {
54 54
 
55 55
         Log.log("Robot: Moving from (" + this.position + ") ");
56  
-        // Draw current position if penDown
57  
-        setPosition(this.position);
58 56
         IPositionActions position;
59 57
         for (int i = 1; i <= number.getValue(); ++i) {
60 58
             
@@ -78,10 +76,14 @@ public void moveForward(Number number) {
78 76
                     throw new RuntimeException("Unknown direction: " + this.position.getDirection());
79 77
             }
80 78
             
81  
-            position = Position.updatePosition((IPosition)this.position, x, y);
  79
+            position = new Position(new Number(this.position.getXPosition().getValue() + x), 
  80
+                    new Number(this.position.getYPosition().getValue() + y), 
  81
+                    Direction.getDirection("x"));//         ))Position.updatePosition((IPosition)this.position, x, y);
  82
+            System.out.println("Log this: " + position);
82 83
             if (grid.legalMove(position)) {
83 84
                 this.setPosition(position);
84 85
             } else {
  86
+                System.out.println("hei hoi");
85 87
                 break;
86 88
             }
87 89
             
@@ -93,6 +95,7 @@ public void moveForward(Number number) {
93 95
     private void setPosition(IPositionActions position) {
94 96
         this.position = position;
95 97
         if (penDown) {
  98
+            System.out.println("Adding to positions: " + position);
96 99
             this.positions.add(position);
97 100
         }
98 101
     }
61  INF3110_Mandatory1/src/statement/StatementList.java
... ...
@@ -0,0 +1,61 @@
  1
+/*
  2
+ * To change this template, choose Tools | Templates
  3
+ * and open the template in the editor.
  4
+ */
  5
+package statement;
  6
+
  7
+import interfaces.Handler;
  8
+import interfaces.IStatement;
  9
+import java.util.Collection;
  10
+import java.util.Iterator;
  11
+import java.util.LinkedList;
  12
+
  13
+/**
  14
+ *
  15
+ * @author haavakno
  16
+ */
  17
+public class StatementList implements Handler, Iterable<IStatement> {
  18
+    private final Collection<IStatement> statementList = new LinkedList<IStatement>();
  19
+    private boolean printed = false;
  20
+    
  21
+    public void addStatement(IStatement statement) {
  22
+        this.statementList.add(statement);
  23
+    }
  24
+    
  25
+    @Override
  26
+    public void interpret() {
  27
+        prettyPrint();
  28
+        for (IStatement statement : statementList) {
  29
+            statement.interpret();
  30
+        }
  31
+    }
  32
+
  33
+    @Override
  34
+    public void prettyPrint() {
  35
+        if (!printed) {
  36
+            setPrinted();
  37
+            System.out.println(this);
  38
+        }
  39
+    }
  40
+
  41
+    @Override
  42
+    public void setPrinted() {
  43
+        this.printed = true;
  44
+    }
  45
+    
  46
+    @Override
  47
+    public String toString() {
  48
+        StringBuilder sb = new StringBuilder();
  49
+        for (IStatement statement : statementList) {
  50
+            statement.setPrinted();
  51
+            sb.append(statement);
  52
+            sb.append("\n");
  53
+        }
  54
+        return sb.toString();
  55
+    }
  56
+
  57
+    @Override
  58
+    public Iterator iterator() {
  59
+        return statementList.iterator();
  60
+    }
  61
+}
27  INF3110_Mandatory1/src/statement/While.java
@@ -4,9 +4,34 @@
4 4
  */
5 5
 package statement;
6 6
 
  7
+import expression.BooleanExpression;
  8
+import interfaces.IStatement;
  9
+
7 10
 
8 11
 public class While extends Statement {
9 12
 
10  
-    //private BooleanExpression boolExp;
  13
+    private BooleanExpression boolExp;
  14
+    private StatementList statementList;
  15
+    
  16
+    public While(BooleanExpression boolExp, StatementList statementList) {
  17
+        this.boolExp = boolExp;
  18
+        this.statementList = statementList;
  19
+    }
  20
+    
  21
+    @Override 
  22
+    public void interpret() {
  23
+        super.interpret();
  24
+        
  25
+        while (boolExp.getValue() == 1) {
  26
+            for (IStatement statement : statementList) {
  27
+                statement.interpret();
  28
+            }
  29
+        }
  30
+    }
  31
+    
  32
+    @Override
  33
+    public String toString() {
  34
+        return "while(" + boolExp + ") {\n" + statementList + "\n}";
  35
+    }
11 36
     
12 37
 }

No commit comments for this range

Something went wrong with that request. Please try again.