Commit 90aeec8c by Paktalin

Refactoring + completed 7.3.a

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