Commit 830017af by Paktalin

Lesson 4 in progress

parent 7ad17e3e
......@@ -8,14 +8,6 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android:theme="@style/AppTheme"></application>
</manifest>
\ No newline at end of file
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.pieces.Pawn;
import java.util.ArrayList;
import java.util.List;
import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
/**
* Created by Paktalin on 23/05/2018.
*/
class Board {
private List<Pawn> seventhRank = new ArrayList<>();
private List<Pawn> secondRank = new ArrayList<>();
private List<Piece> firstRank = new ArrayList<>();
private List<Piece> secondRank = new ArrayList<>();
private List<Piece> seventhRank = new ArrayList<>();
private List<Piece> eightRank = new ArrayList<>();
private final String blankRank = "........" + NEW_LINE;
void initialize() {
fillFirstRank();
for (int i = 0; i < 8; i++) {
addWhitePawn();
addBlackPawn();
secondRank.add(new Piece(Piece.Color.Black, Piece.Name.Pawn));
seventhRank.add(new Piece(Piece.Color.White, Piece.Name.Pawn));
}
fillEightRank();
}
int getNumberOfPieces() {
return seventhRank.size() + secondRank.size();
private void fillFirstRank() {
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Rook));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Knight));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Bishop));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Queen));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.King));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Bishop));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Knight));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Rook));
}
void addWhitePawn() {
seventhRank.add(new Pawn(Pawn.COLOR_WHITE));
private void fillEightRank() {
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Rook));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Knight));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Bishop));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Queen));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.King));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Bishop));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Knight));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Rook));
}
void addBlackPawn() {
secondRank.add(new Pawn(Pawn.COLOR_BLACK));
int pieceCount() {
return firstRank.size() + secondRank.size() + seventhRank.size() + eightRank.size();
}
List<Pawn> getRank(int rankNumber) {
if (rankNumber == 2)
return secondRank;
return seventhRank;
List<Piece> getRank(int rankNumber) {
switch (rankNumber) {
case 1:
return firstRank;
case 2:
return secondRank;
case 7:
return seventhRank;
default:
return eightRank;
}
}
String printRank(int rankNumber) {
StringBuilder rankRepresentation = new StringBuilder();
for (Pawn pawn : getRank(rankNumber))
rankRepresentation.append(pawn.print());
return rankRepresentation.toString();
StringBuilder buffer = new StringBuilder();
for (Piece piece : getRank(rankNumber))
buffer.append(piece.print());
return buffer.toString();
}
void print() {
String print() {
StringBuilder buffer = new StringBuilder();
buffer.append("........\n");
buffer.append(printRank(2) + "\n");
buffer.append(printRank(1) + NEW_LINE);
buffer.append(printRank(2) + NEW_LINE);
for (int i = 3; i < 7; i++)
buffer.append("........\n");
buffer.append(blankRank);
buffer.append(printRank(7) + "\n");
buffer.append("........\n");
buffer.append(printRank(7) + NEW_LINE);
buffer.append(printRank(8) + NEW_LINE);
System.out.print(buffer.toString());
return buffer.toString();
}
}
package com.example.paktalin.agilejava_exercises;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
package com.example.paktalin.agilejava_exercises;
/**
* Created by Paktalin on 23/05/2018.
*/
public class Piece {
enum Name {Pawn, Knight, Rook, Bishop, Queen, King}
enum Color {White, Black}
private Name name;
private Color color;
private Piece() {}
Piece(final Color color, final Name name) {
this.color = color;
this.name = name;
}
public Color getColor() {
return color;
}
public Name getName() {
return name;
}
String print() {
String letter = "";
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 (color == Color.Black)
return letter.toUpperCase();
return letter;
}
}
package com.example.paktalin.agilejava_exercises.pieces;
/**
* Created by Paktalin on 23/05/2018.
*/
public class Pawn {
public static String COLOR_WHITE = "white";
public static String COLOR_BLACK = "black";
private String color;
Pawn() {
color = "white";
}
public Pawn(final String color) {
this.color = color;
}
public String getColor() {
return color;
}
public String print() {
if (this.getColor().equals(COLOR_BLACK))
return "P";
return "p";
}
}
package com.example.paktalin.agilejava_exercises.util;
/**
* Created by Paktalin on 24/05/2018.
*/
public class StringUtil {
public static final String NEW_LINE = "\n";
private StringUtil() {}
public static String appendNewLine(String string) {
return string + NEW_LINE;
}
}
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.pieces.PawnTest;
import junit.framework.TestSuite;
/**
......@@ -13,7 +11,7 @@ public class AllTests extends TestSuite {
public static TestSuite suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(PawnTest.class);
suite.addTestSuite(PieceTest.class);
suite.addTestSuite(BoardTest.class);
suite.addTestSuite(CharacterTest.class);
......
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.util.StringUtil;
import junit.framework.TestCase;
/**
......@@ -7,21 +9,23 @@ import junit.framework.TestCase;
*/
public class BoardTest extends TestCase {
private Board board;
public void setUp() {
protected void setUp() {
board = new Board();
}
public void testBoard() {
public void testCreate() {
board.initialize();
assertEquals(16, board.getNumberOfPieces());
assertEquals("PPPPPPPP", board.printRank(2));
assertEquals("pppppppp", board.printRank(7));
board.print();
assertEquals(32, board.pieceCount());
String blankRank = StringUtil.appendNewLine("........");
assertEquals(
StringUtil.appendNewLine("RNBQKBNR") +
StringUtil.appendNewLine("PPPPPPPP") +
blankRank + blankRank + blankRank + blankRank +
StringUtil.appendNewLine("pppppppp") +
StringUtil.appendNewLine("rnbqkbnr"),
board.print());
}
}
package com.example.paktalin.agilejava_exercises;
import junit.framework.TestCase;
/**
* Created by Paktalin on 23/05/2018.
*/
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 testPawnColor() {
assertEquals(Piece.Color.White, whitePawn.getColor());
assertEquals(Piece.Color.Black, blackPawn.getColor());
}
public void testPawnRepresentation() {
assertEquals("P", blackPawn.print());
assertEquals("p", whitePawn.print());
}
}
package com.example.paktalin.agilejava_exercises;
import junit.framework.TestCase;
/**
* Created by Paktalin on 24/05/2018.
*/
public class StringUtilTest extends TestCase {
}
package com.example.paktalin.agilejava_exercises.pieces;
import junit.framework.TestCase;
import static com.example.paktalin.agilejava_exercises.pieces.Pawn.COLOR_BLACK;
import static com.example.paktalin.agilejava_exercises.pieces.Pawn.COLOR_WHITE;
/**
* Created by Paktalin on 23/05/2018.
*/
public class PawnTest extends TestCase {
public void testCreate() {
Pawn pawn = new Pawn(COLOR_WHITE);
assertEquals("white", pawn.getColor());
Pawn pawn2 = new Pawn(COLOR_BLACK);
assertEquals("black", pawn2.getColor());
}
public void testConstructor() {
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