Commit 830017af by Paktalin

Lesson 4 in progress

parent 7ad17e3e
...@@ -8,14 +8,6 @@ ...@@ -8,14 +8,6 @@
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme"></application>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> </manifest>
\ No newline at end of file
package com.example.paktalin.agilejava_exercises; package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.pieces.Pawn;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
/** /**
* Created by Paktalin on 23/05/2018. * Created by Paktalin on 23/05/2018.
*/ */
class Board { 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() { void initialize() {
fillFirstRank();
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
addWhitePawn(); secondRank.add(new Piece(Piece.Color.Black, Piece.Name.Pawn));
addBlackPawn(); seventhRank.add(new Piece(Piece.Color.White, Piece.Name.Pawn));
} }
fillEightRank();
} }
int getNumberOfPieces() { private void fillFirstRank() {
return seventhRank.size() + secondRank.size(); 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() { private void fillEightRank() {
seventhRank.add(new Pawn(Pawn.COLOR_WHITE)); 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() { int pieceCount() {
secondRank.add(new Pawn(Pawn.COLOR_BLACK)); return firstRank.size() + secondRank.size() + seventhRank.size() + eightRank.size();
} }
List<Pawn> getRank(int rankNumber) { List<Piece> getRank(int rankNumber) {
if (rankNumber == 2) switch (rankNumber) {
return secondRank; case 1:
return seventhRank; return firstRank;
case 2:
return secondRank;
case 7:
return seventhRank;
default:
return eightRank;
}
} }
String printRank(int rankNumber) { String printRank(int rankNumber) {
StringBuilder rankRepresentation = new StringBuilder(); StringBuilder buffer = new StringBuilder();
for (Piece piece : getRank(rankNumber))
for (Pawn pawn : getRank(rankNumber)) buffer.append(piece.print());
rankRepresentation.append(pawn.print()); return buffer.toString();
return rankRepresentation.toString();
} }
void print() { String print() {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
buffer.append("........\n"); buffer.append(printRank(1) + NEW_LINE);
buffer.append(printRank(2) + "\n"); buffer.append(printRank(2) + NEW_LINE);
for (int i = 3; i < 7; i++) for (int i = 3; i < 7; i++)
buffer.append("........\n"); buffer.append(blankRank);
buffer.append(printRank(7) + "\n"); buffer.append(printRank(7) + NEW_LINE);
buffer.append("........\n"); 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; package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.pieces.PawnTest;
import junit.framework.TestSuite; import junit.framework.TestSuite;
/** /**
...@@ -13,7 +11,7 @@ public class AllTests extends TestSuite { ...@@ -13,7 +11,7 @@ public class AllTests extends TestSuite {
public static TestSuite suite() { public static TestSuite suite() {
TestSuite suite = new TestSuite(); TestSuite suite = new TestSuite();
suite.addTestSuite(PawnTest.class); suite.addTestSuite(PieceTest.class);
suite.addTestSuite(BoardTest.class); suite.addTestSuite(BoardTest.class);
suite.addTestSuite(CharacterTest.class); suite.addTestSuite(CharacterTest.class);
......
package com.example.paktalin.agilejava_exercises; package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.util.StringUtil;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**
...@@ -7,21 +9,23 @@ import junit.framework.TestCase; ...@@ -7,21 +9,23 @@ import junit.framework.TestCase;
*/ */
public class BoardTest extends TestCase { public class BoardTest extends TestCase {
private Board board; private Board board;
public void setUp() { protected void setUp() {
board = new Board(); board = new Board();
} }
public void testBoard() { public void testCreate() {
board.initialize(); board.initialize();
assertEquals(16, board.getNumberOfPieces()); assertEquals(32, board.pieceCount());
String blankRank = StringUtil.appendNewLine("........");
assertEquals("PPPPPPPP", board.printRank(2));
assertEquals("pppppppp", board.printRank(7)); assertEquals(
StringUtil.appendNewLine("RNBQKBNR") +
board.print(); 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