Commit 43c0cc25 by Paktalin

Fixed bug with Pawn strength + refactoring in tests

parent 125aa959
......@@ -26,7 +26,7 @@ public class Pawn extends Piece {
private static boolean pawnsOnSameColumn(Piece currentPiece, Piece piece) {
return sameColumn(currentPiece, piece) &&
sameRow(currentPiece, piece) &&
notSameRow(currentPiece, piece) &&
piece.getClass() == Pawn.class;
}
......@@ -34,7 +34,7 @@ public class Pawn extends Piece {
return piece2.getPosition().getColumn() == piece1.getPosition().getColumn();
}
private static boolean sameRow(Piece piece1, Piece piece2) {
return piece2.getPosition().getRow() == piece1.getPosition().getRow();
private static boolean notSameRow(Piece piece1, Piece piece2) {
return piece2.getPosition().getRow() != piece1.getPosition().getRow();
}
}
package com.example.paktalin.agilejava_exercises.pieces;
import com.example.paktalin.agilejava_exercises.Position;
import com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy;
import com.example.paktalin.agilejava_exercises.moves.QueenMoveStrategy;
/**
......
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.moves.*;
import com.example.paktalin.agilejava_exercises.pieces.KingTest;
import com.example.paktalin.agilejava_exercises.pieces.PawnTest;
import junit.framework.TestSuite;
......@@ -13,13 +15,15 @@ public class AllTests extends TestSuite {
public static TestSuite suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(PieceTest.class);
suite.addTestSuite(BoardTest.class);
suite.addTestSuite(CharacterTest.class);
suite.addTestSuite(PositionTest.class);
suite.addTestSuite(KingMoveStrategyTest.class);
suite.addTestSuite(QueenMoveStrategyTest.class);
suite.addTestSuite(KingTest.class);
suite.addTestSuite(PawnTest.class);
return suite;
}
}
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.pieces.Bishop;
import com.example.paktalin.agilejava_exercises.pieces.King;
import com.example.paktalin.agilejava_exercises.pieces.Knight;
import com.example.paktalin.agilejava_exercises.pieces.Pawn;
import com.example.paktalin.agilejava_exercises.pieces.Piece;
import com.example.paktalin.agilejava_exercises.pieces.Queen;
import com.example.paktalin.agilejava_exercises.pieces.Rook;
import junit.framework.TestCase;
import static com.example.paktalin.agilejava_exercises.pieces.Piece.Color.*;
/**
* Created by Paktalin on 23/05/2018.
*/
public class PieceTest extends TestCase {
public void testCreate() {
verifyCreation(new Pawn(White), new Pawn(Black), 'p');
verifyCreation(new King(White), new King(Black), 'k');
verifyCreation(new Bishop(White), new Bishop(Black), 'b');
verifyCreation(new Rook(White), new Rook(Black), 'r');
verifyCreation(new Knight(White), new Knight(Black), 'n');
verifyCreation(new Queen(White), new Queen(Black), 'q');
}
private void verifyCreation(Piece whitePiece, Piece blackPiece, char representation) {
assertTrue(whitePiece.isWhite());
assertEquals(representation, whitePiece.getRepresentation());
assertTrue(blackPiece.isBlack());
assertEquals(Character.toUpperCase(representation), blackPiece.getRepresentation());
}
public void testStrength() {
Board board = Board.createEmpty();
board.placePiece(new Pawn(Black), "a7");
assertEquals(1.0, board.getPieceAtPosition("a7").getStrength());
board.placePiece(new Pawn(Black), "a6");
assertEquals(0.5, board.getPieceAtPosition("a6").getStrength());
}
}
package com.example.paktalin.agilejava_exercises.pieces;
/**
* Created by Paktalin on 01/06/2018.
*/
public class KingTest extends PieceTest {
@Override
void setExpectedRepresentation() {
expectedRepresentation = 'k';
}
@Override
Piece createPiece(Piece.Color color) {
return new King(color);
}
@Override
void setExpectedStrength() {
expectedStrength = 0;
}
}
package com.example.paktalin.agilejava_exercises.pieces;
import static com.example.paktalin.agilejava_exercises.pieces.Piece.Color.Black;
/**
* Created by Paktalin on 01/06/2018.
*/
public class PawnTest extends PieceTest {
@Override
void setExpectedRepresentation() {
expectedRepresentation = 'p';
}
@Override
Piece createPiece(Piece.Color color) {
return new Pawn(color);
}
@Override
void setExpectedStrength() {
expectedStrength = 1;
}
@Override
public void testStrength() {
super.testStrength();
expectedStrength = 0.5;
board.placePiece(createPiece(Black), "a6");
assertEquals(expectedStrength, board.getPieceAtPosition("a6").getStrength());
assertEquals(expectedStrength, board.getPieceAtPosition("a7").getStrength());
}
}
package com.example.paktalin.agilejava_exercises.pieces;
import com.example.paktalin.agilejava_exercises.Board;
import com.example.paktalin.agilejava_exercises.Position;
import junit.framework.TestCase;
import static com.example.paktalin.agilejava_exercises.pieces.Piece.Color.*;
/**
* Created by Paktalin on 23/05/2018.
*/
public abstract class PieceTest extends TestCase {
char expectedRepresentation;
double expectedStrength;
private Position position = Position.create("a7");
Board board;
@Override
protected void setUp() throws Exception {
setExpectedRepresentation();
setExpectedStrength();
}
abstract void setExpectedRepresentation();
abstract Piece createPiece(Piece.Color color);
abstract void setExpectedStrength();
public void testCreate() {
Piece whitePiece = createPiece(White);
Piece blackPiece = createPiece(Black);
assertTrue(whitePiece.isWhite());
assertEquals(expectedRepresentation, whitePiece.getRepresentation());
assertTrue(blackPiece.isBlack());
assertEquals(getBlackRepresentation(), blackPiece.getRepresentation());
}
private char getBlackRepresentation() {
return Character.toUpperCase(expectedRepresentation);
}
public void testStrength() {
board = Board.createEmpty();
board.placePiece(createPiece(Black), position);
assertEquals(expectedStrength, board.getPieceAtPosition(position).getStrength());
}
}
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