diff options
author | Camil Staps | 2015-02-26 17:45:59 +0100 |
---|---|---|
committer | Camil Staps | 2015-02-26 17:45:59 +0100 |
commit | 4c555281988424a83b004c06a3f85e60d1c9fc6e (patch) | |
tree | feefe8cba9ae8ebdc3ae063ea7757a06d1cd4e18 | |
parent | x (diff) |
Finished week 4
-rw-r--r-- | Week4/src/oo15loipe/AsciiArt.java | 50 | ||||
-rw-r--r-- | Week4/src/oo15loipe/Fragment.java | 9 | ||||
-rw-r--r-- | Week4/src/oo15loipe/InfoLoipe.java | 30 | ||||
-rw-r--r-- | Week4/src/oo15loipe/Loipe.java | 63 | ||||
-rw-r--r-- | Week4/src/oo15loipe/TekenLoipe.java | 18 | ||||
-rw-r--r-- | Week4/src/week4/Week4.java | 29 |
6 files changed, 129 insertions, 70 deletions
diff --git a/Week4/src/oo15loipe/AsciiArt.java b/Week4/src/oo15loipe/AsciiArt.java index 3ee3367..4072d54 100644 --- a/Week4/src/oo15loipe/AsciiArt.java +++ b/Week4/src/oo15loipe/AsciiArt.java @@ -1,18 +1,19 @@ -/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
package oo15loipe;
/**
- * a class that draws the loipe as ascii-art.
- * @author theijligenberg
+ * A class that draws the loipe as ascii-art.
+ *
+ * @author Thijs Heijligenberg, s4451414, Camil Staps, s4498062
*/
public class AsciiArt implements TekenLoipe{
private InfoLoipe L;
private Punt man;
+ /**
+ * Create the instance based on a loipe
+ *
+ * @param s
+ */
public AsciiArt(InfoLoipe s){
this.L = s;
this.man = new Punt(null);
@@ -22,30 +23,29 @@ public class AsciiArt implements TekenLoipe{ public void setPosition(Punt p){
this.man = p;
}
- /**
- * draws the loipe by checking each fragment at (i,j) for its direction
- */
+
@Override
public void teken(){
- for(int i = 0; i < L.getWidth(); i++){
- for(int j = 0; j < L.getHeight(); j++){
+ for(int j = 0; j < L.getHeight(); j++){
+ for(int i = 0; i < L.getWidth(); i++){
+ Fragment f = L.getFragment(i,j);
if(i == man.getX() && j == man.getY()){
System.out.print('*');
- break;
- }
- Fragment f = L.getFragment(i,j);
- switch (f){
- case KR: System.out.print('+'); break;
- case NZ: System.out.print('|'); break;
- case OW: System.out.print('-'); break;
- case NO: System.out.print('`'); break;
- case NW: System.out.print(','); break;
- case ZO: System.out.print('|'); break;
- case ZW: System.out.print('\''); break;
- default: System.out.print(' ');
+ } else if (f == null) {
+ System.out.print(' ');
+ } else {
+ switch (f){
+ case KR: System.out.print('+'); break;
+ case NZ: System.out.print('|'); break;
+ case OW: System.out.print('-'); break;
+ case NO: System.out.print('`'); break;
+ case NW: System.out.print(','); break;
+ case ZO: System.out.print(','); break;
+ case ZW: System.out.print('.'); break;
+ }
}
}
- System.out.print('\n');
+ System.out.print("\n");
}
}
}
diff --git a/Week4/src/oo15loipe/Fragment.java b/Week4/src/oo15loipe/Fragment.java index ba488fe..a4668da 100644 --- a/Week4/src/oo15loipe/Fragment.java +++ b/Week4/src/oo15loipe/Fragment.java @@ -1,11 +1,8 @@ -/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
package oo15loipe;
+
/**
* enum to describe a fragment of path by its direction (or as a crossing)
- * @author theijligenberg
+ *
+ * @author Thijs Heijligenberg, s4451414, Camil Staps, s4498062
*/
public enum Fragment {NZ, OW, NO,NW, ZO, ZW, KR};
diff --git a/Week4/src/oo15loipe/InfoLoipe.java b/Week4/src/oo15loipe/InfoLoipe.java index 1796418..a1cf182 100644 --- a/Week4/src/oo15loipe/InfoLoipe.java +++ b/Week4/src/oo15loipe/InfoLoipe.java @@ -1,19 +1,35 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package oo15loipe; /** - * - * @author cstaps + * Interface for a class describing a Loipe + * + * @author Camil Staps, s4498062, Thijs Heijligenberg, s4451414 */ public interface InfoLoipe { + public int getWidth( ) ; // grootte in oostwest r i cht ing public int getHeight( ) ; // grootte in noordzuid r i cht ing + + /** + * Get the fragment at position (x, y) + * + * @param x + * @param y + * @return + */ public Fragment getFragment( int x, int y) ; // fragment van de loipe op po s i t i e (x , y) + + /** + * Get the first position of the loipe + * + * @return + */ public Punt start( ) ; // Het startpunt op de kaart + + /** + * Get the next position in the loipe + * @return + */ public Punt stap( ) ; // het volgende punt op de route } diff --git a/Week4/src/oo15loipe/Loipe.java b/Week4/src/oo15loipe/Loipe.java index 308ac8b..ff034c0 100644 --- a/Week4/src/oo15loipe/Loipe.java +++ b/Week4/src/oo15loipe/Loipe.java @@ -1,27 +1,42 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package oo15loipe; /** + * Loipe class + * * @author Camil Staps, s4498062 // Thijs Heijligenberg, s4451414 */ public class Loipe implements InfoLoipe { - private final static int DIRECTION_EAST = 0, - DIRECTION_SOUTH = 1, - DIRECTION_WEST = 2, - DIRECTION_NORTH = 3; + /** + * Class constants for direction (for readability) + */ + private final static int + DIRECTION_EAST = 0, + DIRECTION_SOUTH = 1, + DIRECTION_WEST = 2, + DIRECTION_NORTH = 3; + /** + * The height and width of the loipe + */ private int height = 0, width = 0; + /** + * Start point + * The path in coordinates + * The current position + * The map as 2D array of Fragments + */ private Punt start; private Punt[] path; private int path_pointer = 0; private Fragment[][] loipe; + /** + * Construct the loipe based on a path + * + * @param pad + */ public Loipe (String pad) { // First calculate width and height @@ -74,20 +89,20 @@ public class Loipe implements InfoLoipe { System.out.println(maxY); System.out.println(minY); - height = maxY - minY; - width = maxX - minX; + height = maxY - minY + 1; + width = maxX - minX + 1; start = new Punt(-minX, -minY); // Now fill the Loipe - loipe = new Fragment[width][height]; + loipe = new Fragment[width + 1][height + 1]; path = new Punt[pad.length()]; - current_point = new Punt(-minX, -minY); + current_point = start; direction = DIRECTION_NORTH; for (int i = 0; i < pad.length(); i++) { path[i] = current_point; - if (loipe[current_point.getX()][current_point.getY()] == null) { + if (loipe[current_point.getX()][current_point.getY()] != null) { loipe[current_point.getX()][current_point.getY()] = Fragment.KR; } else { switch (pad.charAt(i)) { @@ -106,6 +121,17 @@ public class Loipe implements InfoLoipe { case DIRECTION_WEST: loipe[current_point.getX()][current_point.getY()] = Fragment.ZO; break; case DIRECTION_NORTH: loipe[current_point.getX()][current_point.getY()] = Fragment.ZW; break; } + break; + case 's': + switch (direction) { + case DIRECTION_EAST: + case DIRECTION_WEST: + loipe[current_point.getX()][current_point.getY()] = Fragment.OW; + break; + case DIRECTION_SOUTH: + case DIRECTION_NORTH: + loipe[current_point.getX()][current_point.getY()] = Fragment.NZ; + } } } @@ -159,7 +185,7 @@ public class Loipe implements InfoLoipe { public Fragment getFragment(int x, int y) { return loipe[x][y]; } - + @Override public Punt start() { return start; @@ -167,7 +193,12 @@ public class Loipe implements InfoLoipe { @Override public Punt stap() { - return path[path_pointer++]; + if (path_pointer >= path.length) { + path_pointer = 0; + } + Punt result = path[path_pointer]; + path_pointer++; + return result; } } diff --git a/Week4/src/oo15loipe/TekenLoipe.java b/Week4/src/oo15loipe/TekenLoipe.java index de9e742..8c9386f 100644 --- a/Week4/src/oo15loipe/TekenLoipe.java +++ b/Week4/src/oo15loipe/TekenLoipe.java @@ -1,15 +1,19 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package oo15loipe; /** - * - * @author cstaps + * Interface for a class that can show a loipe on the screen + * + * @author Thijs Heijligenberg s4451414, Camil Staps, s4498062 */ public interface TekenLoipe { + /** + * Draw the loipe + */ public void teken() ; // teken de gegeven loipe + + /** + * Set the current position of the skier + * @param p + */ public void setPosition(Punt p) ; // teken de spor ter op de gegeven po s i t i e } diff --git a/Week4/src/week4/Week4.java b/Week4/src/week4/Week4.java index 039eddb..7bad2ce 100644 --- a/Week4/src/week4/Week4.java +++ b/Week4/src/week4/Week4.java @@ -1,25 +1,36 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package week4; +import java.util.Scanner; +import oo15loipe.AsciiArt; import oo15loipe.Loipe; +import oo15loipe.LoipePlaatje; /** - * - * @author cstaps + * Solutions for assignment 4 + * + * @author Thijs Heijligenberg, s4451414, Camil Staps, s4498062 */ public class Week4 { /** - * @param args the command line arguments + * Create a Loipe, draw it using LoipePlaatje and AsciiArt + * + * @param args the command line arguments (are ignored) */ public static void main(String[] args) { - Loipe l = new Loipe("ssrrsllrs"); + Scanner sc = new Scanner(System.in); + + Loipe l = new Loipe("srssrsslsllsss"); System.out.println(l.getWidth() + " x " + l.getHeight()); System.out.println(l); + LoipePlaatje lp = new LoipePlaatje(l); + lp.teken(); + AsciiArt aa = new AsciiArt(l); + do { + aa.setPosition(l.stap()); + aa.teken(); + } while (sc.nextLine() != null); + } } |