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
94b90657
authored
May 24, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in Lesson 5
parent
a00c86ad
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
96 additions
and
104 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 @
94b90657
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
java.util.ArrayList
;
import
java.util.List
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
util
.
StringUtil
.
NEW_LINE
;
/**
...
...
@@ -10,78 +7,88 @@ import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
*/
class
Board
{
private
List
<
Piece
>
pieces
=
new
ArrayList
<>();
static
final
int
ROW_COUNT
=
8
;
static
final
int
COLUMN_COUNT
=
8
;
private
Piece
[][]
positionState
=
new
Piece
[
ROW_COUNT
][
COLUMN_COUNT
];
void
initialize
()
{
fillFirstRank
();
for
(
int
i
=
0
;
i
<
8
;
i
++)
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
Pawn
));
for
(
int
i
=
0
;
i
<
8
;
i
++)
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Pawn
));
fillEightRank
();
initializeKingRank
(
Piece
.
Color
.
Black
,
0
);
initializePawnRank
(
Piece
.
Color
.
Black
,
1
);
initializePawnRank
(
Piece
.
Color
.
White
,
6
);
initializeKingRank
(
Piece
.
Color
.
White
,
7
);
}
private
void
initializeKingRank
(
Piece
.
Color
color
,
int
rank
)
{
setPosition
(
Piece
.
createRook
(
color
),
rank
,
'a'
);
setPosition
(
Piece
.
createKnight
(
color
),
rank
,
'b'
);
setPosition
(
Piece
.
createBishop
(
color
),
rank
,
'c'
);
setPosition
(
Piece
.
createQueen
(
color
),
rank
,
'd'
);
setPosition
(
Piece
.
createKing
(
color
),
rank
,
'e'
);
setPosition
(
Piece
.
createBishop
(
color
),
rank
,
'f'
);
setPosition
(
Piece
.
createKnight
(
color
),
rank
,
'g'
);
setPosition
(
Piece
.
createRook
(
color
),
rank
,
'h'
);
}
private
void
initializePawnRank
(
Piece
.
Color
color
,
int
rank
)
{
for
(
int
i
=
0
;
i
<
COLUMN_COUNT
;
i
++)
setPosition
(
Piece
.
createPawn
(
color
),
rank
,
i
);
}
private
void
fillFirstRank
()
{
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
Queen
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
King
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
Rook
));
private
void
setPosition
(
Piece
piece
,
int
rank
,
char
file
)
{
char
firstColumnLetter
=
'a'
;
int
column
=
(
int
)
file
-
(
int
)
firstColumnLetter
;
setPosition
(
piece
,
rank
,
column
);
}
private
void
fillEightRank
()
{
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Queen
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
King
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Rook
));
private
void
setPosition
(
Piece
piece
,
int
rank
,
int
column
)
{
positionState
[
rank
][
column
]
=
piece
;
}
int
pieceCount
()
{
return
pieces
.
size
();
int
count
=
0
;
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
if
(
positionState
[
i
][
j
]
!=
null
)
count
++;
return
count
;
}
int
color
Count
(
Piece
.
Color
color
)
{
int
piece
Count
(
Piece
.
Color
color
)
{
int
counter
=
0
;
for
(
Piece
piece
:
pieces
)
if
(
piece
.
getColor
()
==
color
)
counter
++;
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
if
(
positionState
[
i
][
j
]
!=
null
)
if
(
positionState
[
i
][
j
].
getColor
()
==
color
)
counter
++;
}
return
counter
;
}
String
printRank
(
int
from
)
{
StringBuilder
buffer
=
new
StringBuilder
();
for
(
int
i
=
from
;
i
<
from
+
8
;
i
++)
buffer
.
append
(
pieces
.
get
(
i
).
getRepresentation
());
return
buffer
.
toString
();
int
pieceCount
(
Piece
.
Type
type
,
Piece
.
Color
color
)
{
int
count
=
0
;
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
{
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
if
(
positionState
[
i
][
j
]
!=
null
)
if
(
positionState
[
i
][
j
].
isType
(
type
)
&&
positionState
[
i
][
j
].
getColor
()
==
color
)
count
++;
}
}
return
count
;
}
String
print
()
{
StringBuilder
buffer
=
new
StringBuilder
();
buffer
.
append
(
printRank
(
0
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
8
)
+
NEW_LINE
);
for
(
int
i
=
3
;
i
<
7
;
i
++)
buffer
.
append
(
"........"
+
NEW_LINE
);
buffer
.
append
(
printRank
(
16
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
24
)
+
NEW_LINE
);
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
{
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
if
(
positionState
[
i
][
j
]
==
null
)
buffer
.
append
(
"."
);
else
buffer
.
append
(
positionState
[
i
][
j
].
getRepresentation
());
}
buffer
.
append
(
NEW_LINE
);
}
return
buffer
.
toString
();
}
int
getCount
(
Piece
.
Type
type
,
Piece
.
Color
color
)
{
int
count
=
0
;
for
(
Piece
piece
:
pieces
)
if
(
piece
.
getType
()
==
type
&&
piece
.
getColor
()
==
color
)
count
++;
return
count
;
}
}
\ No newline at end of file
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
View file @
94b90657
...
...
@@ -54,45 +54,26 @@ public class Piece {
return
this
.
color
==
Color
.
Black
;
}
static
Piece
createWhitePawn
()
{
return
new
Piece
(
Color
.
White
,
Type
.
Pawn
);
}
static
Piece
createBlackPawn
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
Pawn
);
boolean
isType
(
Piece
.
Type
type
)
{
return
this
.
getType
()
==
type
;
}
static
Piece
createWhiteKing
()
{
return
new
Piece
(
Color
.
White
,
Type
.
King
);
}
static
Piece
createBlackKing
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
King
);
static
Piece
createPawn
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Pawn
);
}
static
Piece
createWhiteBishop
()
{
return
new
Piece
(
Color
.
White
,
Type
.
Bishop
);
static
Piece
createKing
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
King
);
}
static
Piece
createBlackBishop
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
Bishop
);
}
static
Piece
createWhiteRook
()
{
return
new
Piece
(
Color
.
White
,
Type
.
Rook
);
static
Piece
createBishop
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Bishop
);
}
static
Piece
create
BlackRook
(
)
{
return
new
Piece
(
Color
.
Black
,
Type
.
Rook
);
static
Piece
create
Rook
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Rook
);
}
static
Piece
createWhiteKnight
()
{
return
new
Piece
(
Color
.
White
,
Type
.
Knight
);
}
static
Piece
createBlackKnight
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
Knight
);
}
static
Piece
createWhiteQueen
()
{
return
new
Piece
(
Color
.
White
,
Type
.
Queen
);
static
Piece
createKnight
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Knight
);
}
static
Piece
create
BlackQueen
(
)
{
return
new
Piece
(
Color
.
Black
,
Type
.
Queen
);
static
Piece
create
Queen
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Queen
);
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
View file @
94b90657
...
...
@@ -28,18 +28,23 @@ public class BoardTest extends TestCase {
StringUtil
.
appendNewLine
(
"rnbqkbnr"
),
board
.
print
());
assertEquals
(
16
,
board
.
color
Count
(
Piece
.
Color
.
Black
));
assertEquals
(
16
,
board
.
color
Count
(
Piece
.
Color
.
White
));
assertEquals
(
16
,
board
.
piece
Count
(
Piece
.
Color
.
Black
));
assertEquals
(
16
,
board
.
piece
Count
(
Piece
.
Color
.
White
));
}
public
void
testCountPieces
()
{
board
.
initialize
();
assertEquals
(
8
,
board
.
getCount
(
Piece
.
Type
.
Pawn
,
Piece
.
Color
.
White
));
assertEquals
(
1
,
board
.
getCount
(
Piece
.
Type
.
King
,
Piece
.
Color
.
Black
));
assertEquals
(
1
,
board
.
getCount
(
Piece
.
Type
.
Queen
,
Piece
.
Color
.
White
));
assertEquals
(
2
,
board
.
getCount
(
Piece
.
Type
.
Bishop
,
Piece
.
Color
.
Black
));
assertEquals
(
2
,
board
.
getCount
(
Piece
.
Type
.
Knight
,
Piece
.
Color
.
White
));
assertEquals
(
2
,
board
.
getCount
(
Piece
.
Type
.
Rook
,
Piece
.
Color
.
Black
));
assertEquals
(
8
,
board
.
pieceCount
(
Piece
.
Type
.
Pawn
,
Piece
.
Color
.
White
));
assertEquals
(
1
,
board
.
pieceCount
(
Piece
.
Type
.
King
,
Piece
.
Color
.
Black
));
assertEquals
(
1
,
board
.
pieceCount
(
Piece
.
Type
.
Queen
,
Piece
.
Color
.
White
));
assertEquals
(
2
,
board
.
pieceCount
(
Piece
.
Type
.
Bishop
,
Piece
.
Color
.
Black
));
assertEquals
(
2
,
board
.
pieceCount
(
Piece
.
Type
.
Knight
,
Piece
.
Color
.
White
));
assertEquals
(
2
,
board
.
pieceCount
(
Piece
.
Type
.
Rook
,
Piece
.
Color
.
Black
));
}
public
void
testBoardDimensions
()
{
assertEquals
(
8
,
board
.
ROW_COUNT
);
assertEquals
(
8
,
board
.
COLUMN_COUNT
);
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/PieceTest.java
View file @
94b90657
...
...
@@ -9,12 +9,12 @@ import junit.framework.TestCase;
public
class
PieceTest
extends
TestCase
{
public
void
testCreate
()
{
verifyCreation
(
Piece
.
create
WhitePawn
(),
Piece
.
createBlackPawn
(
),
Piece
.
Type
.
Pawn
,
'p'
);
verifyCreation
(
Piece
.
create
WhiteKing
(),
Piece
.
createBlackKing
(
),
Piece
.
Type
.
King
,
'k'
);
verifyCreation
(
Piece
.
create
WhiteBishop
(),
Piece
.
createBlackBishop
(
),
Piece
.
Type
.
Bishop
,
'b'
);
verifyCreation
(
Piece
.
create
WhiteRook
(),
Piece
.
createBlackRook
(
),
Piece
.
Type
.
Rook
,
'r'
);
verifyCreation
(
Piece
.
create
WhiteKnight
(),
Piece
.
createBlackKnight
(
),
Piece
.
Type
.
Knight
,
'n'
);
verifyCreation
(
Piece
.
create
WhiteQueen
(),
Piece
.
createBlackQueen
(
),
Piece
.
Type
.
Queen
,
'q'
);
verifyCreation
(
Piece
.
create
Pawn
(
Piece
.
Color
.
White
),
Piece
.
createPawn
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Pawn
,
'p'
);
verifyCreation
(
Piece
.
create
King
(
Piece
.
Color
.
White
),
Piece
.
createKing
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
King
,
'k'
);
verifyCreation
(
Piece
.
create
Bishop
(
Piece
.
Color
.
White
),
Piece
.
createBishop
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Bishop
,
'b'
);
verifyCreation
(
Piece
.
create
Rook
(
Piece
.
Color
.
White
),
Piece
.
createRook
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Rook
,
'r'
);
verifyCreation
(
Piece
.
create
Knight
(
Piece
.
Color
.
White
),
Piece
.
createKnight
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Knight
,
'n'
);
verifyCreation
(
Piece
.
create
Queen
(
Piece
.
Color
.
White
),
Piece
.
createQueen
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Queen
,
'q'
);
}
private
void
verifyCreation
(
Piece
whitePiece
,
Piece
blackPiece
,
Piece
.
Type
type
,
char
representation
)
{
...
...
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