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
a774fec3
authored
6 years ago
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring for exercise 7
parent
611a32a1
master
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
55 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/main/java/com/example/paktalin/agilejava_exercises/util/StringUtil.java
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
View file @
a774fec3
...
...
@@ -7,32 +7,44 @@ import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
*/
class
Board
{
static
final
int
ROW_COUNT
=
8
;
static
final
int
COLUMN_COUNT
=
8
;
private
static
final
int
ROW_COUNT
=
8
;
private
static
final
int
COLUMN_COUNT
=
8
;
private
Piece
[][]
board
=
new
Piece
[
ROW_COUNT
][
COLUMN_COUNT
];
private
Piece
[][]
pieces
=
new
Piece
[
ROW_COUNT
][
COLUMN_COUNT
];
void
initialize
()
{
private
Board
()
{}
static
Board
createEmpty
()
{
return
new
Board
();
}
static
Board
createInitialized
()
{
Board
board
=
new
Board
();
board
.
initialize
();
return
board
;
}
private
void
initialize
()
{
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
r
ank
)
{
placePiece
(
Piece
.
createRook
(
color
),
r
ank
,
'a'
);
placePiece
(
Piece
.
createKnight
(
color
),
r
ank
,
'b'
);
placePiece
(
Piece
.
createBishop
(
color
),
r
ank
,
'c'
);
placePiece
(
Piece
.
createQueen
(
color
),
r
ank
,
'd'
);
placePiece
(
Piece
.
createKing
(
color
),
r
ank
,
'e'
);
placePiece
(
Piece
.
createBishop
(
color
),
r
ank
,
'f'
);
placePiece
(
Piece
.
createKnight
(
color
),
r
ank
,
'g'
);
placePiece
(
Piece
.
createRook
(
color
),
r
ank
,
'h'
);
private
void
initializeKingRank
(
Piece
.
Color
color
,
int
r
ow
)
{
placePiece
(
Piece
.
createRook
(
color
),
r
ow
,
'a'
);
placePiece
(
Piece
.
createKnight
(
color
),
r
ow
,
'b'
);
placePiece
(
Piece
.
createBishop
(
color
),
r
ow
,
'c'
);
placePiece
(
Piece
.
createQueen
(
color
),
r
ow
,
'd'
);
placePiece
(
Piece
.
createKing
(
color
),
r
ow
,
'e'
);
placePiece
(
Piece
.
createBishop
(
color
),
r
ow
,
'f'
);
placePiece
(
Piece
.
createKnight
(
color
),
r
ow
,
'g'
);
placePiece
(
Piece
.
createRook
(
color
),
r
ow
,
'h'
);
}
private
void
initializePawnRank
(
Piece
.
Color
color
,
int
r
ank
)
{
for
(
int
i
=
0
;
i
<
COLUMN_COUNT
;
i
++)
placePiece
(
Piece
.
createPawn
(
color
),
r
ank
,
i
);
private
void
initializePawnRank
(
Piece
.
Color
color
,
int
r
ow
)
{
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
placePiece
(
Piece
.
createPawn
(
color
),
r
ow
,
column
);
}
private
void
placePiece
(
Piece
piece
,
int
row
,
char
file
)
{
...
...
@@ -40,7 +52,7 @@ class Board {
}
private
void
placePiece
(
Piece
piece
,
int
row
,
int
column
)
{
board
[
row
][
column
]
=
piece
;
pieces
[
row
][
column
]
=
piece
;
}
void
placePiece
(
Piece
piece
,
String
position
)
{
...
...
@@ -57,9 +69,9 @@ class Board {
int
pieceCount
()
{
int
count
=
0
;
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
if
(
board
[
i
][
j
]
!=
null
)
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
if
(
pieces
[
row
][
column
]
!=
null
)
count
++;
return
count
;
...
...
@@ -67,10 +79,10 @@ class Board {
int
pieceCount
(
Piece
.
Color
color
)
{
int
counter
=
0
;
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
if
(
board
[
i
][
j
]
!=
null
)
if
(
board
[
i
][
j
].
getColor
()
==
color
)
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
{
if
(
pieces
[
row
][
column
]
!=
null
)
if
(
pieces
[
row
][
column
].
getColor
()
==
color
)
counter
++;
}
return
counter
;
...
...
@@ -78,10 +90,10 @@ class Board {
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
(
board
[
i
][
j
]
!=
null
)
if
(
board
[
i
][
j
].
isType
(
type
)
&&
board
[
i
][
j
].
getColor
()
==
color
)
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
{
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
{
if
(
pieces
[
row
][
column
]
!=
null
)
if
(
pieces
[
row
][
column
].
isType
(
type
)
&&
pieces
[
row
][
column
].
getColor
()
==
color
)
count
++;
}
}
...
...
@@ -90,14 +102,14 @@ class Board {
String
print
()
{
StringBuilder
buffer
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
{
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
if
(
board
[
i
][
j
]
==
null
)
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
{
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
{
if
(
pieces
[
row
][
column
]
==
null
)
buffer
.
append
(
"."
);
else
buffer
.
append
(
board
[
i
][
j
].
getRepresentation
());
buffer
.
append
(
pieces
[
row
][
column
].
getRepresentation
());
}
buffer
.
append
(
" "
+
(
ROW_COUNT
-
i
)
+
NEW_LINE
);
buffer
.
append
(
" "
+
(
ROW_COUNT
-
row
)
+
NEW_LINE
);
}
buffer
.
append
(
"abcdefgh"
);
return
buffer
.
toString
();
...
...
@@ -108,15 +120,15 @@ class Board {
return
(
int
)
file
-
(
int
)
firstColumnLetter
;
}
Piece
getPieceAtPosition
(
int
row
,
int
column
)
{
return
board
[
row
][
column
];
private
Piece
getPieceAtPosition
(
int
row
,
int
column
)
{
return
pieces
[
row
][
column
];
}
Piece
getPieceAtPosition
(
String
position
)
{
return
getPieceAtPosition
(
retrieveRow
(
position
),
retrieveColumn
(
position
));
}
double
getStrength
(
Piece
.
Color
color
)
{
private
double
getStrength
(
Piece
.
Color
color
)
{
double
strength
=
0
;
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
{
...
...
This diff is collapsed.
Click to expand it.
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
View file @
a774fec3
...
...
@@ -13,7 +13,7 @@ public class Piece {
private
Piece
()
{}
Piece
(
final
Color
color
,
final
Type
type
)
{
private
Piece
(
final
Color
color
,
final
Type
type
)
{
this
.
color
=
color
;
this
.
type
=
type
;
}
...
...
@@ -21,7 +21,7 @@ public class Piece {
public
Color
getColor
()
{
return
color
;
}
public
Type
getType
()
{
Type
getType
()
{
return
type
;
}
...
...
This diff is collapsed.
Click to expand it.
app/src/main/java/com/example/paktalin/agilejava_exercises/util/StringUtil.java
View file @
a774fec3
...
...
@@ -8,11 +8,4 @@ public class StringUtil {
public
static
final
String
NEW_LINE
=
"\n"
;
private
StringUtil
()
{}
public
static
String
appendNewLine
(
String
string
)
{
return
string
+
NEW_LINE
;
}
}
This diff is collapsed.
Click to expand it.
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
View file @
a774fec3
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
com.example.paktalin.agilejava_exercises.util.StringUtil
;
import
junit.framework.TestCase
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.*;
...
...
@@ -14,12 +12,8 @@ import static com.example.paktalin.agilejava_exercises.Piece.Type.*;
public
class
BoardTest
extends
TestCase
{
private
Board
board
;
protected
void
setUp
()
{
board
=
new
Board
();
}
public
void
testCreate
()
{
board
.
initialize
();
board
=
Board
.
createInitialized
();
assertEquals
(
32
,
board
.
pieceCount
());
...
...
@@ -28,7 +22,7 @@ public class BoardTest extends TestCase {
}
public
void
testCountPieces
()
{
board
.
initialize
();
board
=
Board
.
createInitialized
();
assertEquals
(
8
,
board
.
pieceCount
(
Pawn
,
White
));
assertEquals
(
1
,
board
.
pieceCount
(
King
,
Black
));
...
...
@@ -39,7 +33,7 @@ public class BoardTest extends TestCase {
}
public
void
testPieceLocation
()
{
board
.
initialize
();
board
=
Board
.
createInitialized
();
Piece
piece
=
board
.
getPieceAtPosition
(
"a8"
);
...
...
@@ -52,7 +46,7 @@ public class BoardTest extends TestCase {
}
public
void
testPlacePieces
()
{
board
.
initialize
();
board
=
Board
.
createInitialized
();
board
.
placePiece
(
Piece
.
createBishop
(
Black
),
"a8"
);
Piece
bishop
=
board
.
getPieceAtPosition
(
"a8"
);
...
...
@@ -61,6 +55,8 @@ public class BoardTest extends TestCase {
}
public
void
testOverallStrength
()
{
board
=
Board
.
createEmpty
();
verifyStrength
(
Piece
.
createQueen
(
Black
),
"e6"
,
9.0
,
0.0
);
verifyStrength
(
Piece
.
createQueen
(
White
),
"g4"
,
9.0
,
9.0
);
...
...
This diff is collapsed.
Click to expand it.
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