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
f0607e45
authored
May 26, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lesson 5 Exercise 8 complete
parent
af649a49
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
83 additions
and
56 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/main/java/com/example/paktalin/agilejava_exercises/Board.java
View file @
f0607e45
package
com
.
example
.
paktalin
.
agilejava_exercises
;
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
.
util
.
StringUtil
.
NEW_LINE
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
util
.
StringUtil
.
NEW_LINE
;
/**
/**
...
@@ -12,6 +16,9 @@ class Board {
...
@@ -12,6 +16,9 @@ class Board {
private
Piece
[][]
pieces
=
new
Piece
[
ROW_COUNT
][
COLUMN_COUNT
];
private
Piece
[][]
pieces
=
new
Piece
[
ROW_COUNT
][
COLUMN_COUNT
];
private
List
<
Piece
>
blackPieces
=
new
ArrayList
<>();
private
List
<
Piece
>
whitePieces
=
new
ArrayList
<>();
private
Board
()
{}
private
Board
()
{}
static
Board
createEmpty
()
{
static
Board
createEmpty
()
{
...
@@ -53,12 +60,23 @@ class Board {
...
@@ -53,12 +60,23 @@ class Board {
private
void
placePiece
(
Piece
piece
,
int
row
,
int
column
)
{
private
void
placePiece
(
Piece
piece
,
int
row
,
int
column
)
{
pieces
[
row
][
column
]
=
piece
;
pieces
[
row
][
column
]
=
piece
;
setPieceStrength
(
piece
,
column
);
setPieceStrength
(
piece
,
column
);
addToCollection
(
piece
);
}
}
void
placePiece
(
Piece
piece
,
String
position
)
{
void
placePiece
(
Piece
piece
,
String
position
)
{
placePiece
(
piece
,
retrieveRow
(
position
),
retrieveColumn
(
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
);
}
}
private
void
setPieceStrength
(
Piece
piece
,
int
column
)
{
private
void
setPieceStrength
(
Piece
piece
,
int
column
)
{
if
(
piece
.
isType
(
Piece
.
Type
.
Pawn
))
if
(
piece
.
isType
(
Piece
.
Type
.
Pawn
))
updatePawnsStrength
(
piece
.
getColor
(),
column
);
updatePawnsStrength
(
piece
.
getColor
(),
column
);
...
@@ -100,37 +118,25 @@ class Board {
...
@@ -100,37 +118,25 @@ class Board {
return
fileToColumn
(
position
.
toCharArray
()[
0
]);
return
fileToColumn
(
position
.
toCharArray
()[
0
]);
}
}
int
pieceCount
()
{
int
getPiecesCount
()
{
return
getBlackPiecesCount
()
+
getWhitePiecesCount
();
}
int
getPiecesCount
(
Piece
.
Type
type
,
List
<
Piece
>
pieces
)
{
int
count
=
0
;
int
count
=
0
;
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++
)
for
(
Piece
piece
:
pieces
)
if
(
pieces
[
row
][
column
]
!=
null
)
if
(
piece
.
isType
(
type
)
)
count
++;
count
++;
return
count
;
return
count
;
}
}
int
pieceCount
(
Piece
.
Color
color
)
{
int
getWhitePiecesCount
(
Piece
.
Type
type
)
{
int
counter
=
0
;
return
getPiecesCount
(
type
,
whitePieces
);
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
;
}
}
int
getBlackPiecesCount
(
Piece
.
Type
type
)
{
int
pieceCount
(
Piece
.
Type
type
,
Piece
.
Color
color
)
{
return
getPiecesCount
(
type
,
blackPieces
);
int
count
=
0
;
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
++;
}
}
return
count
;
}
}
String
print
()
{
String
print
()
{
...
@@ -175,10 +181,24 @@ class Board {
...
@@ -175,10 +181,24 @@ class Board {
return
strength
;
return
strength
;
}
}
List
<
Piece
>
getBlackPieces
()
{
return
blackPieces
;
}
List
<
Piece
>
getWhitePieces
()
{
return
whitePieces
;
}
double
getBlackStrength
()
{
double
getBlackStrength
()
{
return
getStrength
(
Piece
.
Color
.
Black
);
return
getStrength
(
Piece
.
Color
.
Black
);
}
}
double
getWhiteStrength
()
{
double
getWhiteStrength
()
{
return
getStrength
(
Piece
.
Color
.
White
);
return
getStrength
(
Piece
.
Color
.
White
);
}
}
int
getBlackPiecesCount
()
{
return
blackPieces
.
size
();
}
int
getWhitePiecesCount
()
{
return
whitePieces
.
size
();
}
}
}
\ No newline at end of file
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
View file @
f0607e45
package
com
.
example
.
paktalin
.
agilejava_exercises
;
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
android.support.annotation.NonNull
;
/**
/**
* Created by Paktalin on 23/05/2018.
* Created by Paktalin on 23/05/2018.
*/
*/
public
class
Piece
{
public
class
Piece
implements
Comparable
<
Piece
>{
enum
Type
{
Pawn
,
Knight
,
Rook
,
Bishop
,
Queen
,
King
}
enum
Color
{
White
,
Black
}
public
enum
Type
{
Pawn
,
Knight
,
Rook
,
Bishop
,
Queen
,
King
}
public
enum
Color
{
White
,
Black
}
private
Type
type
;
private
Type
type
;
private
Color
color
;
private
Color
color
;
...
@@ -61,6 +64,15 @@ public class Piece {
...
@@ -61,6 +64,15 @@ public class Piece {
strength
=
0.0
;
strength
=
0.0
;
}
}
@Override
public
int
compareTo
(
@NonNull
Piece
that
)
{
if
(
this
.
getStrength
()
>
that
.
getStrength
())
return
-
1
;
if
(
this
.
getStrength
()
<
that
.
getStrength
())
return
1
;
return
0
;
}
void
setStrength
(
double
strength
)
{
void
setStrength
(
double
strength
)
{
this
.
strength
=
strength
;
this
.
strength
=
strength
;
}
}
...
...
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
View file @
f0607e45
...
@@ -2,6 +2,8 @@ package com.example.paktalin.agilejava_exercises;
...
@@ -2,6 +2,8 @@ package com.example.paktalin.agilejava_exercises;
import
junit.framework.TestCase
;
import
junit.framework.TestCase
;
import
java.util.List
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Type
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Type
.*;
...
@@ -15,34 +17,9 @@ public class BoardTest extends TestCase {
...
@@ -15,34 +17,9 @@ public class BoardTest extends TestCase {
public
void
testCreate
()
{
public
void
testCreate
()
{
board
=
Board
.
createInitialized
();
board
=
Board
.
createInitialized
();
assertEquals
(
32
,
board
.
pieceCount
());
assertEquals
(
32
,
board
.
getPiecesCount
());
assertEquals
(
16
,
board
.
getBlackPiecesCount
());
assertEquals
(
16
,
board
.
pieceCount
(
Black
));
assertEquals
(
16
,
board
.
getWhitePiecesCount
());
assertEquals
(
16
,
board
.
pieceCount
(
White
));
}
public
void
testCountPieces
()
{
board
=
Board
.
createInitialized
();
assertEquals
(
8
,
board
.
pieceCount
(
Pawn
,
White
));
assertEquals
(
1
,
board
.
pieceCount
(
King
,
Black
));
assertEquals
(
1
,
board
.
pieceCount
(
Queen
,
White
));
assertEquals
(
2
,
board
.
pieceCount
(
Bishop
,
Black
));
assertEquals
(
2
,
board
.
pieceCount
(
Knight
,
White
));
assertEquals
(
2
,
board
.
pieceCount
(
Rook
,
Black
));
}
public
void
testPieceLocation
()
{
board
=
Board
.
createInitialized
();
Piece
piece
=
board
.
getPieceAtPosition
(
"a8"
);
assertEquals
(
Rook
,
piece
.
getType
());
assertEquals
(
Black
,
piece
.
getColor
());
piece
=
board
.
getPieceAtPosition
(
"e1"
);
assertEquals
(
King
,
piece
.
getType
());
assertEquals
(
White
,
piece
.
getColor
());
}
}
public
void
testPlacePieces
()
{
public
void
testPlacePieces
()
{
...
@@ -80,6 +57,10 @@ public class BoardTest extends TestCase {
...
@@ -80,6 +57,10 @@ public class BoardTest extends TestCase {
verifyStrength
(
Piece
.
createKnight
(
White
),
"f4"
,
20.0
,
19.5
,
2.5
);
verifyStrength
(
Piece
.
createKnight
(
White
),
"f4"
,
20.0
,
19.5
,
2.5
);
verifyPrint
();
}
private
void
verifyPrint
()
{
assertEquals
(
assertEquals
(
".KR..... 8\n"
+
".KR..... 8\n"
+
"P.PB.... 7\n"
+
"P.PB.... 7\n"
+
...
@@ -100,4 +81,17 @@ public class BoardTest extends TestCase {
...
@@ -100,4 +81,17 @@ public class BoardTest extends TestCase {
assertEquals
(
whiteStrength
,
board
.
getWhiteStrength
());
assertEquals
(
whiteStrength
,
board
.
getWhiteStrength
());
assertEquals
(
pieceStrength
,
piece
.
getStrength
());
assertEquals
(
pieceStrength
,
piece
.
getStrength
());
}
}
public
void
testColorCollections
()
{
board
=
Board
.
createInitialized
();
List
<
Piece
>
blackPieces
=
board
.
getBlackPieces
();
assertEquals
(
9.0
,
blackPieces
.
get
(
0
).
getStrength
());
assertEquals
(
5.0
,
blackPieces
.
get
(
1
).
getStrength
());
assertEquals
(
5.0
,
blackPieces
.
get
(
2
).
getStrength
());
assertEquals
(
3.0
,
blackPieces
.
get
(
3
).
getStrength
());
assertEquals
(
3.0
,
blackPieces
.
get
(
4
).
getStrength
());
assertEquals
(
2.5
,
blackPieces
.
get
(
5
).
getStrength
());
assertEquals
(
2.5
,
blackPieces
.
get
(
5
).
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