Commit a00c86ad by Paktalin

Lesson 5 in progress

parent 445de99b
......@@ -6,7 +6,7 @@
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# and specify the fully qualified class type to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
......@@ -17,5 +17,5 @@
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
# hide the original source file type.
#-renamesourcefileattribute SourceFile
......@@ -15,32 +15,32 @@ class Board {
void initialize() {
fillFirstRank();
for (int i = 0; i < 8; i++)
pieces.add(new Piece(Piece.Color.Black, Piece.Name.Pawn));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Pawn));
for (int i = 0; i < 8; i++)
pieces.add(new Piece(Piece.Color.White, Piece.Name.Pawn));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Pawn));
fillEightRank();
}
private void fillFirstRank() {
pieces.add(new Piece(Piece.Color.Black, Piece.Name.Rook));
pieces.add(new Piece(Piece.Color.Black, Piece.Name.Knight));
pieces.add(new Piece(Piece.Color.Black, Piece.Name.Bishop));
pieces.add(new Piece(Piece.Color.Black, Piece.Name.Queen));
pieces.add(new Piece(Piece.Color.Black, Piece.Name.King));
pieces.add(new Piece(Piece.Color.Black, Piece.Name.Bishop));
pieces.add(new Piece(Piece.Color.Black, Piece.Name.Knight));
pieces.add(new Piece(Piece.Color.Black, Piece.Name.Rook));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Rook));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Knight));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Bishop));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Queen));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.King));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Bishop));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Knight));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Rook));
}
private void fillEightRank() {
pieces.add(new Piece(Piece.Color.White, Piece.Name.Rook));
pieces.add(new Piece(Piece.Color.White, Piece.Name.Knight));
pieces.add(new Piece(Piece.Color.White, Piece.Name.Bishop));
pieces.add(new Piece(Piece.Color.White, Piece.Name.Queen));
pieces.add(new Piece(Piece.Color.White, Piece.Name.King));
pieces.add(new Piece(Piece.Color.White, Piece.Name.Bishop));
pieces.add(new Piece(Piece.Color.White, Piece.Name.Knight));
pieces.add(new Piece(Piece.Color.White, Piece.Name.Rook));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Rook));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Knight));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Bishop));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Queen));
pieces.add(new Piece(Piece.Color.White, Piece.Type.King));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Bishop));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Knight));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Rook));
}
int pieceCount() {
......@@ -58,7 +58,7 @@ class Board {
String printRank(int from) {
StringBuilder buffer = new StringBuilder();
for (int i = from; i < from + 8; i++)
buffer.append(pieces.get(i).print());
buffer.append(pieces.get(i).getRepresentation());
return buffer.toString();
}
......@@ -76,4 +76,12 @@ class Board {
return buffer.toString();
}
int getCount(Piece.Type type, Piece.Color color) {
int count = 0;
for (Piece piece : pieces)
if (piece.getType() == type && piece.getColor() == color)
count++;
return count;
}
}
\ No newline at end of file
......@@ -5,53 +5,45 @@ package com.example.paktalin.agilejava_exercises;
*/
public class Piece {
enum Name {Pawn, Knight, Rook, Bishop, Queen, King}
enum Type {Pawn, Knight, Rook, Bishop, Queen, King}
enum Color {White, Black}
private Name name;
private Type type;
private Color color;
private Piece() {}
Piece(final Color color, final Name name) {
Piece(final Color color, final Type type) {
this.color = color;
this.name = name;
this.type = type;
}
public Color getColor() {
return color;
}
public Name getName() {
return name;
public Type getType() {
return type;
}
String print() {
String letter = "";
char getRepresentation() {
char representation = 0;
switch (name) {
case King:
letter = "k";
break;
case Queen:
letter = "q";
break;
case Pawn:
letter = "p";
break;
case Rook:
letter = "r";
break;
case Bishop:
letter = "b";
break;
case Knight:
letter = "n";
break;
}
if (type == Type.King)
representation = 'k';
if (type == Type.Queen)
representation = 'q';
if (type == Type.Pawn)
representation = 'p';
if (type == Type.Rook)
representation = 'r';
if (type == Type.Bishop)
representation = 'b';
if (type == Type.Knight)
representation = 'n';
if (color == Color.Black)
return letter.toUpperCase();
return letter;
if (this.isBlack())
return Character.toUpperCase(representation);
return representation;
}
boolean isWhite() {
......@@ -61,4 +53,46 @@ public class Piece {
boolean isBlack() {
return this.color == Color.Black;
}
static Piece createWhitePawn() {
return new Piece(Color.White, Type.Pawn);
}
static Piece createBlackPawn() {
return new Piece(Color.Black, Type.Pawn);
}
static Piece createWhiteKing() {
return new Piece(Color.White, Type.King);
}
static Piece createBlackKing() {
return new Piece(Color.Black, Type.King);
}
static Piece createWhiteBishop() {
return new Piece(Color.White, Type.Bishop);
}
static Piece createBlackBishop() {
return new Piece(Color.Black, Type.Bishop);
}
static Piece createWhiteRook() {
return new Piece(Color.White, Type.Rook);
}
static Piece createBlackRook() {
return new Piece(Color.Black, Type.Rook);
}
static Piece createWhiteKnight() {
return new Piece(Color.White, Type.Knight);
}
static Piece createBlackKnight() {
return new Piece(Color.Black, Type.Knight);
}
static Piece createWhiteQueen() {
return new Piece(Color.White, Type.Queen);
}
static Piece createBlackQueen() {
return new Piece(Color.Black, Type.Queen);
}
}
......@@ -31,4 +31,15 @@ public class BoardTest extends TestCase {
assertEquals(16, board.colorCount(Piece.Color.Black));
assertEquals(16, board.colorCount(Piece.Color.White));
}
public void testCountPieces() {
board.initialize();
assertEquals(8, board.getCount(Piece.Type.Pawn, Piece.Color.White));
assertEquals(1, board.getCount(Piece.Type.King, Piece.Color.Black));
assertEquals(1, board.getCount(Piece.Type.Queen, Piece.Color.White));
assertEquals(2, board.getCount(Piece.Type.Bishop, Piece.Color.Black));
assertEquals(2, board.getCount(Piece.Type.Knight, Piece.Color.White));
assertEquals(2, board.getCount(Piece.Type.Rook, Piece.Color.Black));
}
}
......@@ -8,23 +8,22 @@ import junit.framework.TestCase;
public class PieceTest extends TestCase {
private Piece blackPawn, whitePawn;
public void setUp() {
blackPawn = new Piece(Piece.Color.Black, Piece.Name.Pawn);
whitePawn = new Piece(Piece.Color.White, Piece.Name.Pawn);
public void testCreate() {
verifyCreation(Piece.createWhitePawn(), Piece.createBlackPawn(), Piece.Type.Pawn, 'p');
verifyCreation(Piece.createWhiteKing(), Piece.createBlackKing(), Piece.Type.King, 'k');
verifyCreation(Piece.createWhiteBishop(), Piece.createBlackBishop(), Piece.Type.Bishop, 'b');
verifyCreation(Piece.createWhiteRook(), Piece.createBlackRook(), Piece.Type.Rook, 'r');
verifyCreation(Piece.createWhiteKnight(), Piece.createBlackKnight(), Piece.Type.Knight, 'n');
verifyCreation(Piece.createWhiteQueen(), Piece.createBlackQueen(), Piece.Type.Queen, 'q');
}
public void testPawnColor() {
assertEquals(Piece.Color.White, whitePawn.getColor());
assertEquals(Piece.Color.Black, blackPawn.getColor());
assertTrue(whitePawn.isWhite());
assertFalse(blackPawn.isWhite());
}
private void verifyCreation(Piece whitePiece, Piece blackPiece, Piece.Type type, char representation) {
assertTrue(whitePiece.isWhite());
assertEquals(type, whitePiece.getType());
assertEquals(representation, whitePiece.getRepresentation());
public void testPawnRepresentation() {
assertEquals("P", blackPawn.print());
assertEquals("p", whitePawn.print());
assertTrue(blackPiece.isBlack());
assertEquals(type, blackPiece.getType());
assertEquals(Character.toUpperCase(representation), blackPiece.getRepresentation());
}
}
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