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
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
48 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,81 +10,70 @@ import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
*/
class
Board
{
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
;
private
List
<
Piece
>
pieces
=
new
ArrayList
<>();
void
initialize
()
{
fillFirstRank
();
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
secondRank
.
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
.
Black
,
Piece
.
Name
.
Pawn
));
for
(
int
i
=
0
;
i
<
8
;
i
++)
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Pawn
));
fillEightRank
();
}
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
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Queen
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
King
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Rook
));
}
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
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Queen
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
King
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Rook
));
}
int
pieceCount
()
{
return
firstRank
.
size
()
+
secondRank
.
size
()
+
seventhRank
.
size
()
+
eightRank
.
size
();
return
pieces
.
size
();
}
List
<
Piece
>
getRank
(
int
rankNumber
)
{
switch
(
rankNumber
)
{
case
1
:
return
firstRank
;
case
2
:
return
secondRank
;
case
7
:
return
seventhRank
;
default
:
return
eightRank
;
}
int
colorCount
(
Piece
.
Color
color
)
{
int
counter
=
0
;
for
(
Piece
piece
:
pieces
)
if
(
piece
.
getColor
()
==
color
)
counter
++;
return
counter
;
}
String
printRank
(
int
rankNumber
)
{
String
printRank
(
int
from
)
{
StringBuilder
buffer
=
new
StringBuilder
();
for
(
Piece
piece
:
getRank
(
rankNumber
)
)
buffer
.
append
(
piece
.
print
());
for
(
int
i
=
from
;
i
<
from
+
8
;
i
++
)
buffer
.
append
(
piece
s
.
get
(
i
)
.
print
());
return
buffer
.
toString
();
}
String
print
()
{
StringBuilder
buffer
=
new
StringBuilder
();
buffer
.
append
(
printRank
(
1
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
2
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
0
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
8
)
+
NEW_LINE
);
for
(
int
i
=
3
;
i
<
7
;
i
++)
buffer
.
append
(
blankRank
);
buffer
.
append
(
"........"
+
NEW_LINE
);
buffer
.
append
(
printRank
(
7
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
8
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
16
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
24
)
+
NEW_LINE
);
return
buffer
.
toString
();
}
}
}
\ No newline at end of file
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
View file @
445de99b
...
...
@@ -53,4 +53,12 @@ public class Piece {
return
letter
.
toUpperCase
();
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 {
StringUtil
.
appendNewLine
(
"pppppppp"
)
+
StringUtil
.
appendNewLine
(
"rnbqkbnr"
),
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 {
public
void
testPawnColor
()
{
assertEquals
(
Piece
.
Color
.
White
,
whitePawn
.
getColor
());
assertEquals
(
Piece
.
Color
.
Black
,
blackPawn
.
getColor
());
assertTrue
(
whitePawn
.
isWhite
());
assertFalse
(
blackPawn
.
isWhite
());
}
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