Commit fc9af304 by Paktalin

Queen move strategy added

parent 1f52be6d
......@@ -4,6 +4,7 @@ import android.support.annotation.NonNull;
import com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy;
import com.example.paktalin.agilejava_exercises.moves.MoveStrategy;
import com.example.paktalin.agilejava_exercises.moves.QueenMoveStrategy;
import java.util.List;
......@@ -34,7 +35,7 @@ public class Piece implements Comparable<Piece> {
Knight(2.5, 'n'),
Rook(5, 'r'),
Bishop(3, 'b'),
Queen(9, 'q'),
Queen(9, 'q', new QueenMoveStrategy()),
King(0, 'k', new KingMoveStrategy());
private double strength;
......@@ -159,7 +160,7 @@ public class Piece implements Comparable<Piece> {
static Piece createKnight(Color color) {
return new Piece(color, Type.Knight);
}
static Piece createQueen(Color color) {
public static Piece createQueen(Color color) {
return new Piece(color, Type.Queen);
}
}
\ No newline at end of file
......@@ -6,18 +6,19 @@ import com.example.paktalin.agilejava_exercises.Position;
* Created by Paktalin on 31/05/2018.
*/
public class KingMoveStrategy implements MoveStrategy{
public class KingMoveStrategy extends MoveStrategy{
@Override
public boolean isMovable(Position from, Position to) {
int fromRow = from.getRow();
int fromColumn = from.getIntColumn();
super.isMovable(from, to);
return !(distantRows() || distantColumns());
}
int toRow = to.getRow();
int toColumn = to.getIntColumn();
private boolean distantRows() {
return Math.abs(toRow - fromRow) > 1;
}
if ((Math.abs(toRow - fromRow) > 1) || (Math.abs(toColumn - fromColumn) > 1))
return false;
return true;
private boolean distantColumns() {
return Math.abs(toColumn - fromColumn) > 1;
}
}
......@@ -6,8 +6,21 @@ import com.example.paktalin.agilejava_exercises.Position;
* Created by Paktalin on 31/05/2018.
*/
public interface MoveStrategy {
public abstract class MoveStrategy {
boolean isMovable(Position from, Position to);
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.example.paktalin.agilejava_exercises.moves;
import com.example.paktalin.agilejava_exercises.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.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.moves.KingMoveStrategyTest;
import com.example.paktalin.agilejava_exercises.moves.*;
import junit.framework.TestSuite;
......@@ -18,6 +18,7 @@ public class AllTests extends TestSuite {
suite.addTestSuite(CharacterTest.class);
suite.addTestSuite(PositionTest.class);
suite.addTestSuite(KingMoveStrategyTest.class);
suite.addTestSuite(QueenMoveStrategyTest.class);
return suite;
}
......
package com.example.paktalin.agilejava_exercises.moves;
import com.example.paktalin.agilejava_exercises.Board;
import com.example.paktalin.agilejava_exercises.Piece;
import com.example.paktalin.agilejava_exercises.Position;
import junit.framework.TestCase;
/**
* Created by Paktalin on 31/05/2018.
*/
public class QueenMoveStrategyTest extends TestCase {
private Piece queen;
private Board board;
@Override
protected void setUp() throws Exception {
board = Board.createEmpty();
board.placePiece(Piece.createQueen(Piece.Color.Black), "b5");
queen = board.getPieceAtPosition("b5");
}
public void testMove() {
Position currentPosition = queen.getPosition();
Position accessiblePosition = Position.create("d3");
board.move(currentPosition, accessiblePosition);
assertTrue(queen.isAtPosition(accessiblePosition));
assertFalse(queen.isAtPosition(currentPosition));
currentPosition = accessiblePosition;
Position notAccessiblePosition = Position.create("f7");
board.move(currentPosition, notAccessiblePosition);
assertTrue(queen.isAtPosition(currentPosition));
assertFalse(queen.isAtPosition(notAccessiblePosition));
}
}
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