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
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
96 additions
and
102 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
;
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
;
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;
...
@@ -10,78 +7,88 @@ import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
*/
*/
class
Board
{
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
()
{
void
initialize
()
{
fillFirstRank
();
initializeKingRank
(
Piece
.
Color
.
Black
,
0
);
for
(
int
i
=
0
;
i
<
8
;
i
++)
initializePawnRank
(
Piece
.
Color
.
Black
,
1
);
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
Pawn
));
initializePawnRank
(
Piece
.
Color
.
White
,
6
);
for
(
int
i
=
0
;
i
<
8
;
i
++)
initializeKingRank
(
Piece
.
Color
.
White
,
7
);
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Pawn
));
}
fillEightRank
();
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
fillFirstRank
()
{
private
void
initializePawnRank
(
Piece
.
Color
color
,
int
rank
)
{
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
Rook
));
for
(
int
i
=
0
;
i
<
COLUMN_COUNT
;
i
++)
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Type
.
Knight
));
setPosition
(
Piece
.
createPawn
(
color
),
rank
,
i
);
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
fillEightRank
()
{
private
void
setPosition
(
Piece
piece
,
int
rank
,
char
file
)
{
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Rook
));
char
firstColumnLetter
=
'a'
;
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Knight
));
int
column
=
(
int
)
file
-
(
int
)
firstColumnLetter
;
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Bishop
));
setPosition
(
piece
,
rank
,
column
);
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
));
private
void
setPosition
(
Piece
piece
,
int
rank
,
int
column
)
{
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Knight
));
positionState
[
rank
][
column
]
=
piece
;
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Type
.
Rook
));
}
}
int
pieceCount
()
{
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
;
int
counter
=
0
;
for
(
Piece
piece
:
pieces
)
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
if
(
piece
.
getColor
()
==
color
)
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
if
(
positionState
[
i
][
j
]
!=
null
)
if
(
positionState
[
i
][
j
].
getColor
()
==
color
)
counter
++;
counter
++;
}
return
counter
;
return
counter
;
}
}
String
printRank
(
int
from
)
{
int
pieceCount
(
Piece
.
Type
type
,
Piece
.
Color
color
)
{
StringBuilder
buffer
=
new
StringBuilder
();
int
count
=
0
;
for
(
int
i
=
from
;
i
<
from
+
8
;
i
++)
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
{
buffer
.
append
(
pieces
.
get
(
i
).
getRepresentation
());
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
return
buffer
.
toString
();
if
(
positionState
[
i
][
j
]
!=
null
)
if
(
positionState
[
i
][
j
].
isType
(
type
)
&&
positionState
[
i
][
j
].
getColor
()
==
color
)
count
++;
}
}
return
count
;
}
}
String
print
()
{
String
print
()
{
StringBuilder
buffer
=
new
StringBuilder
();
StringBuilder
buffer
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
{
buffer
.
append
(
printRank
(
0
)
+
NEW_LINE
);
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
buffer
.
append
(
printRank
(
8
)
+
NEW_LINE
);
if
(
positionState
[
i
][
j
]
==
null
)
buffer
.
append
(
"."
);
for
(
int
i
=
3
;
i
<
7
;
i
++)
else
buffer
.
append
(
"........"
+
NEW_LINE
);
buffer
.
append
(
positionState
[
i
][
j
].
getRepresentation
());
buffer
.
append
(
printRank
(
16
)
+
NEW_LINE
);
buffer
.
append
(
printRank
(
24
)
+
NEW_LINE
);
return
buffer
.
toString
();
}
}
buffer
.
append
(
NEW_LINE
);
int
getCount
(
Piece
.
Type
type
,
Piece
.
Color
color
)
{
}
int
count
=
0
;
return
buffer
.
toString
();
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 {
...
@@ -54,45 +54,26 @@ public class Piece {
return
this
.
color
==
Color
.
Black
;
return
this
.
color
==
Color
.
Black
;
}
}
static
Piece
createWhitePawn
()
{
boolean
isType
(
Piece
.
Type
type
)
{
return
new
Piece
(
Color
.
White
,
Type
.
Pawn
);
return
this
.
getType
()
==
type
;
}
static
Piece
createBlackPawn
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
Pawn
);
}
}
static
Piece
createWhiteKing
()
{
static
Piece
createPawn
(
Color
color
)
{
return
new
Piece
(
Color
.
White
,
Type
.
King
);
return
new
Piece
(
color
,
Type
.
Pawn
);
}
static
Piece
createBlackKing
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
King
);
}
}
static
Piece
createKing
(
Color
color
)
{
static
Piece
createWhiteBishop
()
{
return
new
Piece
(
color
,
Type
.
King
);
return
new
Piece
(
Color
.
White
,
Type
.
Bishop
);
}
}
static
Piece
createBlackBishop
()
{
static
Piece
createBishop
(
Color
color
)
{
return
new
Piece
(
Color
.
Black
,
Type
.
Bishop
);
return
new
Piece
(
color
,
Type
.
Bishop
);
}
static
Piece
createWhiteRook
()
{
return
new
Piece
(
Color
.
White
,
Type
.
Rook
);
}
}
static
Piece
create
BlackRook
(
)
{
static
Piece
create
Rook
(
Color
color
)
{
return
new
Piece
(
Color
.
Black
,
Type
.
Rook
);
return
new
Piece
(
color
,
Type
.
Rook
);
}
}
static
Piece
createKnight
(
Color
color
)
{
static
Piece
createWhiteKnight
()
{
return
new
Piece
(
color
,
Type
.
Knight
);
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
create
BlackQueen
(
)
{
static
Piece
create
Queen
(
Color
color
)
{
return
new
Piece
(
Color
.
Black
,
Type
.
Queen
);
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 {
...
@@ -28,18 +28,23 @@ public class BoardTest extends TestCase {
StringUtil
.
appendNewLine
(
"rnbqkbnr"
),
StringUtil
.
appendNewLine
(
"rnbqkbnr"
),
board
.
print
());
board
.
print
());
assertEquals
(
16
,
board
.
color
Count
(
Piece
.
Color
.
Black
));
assertEquals
(
16
,
board
.
piece
Count
(
Piece
.
Color
.
Black
));
assertEquals
(
16
,
board
.
color
Count
(
Piece
.
Color
.
White
));
assertEquals
(
16
,
board
.
piece
Count
(
Piece
.
Color
.
White
));
}
}
public
void
testCountPieces
()
{
public
void
testCountPieces
()
{
board
.
initialize
();
board
.
initialize
();
assertEquals
(
8
,
board
.
getCount
(
Piece
.
Type
.
Pawn
,
Piece
.
Color
.
White
));
assertEquals
(
8
,
board
.
pieceCount
(
Piece
.
Type
.
Pawn
,
Piece
.
Color
.
White
));
assertEquals
(
1
,
board
.
getCount
(
Piece
.
Type
.
King
,
Piece
.
Color
.
Black
));
assertEquals
(
1
,
board
.
pieceCount
(
Piece
.
Type
.
King
,
Piece
.
Color
.
Black
));
assertEquals
(
1
,
board
.
getCount
(
Piece
.
Type
.
Queen
,
Piece
.
Color
.
White
));
assertEquals
(
1
,
board
.
pieceCount
(
Piece
.
Type
.
Queen
,
Piece
.
Color
.
White
));
assertEquals
(
2
,
board
.
getCount
(
Piece
.
Type
.
Bishop
,
Piece
.
Color
.
Black
));
assertEquals
(
2
,
board
.
pieceCount
(
Piece
.
Type
.
Bishop
,
Piece
.
Color
.
Black
));
assertEquals
(
2
,
board
.
getCount
(
Piece
.
Type
.
Knight
,
Piece
.
Color
.
White
));
assertEquals
(
2
,
board
.
pieceCount
(
Piece
.
Type
.
Knight
,
Piece
.
Color
.
White
));
assertEquals
(
2
,
board
.
getCount
(
Piece
.
Type
.
Rook
,
Piece
.
Color
.
Black
));
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;
...
@@ -9,12 +9,12 @@ import junit.framework.TestCase;
public
class
PieceTest
extends
TestCase
{
public
class
PieceTest
extends
TestCase
{
public
void
testCreate
()
{
public
void
testCreate
()
{
verifyCreation
(
Piece
.
create
WhitePawn
(),
Piece
.
createBlackPawn
(
),
Piece
.
Type
.
Pawn
,
'p'
);
verifyCreation
(
Piece
.
create
Pawn
(
Piece
.
Color
.
White
),
Piece
.
createPawn
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Pawn
,
'p'
);
verifyCreation
(
Piece
.
create
WhiteKing
(),
Piece
.
createBlackKing
(
),
Piece
.
Type
.
King
,
'k'
);
verifyCreation
(
Piece
.
create
King
(
Piece
.
Color
.
White
),
Piece
.
createKing
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
King
,
'k'
);
verifyCreation
(
Piece
.
create
WhiteBishop
(),
Piece
.
createBlackBishop
(
),
Piece
.
Type
.
Bishop
,
'b'
);
verifyCreation
(
Piece
.
create
Bishop
(
Piece
.
Color
.
White
),
Piece
.
createBishop
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Bishop
,
'b'
);
verifyCreation
(
Piece
.
create
WhiteRook
(),
Piece
.
createBlackRook
(
),
Piece
.
Type
.
Rook
,
'r'
);
verifyCreation
(
Piece
.
create
Rook
(
Piece
.
Color
.
White
),
Piece
.
createRook
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Rook
,
'r'
);
verifyCreation
(
Piece
.
create
WhiteKnight
(),
Piece
.
createBlackKnight
(
),
Piece
.
Type
.
Knight
,
'n'
);
verifyCreation
(
Piece
.
create
Knight
(
Piece
.
Color
.
White
),
Piece
.
createKnight
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Knight
,
'n'
);
verifyCreation
(
Piece
.
create
WhiteQueen
(),
Piece
.
createBlackQueen
(
),
Piece
.
Type
.
Queen
,
'q'
);
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
)
{
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