Commit 0269972e by Paktalin

Can move a piece

parent a86e112e
......@@ -8,7 +8,7 @@ import static com.example.paktalin.agilejava_exercises.Piece.Color.*;
* Created by Paktalin on 23/05/2018.
*/
class Board {
public class Board {
private BoardLayout layout;
private Side whiteSide = new Side();
......@@ -16,7 +16,7 @@ class Board {
private Board() {}
static Board createEmpty() {
public static Board createEmpty() {
Board board = new Board();
board.layout = BoardLayout.create(board);
return board;
......@@ -28,11 +28,15 @@ class Board {
return board;
}
void placePiece(Piece piece, String position) {
public void placePiece(Piece piece, String position) {
layout.placePiece(piece, Position.create(position));
}
Piece getPieceAtPosition(String position) {
public void move(Position from, Position to) {
layout.move(from, to);
}
public Piece getPieceAtPosition(String position) {
return layout.getPieceAtPosition(Position.create(position));
}
......
......@@ -53,6 +53,12 @@ class BoardLayout {
pieces[position.getRow()][position.getIntColumn()] = piece;
}
void move(Position from, Position to) {
Piece piece = getPieceAtPosition(from);
pieces[from.getRow()][from.getIntColumn()] = null;
placePiece(piece, to);
}
Piece getPieceAtPosition(Position position) {
return pieces[position.getRow()][position.getIntColumn()];
}
......
......@@ -8,7 +8,7 @@ import java.util.List;
* Created by Paktalin on 23/05/2018.
*/
public class Piece implements Comparable<Piece>{
public class Piece implements Comparable<Piece> {
public enum Color {White, Black}
private Type type;
......@@ -25,7 +25,6 @@ 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');
private double strength;
......@@ -45,14 +44,10 @@ public class Piece implements Comparable<Piece>{
}
}
void setPosition(Position position) {
public void setPosition(Position position) {
this.position = position;
}
Position getPosition() {
return position;
}
char getRepresentation() {
char representation = this.type.getRepresentation();
if (this.isBlack())
......@@ -67,7 +62,7 @@ public class Piece implements Comparable<Piece>{
void setStrength(List<Piece> pieces) {
if (this.type != Type.Pawn)
strength = this.type.getStrength();
else Position.setPawnStrength(this, pieces);
else setPawnStrength(this, pieces);
}
@Override
......@@ -79,6 +74,22 @@ public class Piece implements Comparable<Piece>{
return 0;
}
private static void setPawnStrength(Piece currentPiece, List<Piece> pieces) {
currentPiece.setStrength(1.0);
for (Piece piece : pieces) {
if (pawnsOnSameColumn(currentPiece, piece)) {
piece.setStrength(0.5);
currentPiece.setStrength(0.5);
}
}
}
private static boolean pawnsOnSameColumn(Piece currentPiece, Piece piece) {
return piece.getPosition().getColumn() == currentPiece.getPosition().getColumn() &&
piece.getPosition().getRow() != currentPiece.getPosition().getRow() &&
piece.getType() == currentPiece.getType();
}
double getStrength() {
return strength;
}
......@@ -88,6 +99,9 @@ public class Piece implements Comparable<Piece>{
Type getType() {
return type;
}
public Position getPosition() {
return position;
}
boolean isColor(Color color) {
return this.color == color;
......@@ -105,7 +119,7 @@ public class Piece implements Comparable<Piece>{
static Piece createPawn(Color color) {
return new Piece(color, Type.Pawn);
}
static Piece createKing(Color color) {
public static Piece createKing(Color color) {
return new Piece(color, Type.King);
}
static Piece createBishop(Color color) {
......@@ -120,4 +134,4 @@ public class Piece implements Comparable<Piece>{
static Piece createQueen(Color color) {
return new Piece(color, Type.Queen);
}
}
}
\ No newline at end of file
package com.example.paktalin.agilejava_exercises;
import java.util.List;
/**
* Created by Paktalin on 31/05/2018.
*/
class Position {
public class Position {
private int row;
private char column;
......@@ -18,11 +16,11 @@ class Position {
this.column = column;
}
static Position create(String positionString) {
public static Position create(String positionString) {
return create(retrieveRow(positionString), retrieveColumn(positionString));
}
static Position create(int row, int column) {
public static Position create(int row, int column) {
return new Position(row, intToChar(column));
}
......@@ -60,20 +58,4 @@ class Position {
int getIntColumn() {
return charToInt(column);
}
static void setPawnStrength(Piece currentPiece, List<Piece> pieces) {
currentPiece.setStrength(1.0);
for (Piece piece : pieces) {
if (pawnsOnSameColumn(currentPiece, piece)) {
piece.setStrength(0.5);
currentPiece.setStrength(0.5);
}
}
}
private static boolean pawnsOnSameColumn(Piece currentPiece, Piece piece) {
return piece.getPosition().getColumn() == currentPiece.getPosition().getColumn() &&
piece.getPosition().getRow() != currentPiece.getPosition().getRow() &&
piece.getType() == currentPiece.getType();
}
}
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;
/**
* Created by Paktalin on 31/05/2018.
*/
public abstract class Moves {
}
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.moves.KingMovesTest;
import junit.framework.TestSuite;
/**
......@@ -15,6 +17,7 @@ public class AllTests extends TestSuite {
suite.addTestSuite(BoardTest.class);
suite.addTestSuite(CharacterTest.class);
suite.addTestSuite(PositionTest.class);
suite.addTestSuite(KingMovesTest.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 KingMovesTest extends TestCase {
private Piece king;
private Board board;
@Override
protected void setUp() throws Exception {
board = Board.createEmpty();
board.placePiece(Piece.createKing(Piece.Color.Black), "b5");
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());
}
}
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