Commit 1f52be6d by Paktalin

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

parent 0269972e
...@@ -33,7 +33,9 @@ public class Board { ...@@ -33,7 +33,9 @@ public class Board {
} }
public void move(Position from, Position to) { public void move(Position from, Position to) {
layout.move(from, to); Piece piece = layout.getPieceAtPosition(from);
if (piece.isMovable(from, to))
layout.move(from, to);
} }
public Piece getPieceAtPosition(String position) { public Piece getPieceAtPosition(String position) {
......
...@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava_exercises; ...@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava_exercises;
import android.support.annotation.NonNull; 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; import java.util.List;
/** /**
...@@ -9,6 +12,7 @@ import java.util.List; ...@@ -9,6 +12,7 @@ import java.util.List;
*/ */
public class Piece implements Comparable<Piece> { public class Piece implements Comparable<Piece> {
public enum Color {White, Black} public enum Color {White, Black}
private Type type; private Type type;
...@@ -16,6 +20,7 @@ public class Piece implements Comparable<Piece> { ...@@ -16,6 +20,7 @@ public class Piece implements Comparable<Piece> {
private double strength; private double strength;
private Position position; private Position position;
private MoveStrategy moveStrategy;
private Piece() {} private Piece() {}
...@@ -25,10 +30,22 @@ public class Piece implements Comparable<Piece> { ...@@ -25,10 +30,22 @@ public class Piece implements Comparable<Piece> {
} }
public enum Type { 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 double strength;
private char representation; 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) { Type(double strength, char representation) {
this.strength = strength; this.strength = strength;
...@@ -42,6 +59,10 @@ public class Piece implements Comparable<Piece> { ...@@ -42,6 +59,10 @@ public class Piece implements Comparable<Piece> {
public double getStrength() { public double getStrength() {
return strength; return strength;
} }
public MoveStrategy getMoveStrategy() {
return moveStrategy;
}
} }
public void setPosition(Position position) { public void setPosition(Position position) {
...@@ -90,6 +111,10 @@ public class Piece implements Comparable<Piece> { ...@@ -90,6 +111,10 @@ public class Piece implements Comparable<Piece> {
piece.getType() == currentPiece.getType(); piece.getType() == currentPiece.getType();
} }
boolean isMovable(Position from, Position to) {
return this.getType().getMoveStrategy().isMovable(from, to);
}
double getStrength() { double getStrength() {
return strength; return strength;
} }
...@@ -115,6 +140,9 @@ public class Piece implements Comparable<Piece> { ...@@ -115,6 +140,9 @@ public class Piece implements Comparable<Piece> {
boolean isType(Piece.Type type) { boolean isType(Piece.Type type) {
return this.getType() == type; return this.getType() == type;
} }
public boolean isAtPosition(Position position) {
return this.position.equals(position);
}
static Piece createPawn(Color color) { static Piece createPawn(Color color) {
return new Piece(color, Type.Pawn); return new Piece(color, Type.Pawn);
......
...@@ -47,7 +47,7 @@ public class Position { ...@@ -47,7 +47,7 @@ public class Position {
return (int)c - (int)firstColumnLetter; return (int)c - (int)firstColumnLetter;
} }
int getRow() { public int getRow() {
return row; return row;
} }
...@@ -55,7 +55,7 @@ public class Position { ...@@ -55,7 +55,7 @@ public class Position {
return column; return column;
} }
int getIntColumn() { public int getIntColumn() {
return charToInt(column); 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; package com.example.paktalin.agilejava_exercises.moves;
import com.example.paktalin.agilejava_exercises.Position;
/** /**
* Created by Paktalin on 31/05/2018. * 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; 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; import junit.framework.TestSuite;
...@@ -17,7 +17,7 @@ public class AllTests extends TestSuite { ...@@ -17,7 +17,7 @@ public class AllTests extends TestSuite {
suite.addTestSuite(BoardTest.class); suite.addTestSuite(BoardTest.class);
suite.addTestSuite(CharacterTest.class); suite.addTestSuite(CharacterTest.class);
suite.addTestSuite(PositionTest.class); suite.addTestSuite(PositionTest.class);
suite.addTestSuite(KingMovesTest.class); suite.addTestSuite(KingMoveStrategyTest.class);
return suite; return suite;
} }
......
...@@ -10,7 +10,7 @@ import junit.framework.TestCase; ...@@ -10,7 +10,7 @@ import junit.framework.TestCase;
* Created by Paktalin on 31/05/2018. * Created by Paktalin on 31/05/2018.
*/ */
public class KingMovesTest extends TestCase { public class KingMoveStrategyTest extends TestCase {
private Piece king; private Piece king;
private Board board; private Board board;
...@@ -22,10 +22,20 @@ public class KingMovesTest extends TestCase { ...@@ -22,10 +22,20 @@ public class KingMovesTest extends TestCase {
king = board.getPieceAtPosition("b5"); king = board.getPieceAtPosition("b5");
} }
public void testCreate() { public void testMove() {
assertEquals("b5", king.getPosition().toString()); Position currentPosition = king.getPosition();
board.move(Position.create("b5"), Position.create("a6")); assertEquals("b5", currentPosition.toString());
assertEquals("a6", king.getPosition().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