Commit 450db60e by Paktalin

More refactoring in Position class: introduced Coordinate and inheriting classes

parent a5d90626
package com.paktalin.chess; package com.paktalin.chess;
import com.paktalin.chess.coordinates.Column; import com.paktalin.chess.coordinates.*;
import com.paktalin.chess.coordinates.Row;
/** /**
* Created by Paktalin on 31/05/2018. * Created by Paktalin on 31/05/2018.
...@@ -9,8 +8,7 @@ import com.paktalin.chess.coordinates.Row; ...@@ -9,8 +8,7 @@ import com.paktalin.chess.coordinates.Row;
public class Position { public class Position {
private Column column; private Coordinate column, row;
private Row row;
private Position(Row row, Column column) { private Position(Row row, Column column) {
this.row = row; this.row = row;
...@@ -19,7 +17,7 @@ public class Position { ...@@ -19,7 +17,7 @@ public class Position {
public static Position create(String positionString) { public static Position create(String positionString) {
char column = positionString.charAt(0); char column = positionString.charAt(0);
int row = Character.getNumericValue(positionString.charAt(1)) - 1; char row = positionString.charAt(1);
return new Position(new Row(row), new Column(column)); return new Position(new Row(row), new Column(column));
} }
...@@ -36,6 +34,10 @@ public class Position { ...@@ -36,6 +34,10 @@ public class Position {
return row.getIntCode(); return row.getIntCode();
} }
char getCharRow() {
return row.getCharCode();
}
public char getColumn() { public char getColumn() {
return column.getCharCode(); return column.getCharCode();
} }
......
package com.paktalin.chess.coordinates; package com.paktalin.chess.coordinates;
public class Column extends Row { public class Column extends Coordinate {
private char charCode;
public Column(int intCode) { public Column(int intCode) {
super(intCode); super(intCode);
this.charCode = generateCharCodeFromInt(intCode);
} }
public Column(char charCode) { public Column(char charCode) {
super(); super(charCode);
this.charCode = charCode;
this.intCode = generateIntCodeFromChar(charCode);
} }
private int generateIntCodeFromChar(char c) { @Override
int generateIntCode(char charCode) {
char firstColumnLetter = 'a'; char firstColumnLetter = 'a';
return (int)c - (int)firstColumnLetter; return (int)charCode - (int)firstColumnLetter;
} }
private char generateCharCodeFromInt(int i) { @Override
return (char)(i + 'a'); char generateCharCode(int intCode) {
} return (char)(intCode + 'a');
public char getCharCode() {
return charCode;
} }
} }
package com.paktalin.chess.coordinates;
public abstract class Coordinate {
private char charCode;
private int intCode;
Coordinate(int intCode) {
this.intCode = intCode;
this.charCode = generateCharCode(intCode);
}
Coordinate(char charCode) {
this.charCode = charCode;
this.intCode = generateIntCode(charCode);
}
abstract int generateIntCode(char charCode);
abstract char generateCharCode(int intCode);
public char getCharCode() {
return charCode;
}
public int getIntCode() {
return intCode;
}
}
package com.paktalin.chess.coordinates; package com.paktalin.chess.coordinates;
public class Row { public class Row extends Coordinate {
int intCode; public Row(int intCode) {
super(intCode);
}
Row() {} public Row(char charCode) {
super(charCode);
}
public Row(int intCode) { @Override
this.intCode = intCode; int generateIntCode(char charCode) {
return Character.getNumericValue(charCode) - 1;
} }
public int getIntCode() { @Override
return intCode; char generateCharCode(int intCode) {
return (char) ('1' + intCode);
} }
} }
...@@ -3,6 +3,7 @@ package com.paktalin.chess; ...@@ -3,6 +3,7 @@ package com.paktalin.chess;
import com.paktalin.chess.pieces.Bishop; import com.paktalin.chess.pieces.Bishop;
import com.paktalin.chess.pieces.Pawn; import com.paktalin.chess.pieces.Pawn;
import com.paktalin.chess.pieces.Piece; import com.paktalin.chess.pieces.Piece;
import javafx.geometry.Pos;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -41,4 +42,34 @@ class PositionTest { ...@@ -41,4 +42,34 @@ class PositionTest {
piece = board.getPieceAtPosition(position); piece = board.getPieceAtPosition(position);
assertEquals(strength, piece.getStrength()); assertEquals(strength, piece.getStrength());
} }
@Test
void create() {
Position position = Position.create("b4");
assertEquals('b', position.getColumn());
assertEquals(1, position.getIntColumn());
assertEquals('4', position.getCharRow());
assertEquals(3, position.getRow());
}
// TODO: 2/26/2019 finish the test methods
@Test
void create1() {
}
@Test
void toStringTest() {
}
@Test
void getRow() {
}
@Test
void getColumn() {
}
@Test
void getIntColumn() {
}
} }
package com.paktalin.chess.coordinates;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ColumnTest {
private Column column;
@Test
void generateIntCode() {
column = new Column('d');
assertEquals(3, column.getIntCode());
}
@Test
void generateCharCode() {
column = new Column(2);
assertEquals('c', column.getCharCode());
}
}
\ No newline at end of file
package com.paktalin.chess.coordinates;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class RowTest {
private Row row;
@Test
void generateIntCode() {
row = new Row('2');
assertEquals(1, row.getIntCode());
}
@Test
void generateCharCode() {
row = new Row(3);
assertEquals('4', row.getCharCode());
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment