Commit 75f9d515 by Paktalin

Created different piece classes

parent cb3f5fad
Showing with 265 additions and 171 deletions
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.pieces.Piece;
import java.util.List;
import static com.example.paktalin.agilejava_exercises.Piece.Color.*;
import static com.example.paktalin.agilejava_exercises.pieces.Piece.Color.*;
/**
* Created by Paktalin on 23/05/2018.
......@@ -80,7 +82,4 @@ public class Board {
int getPiecesCount(Piece.Color color){
return getSide(color).getPiecesCount();
}
int getPiecesCount(Piece.Color color, Piece.Type type){
return getSide(color).getPiecesCount(type);
}
}
\ No newline at end of file
package com.example.paktalin.agilejava_exercises;
import static com.example.paktalin.agilejava_exercises.Piece.Color.Black;
import static com.example.paktalin.agilejava_exercises.Piece.Color.White;
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 static com.example.paktalin.agilejava_exercises.pieces.Piece.Color.Black;
import static com.example.paktalin.agilejava_exercises.pieces.Piece.Color.White;
import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
/**
......@@ -31,19 +39,19 @@ class BoardLayout {
}
private void initializeKingRank(Piece.Color color, int row) {
placePiece(Piece.createRook(color), Position.create(row, 0));
placePiece(Piece.createKnight(color), Position.create(row, 1));
placePiece(Piece.createBishop(color), Position.create(row, 2));
placePiece(Piece.createQueen(color), Position.create(row, 3));
placePiece(Piece.createKing(color), Position.create(row, 4));
placePiece(Piece.createBishop(color), Position.create(row, 5));
placePiece(Piece.createKnight(color), Position.create(row, 6));
placePiece(Piece.createRook(color), Position.create(row, 7));
placePiece(new Rook(color), Position.create(row, 0));
placePiece(new Knight(color), Position.create(row, 1));
placePiece(new Bishop(color), Position.create(row, 2));
placePiece(new Queen(color), Position.create(row, 3));
placePiece(new King(color), Position.create(row, 4));
placePiece(new Bishop(color), Position.create(row, 5));
placePiece(new Knight(color), Position.create(row, 6));
placePiece(new Rook(color), Position.create(row, 7));
}
private void initializePawnRank(Piece.Color color, int row) {
for (int column = 0; column < COLUMN_COUNT; column++)
placePiece(Piece.createPawn(color), Position.create(row, column));
placePiece(new Pawn(color), Position.create(row, column));
}
void placePiece(Piece piece, Position position) {
......
package com.example.paktalin.agilejava_exercises;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Game {
}
......@@ -51,7 +51,7 @@ public class Position {
return row;
}
char getColumn() {
public char getColumn() {
return column;
}
......
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.pieces.Piece;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
......@@ -26,16 +28,6 @@ class Side {
return pieces.size();
}
int getPiecesCount(Piece.Type type) {
int count = 0;
for (Piece piece : pieces)
if (piece.isType(type))
count++;
return count;
}
void addPiece(Piece piece) {
pieces.add(piece);
Collections.sort(pieces);
......
package com.example.paktalin.agilejava_exercises.pieces;
import com.example.paktalin.agilejava_exercises.Position;
import com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Bishop extends Piece {
public Bishop(Piece.Color color) {
strength = 0;
representation = 'k';
moveStrategy = new KingMoveStrategy();
this.color = color;
}
@Override
public boolean isMovable(Position from, Position to) {
return false;
}
}
package com.example.paktalin.agilejava_exercises.pieces;
import com.example.paktalin.agilejava_exercises.Position;
import com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy;
/**
* Created by Paktalin on 01/06/2018.
*/
public class King extends Piece {
public King(Color color) {
strength = 0;
representation = 'k';
moveStrategy = new KingMoveStrategy();
this.color = color;
}
@Override
public boolean isMovable(Position from, Position to) {
return (new KingMoveStrategy()).isMovable(from, to);
}
}
package com.example.paktalin.agilejava_exercises.pieces;
import com.example.paktalin.agilejava_exercises.Position;
import com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Knight extends Piece {
public Knight(Piece.Color color) {
strength = 0;
representation = 'k';
moveStrategy = new KingMoveStrategy();
this.color = color;
}
@Override
public boolean isMovable(Position from, Position to) {
return false;
}
}
package com.example.paktalin.agilejava_exercises.pieces;
import com.example.paktalin.agilejava_exercises.Position;
import com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy;
import java.util.List;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Pawn extends Piece {
public Pawn(Piece.Color color) {
strength = 0;
representation = 'k';
moveStrategy = new KingMoveStrategy();
this.color = color;
}
public void setStrength(List<Piece> pieces) {
this.setStrength(1.0);
for (Piece piece : pieces) {
if (pawnsOnSameColumn(this, piece)) {
piece.setStrength(0.5);
this.setStrength(0.5);
}
}
}
@Override
public boolean isMovable(Position from, Position to) {
return false;
}
private static boolean pawnsOnSameColumn(Piece currentPiece, Piece piece) {
return piece.getPosition().getColumn() == currentPiece.getPosition().getColumn() &&
piece.getPosition().getRow() != currentPiece.getPosition().getRow() &&
piece.getClass() == currentPiece.getClass();
}
}
package com.example.paktalin.agilejava_exercises;
package com.example.paktalin.agilejava_exercises.pieces;
import android.support.annotation.NonNull;
import com.example.paktalin.agilejava_exercises.Position;
import com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy;
import com.example.paktalin.agilejava_exercises.moves.MoveStrategy;
import com.example.paktalin.agilejava_exercises.moves.QueenMoveStrategy;
......@@ -12,31 +13,24 @@ import java.util.List;
* Created by Paktalin on 23/05/2018.
*/
public class Piece implements Comparable<Piece> {
public abstract class Piece implements Comparable<Piece> {
public enum Color {White, Black}
Color color;
private Type type;
private Color color;
double strength;
Position position;
MoveStrategy moveStrategy;
char representation;
private double strength;
private Position position;
private MoveStrategy moveStrategy;
private Piece() {}
private Piece(final Color color, final Type type) {
this.color = color;
this.type = type;
}
Piece() {}
public enum Type {
Pawn(1, 'p'),
Knight(2.5, 'n'),
Rook(5, 'r'),
Bishop(3, 'b'),
Queen(9, 'q', new QueenMoveStrategy()),
King(0, 'k', new KingMoveStrategy());
Queen(9, 'q', new QueenMoveStrategy());
private double strength;
private char representation;
......@@ -70,21 +64,14 @@ public class Piece implements Comparable<Piece> {
this.position = position;
}
char getRepresentation() {
char representation = this.type.getRepresentation();
if (this.isBlack())
return Character.toUpperCase(representation);
public char getRepresentation() {
return representation;
}
void setStrength(double strength) {
this.strength = strength;
}
public void setStrength(List<Piece> pieces) {}
void setStrength(List<Piece> pieces) {
if (this.type != Type.Pawn)
strength = this.type.getStrength();
else setPawnStrength(this, pieces);
void setStrength(double strength){
this.strength = strength;
}
@Override
......@@ -96,35 +83,14 @@ 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);
}
}
}
public abstract boolean isMovable(Position from, Position to);
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();
}
boolean isMovable(Position from, Position to) {
return this.getType().getMoveStrategy().isMovable(from, to);
}
double getStrength() {
return strength;
public double getStrength() {
return this.strength;
}
public Color getColor() {
return color;
}
Type getType() {
return type;
}
public Position getPosition() {
return position;
}
......@@ -132,35 +98,13 @@ public class Piece implements Comparable<Piece> {
boolean isColor(Color color) {
return this.color == color;
}
boolean isWhite() {
public boolean isWhite() {
return isColor(Color.White);
}
boolean isBlack() {
public boolean isBlack() {
return isColor(Color.Black);
}
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);
}
public static Piece createKing(Color color) {
return new Piece(color, Type.King);
}
static Piece createBishop(Color color) {
return new Piece(color, Type.Bishop);
}
static Piece createRook(Color color) {
return new Piece(color, Type.Rook);
}
static Piece createKnight(Color color) {
return new Piece(color, Type.Knight);
}
public static Piece createQueen(Color color) {
return new Piece(color, Type.Queen);
}
}
\ No newline at end of file
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;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Queen extends Piece {
public Queen(Piece.Color color) {
strength = 0;
representation = 'k';
moveStrategy = new KingMoveStrategy();
this.color = color;
}
@Override
public boolean isMovable(Position from, Position to) {
return (new QueenMoveStrategy()).isMovable(from, to);
}
}
package com.example.paktalin.agilejava_exercises.pieces;
import com.example.paktalin.agilejava_exercises.Position;
import com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Rook extends Piece{
public Rook(Piece.Color color) {
strength = 0;
representation = 'k';
moveStrategy = new KingMoveStrategy();
this.color = color;
}
@Override
public boolean isMovable(Position from, Position to) {
return false;
}
}
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 java.util.List;
import static com.example.paktalin.agilejava_exercises.Piece.Color.*;
import static com.example.paktalin.agilejava_exercises.Piece.Type.*;
import static com.example.paktalin.agilejava_exercises.pieces.Piece.Color.*;
import static com.example.paktalin.agilejava_exercises.pieces.Piece.Type.*;
/**
* Created by Paktalin on 23/05/2018.
......@@ -25,37 +33,36 @@ public class BoardTest extends TestCase {
public void testPlacePieces() {
board = Board.createInitialized();
board.placePiece(Piece.createBishop(Black), "a8");
board.placePiece(new Bishop(Black), "a8");
Piece bishop = board.getPieceAtPosition("a8");
assertEquals(Bishop, bishop.getType());
assertEquals(Black, bishop.getColor());
}
public void testOverallStrength() {
board = Board.createEmpty();
verifyStrength(Piece.createQueen(Black), "e6", 9.0, 0.0, 9.0);
verifyStrength(Piece.createQueen(White), "g4", 9.0, 9.0, 9.0);
verifyStrength(new Queen(Black), "e6", 9.0, 0.0, 9.0);
verifyStrength(new Queen(White), "g4", 9.0, 9.0, 9.0);
verifyStrength(Piece.createKing(Black), "b8", 9.0, 9.0, 0.0);
verifyStrength(Piece.createKing(White), "f1", 9.0, 9.0, 0.0);
verifyStrength(new King(Black), "b8", 9.0, 9.0, 0.0);
verifyStrength(new King(White), "f1", 9.0, 9.0, 0.0);
verifyStrength(Piece.createRook(Black), "c8", 14.0, 9.0, 5.0);
verifyStrength(Piece.createRook(White), "e1", 14.0, 14.0, 5.0);
verifyStrength(new Rook(Black), "c8", 14.0, 9.0, 5.0);
verifyStrength(new Rook(White), "e1", 14.0, 14.0, 5.0);
verifyStrength(Piece.createPawn(Black), "a7", 15.0, 14.0, 1.0);
verifyStrength(Piece.createPawn(White), "f2", 15.0, 15.0, 1.0);
verifyStrength(new Pawn(Black), "a7", 15.0, 14.0, 1.0);
verifyStrength(new Pawn(White), "f2", 15.0, 15.0, 1.0);
verifyStrength(Piece.createPawn(Black), "c7", 16.0, 15.0, 1.0);
verifyStrength(Piece.createPawn(White), "g2", 16.0, 16.0, 1.0);
verifyStrength(new Pawn(Black), "c7", 16.0, 15.0, 1.0);
verifyStrength(new Pawn(White), "g2", 16.0, 16.0, 1.0);
verifyStrength(Piece.createBishop(Black), "d7", 19.0, 16.0, 3.0);
verifyStrength(Piece.createPawn(White), "f3", 19.0, 16.0, 0.5);
verifyStrength(new Bishop(Black), "d7", 19.0, 16.0, 3.0);
verifyStrength(new Pawn(White), "f3", 19.0, 16.0, 0.5);
verifyStrength(Piece.createPawn(Black), "b6", 20.0, 16.0, 1.0);
verifyStrength(Piece.createPawn(White), "h3", 20.0, 17.0, 1.0);
verifyStrength(new Pawn(Black), "b6", 20.0, 16.0, 1.0);
verifyStrength(new Pawn(White), "h3", 20.0, 17.0, 1.0);
verifyStrength(Piece.createKnight(White), "f4", 20.0, 19.5, 2.5);
verifyStrength(new Knight(White), "f4", 20.0, 19.5, 2.5);
verifyPrint();
}
......
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.Piece.Type.*;
import static com.example.paktalin.agilejava_exercises.Piece.Color.*;
import static com.example.paktalin.agilejava_exercises.pieces.Piece.Type.*;
import static com.example.paktalin.agilejava_exercises.pieces.Piece.Color.*;
/**
* Created by Paktalin on 23/05/2018.
......@@ -12,30 +20,28 @@ import static com.example.paktalin.agilejava_exercises.Piece.Color.*;
public class PieceTest extends TestCase {
public void testCreate() {
verifyCreation(Piece.createPawn(White), Piece.createPawn(Black), Pawn, 'p');
verifyCreation(Piece.createKing(White), Piece.createKing(Black), King, 'k');
verifyCreation(Piece.createBishop(White), Piece.createBishop(Black), Bishop, 'b');
verifyCreation(Piece.createRook(White), Piece.createRook(Black), Rook, 'r');
verifyCreation(Piece.createKnight(White), Piece.createKnight(Black), Knight, 'n');
verifyCreation(Piece.createQueen(White), Piece.createQueen(Black), Queen, 'q');
verifyCreation(new Pawn(White), new Pawn(Black), Pawn, 'p');
//verifyCreation(new King(White), new King(Black), King, 'k');
verifyCreation(new Bishop(White), new Bishop(Black), Bishop, 'b');
verifyCreation(new Rook(White), new Rook(Black), Rook, 'r');
verifyCreation(new Knight(White), new Knight(Black), Knight, 'n');
verifyCreation(new Queen(White), new Queen(Black), Queen, 'q');
}
private void verifyCreation(Piece whitePiece, Piece blackPiece, Piece.Type type, char representation) {
assertTrue(whitePiece.isWhite());
assertEquals(type, whitePiece.getType());
assertEquals(representation, whitePiece.getRepresentation());
assertTrue(blackPiece.isBlack());
assertEquals(type, blackPiece.getType());
assertEquals(Character.toUpperCase(representation), blackPiece.getRepresentation());
}
public void testStrength() {
Board board = Board.createEmpty();
board.placePiece(Piece.createPawn(Black), "a7");
board.placePiece(new Pawn(Black), "a7");
assertEquals(1.0, board.getPieceAtPosition("a7").getStrength());
board.placePiece(Piece.createPawn(Black), "a6");
board.placePiece(new Pawn(Black), "a6");
assertEquals(0.5, board.getPieceAtPosition("a6").getStrength());
}
......
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.pieces.Bishop;
import com.example.paktalin.agilejava_exercises.pieces.Pawn;
import com.example.paktalin.agilejava_exercises.pieces.Piece;
import junit.framework.TestCase;
import java.util.List;
......@@ -24,9 +28,9 @@ public class PositionTest extends TestCase {
}
public void testStrengthByPosition() {
verifyStrength(Piece.createBishop(Piece.Color.Black), "b5", 3.0);
verifyStrength(Piece.createPawn(Piece.Color.White), "a2", 1.0);
verifyStrength(Piece.createPawn(Piece.Color.White), "a3", 0.5);
verifyStrength(new Bishop(Piece.Color.Black), "b5", 3.0);
verifyStrength(new Pawn(Piece.Color.White), "a2", 1.0);
verifyStrength(new Pawn(Piece.Color.White), "a3", 0.5);
assertEquals(0.5, board.getPieceAtPosition("a2").getStrength());
}
......@@ -35,33 +39,4 @@ public class PositionTest extends TestCase {
piece = board.getPieceAtPosition(position);
assertEquals(strength, piece.getStrength());
}
public void testSetPawnStrength() {
board.placePiece(Piece.createPawn(Piece.Color.Black), "a6");
board.placePiece(Piece.createKnight(Piece.Color.Black), "a5");
board.placePiece(Piece.createPawn(Piece.Color.Black), "a4");
Piece currentPiece = board.getPieceAtPosition("a4");
char column = currentPiece.getPosition().getColumn();
assertEquals('a', column);
int row = currentPiece.getPosition().getRow();
assertEquals(3, row);
List<Piece> pieces = board.getPieces(Piece.Color.Black);
Piece secondPiece = pieces.get(1);
//assertTrue((secondPiece.getPosition().getColumn() == column) && (secondPiece.getPosition().getRow() != row));
for (Piece piece : board.getPieces(Piece.Color.Black)) {
Position position = piece.getPosition();
if ((position.getColumn() == column) && (position.getRow() != row)) {
piece.setStrength(0.5);
currentPiece.setStrength(0.5);
}
}
}
}
package com.example.paktalin.agilejava_exercises.moves;
import com.example.paktalin.agilejava_exercises.Piece;
import com.example.paktalin.agilejava_exercises.pieces.King;
import com.example.paktalin.agilejava_exercises.pieces.Piece;
import com.example.paktalin.agilejava_exercises.Position;
/**
......@@ -11,7 +12,7 @@ public class KingMoveStrategyTest extends MoveStrategyTest {
@Override
void putPieceOnBoard() {
board.placePiece(Piece.createKing(Piece.Color.Black), currentPosition);
board.placePiece(new King(Piece.Color.Black), currentPosition);
}
@Override
......
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.pieces.Piece;
import com.example.paktalin.agilejava_exercises.Position;
import junit.framework.TestCase;
......
package com.example.paktalin.agilejava_exercises.moves;
import com.example.paktalin.agilejava_exercises.Piece;
import com.example.paktalin.agilejava_exercises.pieces.Piece;
import com.example.paktalin.agilejava_exercises.Position;
import com.example.paktalin.agilejava_exercises.pieces.Queen;
/**
* Created by Paktalin on 31/05/2018.
......@@ -11,7 +12,7 @@ public class QueenMoveStrategyTest extends MoveStrategyTest {
@Override
void putPieceOnBoard() {
board.placePiece(Piece.createQueen(Piece.Color.Black), currentPosition);
board.placePiece(new Queen(Piece.Color.Black), currentPosition);
}
@Override
......
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