//This is only to make the code more readable
public class Knot{
public Point Coordinate{get;set;}
public Knot(Point _coordinate){
Coordinate = _coordinate;
}
//This method checks if the current knot is touching the previous knot in the rope in any way in a 3X3 grid
//If true, the current knot is not supposed to move
public bool Touch(Knot head){
for(int scanX = -1; scanX < 2; scanX++){
for(int scanY = -1; scanY < 2; scanY++){
Point check = new Point(this.Coordinate.X + scanX, this.Coordinate.Y + scanY);
if(check == head.Coordinate) return true;
}
}
return false;
}
}
public class Head : Knot{
void moveHead(string direction){
//move head one step at a time
if(direction is "U") Coordinate.X--;
if(direction is "D") Coordinate.X++;
if(direction is "L") Coordinate.Y--;
if(direction is "R") Coordinate.Y++;
}
}
//This is only to make the code more readable
public class Knot{
public Point Coordinate{get;set;}
public Knot(Point _coordinate){
Coordinate = _coordinate;
}
//This method checks if the current knot is touching the previous knot in the rope in any way in a 3X3 grid
//If true, the current knot is not supposed to move
public bool Touch(Knot head){
for(int scanX = -1; scanX < 2; scanX++){
for(int scanY = -1; scanY < 2; scanY++){
Point check = new Point(this.Coordinate.X + scanX, this.Coordinate.Y + scanY);
if(check == head.Coordinate) return true;
}
}
return false;
}
}
public class Head : Knot{
void moveHead(string direction){
//move head one step at a time
if(direction is "U") Coordinate.X--;
if(direction is "D") Coordinate.X++;
if(direction is "L") Coordinate.Y--;
if(direction is "R") Coordinate.Y++;
}
}