Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
agile-java
/
ChessAndroid
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
445de99b
authored
May 24, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lesson 4 done
parent
830017af
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
50 additions
and
47 deletions
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
app/src/test/java/com/example/paktalin/agilejava_exercises/PieceTest.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
View file @
445de99b
...
@@ -10,80 +10,69 @@ import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
...
@@ -10,80 +10,69 @@ import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
*/
*/
class
Board
{
class
Board
{
private
List
<
Piece
>
pieces
=
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
();
fillFirstRank
();
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
for
(
int
i
=
0
;
i
<
8
;
i
++)
secondRank
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Pawn
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Pawn
));
seventhRank
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Pawn
));
for
(
int
i
=
0
;
i
<
8
;
i
++)
}
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Pawn
));
fillEightRank
();
fillEightRank
();
}
}
private
void
fillFirstRank
()
{
private
void
fillFirstRank
()
{
firstRank
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Rook
));
firstRank
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Knight
));
firstRank
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Bishop
));
firstRank
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Queen
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Queen
));
firstRank
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
King
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
King
));
firstRank
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Bishop
));
firstRank
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Knight
));
firstRank
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Rook
));
}
}
private
void
fillEightRank
()
{
private
void
fillEightRank
()
{
eightRank
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Rook
));
eightRank
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Knight
));
eightRank
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Bishop
));
eightRank
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Queen
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Queen
));
eightRank
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
King
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
King
));
eightRank
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Bishop
));
eightRank
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Knight
));
eightRank
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Rook
));
}
}
int
pieceCount
()
{
int
pieceCount
()
{
return
firstRank
.
size
()
+
secondRank
.
size
()
+
seventhRank
.
size
()
+
eightRank
.
size
();
return
pieces
.
size
();
}
}
List
<
Piece
>
getRank
(
int
rankNumber
)
{
int
colorCount
(
Piece
.
Color
color
)
{
switch
(
rankNumber
)
{
int
counter
=
0
;
case
1
:
for
(
Piece
piece
:
pieces
)
return
firstRank
;
if
(
piece
.
getColor
()
==
color
)
case
2
:
counter
++;
return
secondRank
;
return
counter
;
case
7
:
return
seventhRank
;
default
:
return
eightRank
;
}
}
}
String
printRank
(
int
rankNumber
)
{
String
printRank
(
int
from
)
{
StringBuilder
buffer
=
new
StringBuilder
();
StringBuilder
buffer
=
new
StringBuilder
();
for
(
Piece
piece
:
getRank
(
rankNumber
)
)
for
(
int
i
=
from
;
i
<
from
+
8
;
i
++
)
buffer
.
append
(
piece
.
print
());
buffer
.
append
(
piece
s
.
get
(
i
)
.
print
());
return
buffer
.
toString
();
return
buffer
.
toString
();
}
}
String
print
()
{
String
print
()
{
StringBuilder
buffer
=
new
StringBuilder
();
StringBuilder
buffer
=
new
StringBuilder
();
buffer
.
append
(
printRank
(
1
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
0
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
2
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
8
)
+
NEW_LINE
);
for
(
int
i
=
3
;
i
<
7
;
i
++)
for
(
int
i
=
3
;
i
<
7
;
i
++)
buffer
.
append
(
blankRank
);
buffer
.
append
(
"........"
+
NEW_LINE
);
buffer
.
append
(
printRank
(
7
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
16
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
8
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
24
)
+
NEW_LINE
);
return
buffer
.
toString
();
return
buffer
.
toString
();
}
}
...
...
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
View file @
445de99b
...
@@ -53,4 +53,12 @@ public class Piece {
...
@@ -53,4 +53,12 @@ public class Piece {
return
letter
.
toUpperCase
();
return
letter
.
toUpperCase
();
return
letter
;
return
letter
;
}
}
boolean
isWhite
()
{
return
this
.
color
==
Color
.
White
;
}
boolean
isBlack
()
{
return
this
.
color
==
Color
.
Black
;
}
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
View file @
445de99b
...
@@ -27,5 +27,8 @@ public class BoardTest extends TestCase {
...
@@ -27,5 +27,8 @@ public class BoardTest extends TestCase {
StringUtil
.
appendNewLine
(
"pppppppp"
)
+
StringUtil
.
appendNewLine
(
"pppppppp"
)
+
StringUtil
.
appendNewLine
(
"rnbqkbnr"
),
StringUtil
.
appendNewLine
(
"rnbqkbnr"
),
board
.
print
());
board
.
print
());
assertEquals
(
16
,
board
.
colorCount
(
Piece
.
Color
.
Black
));
assertEquals
(
16
,
board
.
colorCount
(
Piece
.
Color
.
White
));
}
}
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/PieceTest.java
View file @
445de99b
...
@@ -18,6 +18,9 @@ public class PieceTest extends TestCase {
...
@@ -18,6 +18,9 @@ public class PieceTest extends TestCase {
public
void
testPawnColor
()
{
public
void
testPawnColor
()
{
assertEquals
(
Piece
.
Color
.
White
,
whitePawn
.
getColor
());
assertEquals
(
Piece
.
Color
.
White
,
whitePawn
.
getColor
());
assertEquals
(
Piece
.
Color
.
Black
,
blackPawn
.
getColor
());
assertEquals
(
Piece
.
Color
.
Black
,
blackPawn
.
getColor
());
assertTrue
(
whitePawn
.
isWhite
());
assertFalse
(
blackPawn
.
isWhite
());
}
}
public
void
testPawnRepresentation
()
{
public
void
testPawnRepresentation
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment