Commit a5d90626 by Paktalin

Refactoring in Position class

parent 90aeec8c
package com.paktalin.chess; package com.paktalin.chess;
import com.paktalin.chess.coordinates.Column;
import com.paktalin.chess.coordinates.Row;
/** /**
* Created by Paktalin on 31/05/2018. * Created by Paktalin on 31/05/2018.
*/ */
public class Position { public class Position {
private int row; private Column column;
private char column; private Row row;
private Position(int row, char column) { private Position(Row row, Column column) {
this.row = row; this.row = row;
this.column = column; this.column = column;
} }
public static Position create(String positionString) { public static Position create(String positionString) {
return create(retrieveRow(positionString), retrieveColumn(positionString)); char column = positionString.charAt(0);
int row = Character.getNumericValue(positionString.charAt(1)) - 1;
return new Position(new Row(row), new Column(column));
} }
static Position create(int row, int column) { static Position create(int row, int column) {
return new Position(row, intToChar(column)); return new Position(new Row(row), new Column(column));
}
private static int retrieveRow(String position) {
return Integer.parseInt(position.split("")[1]) - 1;
}
private static int retrieveColumn(String position) {
char file = position.toCharArray()[0];
return charToInt(file);
} }
@Override @Override
public String toString() { public String toString() {
return column + "" + (row + 1); return column.getCharCode() + "" + (row.getIntCode() + 1);
}
private static char intToChar(int i) {
return (char)(i + 'a');
}
private static int charToInt(char c) {
char firstColumnLetter = 'a';
return (int)c - (int)firstColumnLetter;
} }
public int getRow() { public int getRow() {
return row; return row.getIntCode();
} }
public char getColumn() { public char getColumn() {
return column; return column.getCharCode();
} }
public int getIntColumn() { public int getIntColumn() {
return charToInt(column); return column.getIntCode();
} }
} }
\ No newline at end of file
package com.paktalin.chess.coordinates;
public class Column extends Row {
private char charCode;
public Column(int intCode) {
super(intCode);
this.charCode = generateCharCodeFromInt(intCode);
}
public Column(char charCode) {
super();
this.charCode = charCode;
this.intCode = generateIntCodeFromChar(charCode);
}
private int generateIntCodeFromChar(char c) {
char firstColumnLetter = 'a';
return (int)c - (int)firstColumnLetter;
}
private char generateCharCodeFromInt(int i) {
return (char)(i + 'a');
}
public char getCharCode() {
return charCode;
}
}
package com.paktalin.chess.coordinates;
public class Row {
int intCode;
Row() {}
public Row(int intCode) {
this.intCode = intCode;
}
public int getIntCode() {
return intCode;
}
}
...@@ -11,9 +11,8 @@ import java.util.List; ...@@ -11,9 +11,8 @@ import java.util.List;
*/ */
public abstract class Piece implements Comparable<Piece> { public abstract class Piece implements Comparable<Piece> {
Color color;
private Position position; private Position position;
Color color;
double strength; double strength;
MoveStrategy moveStrategy; MoveStrategy moveStrategy;
char representation; char representation;
...@@ -33,7 +32,9 @@ public abstract class Piece implements Comparable<Piece> { ...@@ -33,7 +32,9 @@ public abstract class Piece implements Comparable<Piece> {
this.position = position; this.position = position;
} }
public void setStrength(List<Piece> pieces) {} public void setStrength(List<Piece> pieces) {
}
void setStrength(double strength){ void setStrength(double strength){
this.strength = strength; this.strength = strength;
......
...@@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test; ...@@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
import static com.paktalin.chess.pieces.Piece.Color.*; import static com.paktalin.chess.Color.*;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
......
...@@ -30,9 +30,9 @@ class PositionTest { ...@@ -30,9 +30,9 @@ class PositionTest {
@Test @Test
void testStrengthByPosition() { void testStrengthByPosition() {
verifyStrength(new Bishop(Piece.Color.Black), "b5", 3.0); verifyStrength(new Bishop(Color.Black), "b5", 3.0);
verifyStrength(new Pawn(Piece.Color.White), "a2", 1.0); verifyStrength(new Pawn(Color.White), "a2", 1.0);
verifyStrength(new Pawn(Piece.Color.White), "a3", 0.5); verifyStrength(new Pawn(Color.White), "a3", 0.5);
assertEquals(0.5, board.getPieceAtPosition("a2").getStrength()); assertEquals(0.5, board.getPieceAtPosition("a2").getStrength());
} }
......
package com.paktalin.chess.pieces; package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/** /**
* Created by Paktalin on 01/06/2018. * Created by Paktalin on 01/06/2018.
*/ */
...@@ -12,7 +14,7 @@ class BishopTest extends PieceTest { ...@@ -12,7 +14,7 @@ class BishopTest extends PieceTest {
} }
@Override @Override
Piece createPiece(Piece.Color color) { Piece createPiece(Color color) {
return new Bishop(color); return new Bishop(color);
} }
......
package com.paktalin.chess.pieces; package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/** /**
* Created by Paktalin on 01/06/2018. * Created by Paktalin on 01/06/2018.
*/ */
...@@ -12,7 +14,7 @@ class KingTest extends PieceTest { ...@@ -12,7 +14,7 @@ class KingTest extends PieceTest {
} }
@Override @Override
Piece createPiece(Piece.Color color) { Piece createPiece(Color color) {
return new King(color); return new King(color);
} }
......
package com.paktalin.chess.pieces; package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/** /**
* Created by Paktalin on 01/06/2018. * Created by Paktalin on 01/06/2018.
*/ */
...@@ -12,7 +14,7 @@ class KnightTest extends PieceTest { ...@@ -12,7 +14,7 @@ class KnightTest extends PieceTest {
} }
@Override @Override
Piece createPiece(Piece.Color color) { Piece createPiece(Color color) {
return new Knight(color); return new Knight(color);
} }
......
package com.paktalin.chess.pieces; package com.paktalin.chess.pieces;
import static com.paktalin.chess.pieces.Piece.Color.Black; import com.paktalin.chess.Color;
import static com.paktalin.chess.Color.Black;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
...@@ -15,7 +17,7 @@ class PawnTest extends PieceTest { ...@@ -15,7 +17,7 @@ class PawnTest extends PieceTest {
} }
@Override @Override
Piece createPiece(Piece.Color color) { Piece createPiece(Color color) {
return new Pawn(color); return new Pawn(color);
} }
......
package com.paktalin.chess.pieces; package com.paktalin.chess.pieces;
import com.paktalin.chess.Board; import com.paktalin.chess.Board;
import com.paktalin.chess.Color;
import com.paktalin.chess.Position; import com.paktalin.chess.Position;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static com.paktalin.chess.pieces.Piece.Color.*; import static com.paktalin.chess.Color.*;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
...@@ -27,7 +28,7 @@ abstract class PieceTest { ...@@ -27,7 +28,7 @@ abstract class PieceTest {
} }
abstract void setExpectedRepresentation(); abstract void setExpectedRepresentation();
abstract Piece createPiece(Piece.Color color); abstract Piece createPiece(Color color);
abstract void setExpectedStrength(); abstract void setExpectedStrength();
......
package com.paktalin.chess.pieces; package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/** /**
* Created by Paktalin on 01/06/2018. * Created by Paktalin on 01/06/2018.
*/ */
...@@ -12,7 +14,7 @@ class QueenTest extends PieceTest { ...@@ -12,7 +14,7 @@ class QueenTest extends PieceTest {
} }
@Override @Override
Piece createPiece(Piece.Color color) { Piece createPiece(Color color) {
return new Queen(color); return new Queen(color);
} }
......
package com.paktalin.chess.pieces; package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/** /**
* Created by Paktalin on 01/06/2018. * Created by Paktalin on 01/06/2018.
*/ */
...@@ -12,7 +14,7 @@ class RookTest extends PieceTest { ...@@ -12,7 +14,7 @@ class RookTest extends PieceTest {
} }
@Override @Override
Piece createPiece(Piece.Color color) { Piece createPiece(Color color) {
return new Rook(color); return new Rook(color);
} }
......
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