Commit a00c86ad by Paktalin

Lesson 5 in progress

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