Commit cf3f685e by Paktalin

Initial commit

parents
Showing with 1224 additions and 0 deletions
<component name="ProjectDictionaryState">
<dictionary name="Paktalin">
<words>
<w>paktalin</w>
</words>
</dictionary>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="true" project-jdk-name="10" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Chess.iml" filepath="$PROJECT_DIR$/Chess.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.0.0/junit-jupiter-api-5.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.0.0/opentest4j-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.0.0/junit-platform-commons-1.0.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
\ No newline at end of file
package com.paktalin.chess;
import com.paktalin.chess.pieces.Piece;
import java.util.List;
import static com.paktalin.chess.pieces.Piece.Color.Black;
import static com.paktalin.chess.pieces.Piece.Color.White;
/**
* Created by Paktalin on 23/05/2018.
*/
public class Board {
private BoardLayout layout;
private Side whiteSide = new Side();
private Side blackSide = new Side();
private Board() {}
public static Board createEmpty() {
Board board = new Board();
board.layout = BoardLayout.create(board);
return board;
}
static Board createInitialized() {
Board board = Board.createEmpty();
board.layout.initialize();
return board;
}
public void placePiece(Piece piece, String position) {
layout.placePiece(piece, Position.create(position));
}
public void placePiece(Piece piece, Position position) {
layout.placePiece(piece, position);
}
private void move(Position from, Position to) {
Piece piece = layout.getPieceAtPosition(from);
if (piece.isMovable(from, to))
layout.move(from, to);
}
public void move(Piece piece, Position to) {
move(piece.getPosition(), to);
}
public Piece getPieceAtPosition(String position) {
return layout.getPieceAtPosition(Position.create(position));
}
public Piece getPieceAtPosition(Position position) {
return layout.getPieceAtPosition(position);
}
String print() {
return layout.print();
}
void addToCollection(Piece piece) {
getSide(piece.getColor()).addPiece(piece);
}
private Side getSide(Piece.Color color){
if (color == White)
return whiteSide;
return blackSide;
}
int getPiecesCount() {
return getPiecesCount(Black) + getPiecesCount(White);
}
double getStrength(Piece.Color color) {
return getSide(color).getStrength();
}
List<Piece> getPieces(Piece.Color color) {
return getSide(color).getPieces();
}
int getPiecesCount(Piece.Color color){
return getSide(color).getPiecesCount();
}
}
\ No newline at end of file
package com.paktalin.chess;
import com.paktalin.chess.pieces.Piece;
import com.paktalin.chess.pieces.*;
import static com.paktalin.chess.pieces.Piece.Color.*;
import static com.paktalin.chess.util.StringUtil.NEW_LINE;
/**
* Created by Paktalin on 27/05/2018.
*/
class BoardLayout {
private static final int ROW_COUNT = 8;
private static final int COLUMN_COUNT = 8;
private Piece[][] pieces = new Piece[ROW_COUNT][COLUMN_COUNT];
private Board board;
private BoardLayout() {}
void initialize() {
initializeKingRank(Black, 0);
initializePawnRank(Black, 1);
initializePawnRank(White, 6);
initializeKingRank(White, 7);
}
static BoardLayout create(Board board) {
BoardLayout layout = new BoardLayout();
layout.board = board;
return layout;
}
private void initializeKingRank(Piece.Color color, int row) {
placePiece(new Rook(color), Position.create(row, 0));
placePiece(new Knight(color), Position.create(row, 1));
placePiece(new Bishop(color), Position.create(row, 2));
placePiece(new Queen(color), Position.create(row, 3));
placePiece(new King(color), Position.create(row, 4));
placePiece(new Bishop(color), Position.create(row, 5));
placePiece(new Knight(color), Position.create(row, 6));
placePiece(new Rook(color), Position.create(row, 7));
}
private void initializePawnRank(Piece.Color color, int row) {
for (int column = 0; column < COLUMN_COUNT; column++)
placePiece(new Pawn(color), Position.create(row, column));
}
void placePiece(Piece piece, Position position) {
piece.setPosition(position);
piece.setStrength(board.getPieces(piece.getColor()));
board.addToCollection(piece);
pieces[position.getRow()][position.getIntColumn()] = piece;
}
void move(Position from, Position to) {
Piece piece = getPieceAtPosition(from);
pieces[from.getRow()][from.getIntColumn()] = null;
placePiece(piece, to);
}
Piece getPieceAtPosition(Position position) {
return pieces[position.getRow()][position.getIntColumn()];
}
String print() {
StringBuilder buffer = new StringBuilder();
for (int row = 0; row < ROW_COUNT; row++) {
for (int column = 0; column < COLUMN_COUNT; column++) {
if (pieces[row][column] == null)
buffer.append(".");
else
buffer.append(pieces[row][column].getRepresentation());
}
buffer.append(" ").append(row + 1).append(NEW_LINE);
}
buffer.append("abcdefgh");
return buffer.toString();
}
}
package com.paktalin.chess;
class Factorial {
static int getWhileFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
else {
int increment = 1;
factorial = increment;
while (increment <= number) {
factorial *= increment;
increment++;
}
}
return factorial;
}
static int getDoWhileFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
else {
int i = 1;
factorial = 1;
do {
factorial *= i;
i++;
} while (i <= number);
}
return factorial;
}
static int getForFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
else {
factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
}
return factorial;
}
static int getBreakFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
else {
int i = 1;
factorial = 1;
while (true) {
if (i > number)
break;
factorial *= i;
i++;
}
}
return factorial;
}
}
package com.paktalin.chess;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Game {
}
package com.paktalin.chess;
/**
* Created by Paktalin on 31/05/2018.
*/
public class Position {
private int row;
private char column;
private Position() {}
private Position(int row, char column) {
this.row = row;
this.column = column;
}
public static Position create(String positionString) {
return create(retrieveRow(positionString), retrieveColumn(positionString));
}
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);
}
@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;
}
public int getRow() {
return row;
}
public char getColumn() {
return column;
}
public int getIntColumn() {
return charToInt(column);
}
}
package com.paktalin.chess;
import com.paktalin.chess.pieces.Piece;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by Paktalin on 27/05/2018.
*/
class Side {
private List<Piece> pieces = new ArrayList<>();
List<Piece> getPieces() {
return pieces;
}
double getStrength() {
double strength = 0.0;
for (Piece piece : pieces)
strength += piece.getStrength();
return strength;
}
int getPiecesCount() {
return pieces.size();
}
void addPiece(Piece piece) {
pieces.add(piece);
Collections.sort(pieces);
}
}
package com.paktalin.chess.moves;
import com.paktalin.chess.Position;
/**
* Created by Paktalin on 31/05/2018.
*/
public class KingMoveStrategy extends MoveStrategy{
@Override
public boolean isMovable(Position from, Position to) {
super.isMovable(from, to);
return !(distantRows() || distantColumns());
}
private boolean distantRows() {
return Math.abs(toRow - fromRow) > 1;
}
private boolean distantColumns() {
return Math.abs(toColumn - fromColumn) > 1;
}
}
package com.paktalin.chess.moves;
import com.paktalin.chess.Position;
/**
* Created by Paktalin on 31/05/2018.
*/
public abstract class MoveStrategy {
int fromRow, fromColumn;
int toRow, toColumn;
private void initialize(Position from, Position to) {
fromRow = from.getRow();
fromColumn = from.getIntColumn();
toRow = to.getRow();
toColumn = to.getIntColumn();
}
public boolean isMovable(Position from, Position to) {
initialize(from, to);
return false;
}
}
package com.paktalin.chess.moves;
import com.paktalin.chess.Position;
/**
* Created by Paktalin on 31/05/2018.
*/
public class QueenMoveStrategy extends MoveStrategy {
@Override
public boolean isMovable(Position from, Position to) {
super.isMovable(from, to);
return sameRow() || sameColumn() || diagonal();
}
private boolean sameRow() {
return fromRow == toRow;
}
private boolean sameColumn() {
return fromColumn == toColumn;
}
private boolean diagonal() {
return Math.abs(toRow - fromRow) == Math.abs(toColumn - fromColumn);
}
}
package com.paktalin.chess.pieces;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Bishop extends Piece {
public Bishop(Piece.Color color) {
strength = 3;
representation = 'b';
this.color = color;
}
}
package com.paktalin.chess.pieces;
import com.paktalin.chess.moves.KingMoveStrategy;
/**
* Created by Paktalin on 01/06/2018.
*/
public class King extends Piece {
public King(Color color) {
strength = 0;
representation = 'k';
moveStrategy = new KingMoveStrategy();
this.color = color;
}
}
package com.paktalin.chess.pieces;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Knight extends Piece {
public Knight(Piece.Color color) {
strength = 2.5;
representation = 'n';
this.color = color;
}
}
package com.paktalin.chess.pieces;
import java.util.List;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Pawn extends Piece {
public Pawn(Piece.Color color) {
strength = 1;
representation = 'p';
this.color = color;
}
public void setStrength(List<Piece> pieces) {
this.setStrength(1.0);
for (Piece piece : pieces) {
if (pawnsOnSameColumn(this, piece)) {
piece.setStrength(0.5);
this.setStrength(0.5);
}
}
}
private static boolean pawnsOnSameColumn(Piece currentPiece, Piece piece) {
return sameColumn(currentPiece, piece) &&
notSameRow(currentPiece, piece) &&
piece.getClass() == Pawn.class;
}
private static boolean sameColumn(Piece piece1, Piece piece2) {
return piece2.getPosition().getColumn() == piece1.getPosition().getColumn();
}
private static boolean notSameRow(Piece piece1, Piece piece2) {
return piece2.getPosition().getRow() != piece1.getPosition().getRow();
}
}
package com.paktalin.chess.pieces;
import com.paktalin.chess.Position;
import com.paktalin.chess.moves.MoveStrategy;
import java.util.List;
/**
* Created by Paktalin on 23/05/2018.
*/
public abstract class Piece implements Comparable<Piece> {
public enum Color {White, Black}
Color color;
private Position position;
double strength;
MoveStrategy moveStrategy;
char representation;
Piece() {}
@Override
public int compareTo(Piece that) {
if (this.getStrength() > that.getStrength())
return -1;
if (this.getStrength() < that.getStrength())
return 1;
return 0;
}
public void setPosition(Position position) {
this.position = position;
}
public void setStrength(List<Piece> pieces) {}
void setStrength(double strength){
this.strength = strength;
}
public char getRepresentation() {
if (isBlack())
return Character.toUpperCase(representation);
return representation;
}
public double getStrength() {
return strength;
}
public Color getColor() {
return color;
}
public Position getPosition() {
return position;
}
public boolean isWhite() {
return this.color == Color.White;
}
boolean isBlack() {
return this.color == Color.Black;
}
public boolean isAtPosition(Position position) {
return this.position.equals(position);
}
public boolean isMovable(Position from, Position to){
return moveStrategy.isMovable(from, to);
}
}
\ No newline at end of file
package com.paktalin.chess.pieces;
import com.paktalin.chess.moves.QueenMoveStrategy;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Queen extends Piece {
public Queen(Piece.Color color) {
strength = 9;
representation = 'q';
moveStrategy = new QueenMoveStrategy();
this.color = color;
}
}
package com.paktalin.chess.pieces;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Rook extends Piece{
public Rook(Piece.Color color) {
strength = 5;
representation = 'r';
this.color = color;
}
}
package com.paktalin.chess.util;
/**
* Created by Paktalin on 24/05/2018.
*/
public class StringUtil {
public static final String NEW_LINE = "\n";
private StringUtil() {}
}
package com.paktalin.chess;
import com.paktalin.chess.pieces.*;
import org.junit.jupiter.api.Test;
import java.util.List;
import static com.paktalin.chess.pieces.Piece.Color.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Created by Paktalin on 23/05/2018.
*/
class BoardTest {
private Board board;
@Test
void testCreate() {
board = Board.createInitialized();
assertEquals(32, board.getPiecesCount());
assertEquals(16, board.getPiecesCount(Black));
assertEquals(16, board.getPiecesCount(White));
}
@Test
void testPlacePieces() {
board = Board.createInitialized();
board.placePiece(new Bishop(Black), "a8");
Piece bishop = board.getPieceAtPosition("a8");
assertEquals(Black, bishop.getColor());
}
@Test
void testOverallStrength() {
board = Board.createEmpty();
verifyStrength(new Queen(Black), "e6", 9.0, 0.0, 9.0);
verifyStrength(new Queen(White), "g4", 9.0, 9.0, 9.0);
verifyStrength(new King(Black), "b8", 9.0, 9.0, 0.0);
verifyStrength(new King(White), "f1", 9.0, 9.0, 0.0);
verifyStrength(new Rook(Black), "c8", 14.0, 9.0, 5.0);
verifyStrength(new Rook(White), "e1", 14.0, 14.0, 5.0);
verifyStrength(new Pawn(Black), "a7", 15.0, 14.0, 1.0);
verifyStrength(new Pawn(White), "f2", 15.0, 15.0, 1.0);
verifyStrength(new Pawn(Black), "c7", 16.0, 15.0, 1.0);
verifyStrength(new Pawn(White), "g2", 16.0, 16.0, 1.0);
verifyStrength(new Bishop(Black), "d7", 19.0, 16.0, 3.0);
verifyStrength(new Pawn(White), "f3", 19.0, 16.0, 0.5);
verifyStrength(new Pawn(Black), "b6", 20.0, 16.0, 1.0);
verifyStrength(new Pawn(White), "h3", 20.0, 17.0, 1.0);
verifyStrength(new Knight(White), "f4", 20.0, 19.5, 2.5);
verifyPrint();
}
@Test
void testColorCollections() {
board = Board.createInitialized();
List<Piece> blackPieces = board.getPieces(Black);
assertEquals(9.0, blackPieces.get(0).getStrength());
assertEquals(5.0, blackPieces.get(1).getStrength());
assertEquals(5.0, blackPieces.get(2).getStrength());
assertEquals(3.0, blackPieces.get(3).getStrength());
assertEquals(3.0, blackPieces.get(4).getStrength());
assertEquals(2.5, blackPieces.get(5).getStrength());
assertEquals(2.5, blackPieces.get(5).getStrength());
}
private void verifyPrint() {
assertEquals(
"....rk.. 1\n" +
".....pp. 2\n" +
".....p.p 3\n" +
".....nq. 4\n" +
"........ 5\n" +
".P..Q... 6\n" +
"P.PB.... 7\n" +
".KR..... 8\n" +
"abcdefgh",
board.print());
}
private void verifyStrength(Piece piece, String position,
double blackStrength, double whiteStrength, double pieceStrength) {
board.placePiece(piece, position);
assertEquals(blackStrength, board.getStrength(Black));
assertEquals(whiteStrength, board.getStrength(White));
assertEquals(pieceStrength, piece.getStrength());
}
}
package com.paktalin.chess;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Created by Paktalin on 23/05/2018.
*/
class CharacterTest {
@Test
void testWhiteSpace() {
assertTrue(Character.isWhitespace(' '));
assertTrue(Character.isWhitespace('\t'));
assertTrue(Character.isWhitespace('\n'));
assertFalse(Character.isWhitespace('m'));
assertFalse(Character.isWhitespace('a'));
}
@Test
void testIdentifiers() {
assertFalse(Character.isJavaIdentifierPart('^'));
assertTrue(Character.isJavaIdentifierPart('k'));
}
}
package com.paktalin.chess;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class FactorialTest {
@Test
void testWhile() {
int factorial = Factorial.getWhileFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getWhileFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getWhileFactorial(3);
assertEquals(getFactorial(3), factorial);
}
@Test
void testDoWhile() {
int factorial = Factorial.getDoWhileFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getDoWhileFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getDoWhileFactorial(3);
assertEquals(getFactorial(3), factorial);
}
@Test
void testFor() {
int factorial = Factorial.getForFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getForFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getForFactorial(3);
assertEquals(getFactorial(3), factorial);
}
@Test
void testBreak() {
int factorial = Factorial.getBreakFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getBreakFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getBreakFactorial(3);
assertEquals(getFactorial(3), factorial);
}
private int getFactorial(int number) {
int[] factorials = {1, 1, 2, 6, 24, 120};
return factorials[number];
}
}
package com.paktalin.chess;
import com.paktalin.chess.pieces.Bishop;
import com.paktalin.chess.pieces.Pawn;
import com.paktalin.chess.pieces.Piece;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Created by Paktalin on 31/05/2018.
*/
class PositionTest {
private Board board;
@BeforeEach
void init() {
board = Board.createEmpty();
}
@Test
void testCreate() {
Position position = Position.create("a8");
assertEquals("a8", position.toString());
assertEquals(0, position.getIntColumn());
assertEquals(7, position.getRow());
}
@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);
assertEquals(0.5, board.getPieceAtPosition("a2").getStrength());
}
private void verifyStrength(Piece piece, String position, double strength) {
board.placePiece(piece, position);
piece = board.getPieceAtPosition(position);
assertEquals(strength, piece.getStrength());
}
}
package com.paktalin.chess;
/**
* Created by Paktalin on 24/05/2018.
*/
public class StringUtilTest {
}
package com.paktalin.chess.moves;
import com.paktalin.chess.Position;
import com.paktalin.chess.pieces.King;
import com.paktalin.chess.pieces.Piece;
/**
* Created by Paktalin on 31/05/2018.
*/
class KingMoveStrategyTest extends MoveStrategyTest {
@Override
void putPieceOnBoard() {
board.placePiece(new King(Piece.Color.Black), currentPosition);
}
@Override
void setAccessiblePosition() {
accessiblePosition = Position.create("a6");
}
@Override
void setNotAccessiblePosition() {
notAccessiblePosition = Position.create("d4");
}
}
package com.paktalin.chess.moves;
import com.paktalin.chess.Board;
import com.paktalin.chess.Position;
import com.paktalin.chess.pieces.Piece;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Created by Paktalin on 31/05/2018.
*/
abstract class MoveStrategyTest {
private Piece piece;
Board board;
Position accessiblePosition, notAccessiblePosition;
Position currentPosition = Position.create("b5");
@BeforeEach
void init() {
board = Board.createEmpty();
putPieceOnBoard();
piece = board.getPieceAtPosition(currentPosition);
setAccessiblePosition();
setNotAccessiblePosition();
}
abstract void putPieceOnBoard();
abstract void setAccessiblePosition();
abstract void setNotAccessiblePosition();
@Test
void testMove() {
board.move(piece, accessiblePosition);
assertTrue(piece.isAtPosition(accessiblePosition));
assertFalse(piece.isAtPosition(currentPosition));
currentPosition = accessiblePosition;
board.move(piece, notAccessiblePosition);
assertTrue(piece.isAtPosition(currentPosition));
assertFalse(piece.isAtPosition(notAccessiblePosition));
}
}
package com.paktalin.chess.moves;
import com.paktalin.chess.Position;
import com.paktalin.chess.pieces.Piece;
import com.paktalin.chess.pieces.Queen;
/**
* Created by Paktalin on 31/05/2018.
*/
class QueenMoveStrategyTest extends MoveStrategyTest {
@Override
void putPieceOnBoard() {
board.placePiece(new Queen(Piece.Color.Black), currentPosition);
}
@Override
void setAccessiblePosition() {
accessiblePosition = Position.create("d3");
}
@Override
void setNotAccessiblePosition() {
notAccessiblePosition = Position.create("f7");
}
}
package com.paktalin.chess.pieces;
/**
* Created by Paktalin on 01/06/2018.
*/
class BishopTest extends PieceTest {
@Override
void setExpectedRepresentation() {
representation = 'b';
}
@Override
Piece createPiece(Piece.Color color) {
return new Bishop(color);
}
@Override
void setExpectedStrength() {
strength = 3;
}
}
package com.paktalin.chess.pieces;
/**
* Created by Paktalin on 01/06/2018.
*/
class KingTest extends PieceTest {
@Override
void setExpectedRepresentation() {
representation = 'k';
}
@Override
Piece createPiece(Piece.Color color) {
return new King(color);
}
@Override
void setExpectedStrength() {
strength = 0;
}
}
package com.paktalin.chess.pieces;
/**
* Created by Paktalin on 01/06/2018.
*/
class KnightTest extends PieceTest {
@Override
void setExpectedRepresentation() {
representation = 'n';
}
@Override
Piece createPiece(Piece.Color color) {
return new Knight(color);
}
@Override
void setExpectedStrength() {
strength = 2.5;
}
}
package com.paktalin.chess.pieces;
import static com.paktalin.chess.pieces.Piece.Color.Black;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Created by Paktalin on 01/06/2018.
*/
class PawnTest extends PieceTest {
@Override
void setExpectedRepresentation() {
representation = 'p';
}
@Override
Piece createPiece(Piece.Color color) {
return new Pawn(color);
}
@Override
void setExpectedStrength() {
strength = 1;
}
@Override
void testStrength() {
super.testStrength();
strength = 0.5;
board.placePiece(createPiece(Black), "a6");
assertEquals(strength, board.getPieceAtPosition("a6").getStrength());
assertEquals(strength, board.getPieceAtPosition("a7").getStrength());
}
}
package com.paktalin.chess.pieces;
import com.paktalin.chess.Board;
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 org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Created by Paktalin on 23/05/2018.
*/
abstract class PieceTest {
char representation;
double strength;
private Position position = Position.create("a7");
Board board;
@BeforeEach
void init() {
setExpectedRepresentation();
setExpectedStrength();
}
abstract void setExpectedRepresentation();
abstract Piece createPiece(Piece.Color color);
abstract void setExpectedStrength();
@Test
void testCreate() {
Piece whitePiece = createPiece(White);
Piece blackPiece = createPiece(Black);
assertTrue(whitePiece.isWhite());
assertEquals(representation, whitePiece.getRepresentation());
assertTrue(blackPiece.isBlack());
assertEquals(getBlackRepresentation(), blackPiece.getRepresentation());
}
@Test
void testStrength() {
board = Board.createEmpty();
board.placePiece(createPiece(Black), position);
assertEquals(strength, board.getPieceAtPosition(position).getStrength());
}
private char getBlackRepresentation() {
return Character.toUpperCase(representation);
}
}
package com.paktalin.chess.pieces;
/**
* Created by Paktalin on 01/06/2018.
*/
class QueenTest extends PieceTest {
@Override
void setExpectedRepresentation() {
representation = 'q';
}
@Override
Piece createPiece(Piece.Color color) {
return new Queen(color);
}
@Override
void setExpectedStrength() {
strength = 9;
}
}
package com.paktalin.chess.pieces;
/**
* Created by Paktalin on 01/06/2018.
*/
class RookTest extends PieceTest {
@Override
void setExpectedRepresentation() {
representation = 'r';
}
@Override
Piece createPiece(Piece.Color color) {
return new Rook(color);
}
@Override
void setExpectedStrength() {
strength = 5;
}
}
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