Commit 7ad17e3e by Paktalin

Lesson 3 done

parent 2a341b7f
......@@ -10,17 +10,54 @@ import java.util.List;
*/
class Board {
private List<Pawn> pawns = new ArrayList<>();
private List<Pawn> seventhRank = new ArrayList<>();
private List<Pawn> secondRank = new ArrayList<>();
void initialize() {
for (int i = 0; i < 8; i++) {
addWhitePawn();
addBlackPawn();
}
}
int getNumberOfPieces() {
return pawns.size();
return seventhRank.size() + secondRank.size();
}
void addWhitePawn() {
seventhRank.add(new Pawn(Pawn.COLOR_WHITE));
}
void addPawn(Pawn pawn) {
pawns.add(pawn);
void addBlackPawn() {
secondRank.add(new Pawn(Pawn.COLOR_BLACK));
}
Pawn getPawn(int index) {
return pawns.get(index);
List<Pawn> getRank(int rankNumber) {
if (rankNumber == 2)
return secondRank;
return seventhRank;
}
String printRank(int rankNumber) {
StringBuilder rankRepresentation = new StringBuilder();
for (Pawn pawn : getRank(rankNumber))
rankRepresentation.append(pawn.print());
return rankRepresentation.toString();
}
void print() {
StringBuilder buffer = new StringBuilder();
buffer.append("........\n");
buffer.append(printRank(2) + "\n");
for (int i = 3; i < 7; i++)
buffer.append("........\n");
buffer.append(printRank(7) + "\n");
buffer.append("........\n");
System.out.print(buffer.toString());
}
}
......@@ -10,7 +10,7 @@ public class Pawn {
private String color;
public Pawn() {
Pawn() {
color = "white";
}
......@@ -21,4 +21,10 @@ public class Pawn {
public String getColor() {
return color;
}
public String print() {
if (this.getColor().equals(COLOR_BLACK))
return "P";
return "p";
}
}
......@@ -15,6 +15,7 @@ public class AllTests extends TestSuite {
suite.addTestSuite(PawnTest.class);
suite.addTestSuite(BoardTest.class);
suite.addTestSuite(CharacterTest.class);
return suite;
}
......
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.pieces.Pawn;
import junit.framework.TestCase;
/**
......@@ -17,15 +15,13 @@ public class BoardTest extends TestCase {
}
public void testBoard() {
assertEquals(0, board.getNumberOfPieces());
}
board.initialize();
assertEquals(16, board.getNumberOfPieces());
public void testNumberOfPieces() {
Pawn pawn = new Pawn(Pawn.COLOR_BLACK);
assertEquals("PPPPPPPP", board.printRank(2));
assertEquals("pppppppp", board.printRank(7));
board.addPawn(pawn);
assertEquals(1, board.getNumberOfPieces());
assertEquals("black", board.getPawn(0).getColor());
board.print();
}
}
package com.example.paktalin.agilejava_exercises;
import junit.framework.TestCase;
/**
* Created by Paktalin on 23/05/2018.
*/
public class CharacterTest extends TestCase {
public void testWhiteSpace() {
assertTrue(Character.isWhitespace(' '));
assertTrue(Character.isWhitespace('\t'));
assertTrue(Character.isWhitespace('\n'));
assertFalse(Character.isWhitespace('m'));
assertFalse(Character.isWhitespace('a'));
}
public void testIdentifiers() {
assertFalse(Character.isJavaIdentifierPart('^'));
assertTrue(Character.isJavaIdentifierPart('k'));
}
}
......@@ -23,4 +23,12 @@ public class PawnTest extends TestCase {
Pawn pawn = new Pawn();
assertEquals("white", pawn.getColor());
}
public void testPawnRepresentation() {
Pawn blackPawn = new Pawn(Pawn.COLOR_BLACK);
assertEquals("P", blackPawn.print());
Pawn whitePawn = new Pawn(Pawn.COLOR_WHITE);
assertEquals("p", whitePawn.print());
}
}
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