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
1263e172
authored
May 27, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring for Lesson 5 Exercise 9
parent
f0607e45
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
145 additions
and
73 deletions
app/src/main/java/com/example/paktalin/agilejava_exercises/BlackSide.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Side.java
app/src/main/java/com/example/paktalin/agilejava_exercises/WhiteSide.java
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
app/src/main/java/com/example/paktalin/agilejava_exercises/BlackSide.java
0 → 100644
View file @
1263e172
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
/**
* Created by Paktalin on 26/05/2018.
*/
class
BlackSide
implements
Side
{
private
List
<
Piece
>
pieces
=
new
ArrayList
<>();
@Override
public
List
<
Piece
>
getPieces
()
{
return
pieces
;
}
@Override
public
double
getStrength
()
{
double
strength
=
0.0
;
for
(
Piece
piece
:
pieces
)
strength
+=
piece
.
getStrength
();
return
strength
;
}
@Override
public
int
getPiecesCount
()
{
return
pieces
.
size
();
}
@Override
public
int
getPiecesCount
(
Piece
.
Type
type
)
{
int
count
=
0
;
for
(
Piece
piece
:
pieces
)
if
(
piece
.
isType
(
type
))
count
++;
return
count
;
}
@Override
public
void
addPiece
(
Piece
piece
)
{
pieces
.
add
(
piece
);
Collections
.
sort
(
pieces
);
}
}
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
View file @
1263e172
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
util
.
StringUtil
.
NEW_LINE
;
/**
...
...
@@ -16,8 +15,8 @@ class Board {
private
Piece
[][]
pieces
=
new
Piece
[
ROW_COUNT
][
COLUMN_COUNT
];
private
List
<
Piece
>
blackPieces
=
new
ArrayList
<>
();
private
List
<
Piece
>
whitePieces
=
new
ArrayList
<>
();
private
WhiteSide
whiteSide
=
new
WhiteSide
();
private
BlackSide
blackSide
=
new
BlackSide
();
private
Board
()
{}
...
...
@@ -32,10 +31,10 @@ class Board {
}
private
void
initialize
()
{
initializeKingRank
(
Piece
.
Color
.
Black
,
0
);
initializePawnRank
(
Piece
.
Color
.
Black
,
1
);
initializePawnRank
(
Piece
.
Color
.
White
,
6
);
initializeKingRank
(
Piece
.
Color
.
White
,
7
);
initializeKingRank
(
Black
,
0
);
initializePawnRank
(
Black
,
1
);
initializePawnRank
(
White
,
6
);
initializeKingRank
(
White
,
7
);
}
private
void
initializeKingRank
(
Piece
.
Color
color
,
int
row
)
{
...
...
@@ -54,6 +53,9 @@ class Board {
placePiece
(
Piece
.
createPawn
(
color
),
row
,
column
);
}
void
placePiece
(
Piece
piece
,
String
position
)
{
placePiece
(
piece
,
retrieveRow
(
position
),
retrieveColumn
(
position
));
}
private
void
placePiece
(
Piece
piece
,
int
row
,
char
file
)
{
placePiece
(
piece
,
row
,
fileToColumn
(
file
));
}
...
...
@@ -63,18 +65,8 @@ class Board {
addToCollection
(
piece
);
}
void
placePiece
(
Piece
piece
,
String
position
)
{
placePiece
(
piece
,
retrieveRow
(
position
),
retrieveColumn
(
position
));
}
void
addToCollection
(
Piece
piece
)
{
if
(
piece
.
isWhite
())
{
whitePieces
.
add
(
piece
);
Collections
.
sort
(
whitePieces
);
}
else
{
blackPieces
.
add
(
piece
);
Collections
.
sort
(
blackPieces
);
}
getSide
(
piece
.
getColor
()).
addPiece
(
piece
);
}
private
void
setPieceStrength
(
Piece
piece
,
int
column
)
{
...
...
@@ -113,32 +105,10 @@ class Board {
private
int
retrieveRow
(
String
position
)
{
return
ROW_COUNT
-
Integer
.
parseInt
(
position
.
split
(
""
)[
1
]);
}
private
int
retrieveColumn
(
String
position
)
{
return
fileToColumn
(
position
.
toCharArray
()[
0
]);
}
int
getPiecesCount
()
{
return
getBlackPiecesCount
()
+
getWhitePiecesCount
();
}
int
getPiecesCount
(
Piece
.
Type
type
,
List
<
Piece
>
pieces
)
{
int
count
=
0
;
for
(
Piece
piece
:
pieces
)
if
(
piece
.
isType
(
type
))
count
++;
return
count
;
}
int
getWhitePiecesCount
(
Piece
.
Type
type
)
{
return
getPiecesCount
(
type
,
whitePieces
);
}
int
getBlackPiecesCount
(
Piece
.
Type
type
)
{
return
getPiecesCount
(
type
,
blackPieces
);
}
String
print
()
{
StringBuilder
buffer
=
new
StringBuilder
();
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
{
...
...
@@ -167,38 +137,25 @@ class Board {
return
getPieceAtPosition
(
retrieveRow
(
position
),
retrieveColumn
(
position
));
}
private
double
getStrength
(
Piece
.
Color
color
)
{
double
strength
=
0
;
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
{
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
{
Piece
piece
=
getPieceAtPosition
(
row
,
column
);
if
(
piece
!=
null
&&
piece
.
getColor
()
==
color
)
{
strength
+=
piece
.
getStrength
();
}
}
}
return
strength
;
Side
getSide
(
Piece
.
Color
color
){
if
(
color
==
White
)
return
whiteSide
;
return
blackSide
;
}
List
<
Piece
>
getBlackPieces
()
{
return
blackPieces
;
}
List
<
Piece
>
getWhitePieces
()
{
return
whitePieces
;
int
getPiecesCount
()
{
return
getPiecesCount
(
Black
)
+
getPiecesCount
(
White
);
}
double
getBlackStrength
()
{
return
getStrength
(
Piece
.
Color
.
Black
);
double
getStrength
(
Piece
.
Color
color
)
{
return
getSide
(
color
).
getStrength
();
}
double
getWhiteStrength
(
)
{
return
getS
trength
(
Piece
.
Color
.
White
);
List
<
Piece
>
getPieces
(
Piece
.
Color
color
)
{
return
getS
ide
(
color
).
getPieces
(
);
}
int
getBlackPiecesCount
()
{
return
blackPieces
.
size
();
int
getPiecesCount
(
Piece
.
Color
color
){
return
getSide
(
color
).
getPiecesCount
();
}
int
get
WhitePiecesCount
()
{
return
whitePieces
.
size
(
);
int
get
PiecesCount
(
Piece
.
Color
color
,
Piece
.
Type
type
)
{
return
getSide
(
color
).
getPiecesCount
(
type
);
}
}
\ No newline at end of file
app/src/main/java/com/example/paktalin/agilejava_exercises/Side.java
0 → 100644
View file @
1263e172
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
java.util.List
;
/**
* Created by Paktalin on 26/05/2018.
*/
interface
Side
{
List
<
Piece
>
getPieces
();
double
getStrength
();
int
getPiecesCount
();
int
getPiecesCount
(
Piece
.
Type
type
);
void
addPiece
(
Piece
piece
);
}
app/src/main/java/com/example/paktalin/agilejava_exercises/WhiteSide.java
0 → 100644
View file @
1263e172
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
/**
* Created by Paktalin on 26/05/2018.
*/
public
class
WhiteSide
implements
Side
{
private
List
<
Piece
>
pieces
=
new
ArrayList
<>();
@Override
public
List
<
Piece
>
getPieces
()
{
return
pieces
;
}
@Override
public
double
getStrength
()
{
double
strength
=
0.0
;
for
(
Piece
piece
:
pieces
)
strength
+=
piece
.
getStrength
();
return
strength
;
}
@Override
public
int
getPiecesCount
()
{
return
pieces
.
size
();
}
@Override
public
int
getPiecesCount
(
Piece
.
Type
type
)
{
int
count
=
0
;
for
(
Piece
piece
:
pieces
)
if
(
piece
.
isType
(
type
))
count
++;
return
count
;
}
@Override
public
void
addPiece
(
Piece
piece
)
{
pieces
.
add
(
piece
);
Collections
.
sort
(
pieces
);
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
View file @
1263e172
...
...
@@ -18,8 +18,8 @@ public class BoardTest extends TestCase {
board
=
Board
.
createInitialized
();
assertEquals
(
32
,
board
.
getPiecesCount
());
assertEquals
(
16
,
board
.
get
BlackPiecesCount
(
));
assertEquals
(
16
,
board
.
get
WhitePiecesCount
(
));
assertEquals
(
16
,
board
.
get
PiecesCount
(
Black
));
assertEquals
(
16
,
board
.
get
PiecesCount
(
White
));
}
public
void
testPlacePieces
()
{
...
...
@@ -77,15 +77,15 @@ public class BoardTest extends TestCase {
private
void
verifyStrength
(
Piece
piece
,
String
position
,
double
blackStrength
,
double
whiteStrength
,
double
pieceStrength
)
{
board
.
placePiece
(
piece
,
position
);
assertEquals
(
blackStrength
,
board
.
get
BlackStrength
(
));
assertEquals
(
whiteStrength
,
board
.
get
WhiteStrength
(
));
assertEquals
(
blackStrength
,
board
.
get
Strength
(
Black
));
assertEquals
(
whiteStrength
,
board
.
get
Strength
(
White
));
assertEquals
(
pieceStrength
,
piece
.
getStrength
());
}
public
void
testColorCollections
()
{
board
=
Board
.
createInitialized
();
List
<
Piece
>
blackPieces
=
board
.
get
BlackPieces
(
);
List
<
Piece
>
blackPieces
=
board
.
get
Pieces
(
Black
);
assertEquals
(
9.0
,
blackPieces
.
get
(
0
).
getStrength
());
assertEquals
(
5.0
,
blackPieces
.
get
(
1
).
getStrength
());
assertEquals
(
5.0
,
blackPieces
.
get
(
2
).
getStrength
());
...
...
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