Commit 7ad17e3e by Paktalin

Lesson 3 done

parent 2a341b7f
...@@ -10,17 +10,54 @@ import java.util.List; ...@@ -10,17 +10,54 @@ import java.util.List;
*/ */
class Board { 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() { int getNumberOfPieces() {
return pawns.size(); return seventhRank.size() + secondRank.size();
}
void addWhitePawn() {
seventhRank.add(new Pawn(Pawn.COLOR_WHITE));
}
void addBlackPawn() {
secondRank.add(new Pawn(Pawn.COLOR_BLACK));
} }
void addPawn(Pawn pawn) { List<Pawn> getRank(int rankNumber) {
pawns.add(pawn); if (rankNumber == 2)
return secondRank;
return seventhRank;
} }
Pawn getPawn(int index) { String printRank(int rankNumber) {
return pawns.get(index); 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 { ...@@ -10,7 +10,7 @@ public class Pawn {
private String color; private String color;
public Pawn() { Pawn() {
color = "white"; color = "white";
} }
...@@ -21,4 +21,10 @@ public class Pawn { ...@@ -21,4 +21,10 @@ public class Pawn {
public String getColor() { public String getColor() {
return color; return color;
} }
public String print() {
if (this.getColor().equals(COLOR_BLACK))
return "P";
return "p";
}
} }
...@@ -15,6 +15,7 @@ public class AllTests extends TestSuite { ...@@ -15,6 +15,7 @@ public class AllTests extends TestSuite {
suite.addTestSuite(PawnTest.class); suite.addTestSuite(PawnTest.class);
suite.addTestSuite(BoardTest.class); suite.addTestSuite(BoardTest.class);
suite.addTestSuite(CharacterTest.class);
return suite; return suite;
} }
......
package com.example.paktalin.agilejava_exercises; package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.pieces.Pawn;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**
...@@ -17,15 +15,13 @@ public class BoardTest extends TestCase { ...@@ -17,15 +15,13 @@ public class BoardTest extends TestCase {
} }
public void testBoard() { public void testBoard() {
assertEquals(0, board.getNumberOfPieces()); board.initialize();
} assertEquals(16, board.getNumberOfPieces());
public void testNumberOfPieces() { assertEquals("PPPPPPPP", board.printRank(2));
Pawn pawn = new Pawn(Pawn.COLOR_BLACK); assertEquals("pppppppp", board.printRank(7));
board.addPawn(pawn); board.print();
assertEquals(1, board.getNumberOfPieces());
assertEquals("black", board.getPawn(0).getColor());
} }
} }
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 { ...@@ -23,4 +23,12 @@ public class PawnTest extends TestCase {
Pawn pawn = new Pawn(); Pawn pawn = new Pawn();
assertEquals("white", pawn.getColor()); 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