Commit 450db60e by Paktalin

More refactoring in Position class: introduced Coordinate and inheriting classes

parent a5d90626
package com.paktalin.chess;
import com.paktalin.chess.coordinates.Column;
import com.paktalin.chess.coordinates.Row;
import com.paktalin.chess.coordinates.*;
/**
* Created by Paktalin on 31/05/2018.
......@@ -9,8 +8,7 @@ import com.paktalin.chess.coordinates.Row;
public class Position {
private Column column;
private Row row;
private Coordinate column, row;
private Position(Row row, Column column) {
this.row = row;
......@@ -19,7 +17,7 @@ public class Position {
public static Position create(String positionString) {
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));
}
......@@ -36,6 +34,10 @@ public class Position {
return row.getIntCode();
}
char getCharRow() {
return row.getCharCode();
}
public char getColumn() {
return column.getCharCode();
}
......
package com.paktalin.chess.coordinates;
public class Column extends Row {
private char charCode;
public class Column extends Coordinate {
public Column(int intCode) {
super(intCode);
this.charCode = generateCharCodeFromInt(intCode);
}
public Column(char charCode) {
super();
this.charCode = charCode;
this.intCode = generateIntCodeFromChar(charCode);
super(charCode);
}
private int generateIntCodeFromChar(char c) {
@Override
int generateIntCode(char charCode) {
char firstColumnLetter = 'a';
return (int)c - (int)firstColumnLetter;
return (int)charCode - (int)firstColumnLetter;
}
private char generateCharCodeFromInt(int i) {
return (char)(i + 'a');
}
public char getCharCode() {
return charCode;
@Override
char generateCharCode(int intCode) {
return (char)(intCode + 'a');
}
}
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;
public class Row {
int intCode;
public class Row extends Coordinate {
public Row(int intCode) {
super(intCode);
}
Row() {}
public Row(char charCode) {
super(charCode);
}
public Row(int intCode) {
this.intCode = intCode;
@Override
int generateIntCode(char charCode) {
return Character.getNumericValue(charCode) - 1;
}
public int getIntCode() {
return intCode;
@Override
char generateCharCode(int intCode) {
return (char) ('1' + intCode);
}
}
......@@ -3,6 +3,7 @@ package com.paktalin.chess;
import com.paktalin.chess.pieces.Bishop;
import com.paktalin.chess.pieces.Pawn;
import com.paktalin.chess.pieces.Piece;
import javafx.geometry.Pos;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
......@@ -41,4 +42,34 @@ class PositionTest {
piece = board.getPieceAtPosition(position);
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