Created
December 23, 2020 03:41
-
-
Save robgray/51881509082354d23f6d93cd64354a59 to your computer and use it in GitHub Desktop.
C# Linqpad script with classes, to model a problem I was trying to solve.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
new Square(-3,-1).Dump(); | |
} | |
public class Square | |
{ | |
public Square(int x, int y) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
private int x; | |
private int y; | |
public Ring Ring => new Ring(Math.Max(Math.Abs(x), Math.Abs(y))); | |
public void MoveUp() { | |
y++; | |
} | |
public void MoveLeft() { | |
var currentRing = Ring; | |
x--; | |
} | |
public void MoveRight() { | |
x++; | |
} | |
public void MoveDown() { | |
y--; | |
} | |
public int Number | |
{ | |
get | |
{ | |
if (Ring.Number == 0) | |
return 1; | |
if (Math.Abs(y) == Ring.Number) { | |
return (y >= 0 ? Ring.PositiveYValue + x : Ring.NegativeYValue -x); | |
} | |
// If y doesn't equal ring number, X must, or it'd be a different ring. | |
return (x >= 0 ? Ring.PositiveXValue - y : Ring.NegativeXValue + y); | |
} | |
} | |
} | |
public class Ring | |
{ | |
public Ring(int number) | |
{ | |
Number = number; | |
} | |
public int Number; | |
public int Size => GetSize(Number); | |
public static int GetSize(int ring) | |
{ | |
if (ring == 0) | |
return 1; | |
if (ring == 1) | |
return 8; | |
return GetSize(ring - 1) + 8; | |
} | |
public int PositiveXValue => GetPositiveX(Number); | |
public int PositiveYValue => GetPositiveY(Number); | |
public int NegativeXValue => GetNegativeX(Number); | |
public int NegativeYValue => GetNegativeY(Number); | |
private int GetNegativeX(int ringNumber) | |
{ | |
if (ringNumber == 0) | |
return 1; | |
return GetNegativeXJump(ringNumber) + GetNegativeX(ringNumber - 1); | |
} | |
private int GetNegativeY(int ringNumber) | |
{ | |
if (ringNumber == 0) | |
return 1; | |
return GetNegativeYJump(ringNumber) + GetNegativeY(ringNumber - 1); | |
} | |
private int GetPositiveY(int ringNumber) | |
{ | |
if (ringNumber == 0) | |
return 1; | |
return GetPositiveYJump(ringNumber) + GetPositiveY(ringNumber - 1); | |
} | |
private int GetPositiveX(int ringNumber) | |
{ | |
if (ringNumber == 0) | |
return 1; | |
return GetPositiveXJump(ringNumber) + GetPositiveX(ringNumber - 1); | |
} | |
private int GetPositiveXJump(int ringNumber) | |
{ | |
if (ringNumber == 1) | |
return 3; | |
return 8 + GetPositiveXJump(ringNumber - 1); | |
} | |
private int GetPositiveYJump(int ringNumber) | |
{ | |
if (ringNumber == 1) | |
return 1; | |
return 8 + GetPositiveYJump(ringNumber - 1); | |
} | |
private int GetNegativeXJump(int ringNumber) | |
{ | |
if (ringNumber == 1) | |
return 7; | |
return 8 + GetNegativeXJump(ringNumber - 1); | |
} | |
private int GetNegativeYJump(int ringNumber) | |
{ | |
if (ringNumber == 1) | |
return 5; | |
return 8 + GetNegativeYJump(ringNumber - 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment