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
59a9a544
authored
May 26, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lesson 5 Exercise 7 half done
parent
e73c7a32
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
91 additions
and
32 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 @
59a9a544
...
@@ -47,6 +47,14 @@ class Board {
...
@@ -47,6 +47,14 @@ class Board {
placePiece
(
piece
,
retrieveRow
(
position
),
retrieveColumn
(
position
));
placePiece
(
piece
,
retrieveRow
(
position
),
retrieveColumn
(
position
));
}
}
private
int
retrieveRow
(
String
position
)
{
return
ROW_COUNT
-
Integer
.
parseInt
(
position
.
split
(
""
)[
1
]);
}
private
int
retrieveColumn
(
String
position
)
{
return
fileToColumn
(
position
.
toCharArray
()[
0
]);
}
int
pieceCount
()
{
int
pieceCount
()
{
int
count
=
0
;
int
count
=
0
;
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
...
@@ -89,8 +97,9 @@ class Board {
...
@@ -89,8 +97,9 @@ class Board {
else
else
buffer
.
append
(
board
[
i
][
j
].
getRepresentation
());
buffer
.
append
(
board
[
i
][
j
].
getRepresentation
());
}
}
buffer
.
append
(
NEW_LINE
);
buffer
.
append
(
" "
+
(
ROW_COUNT
-
i
)
+
NEW_LINE
);
}
}
buffer
.
append
(
"abcdefgh"
);
return
buffer
.
toString
();
return
buffer
.
toString
();
}
}
...
@@ -99,18 +108,30 @@ class Board {
...
@@ -99,18 +108,30 @@ class Board {
return
(
int
)
file
-
(
int
)
firstColumnLetter
;
return
(
int
)
file
-
(
int
)
firstColumnLetter
;
}
}
private
int
retrieveRow
(
String
positio
n
)
{
Piece
getPieceAtPosition
(
int
row
,
int
colum
n
)
{
return
Integer
.
parseInt
(
position
.
split
(
""
)[
1
])
-
1
;
return
board
[
row
][
column
]
;
}
}
private
int
retrieveColum
n
(
String
position
)
{
Piece
getPieceAtPositio
n
(
String
position
)
{
return
fileToColumn
(
position
.
toCharArray
()[
0
]
);
return
getPieceAtPosition
(
retrieveRow
(
position
),
retrieveColumn
(
position
)
);
}
}
Piece
getPieceAtPosition
(
String
position
)
{
double
getStrength
(
Piece
.
Color
color
)
{
int
row
=
retrieveRow
(
position
);
double
strength
=
0
;
int
column
=
retrieveColumn
(
position
);
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
{
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
Piece
piece
=
getPieceAtPosition
(
i
,
j
);
if
(
piece
!=
null
&&
piece
.
getColor
()
==
color
)
strength
+=
piece
.
getStrength
();
}
}
return
strength
;
}
return
board
[
row
][
column
];
double
getBlackStrength
()
{
return
getStrength
(
Piece
.
Color
.
Black
);
}
double
getWhiteStrength
()
{
return
getStrength
(
Piece
.
Color
.
White
);
}
}
}
}
\ No newline at end of file
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
View file @
59a9a544
...
@@ -46,14 +46,26 @@ public class Piece {
...
@@ -46,14 +46,26 @@ public class Piece {
return
representation
;
return
representation
;
}
}
double
getStrength
()
{
if
(
type
==
Type
.
Queen
)
return
9
;
if
(
type
==
Type
.
Rook
)
return
5
;
if
(
type
==
Type
.
Bishop
)
return
3
;
if
(
type
==
Type
.
Knight
)
return
2.5
;
if
(
type
==
Type
.
Pawn
)
return
0.5
;
return
0
;
}
boolean
isWhite
()
{
boolean
isWhite
()
{
return
this
.
color
==
Color
.
White
;
return
this
.
color
==
Color
.
White
;
}
}
boolean
isBlack
()
{
boolean
isBlack
()
{
return
this
.
color
==
Color
.
Black
;
return
this
.
color
==
Color
.
Black
;
}
}
boolean
isType
(
Piece
.
Type
type
)
{
boolean
isType
(
Piece
.
Type
type
)
{
return
this
.
getType
()
==
type
;
return
this
.
getType
()
==
type
;
}
}
...
...
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
View file @
59a9a544
...
@@ -20,16 +20,8 @@ public class BoardTest extends TestCase {
...
@@ -20,16 +20,8 @@ public class BoardTest extends TestCase {
public
void
testCreate
()
{
public
void
testCreate
()
{
board
.
initialize
();
board
.
initialize
();
assertEquals
(
32
,
board
.
pieceCount
());
String
blankRank
=
StringUtil
.
appendNewLine
(
"........"
);
assertEquals
(
assertEquals
(
32
,
board
.
pieceCount
());
StringUtil
.
appendNewLine
(
"RNBQKBNR"
)
+
StringUtil
.
appendNewLine
(
"PPPPPPPP"
)
+
blankRank
+
blankRank
+
blankRank
+
blankRank
+
StringUtil
.
appendNewLine
(
"pppppppp"
)
+
StringUtil
.
appendNewLine
(
"rnbqkbnr"
),
board
.
print
());
assertEquals
(
16
,
board
.
pieceCount
(
Black
));
assertEquals
(
16
,
board
.
pieceCount
(
Black
));
assertEquals
(
16
,
board
.
pieceCount
(
White
));
assertEquals
(
16
,
board
.
pieceCount
(
White
));
...
@@ -52,11 +44,11 @@ public class BoardTest extends TestCase {
...
@@ -52,11 +44,11 @@ public class BoardTest extends TestCase {
Piece
piece
=
board
.
getPieceAtPosition
(
"a8"
);
Piece
piece
=
board
.
getPieceAtPosition
(
"a8"
);
assertEquals
(
Rook
,
piece
.
getType
());
assertEquals
(
Rook
,
piece
.
getType
());
assertEquals
(
White
,
piece
.
getColor
());
assertEquals
(
Black
,
piece
.
getColor
());
piece
=
board
.
getPieceAtPosition
(
"e1"
);
piece
=
board
.
getPieceAtPosition
(
"e1"
);
assertEquals
(
King
,
piece
.
getType
());
assertEquals
(
King
,
piece
.
getType
());
assertEquals
(
Black
,
piece
.
getColor
());
assertEquals
(
White
,
piece
.
getColor
());
}
}
public
void
testPlacePieces
()
{
public
void
testPlacePieces
()
{
...
@@ -67,4 +59,47 @@ public class BoardTest extends TestCase {
...
@@ -67,4 +59,47 @@ public class BoardTest extends TestCase {
assertEquals
(
Bishop
,
bishop
.
getType
());
assertEquals
(
Bishop
,
bishop
.
getType
());
assertEquals
(
Black
,
bishop
.
getColor
());
assertEquals
(
Black
,
bishop
.
getColor
());
}
}
public
void
testOverallStrength
()
{
verifyStrength
(
Piece
.
createQueen
(
Black
),
"e6"
,
9.0
,
0.0
);
verifyStrength
(
Piece
.
createQueen
(
White
),
"g4"
,
9.0
,
9.0
);
verifyStrength
(
Piece
.
createKing
(
Black
),
"b8"
,
9.0
,
9.0
);
verifyStrength
(
Piece
.
createKing
(
White
),
"f1"
,
9.0
,
9.0
);
verifyStrength
(
Piece
.
createRook
(
Black
),
"c8"
,
14.0
,
9.0
);
verifyStrength
(
Piece
.
createRook
(
White
),
"e1"
,
14.0
,
14.0
);
verifyStrength
(
Piece
.
createPawn
(
Black
),
"a7"
,
15.0
,
14.0
);
verifyStrength
(
Piece
.
createPawn
(
White
),
"f2"
,
15.0
,
14.5
);
verifyStrength
(
Piece
.
createPawn
(
Black
),
"c7"
,
16.0
,
14.5
);
verifyStrength
(
Piece
.
createPawn
(
White
),
"g2"
,
16.0
,
15.0
);
verifyStrength
(
Piece
.
createBishop
(
Black
),
"d7"
,
19.0
,
15.0
);
verifyStrength
(
Piece
.
createPawn
(
White
),
"f3"
,
19.0
,
15.5
);
verifyStrength
(
Piece
.
createPawn
(
Black
),
"b6"
,
20.0
,
15.5
);
verifyStrength
(
Piece
.
createPawn
(
White
),
"h3"
,
18.5
,
16.0
);
verifyStrength
(
Piece
.
createKnight
(
White
),
"f4"
,
18.5
,
18.5
);
assertEquals
(
".KR..... 8\n"
+
"P.PB.... 7\n"
+
".P..Q... 6\n"
+
"........ 5\n"
+
".....nq. 4\n"
+
".....p.p 3\n"
+
".....pp. 2\n"
+
"....rk.. 1\n"
+
"abcdefgh"
,
board
.
print
());
}
private
void
verifyStrength
(
Piece
piece
,
String
position
,
double
blackStrength
,
double
whiteStrength
)
{
board
.
placePiece
(
piece
,
position
);
assertEquals
(
blackStrength
,
board
.
getBlackStrength
());
assertEquals
(
whiteStrength
,
board
.
getWhiteStrength
());
}
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/PieceTest.java
View file @
59a9a544
...
@@ -29,14 +29,4 @@ public class PieceTest extends TestCase {
...
@@ -29,14 +29,4 @@ public class PieceTest extends TestCase {
assertEquals
(
type
,
blackPiece
.
getType
());
assertEquals
(
type
,
blackPiece
.
getType
());
assertEquals
(
Character
.
toUpperCase
(
representation
),
blackPiece
.
getRepresentation
());
assertEquals
(
Character
.
toUpperCase
(
representation
),
blackPiece
.
getRepresentation
());
}
}
public
void
testLanguage
()
{
assertEquals
(
8
,
Integer
.
parseInt
(
"a8"
.
split
(
""
)[
1
]));
assertEquals
(
0
,
fileToColumn
(
"a8"
.
toCharArray
()[
0
]));
}
private
int
fileToColumn
(
char
file
)
{
char
firstColumnLetter
=
'a'
;
return
(
int
)
file
-
(
int
)
firstColumnLetter
;
}
}
}
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