Commit a5d90626 by Paktalin

Refactoring in Position class

parent 90aeec8c
package com.paktalin.chess;
import com.paktalin.chess.coordinates.Column;
import com.paktalin.chess.coordinates.Row;
/**
* Created by Paktalin on 31/05/2018.
*/
public class Position {
private int row;
private char column;
private Column column;
private Row row;
private Position(int row, char column) {
private Position(Row row, Column column) {
this.row = row;
this.column = column;
}
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) {
return new Position(row, intToChar(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);
return new Position(new Row(row), new Column(column));
}
@Override
public String toString() {
return column + "" + (row + 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;
return column.getCharCode() + "" + (row.getIntCode() + 1);
}
public int getRow() {
return row;
return row.getIntCode();
}
public char getColumn() {
return column;
return column.getCharCode();
}
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;
*/
public abstract class Piece implements Comparable<Piece> {
Color color;
private Position position;
Color color;
double strength;
MoveStrategy moveStrategy;
char representation;
......@@ -33,7 +32,9 @@ public abstract class Piece implements Comparable<Piece> {
this.position = position;
}
public void setStrength(List<Piece> pieces) {}
public void setStrength(List<Piece> pieces) {
}
void setStrength(double strength){
this.strength = strength;
......
......@@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test;
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;
/**
......
......@@ -30,9 +30,9 @@ class PositionTest {
@Test
void testStrengthByPosition() {
verifyStrength(new Bishop(Piece.Color.Black), "b5", 3.0);
verifyStrength(new Pawn(Piece.Color.White), "a2", 1.0);
verifyStrength(new Pawn(Piece.Color.White), "a3", 0.5);
verifyStrength(new Bishop(Color.Black), "b5", 3.0);
verifyStrength(new Pawn(Color.White), "a2", 1.0);
verifyStrength(new Pawn(Color.White), "a3", 0.5);
assertEquals(0.5, board.getPieceAtPosition("a2").getStrength());
}
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/**
* Created by Paktalin on 01/06/2018.
*/
......@@ -12,7 +14,7 @@ class BishopTest extends PieceTest {
}
@Override
Piece createPiece(Piece.Color color) {
Piece createPiece(Color color) {
return new Bishop(color);
}
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/**
* Created by Paktalin on 01/06/2018.
*/
......@@ -12,7 +14,7 @@ class KingTest extends PieceTest {
}
@Override
Piece createPiece(Piece.Color color) {
Piece createPiece(Color color) {
return new King(color);
}
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/**
* Created by Paktalin on 01/06/2018.
*/
......@@ -12,7 +14,7 @@ class KnightTest extends PieceTest {
}
@Override
Piece createPiece(Piece.Color color) {
Piece createPiece(Color color) {
return new Knight(color);
}
......
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;
/**
......@@ -15,7 +17,7 @@ class PawnTest extends PieceTest {
}
@Override
Piece createPiece(Piece.Color color) {
Piece createPiece(Color color) {
return new Pawn(color);
}
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Board;
import com.paktalin.chess.Color;
import com.paktalin.chess.Position;
import org.junit.jupiter.api.BeforeEach;
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.assertTrue;
......@@ -27,7 +28,7 @@ abstract class PieceTest {
}
abstract void setExpectedRepresentation();
abstract Piece createPiece(Piece.Color color);
abstract Piece createPiece(Color color);
abstract void setExpectedStrength();
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/**
* Created by Paktalin on 01/06/2018.
*/
......@@ -12,7 +14,7 @@ class QueenTest extends PieceTest {
}
@Override
Piece createPiece(Piece.Color color) {
Piece createPiece(Color color) {
return new Queen(color);
}
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/**
* Created by Paktalin on 01/06/2018.
*/
......@@ -12,7 +14,7 @@ class RookTest extends PieceTest {
}
@Override
Piece createPiece(Piece.Color color) {
Piece createPiece(Color 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