Commit 1f52be6d by Paktalin

Exercise 6.6 done. Now you can move a king, but only to accessible position

parent 0269972e
......@@ -33,6 +33,8 @@ public class Board {
}
public void move(Position from, Position to) {
Piece piece = layout.getPieceAtPosition(from);
if (piece.isMovable(from, to))
layout.move(from, to);
}
......
......@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava_exercises;
import android.support.annotation.NonNull;
import com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy;
import com.example.paktalin.agilejava_exercises.moves.MoveStrategy;
import java.util.List;
/**
......@@ -9,6 +12,7 @@ import java.util.List;
*/
public class Piece implements Comparable<Piece> {
public enum Color {White, Black}
private Type type;
......@@ -16,6 +20,7 @@ public class Piece implements Comparable<Piece> {
private double strength;
private Position position;
private MoveStrategy moveStrategy;
private Piece() {}
......@@ -25,10 +30,22 @@ public class Piece implements Comparable<Piece> {
}
public enum Type {
Pawn(1, 'p'), Knight(2.5, 'n'), Rook(5, 'r'), Bishop(3, 'b'), Queen(9, 'q'), King(0, 'k');
Pawn(1, 'p'),
Knight(2.5, 'n'),
Rook(5, 'r'),
Bishop(3, 'b'),
Queen(9, 'q'),
King(0, 'k', new KingMoveStrategy());
private double strength;
private char representation;
private MoveStrategy moveStrategy;
Type(double strength, char representation, MoveStrategy moveStrategy) {
this.strength = strength;
this.representation = representation;
this.moveStrategy = moveStrategy;
}
Type(double strength, char representation) {
this.strength = strength;
......@@ -42,6 +59,10 @@ public class Piece implements Comparable<Piece> {
public double getStrength() {
return strength;
}
public MoveStrategy getMoveStrategy() {
return moveStrategy;
}
}
public void setPosition(Position position) {
......@@ -90,6 +111,10 @@ public class Piece implements Comparable<Piece> {
piece.getType() == currentPiece.getType();
}
boolean isMovable(Position from, Position to) {
return this.getType().getMoveStrategy().isMovable(from, to);
}
double getStrength() {
return strength;
}
......@@ -115,6 +140,9 @@ public class Piece implements Comparable<Piece> {
boolean isType(Piece.Type type) {
return this.getType() == type;
}
public boolean isAtPosition(Position position) {
return this.position.equals(position);
}
static Piece createPawn(Color color) {
return new Piece(color, Type.Pawn);
......
......@@ -47,7 +47,7 @@ public class Position {
return (int)c - (int)firstColumnLetter;
}
int getRow() {
public int getRow() {
return row;
}
......@@ -55,7 +55,7 @@ public class Position {
return column;
}
int getIntColumn() {
public int getIntColumn() {
return charToInt(column);
}
}
package com.example.paktalin.agilejava_exercises.moves;
import com.example.paktalin.agilejava_exercises.Position;
/**
* Created by Paktalin on 31/05/2018.
*/
public class KingMoveStrategy implements MoveStrategy{
@Override
public boolean isMovable(Position from, Position to) {
int fromRow = from.getRow();
int fromColumn = from.getIntColumn();
int toRow = to.getRow();
int toColumn = to.getIntColumn();
if ((Math.abs(toRow - fromRow) > 1) || (Math.abs(toColumn - fromColumn) > 1))
return false;
return true;
}
}
package com.example.paktalin.agilejava_exercises.moves;
import com.example.paktalin.agilejava_exercises.Piece;
import com.example.paktalin.agilejava_exercises.Position;
import java.util.Map;
/**
* Created by Paktalin on 31/05/2018.
*/
public class KingMoves {
}
package com.example.paktalin.agilejava_exercises.moves;
import com.example.paktalin.agilejava_exercises.Position;
/**
* Created by Paktalin on 31/05/2018.
*/
public abstract class Moves {
public interface MoveStrategy {
boolean isMovable(Position from, Position to);
}
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.moves.KingMovesTest;
import com.example.paktalin.agilejava_exercises.moves.KingMoveStrategyTest;
import junit.framework.TestSuite;
......@@ -17,7 +17,7 @@ public class AllTests extends TestSuite {
suite.addTestSuite(BoardTest.class);
suite.addTestSuite(CharacterTest.class);
suite.addTestSuite(PositionTest.class);
suite.addTestSuite(KingMovesTest.class);
suite.addTestSuite(KingMoveStrategyTest.class);
return suite;
}
......
......@@ -10,7 +10,7 @@ import junit.framework.TestCase;
* Created by Paktalin on 31/05/2018.
*/
public class KingMovesTest extends TestCase {
public class KingMoveStrategyTest extends TestCase {
private Piece king;
private Board board;
......@@ -22,10 +22,20 @@ public class KingMovesTest extends TestCase {
king = board.getPieceAtPosition("b5");
}
public void testCreate() {
assertEquals("b5", king.getPosition().toString());
board.move(Position.create("b5"), Position.create("a6"));
assertEquals("a6", king.getPosition().toString());
public void testMove() {
Position currentPosition = king.getPosition();
assertEquals("b5", currentPosition.toString());
Position accessiblePosition = Position.create("a6");
board.move(currentPosition, accessiblePosition);
assertTrue(king.isAtPosition(accessiblePosition));
assertFalse(king.isAtPosition(currentPosition));
currentPosition = accessiblePosition;
Position notAccessiblePosition = Position.create("d4");
board.move(currentPosition, notAccessiblePosition);
assertTrue(king.isAtPosition(currentPosition));
assertFalse(king.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