Advertisement
Guest User

Tile

a guest
Mar 30th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.12 KB | None | 0 0
  1. package jpp.infinityloop.basics;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class Tile {
  6.  
  7.     private TileType tileType;
  8.     private Orientation [] orientation;
  9.     private CoordinatesOfTile coord;
  10.     private final Orientation [] initOrientation;
  11.  
  12.     public Tile(TileType tileType, Orientation [] orientation){
  13.         this.tileType = tileType;
  14.         this.orientation = orientation;
  15.         this.initOrientation = orientation;
  16.     }
  17.    
  18.     public Tile(CoordinatesOfTile coord, TileType tileType, Orientation orientation){
  19.         this.tileType = tileType;
  20.         this.orientation = new Orientation[1];
  21.         this.orientation[0] = orientation;
  22.         this.coord = coord;
  23.         this.initOrientation = new Orientation[1];
  24.         this.initOrientation[0] = orientation;
  25.     }
  26.  
  27.     public Tile(CoordinatesOfTile coord,TileType tileType, Orientation [] orientation){
  28.         this.tileType = tileType;
  29.         this.orientation = orientation;
  30.         this.coord = coord;
  31.         this.initOrientation = orientation;
  32.     }
  33.  
  34.     public Tile getRotatedTile(){
  35.         switch(this.tileType){
  36.         case DEAD_END:
  37.             return new Tile(this.tileType,rotateDeadEnd());
  38.         case BEND:
  39.             return new Tile(this.tileType,rotateBend());
  40.         case TEE:
  41.             return new Tile(this.tileType,rotateTee());
  42.         case STRAIGHT:
  43.             return new Tile(this.tileType,rotateStraight());
  44.         default:
  45.             return this;
  46.         }
  47.     }
  48.  
  49.     public TileType getType(){
  50.         return this.tileType;
  51.     }
  52.  
  53.     public Orientation [] getInitOrientation(){
  54.         return this.initOrientation;
  55.     }
  56.    
  57.     public boolean hasConnectionTo(Orientation neighbor){
  58.         Orientation counter = neighbor.findCounterpart();
  59.         switch(this.tileType){
  60.         case DEAD_END:
  61.             for(int i = 0; i < orientation.length; i++){
  62.                 if(orientation[i] == counter){
  63.                     return true;
  64.                 }
  65.             }
  66.             return false;
  67.         case BEND:
  68.             switch(orientation[0]){
  69.             case RIGHT:            
  70.                 if(counter == Orientation.RIGHT || counter == Orientation.DOWN){
  71.                     return true;
  72.                 }else
  73.                     return false;
  74.             case DOWN:
  75.                 if(counter == Orientation.LEFT || counter == Orientation.DOWN){
  76.                     return true;
  77.                 }else
  78.                     return false;
  79.             case UP:               
  80.                 if(counter == Orientation.RIGHT || counter == Orientation.UP){
  81.                     return true;
  82.                 }else
  83.                     return false;
  84.             default:
  85.                 if(counter == Orientation.LEFT || counter == Orientation.UP){
  86.                     return true;
  87.                 }else
  88.                     return false;
  89.             }
  90.         case TEE:
  91.             switch(orientation[0]){
  92.             case RIGHT:
  93.                 if(counter != Orientation.UP) return true;
  94.                 else return false;
  95.             case UP:
  96.                 if(counter != Orientation.LEFT) return true;
  97.                 else return false;
  98.             case LEFT:
  99.                 if(counter != Orientation.DOWN) return true;
  100.                 else return false;
  101.             default:
  102.                 if(counter != Orientation.RIGHT) return true;
  103.                 else return false;
  104.             }
  105.         case STRAIGHT:
  106.             for(int i = 0; i < orientation.length; i++){
  107.                 if(orientation[i] == counter){
  108.                     return true;
  109.                 }
  110.             }
  111.             return false;
  112.         case CROSS:
  113.             return true;
  114.         default:
  115.             return false;
  116.         }
  117.     }
  118.  
  119.     public void reset(){
  120.         if(!this.orientation.equals(this.initOrientation)){
  121.             this.orientation = initOrientation;
  122.         }
  123.     }
  124.  
  125.     public Orientation []  getOrientation(){
  126.         return this.orientation;
  127.     }
  128.  
  129.     public void setCoord(CoordinatesOfTile newCoord){
  130.         this.coord = newCoord;
  131.     }
  132.  
  133.     public CoordinatesOfTile getCoord(){
  134.         return this.coord;
  135.     }
  136.  
  137.     public boolean sameOrientation(Orientation [] compareOre){
  138.         if(this.orientation.length != compareOre.length){
  139.             return false;
  140.         }else{
  141.             ArrayList<Orientation> compareList = new ArrayList<Orientation>();
  142.             for(int i = 0;i<= compareOre.length-1;i++){
  143.                 compareList.add(compareOre[i]);
  144.             }
  145.             for(int i = 0;i<= this.orientation.length-1;i++){
  146.                 if(compareList.contains(this.orientation[i])){
  147.                 }else{
  148.                     return false;
  149.                 }
  150.             }
  151.             return true;
  152.         }
  153.     }
  154.  
  155.     public String getOrientationAsString(){
  156.         StringBuilder builder = new StringBuilder();
  157.         for(int i = 0;i<= this.orientation.length-1;i++){
  158.             builder.append(this.orientation[i] + " ");
  159.         }
  160.         return builder.toString();
  161.     }
  162.  
  163.     public void rotate(){
  164.         switch(this.tileType){
  165.         case DEAD_END:
  166.             this.orientation = rotateDeadEnd();
  167.             break;
  168.         case BEND:
  169.             this.orientation = rotateBend();
  170.             break;
  171.         case TEE:
  172.             this.orientation = rotateTee();
  173.             break;
  174.         case STRAIGHT:
  175.             this.orientation = rotateStraight();
  176.             break;
  177.         default:
  178.             break;
  179.         }
  180.     }
  181.  
  182.     public Orientation [] rotateDeadEnd(){
  183.         Orientation [] orientationArr = new Orientation [1];
  184.         switch(this.orientation[0]){
  185.         case LEFT:
  186.             orientationArr[0] = Orientation.UP;
  187.             return orientationArr;
  188.         case RIGHT:
  189.             orientationArr[0] = Orientation.DOWN;
  190.             return orientationArr;
  191.         case DOWN:
  192.             orientationArr[0] = Orientation.LEFT;
  193.             return orientationArr;
  194.         default:
  195.             orientationArr[0] = Orientation.RIGHT;
  196.             return orientationArr;
  197.         }
  198.     }
  199.  
  200.     public Orientation [] rotateBend(){
  201.         Orientation [] orientationArr = new Orientation [1];
  202.         switch(this.orientation[0]){
  203.         case LEFT:
  204.             orientationArr[0] = Orientation.UP;
  205.             return orientationArr;
  206.         case RIGHT:
  207.             orientationArr[0] = Orientation.DOWN;
  208.             return orientationArr;
  209.         case DOWN:
  210.             orientationArr[0] = Orientation.LEFT;
  211.             return orientationArr;
  212.         default:
  213.             orientationArr[0] = Orientation.RIGHT;
  214.             return orientationArr;
  215.         }
  216.     }
  217.  
  218.     public Orientation [] rotateTee(){
  219.         Orientation [] orientationArr = new Orientation [1];
  220.         switch(this.orientation[0]){
  221.         case LEFT:
  222.             orientationArr[0] = Orientation.UP;
  223.             return orientationArr;
  224.         case RIGHT:
  225.             orientationArr[0] = Orientation.DOWN;
  226.             return orientationArr;
  227.         case DOWN:
  228.             orientationArr[0] = Orientation.LEFT;
  229.             return orientationArr;
  230.         default:
  231.             orientationArr[0] = Orientation.RIGHT;
  232.             return orientationArr;
  233.         }
  234.     }
  235.  
  236.     public Orientation [] rotateStraight(){
  237.         Orientation [] orientationArr = new Orientation [2];
  238.         if(this.orientation[0].equals(Orientation.UP) && this.orientation[1].equals(Orientation.DOWN)
  239.                 || this.orientation[0].equals(Orientation.DOWN) && this.orientation[1].equals(Orientation.UP)) {
  240.             orientationArr[0] = Orientation.LEFT;
  241.             orientationArr[1] = Orientation.RIGHT;
  242.             return orientationArr;
  243.         }else if(this.orientation[0].equals(Orientation.LEFT) && this.orientation[1].equals(Orientation.RIGHT)
  244.                 || this.orientation[0].equals(Orientation.RIGHT) && this.orientation[1].equals(Orientation.LEFT)) {
  245.             orientationArr[0] = Orientation.UP;
  246.             orientationArr[1] = Orientation.DOWN;
  247.             return orientationArr;
  248.         }else{
  249.             throw new IllegalArgumentException("Type Straight has false orientation!");
  250.         }
  251.     }
  252.  
  253.     public String toString(){
  254.         StringBuilder builder = new StringBuilder();
  255.         if(this.orientation.length == 1){
  256.             builder.append("("+this.coord.getRow()+","+this.coord.getColumn()+") " + this.tileType.toString()  + " " + this.orientation[0]);
  257.         }else if(this.orientation.length == 2){
  258.             builder.append("("+this.coord.getRow()+","+this.coord.getColumn()+") " + this.tileType.toString() + " " + this.orientation[0] + " " + this.orientation[1]);
  259.         }else if(this.orientation.length == 3){
  260.             builder.append("("+this.coord.getRow()+","+this.coord.getColumn()+") " + this.tileType.toString() + " " + this.orientation[0] + " " + this.orientation[1] + " "
  261.                     + this.orientation[2]);
  262.         }else{
  263.             builder.append("("+this.coord.getRow()+","+this.coord.getColumn()+") " + this.tileType.toString() + " " + this.orientation[0] + " " + this.orientation[1]
  264.                     + " " + this.orientation[2] + " " +  this.orientation[3]);
  265.  
  266.         }
  267.         return builder.toString();
  268.     }
  269.  
  270.     @Override
  271.     public int hashCode() {
  272.         final int prime = 31;
  273.         int result = 1;
  274.         result = prime * result + ((coord == null) ? 0 : coord.hashCode());
  275.         result = prime * result + ((tileType == null) ? 0 : tileType.hashCode());
  276.         return result;
  277.     }
  278.  
  279.     @Override
  280.     public boolean equals(Object obj) {
  281.         if (this == obj)
  282.             return true;
  283.         if (obj == null)
  284.             return false;
  285.         if (getClass() != obj.getClass())
  286.             return false;
  287.         Tile other = (Tile) obj;
  288.         if (coord == null) {
  289.             if (other.coord != null)
  290.                 return false;
  291.         } else if (!coord.equals(other.coord))
  292.             return false;
  293.         if (tileType != other.tileType)
  294.             return false;
  295.         if (!this.sameOrientation(other.getOrientation())){
  296.             return false;
  297.         }
  298.         return true;
  299.     }
  300.  
  301.  
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement