Commit 90aeec8c by Paktalin

Refactoring + completed 7.3.a

parent 816ed672
......@@ -4,8 +4,7 @@ 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;
import static com.paktalin.chess.Color.*;
/**
* Created by Paktalin on 23/05/2018.
......@@ -65,7 +64,7 @@ public class Board {
getSide(piece.getColor()).addPiece(piece);
}
private Side getSide(Piece.Color color){
private Side getSide(Color color){
if (color == White)
return whiteSide;
return blackSide;
......@@ -74,13 +73,13 @@ public class Board {
int getPiecesCount() {
return getPiecesCount(Black) + getPiecesCount(White);
}
double getStrength(Piece.Color color) {
double getStrength(Color color) {
return getSide(color).getStrength();
}
List<Piece> getPieces(Piece.Color color) {
List<Piece> getPieces(Color color) {
return getSide(color).getPieces();
}
int getPiecesCount(Piece.Color color){
int getPiecesCount(Color color){
return getSide(color).getPiecesCount();
}
}
\ No newline at end of file
......@@ -3,7 +3,7 @@ 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.Color.*;
import static com.paktalin.chess.util.StringUtil.NEW_LINE;
......@@ -33,7 +33,7 @@ class BoardLayout {
return layout;
}
private void initializeKingRank(Piece.Color color, int row) {
private void initializeKingRank(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));
......@@ -44,7 +44,7 @@ class BoardLayout {
placePiece(new Rook(color), Position.create(row, 7));
}
private void initializePawnRank(Piece.Color color, int row) {
private void initializePawnRank(Color color, int row) {
for (int column = 0; column < COLUMN_COUNT; column++)
placePiece(new Pawn(color), Position.create(row, column));
}
......
package com.paktalin.chess;
public enum Color {White, Black}
......@@ -9,8 +9,6 @@ public class Position {
private int row;
private char column;
private Position() {}
private Position(int row, char column) {
this.row = row;
this.column = column;
......
package com.paktalin.chess.language;
import java.util.Vector;
class Continue {
private final static char WHITESPACE = ' ';
private final static char ASTERISK = '*';
static String generateOneToNString(int n) {
static String generateStringOneToN(int n) {
if (n < 1) return null;
StringBuilder result = new StringBuilder();
for (int i = 1; i <= n; i++) {
......@@ -15,4 +17,17 @@ class Continue {
}
return result.toString();
}
static Vector<String> generateVectorOneToN(int n) {
if (n < 1) return null;
Vector<String> resultVector = new Vector<>();
StringBuilder currentBuilder;
for (int i = 1; i <= n; i++) {
currentBuilder = new StringBuilder(String.valueOf(i));
if (i % 5 == 0)
currentBuilder.append(ASTERISK);
resultVector.add(currentBuilder.toString());
}
return resultVector;
}
}
......@@ -2,7 +2,7 @@ package com.paktalin.chess.language;
class Factorial {
static int getWhileFactorial(int number) {
static int whileFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
......@@ -17,7 +17,7 @@ class Factorial {
return factorial;
}
static int getDoWhileFactorial(int number) {
static int doWhileFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
......@@ -32,7 +32,7 @@ class Factorial {
return factorial;
}
static int getForFactorial(int number) {
static int forFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
......@@ -45,7 +45,7 @@ class Factorial {
return factorial;
}
static int getBreakFactorial(int number) {
static int breakFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
......
package com.paktalin.chess.moves;
import com.paktalin.chess.Position;
/**
......@@ -11,7 +10,7 @@ public class KingMoveStrategy extends MoveStrategy{
@Override
public boolean isMovable(Position from, Position to) {
super.isMovable(from, to);
initialize(from, to);
return !(distantRows() || distantColumns());
}
......
package com.paktalin.chess.moves;
import com.paktalin.chess.Position;
/**
......@@ -8,20 +7,15 @@ import com.paktalin.chess.Position;
*/
public abstract class MoveStrategy {
int fromRow, fromColumn;
int toRow, toColumn;
private void initialize(Position from, Position to) {
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;
}
public abstract boolean isMovable(Position from, Position to);
}
......@@ -10,7 +10,7 @@ public class QueenMoveStrategy extends MoveStrategy {
@Override
public boolean isMovable(Position from, Position to) {
super.isMovable(from, to);
initialize(from, to);
return sameRow() || sameColumn() || diagonal();
}
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Bishop extends Piece {
public Bishop(Piece.Color color) {
public Bishop(Color color) {
strength = 3;
representation = 'b';
this.color = color;
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
import com.paktalin.chess.moves.KingMoveStrategy;
/**
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Knight extends Piece {
public Knight(Piece.Color color) {
public Knight(Color color) {
strength = 2.5;
representation = 'n';
this.color = color;
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
import java.util.List;
/**
......@@ -8,7 +10,7 @@ import java.util.List;
public class Pawn extends Piece {
public Pawn(Piece.Color color) {
public Pawn(Color color) {
strength = 1;
representation = 'p';
this.color = color;
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
import com.paktalin.chess.Position;
import com.paktalin.chess.moves.MoveStrategy;
......@@ -10,8 +11,6 @@ import java.util.List;
*/
public abstract class Piece implements Comparable<Piece> {
public enum Color {White, Black}
Color color;
private Position position;
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
import com.paktalin.chess.moves.QueenMoveStrategy;
/**
......@@ -8,7 +9,7 @@ import com.paktalin.chess.moves.QueenMoveStrategy;
public class Queen extends Piece {
public Queen(Piece.Color color) {
public Queen(Color color) {
strength = 9;
representation = 'q';
moveStrategy = new QueenMoveStrategy();
......
package com.paktalin.chess.pieces;
import com.paktalin.chess.Color;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Rook extends Piece{
public Rook(Piece.Color color) {
public Rook(Color color) {
strength = 5;
representation = 'r';
this.color = color;
......
package com.paktalin.chess;
package com.paktalin.chess.language;
import org.junit.jupiter.api.Test;
......
......@@ -2,17 +2,30 @@ package com.paktalin.chess.language;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Vector;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
class ContinueTest {
@Test
void testContinue(){
int n = 12;
void generateStringOneToN() {
int n = 0;
assertNull(Continue.generateStringOneToN(n));
n = 12;
String oneToN = "1 2 3 4 5* 6 7 8 9 10* 11 12";
assertEquals(oneToN, Continue.generateOneToNString(n));
assertEquals(oneToN, Continue.generateStringOneToN(n));
}
@Test
void generateVectorOneToN() {
int n = 0;
assertNull(Continue.generateStringOneToN(n));
n = 0;
assertNull(Continue.generateOneToNString(n));
n = 10;
Vector<String> oneToNVector = new Vector<>(List.of("1", "2", "3", "4", "5*", "6", "7", "8", "9", "10*"));
assertEquals(oneToNVector, Continue.generateVectorOneToN(n));
}
}
\ No newline at end of file
package com.paktalin.chess.language;
import com.paktalin.chess.language.Factorial;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
......@@ -8,42 +7,42 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class FactorialTest {
@Test
void testWhile() {
int factorial = Factorial.getWhileFactorial(5);
void whileFactorial() {
int factorial = Factorial.whileFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getWhileFactorial(4);
factorial = Factorial.whileFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getWhileFactorial(3);
factorial = Factorial.whileFactorial(3);
assertEquals(getFactorial(3), factorial);
}
@Test
void testDoWhile() {
int factorial = Factorial.getDoWhileFactorial(5);
void doWhileFactorial() {
int factorial = Factorial.doWhileFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getDoWhileFactorial(4);
factorial = Factorial.doWhileFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getDoWhileFactorial(3);
factorial = Factorial.doWhileFactorial(3);
assertEquals(getFactorial(3), factorial);
}
@Test
void testFor() {
int factorial = Factorial.getForFactorial(5);
void forFactorial() {
int factorial = Factorial.forFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getForFactorial(4);
factorial = Factorial.forFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getForFactorial(3);
factorial = Factorial.forFactorial(3);
assertEquals(getFactorial(3), factorial);
}
@Test
void testBreak() {
int factorial = Factorial.getBreakFactorial(5);
void breakFactorial() {
int factorial = Factorial.breakFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getBreakFactorial(4);
factorial = Factorial.breakFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getBreakFactorial(3);
factorial = Factorial.breakFactorial(3);
assertEquals(getFactorial(3), factorial);
}
......
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.
......@@ -11,17 +9,17 @@ import com.paktalin.chess.pieces.Piece;
class KingMoveStrategyTest extends MoveStrategyTest {
@Override
void putPieceOnBoard() {
board.placePiece(new King(Piece.Color.Black), currentPosition);
MoveStrategy moveStrategy() {
return new KingMoveStrategy();
}
@Override
void setAccessiblePosition() {
accessiblePosition = Position.create("a6");
Position accessiblePosition() {
return Position.create("a6");
}
@Override
void setNotAccessiblePosition() {
notAccessiblePosition = Position.create("d4");
Position notAccessiblePosition() {
return 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;
......@@ -14,33 +12,24 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/
abstract class MoveStrategyTest {
private Piece piece;
Board board;
Position accessiblePosition, notAccessiblePosition;
Position currentPosition = Position.create("b5");
private Position accessiblePosition, notAccessiblePosition, initialPosition;
private MoveStrategy moveStrategy;
@BeforeEach
void init() {
board = Board.createEmpty();
putPieceOnBoard();
piece = board.getPieceAtPosition(currentPosition);
setAccessiblePosition();
setNotAccessiblePosition();
initialPosition = Position.create("b5");
moveStrategy = moveStrategy();
accessiblePosition = accessiblePosition();
notAccessiblePosition = notAccessiblePosition();
}
abstract void putPieceOnBoard();
abstract void setAccessiblePosition();
abstract void setNotAccessiblePosition();
abstract MoveStrategy moveStrategy();
abstract Position accessiblePosition();
abstract Position notAccessiblePosition();
@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));
void isMovable() {
assertTrue(moveStrategy.isMovable(initialPosition, accessiblePosition));
assertFalse(moveStrategy.isMovable(initialPosition, 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.
......@@ -11,17 +9,17 @@ import com.paktalin.chess.pieces.Queen;
class QueenMoveStrategyTest extends MoveStrategyTest {
@Override
void putPieceOnBoard() {
board.placePiece(new Queen(Piece.Color.Black), currentPosition);
MoveStrategy moveStrategy() {
return new QueenMoveStrategy();
}
@Override
void setAccessiblePosition() {
accessiblePosition = Position.create("d3");
Position accessiblePosition() {
return Position.create("d3");
}
@Override
void setNotAccessiblePosition() {
notAccessiblePosition = Position.create("f7");
Position notAccessiblePosition() {
return Position.create("f7");
}
}
......@@ -30,6 +30,8 @@ abstract class PieceTest {
abstract Piece createPiece(Piece.Color color);
abstract void setExpectedStrength();
@Test
void testCreate() {
Piece whitePiece = createPiece(White);
......
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