Skip to content

Commit

Permalink
Removes duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed May 14, 2024
1 parent 8e9930c commit 4a82474
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 53 deletions.
13 changes: 12 additions & 1 deletion src/main/java/com/fathzer/jchess/CoordinatesSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@ public interface CoordinatesSystem {
int getRow(int index);
int getColumn(int index);

int getIndex(String algebraicNotation);
default String getAlgebraicNotation(int index) {
return getAlgebraicNotation(getRow(index), getColumn(index));
}
default String getAlgebraicNotation(int row, int column) {
final char x = (char)('a' + column);
return x+Integer.toString(row+1);
}

default int getIndex(String algebraicNotation) {
if (algebraicNotation.length()<2) {
throw new IllegalArgumentException();
}
final int column = algebraicNotation.charAt(0)-'a';
final int row = getDimension().getHeight()-Integer.parseInt(algebraicNotation.substring(1));
if (column<0 || row <0) {
throw new IllegalArgumentException();
}
return getIndex(row, column);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,6 @@ public int previousRow(int index) {
return index-dimension.getWidth();
}

@Override
public int getIndex(String algebraicNotation) {
if (algebraicNotation.length()<2) {
throw new IllegalArgumentException();
}
final int column = getColumn(algebraicNotation);
final int row = getRow(algebraicNotation);
return row*dimension.getWidth() + column;
}

private int getRow(String pos) {
final int y = dimension.getHeight()-Integer.parseInt(pos.substring(1));
if (y<0) {
throw new IllegalArgumentException();
}
return y;
}

private int getColumn(String pos) {
final int x = pos.charAt(0)-'a';
if (x<0) {
throw new IllegalArgumentException();
}
return x;
}

@Override
public int getRow(int index) {
return index/dimension.getWidth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,6 @@ public int previousRow(int index) {
return index-arrayWidth;
}

@Override
public int getIndex(String algebraicNotation) {
if (algebraicNotation.length()<2) {
throw new IllegalArgumentException();
}
final int column = getColumn(algebraicNotation);
final int row = getRow(algebraicNotation);
return getIndex(row, column);
}

private int getRow(String pos) {
final int y = dimension.getHeight()-Integer.parseInt(pos.substring(1));
if (y<0) {
throw new IllegalArgumentException();
}
return y;
}

private int getColumn(String pos) {
final int x = pos.charAt(0)-'a';
if (x<0) {
throw new IllegalArgumentException();
}
return x;
}

@Override
public int getRow(int index) {
return index/arrayWidth;
Expand Down

0 comments on commit 4a82474

Please sign in to comment.