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
75f9d515
authored
Jun 01, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Created different piece classes
parent
cb3f5fad
Hide whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
265 additions
and
171 deletions
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
app/src/main/java/com/example/paktalin/agilejava_exercises/BoardLayout.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Game.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Position.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Side.java
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Bishop.java
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/King.java
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Knight.java
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Pawn.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java → app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Piece.java
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Queen.java
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Rook.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/test/java/com/example/paktalin/agilejava_exercises/PositionTest.java
app/src/test/java/com/example/paktalin/agilejava_exercises/moves/KingMoveStrategyTest.java
app/src/test/java/com/example/paktalin/agilejava_exercises/moves/MoveStrategyTest.java
app/src/test/java/com/example/paktalin/agilejava_exercises/moves/QueenMoveStrategyTest.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
com.example.paktalin.agilejava_exercises.pieces.Piece
;
import
java.util.List
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
.
Piece
.
Color
.*;
/**
* Created by Paktalin on 23/05/2018.
...
...
@@ -80,7 +82,4 @@ public class Board {
int
getPiecesCount
(
Piece
.
Color
color
){
return
getSide
(
color
).
getPiecesCount
();
}
int
getPiecesCount
(
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/BoardLayout.java
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.
Black
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.
White
;
import
com.example.paktalin.agilejava_exercises.pieces.Bishop
;
import
com.example.paktalin.agilejava_exercises.pieces.King
;
import
com.example.paktalin.agilejava_exercises.pieces.Knight
;
import
com.example.paktalin.agilejava_exercises.pieces.Pawn
;
import
com.example.paktalin.agilejava_exercises.pieces.Piece
;
import
com.example.paktalin.agilejava_exercises.pieces.Queen
;
import
com.example.paktalin.agilejava_exercises.pieces.Rook
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
.
Piece
.
Color
.
Black
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
.
Piece
.
Color
.
White
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
util
.
StringUtil
.
NEW_LINE
;
/**
...
...
@@ -31,19 +39,19 @@ class BoardLayout {
}
private
void
initializeKingRank
(
Piece
.
Color
color
,
int
row
)
{
placePiece
(
Piece
.
create
Rook
(
color
),
Position
.
create
(
row
,
0
));
placePiece
(
Piece
.
create
Knight
(
color
),
Position
.
create
(
row
,
1
));
placePiece
(
Piece
.
create
Bishop
(
color
),
Position
.
create
(
row
,
2
));
placePiece
(
Piece
.
create
Queen
(
color
),
Position
.
create
(
row
,
3
));
placePiece
(
Piece
.
create
King
(
color
),
Position
.
create
(
row
,
4
));
placePiece
(
Piece
.
create
Bishop
(
color
),
Position
.
create
(
row
,
5
));
placePiece
(
Piece
.
create
Knight
(
color
),
Position
.
create
(
row
,
6
));
placePiece
(
Piece
.
create
Rook
(
color
),
Position
.
create
(
row
,
7
));
placePiece
(
new
Rook
(
color
),
Position
.
create
(
row
,
0
));
placePiece
(
new
Knight
(
color
),
Position
.
create
(
row
,
1
));
placePiece
(
new
Bishop
(
color
),
Position
.
create
(
row
,
2
));
placePiece
(
new
Queen
(
color
),
Position
.
create
(
row
,
3
));
placePiece
(
new
King
(
color
),
Position
.
create
(
row
,
4
));
placePiece
(
new
Bishop
(
color
),
Position
.
create
(
row
,
5
));
placePiece
(
new
Knight
(
color
),
Position
.
create
(
row
,
6
));
placePiece
(
new
Rook
(
color
),
Position
.
create
(
row
,
7
));
}
private
void
initializePawnRank
(
Piece
.
Color
color
,
int
row
)
{
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
placePiece
(
Piece
.
create
Pawn
(
color
),
Position
.
create
(
row
,
column
));
placePiece
(
new
Pawn
(
color
),
Position
.
create
(
row
,
column
));
}
void
placePiece
(
Piece
piece
,
Position
position
)
{
...
...
app/src/main/java/com/example/paktalin/agilejava_exercises/Game.java
0 → 100644
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
;
/**
* Created by Paktalin on 01/06/2018.
*/
public
class
Game
{
}
app/src/main/java/com/example/paktalin/agilejava_exercises/Position.java
View file @
75f9d515
...
...
@@ -51,7 +51,7 @@ public class Position {
return
row
;
}
char
getColumn
()
{
public
char
getColumn
()
{
return
column
;
}
...
...
app/src/main/java/com/example/paktalin/agilejava_exercises/Side.java
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
com.example.paktalin.agilejava_exercises.pieces.Piece
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
...
...
@@ -26,16 +28,6 @@ class Side {
return
pieces
.
size
();
}
int
getPiecesCount
(
Piece
.
Type
type
)
{
int
count
=
0
;
for
(
Piece
piece
:
pieces
)
if
(
piece
.
isType
(
type
))
count
++;
return
count
;
}
void
addPiece
(
Piece
piece
)
{
pieces
.
add
(
piece
);
Collections
.
sort
(
pieces
);
...
...
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Bishop.java
0 → 100644
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
import
com.example.paktalin.agilejava_exercises.Position
;
import
com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy
;
/**
* Created by Paktalin on 01/06/2018.
*/
public
class
Bishop
extends
Piece
{
public
Bishop
(
Piece
.
Color
color
)
{
strength
=
0
;
representation
=
'k'
;
moveStrategy
=
new
KingMoveStrategy
();
this
.
color
=
color
;
}
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
return
false
;
}
}
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/King.java
0 → 100644
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
import
com.example.paktalin.agilejava_exercises.Position
;
import
com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy
;
/**
* Created by Paktalin on 01/06/2018.
*/
public
class
King
extends
Piece
{
public
King
(
Color
color
)
{
strength
=
0
;
representation
=
'k'
;
moveStrategy
=
new
KingMoveStrategy
();
this
.
color
=
color
;
}
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
return
(
new
KingMoveStrategy
()).
isMovable
(
from
,
to
);
}
}
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Knight.java
0 → 100644
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
import
com.example.paktalin.agilejava_exercises.Position
;
import
com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy
;
/**
* Created by Paktalin on 01/06/2018.
*/
public
class
Knight
extends
Piece
{
public
Knight
(
Piece
.
Color
color
)
{
strength
=
0
;
representation
=
'k'
;
moveStrategy
=
new
KingMoveStrategy
();
this
.
color
=
color
;
}
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
return
false
;
}
}
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Pawn.java
0 → 100644
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
import
com.example.paktalin.agilejava_exercises.Position
;
import
com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy
;
import
java.util.List
;
/**
* Created by Paktalin on 01/06/2018.
*/
public
class
Pawn
extends
Piece
{
public
Pawn
(
Piece
.
Color
color
)
{
strength
=
0
;
representation
=
'k'
;
moveStrategy
=
new
KingMoveStrategy
();
this
.
color
=
color
;
}
public
void
setStrength
(
List
<
Piece
>
pieces
)
{
this
.
setStrength
(
1.0
);
for
(
Piece
piece
:
pieces
)
{
if
(
pawnsOnSameColumn
(
this
,
piece
))
{
piece
.
setStrength
(
0.5
);
this
.
setStrength
(
0.5
);
}
}
}
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
return
false
;
}
private
static
boolean
pawnsOnSameColumn
(
Piece
currentPiece
,
Piece
piece
)
{
return
piece
.
getPosition
().
getColumn
()
==
currentPiece
.
getPosition
().
getColumn
()
&&
piece
.
getPosition
().
getRow
()
!=
currentPiece
.
getPosition
().
getRow
()
&&
piece
.
getClass
()
==
currentPiece
.
getClass
();
}
}
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
→
app/src/main/java/com/example/paktalin/agilejava_exercises/
pieces/
Piece.java
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
;
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
import
android.support.annotation.NonNull
;
import
com.example.paktalin.agilejava_exercises.Position
;
import
com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy
;
import
com.example.paktalin.agilejava_exercises.moves.MoveStrategy
;
import
com.example.paktalin.agilejava_exercises.moves.QueenMoveStrategy
;
...
...
@@ -12,31 +13,24 @@ import java.util.List;
* Created by Paktalin on 23/05/2018.
*/
public
class
Piece
implements
Comparable
<
Piece
>
{
public
abstract
class
Piece
implements
Comparable
<
Piece
>
{
public
enum
Color
{
White
,
Black
}
Color
color
;
private
Type
type
;
private
Color
color
;
double
strength
;
Position
position
;
MoveStrategy
moveStrategy
;
char
representation
;
private
double
strength
;
private
Position
position
;
private
MoveStrategy
moveStrategy
;
private
Piece
()
{}
private
Piece
(
final
Color
color
,
final
Type
type
)
{
this
.
color
=
color
;
this
.
type
=
type
;
}
Piece
()
{}
public
enum
Type
{
Pawn
(
1
,
'p'
),
Knight
(
2.5
,
'n'
),
Rook
(
5
,
'r'
),
Bishop
(
3
,
'b'
),
Queen
(
9
,
'q'
,
new
QueenMoveStrategy
()),
King
(
0
,
'k'
,
new
KingMoveStrategy
());
Queen
(
9
,
'q'
,
new
QueenMoveStrategy
());
private
double
strength
;
private
char
representation
;
...
...
@@ -70,21 +64,14 @@ public class Piece implements Comparable<Piece> {
this
.
position
=
position
;
}
char
getRepresentation
()
{
char
representation
=
this
.
type
.
getRepresentation
();
if
(
this
.
isBlack
())
return
Character
.
toUpperCase
(
representation
);
public
char
getRepresentation
()
{
return
representation
;
}
void
setStrength
(
double
strength
)
{
this
.
strength
=
strength
;
}
public
void
setStrength
(
List
<
Piece
>
pieces
)
{}
void
setStrength
(
List
<
Piece
>
pieces
)
{
if
(
this
.
type
!=
Type
.
Pawn
)
strength
=
this
.
type
.
getStrength
();
else
setPawnStrength
(
this
,
pieces
);
void
setStrength
(
double
strength
){
this
.
strength
=
strength
;
}
@Override
...
...
@@ -96,35 +83,14 @@ public class Piece implements Comparable<Piece> {
return
0
;
}
private
static
void
setPawnStrength
(
Piece
currentPiece
,
List
<
Piece
>
pieces
)
{
currentPiece
.
setStrength
(
1.0
);
for
(
Piece
piece
:
pieces
)
{
if
(
pawnsOnSameColumn
(
currentPiece
,
piece
))
{
piece
.
setStrength
(
0.5
);
currentPiece
.
setStrength
(
0.5
);
}
}
}
public
abstract
boolean
isMovable
(
Position
from
,
Position
to
);
private
static
boolean
pawnsOnSameColumn
(
Piece
currentPiece
,
Piece
piece
)
{
return
piece
.
getPosition
().
getColumn
()
==
currentPiece
.
getPosition
().
getColumn
()
&&
piece
.
getPosition
().
getRow
()
!=
currentPiece
.
getPosition
().
getRow
()
&&
piece
.
getType
()
==
currentPiece
.
getType
();
}
boolean
isMovable
(
Position
from
,
Position
to
)
{
return
this
.
getType
().
getMoveStrategy
().
isMovable
(
from
,
to
);
}
double
getStrength
()
{
return
strength
;
public
double
getStrength
()
{
return
this
.
strength
;
}
public
Color
getColor
()
{
return
color
;
}
Type
getType
()
{
return
type
;
}
public
Position
getPosition
()
{
return
position
;
}
...
...
@@ -132,35 +98,13 @@ public class Piece implements Comparable<Piece> {
boolean
isColor
(
Color
color
)
{
return
this
.
color
==
color
;
}
boolean
isWhite
()
{
public
boolean
isWhite
()
{
return
isColor
(
Color
.
White
);
}
boolean
isBlack
()
{
public
boolean
isBlack
()
{
return
isColor
(
Color
.
Black
);
}
boolean
isType
(
Piece
.
Type
type
)
{
return
this
.
getType
()
==
type
;
}
public
boolean
isAtPosition
(
Position
position
)
{
return
this
.
position
.
equals
(
position
);
}
static
Piece
createPawn
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Pawn
);
}
public
static
Piece
createKing
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
King
);
}
static
Piece
createBishop
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Bishop
);
}
static
Piece
createRook
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Rook
);
}
static
Piece
createKnight
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Knight
);
}
public
static
Piece
createQueen
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Queen
);
}
}
\ No newline at end of file
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Queen.java
0 → 100644
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
import
com.example.paktalin.agilejava_exercises.Position
;
import
com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy
;
import
com.example.paktalin.agilejava_exercises.moves.QueenMoveStrategy
;
/**
* Created by Paktalin on 01/06/2018.
*/
public
class
Queen
extends
Piece
{
public
Queen
(
Piece
.
Color
color
)
{
strength
=
0
;
representation
=
'k'
;
moveStrategy
=
new
KingMoveStrategy
();
this
.
color
=
color
;
}
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
return
(
new
QueenMoveStrategy
()).
isMovable
(
from
,
to
);
}
}
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Rook.java
0 → 100644
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
import
com.example.paktalin.agilejava_exercises.Position
;
import
com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy
;
/**
* Created by Paktalin on 01/06/2018.
*/
public
class
Rook
extends
Piece
{
public
Rook
(
Piece
.
Color
color
)
{
strength
=
0
;
representation
=
'k'
;
moveStrategy
=
new
KingMoveStrategy
();
this
.
color
=
color
;
}
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
return
false
;
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
com.example.paktalin.agilejava_exercises.pieces.Bishop
;
import
com.example.paktalin.agilejava_exercises.pieces.King
;
import
com.example.paktalin.agilejava_exercises.pieces.Knight
;
import
com.example.paktalin.agilejava_exercises.pieces.Pawn
;
import
com.example.paktalin.agilejava_exercises.pieces.Piece
;
import
com.example.paktalin.agilejava_exercises.pieces.Queen
;
import
com.example.paktalin.agilejava_exercises.pieces.Rook
;
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
.
Type
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
.
Piece
.
Color
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
.
Piece
.
Type
.*;
/**
* Created by Paktalin on 23/05/2018.
...
...
@@ -25,37 +33,36 @@ public class BoardTest extends TestCase {
public
void
testPlacePieces
()
{
board
=
Board
.
createInitialized
();
board
.
placePiece
(
Piece
.
create
Bishop
(
Black
),
"a8"
);
board
.
placePiece
(
new
Bishop
(
Black
),
"a8"
);
Piece
bishop
=
board
.
getPieceAtPosition
(
"a8"
);
assertEquals
(
Bishop
,
bishop
.
getType
());
assertEquals
(
Black
,
bishop
.
getColor
());
}
public
void
testOverallStrength
()
{
board
=
Board
.
createEmpty
();
verifyStrength
(
Piece
.
create
Queen
(
Black
),
"e6"
,
9.0
,
0.0
,
9.0
);
verifyStrength
(
Piece
.
create
Queen
(
White
),
"g4"
,
9.0
,
9.0
,
9.0
);
verifyStrength
(
new
Queen
(
Black
),
"e6"
,
9.0
,
0.0
,
9.0
);
verifyStrength
(
new
Queen
(
White
),
"g4"
,
9.0
,
9.0
,
9.0
);
verifyStrength
(
Piece
.
create
King
(
Black
),
"b8"
,
9.0
,
9.0
,
0.0
);
verifyStrength
(
Piece
.
create
King
(
White
),
"f1"
,
9.0
,
9.0
,
0.0
);
verifyStrength
(
new
King
(
Black
),
"b8"
,
9.0
,
9.0
,
0.0
);
verifyStrength
(
new
King
(
White
),
"f1"
,
9.0
,
9.0
,
0.0
);
verifyStrength
(
Piece
.
create
Rook
(
Black
),
"c8"
,
14.0
,
9.0
,
5.0
);
verifyStrength
(
Piece
.
create
Rook
(
White
),
"e1"
,
14.0
,
14.0
,
5.0
);
verifyStrength
(
new
Rook
(
Black
),
"c8"
,
14.0
,
9.0
,
5.0
);
verifyStrength
(
new
Rook
(
White
),
"e1"
,
14.0
,
14.0
,
5.0
);
verifyStrength
(
Piece
.
create
Pawn
(
Black
),
"a7"
,
15.0
,
14.0
,
1.0
);
verifyStrength
(
Piece
.
create
Pawn
(
White
),
"f2"
,
15.0
,
15.0
,
1.0
);
verifyStrength
(
new
Pawn
(
Black
),
"a7"
,
15.0
,
14.0
,
1.0
);
verifyStrength
(
new
Pawn
(
White
),
"f2"
,
15.0
,
15.0
,
1.0
);
verifyStrength
(
Piece
.
create
Pawn
(
Black
),
"c7"
,
16.0
,
15.0
,
1.0
);
verifyStrength
(
Piece
.
create
Pawn
(
White
),
"g2"
,
16.0
,
16.0
,
1.0
);
verifyStrength
(
new
Pawn
(
Black
),
"c7"
,
16.0
,
15.0
,
1.0
);
verifyStrength
(
new
Pawn
(
White
),
"g2"
,
16.0
,
16.0
,
1.0
);
verifyStrength
(
Piece
.
create
Bishop
(
Black
),
"d7"
,
19.0
,
16.0
,
3.0
);
verifyStrength
(
Piece
.
create
Pawn
(
White
),
"f3"
,
19.0
,
16.0
,
0.5
);
verifyStrength
(
new
Bishop
(
Black
),
"d7"
,
19.0
,
16.0
,
3.0
);
verifyStrength
(
new
Pawn
(
White
),
"f3"
,
19.0
,
16.0
,
0.5
);
verifyStrength
(
Piece
.
create
Pawn
(
Black
),
"b6"
,
20.0
,
16.0
,
1.0
);
verifyStrength
(
Piece
.
create
Pawn
(
White
),
"h3"
,
20.0
,
17.0
,
1.0
);
verifyStrength
(
new
Pawn
(
Black
),
"b6"
,
20.0
,
16.0
,
1.0
);
verifyStrength
(
new
Pawn
(
White
),
"h3"
,
20.0
,
17.0
,
1.0
);
verifyStrength
(
Piece
.
create
Knight
(
White
),
"f4"
,
20.0
,
19.5
,
2.5
);
verifyStrength
(
new
Knight
(
White
),
"f4"
,
20.0
,
19.5
,
2.5
);
verifyPrint
();
}
...
...
app/src/test/java/com/example/paktalin/agilejava_exercises/PieceTest.java
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
com.example.paktalin.agilejava_exercises.pieces.Bishop
;
import
com.example.paktalin.agilejava_exercises.pieces.King
;
import
com.example.paktalin.agilejava_exercises.pieces.Knight
;
import
com.example.paktalin.agilejava_exercises.pieces.Pawn
;
import
com.example.paktalin.agilejava_exercises.pieces.Piece
;
import
com.example.paktalin.agilejava_exercises.pieces.Queen
;
import
com.example.paktalin.agilejava_exercises.pieces.Rook
;
import
junit.framework.TestCase
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Type
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
.
Piece
.
Type
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
.
Piece
.
Color
.*;
/**
* Created by Paktalin on 23/05/2018.
...
...
@@ -12,30 +20,28 @@ import static com.example.paktalin.agilejava_exercises.Piece.Color.*;
public
class
PieceTest
extends
TestCase
{
public
void
testCreate
()
{
verifyCreation
(
Piece
.
createPawn
(
White
),
Piece
.
create
Pawn
(
Black
),
Pawn
,
'p'
);
verifyCreation
(
Piece
.
createKing
(
White
),
Piece
.
create
King
(
Black
),
King
,
'k'
);
verifyCreation
(
Piece
.
createBishop
(
White
),
Piece
.
create
Bishop
(
Black
),
Bishop
,
'b'
);
verifyCreation
(
Piece
.
createRook
(
White
),
Piece
.
create
Rook
(
Black
),
Rook
,
'r'
);
verifyCreation
(
Piece
.
createKnight
(
White
),
Piece
.
create
Knight
(
Black
),
Knight
,
'n'
);
verifyCreation
(
Piece
.
createQueen
(
White
),
Piece
.
create
Queen
(
Black
),
Queen
,
'q'
);
verifyCreation
(
new
Pawn
(
White
),
new
Pawn
(
Black
),
Pawn
,
'p'
);
//verifyCreation(new King(White), new
King(Black), King, 'k');
verifyCreation
(
new
Bishop
(
White
),
new
Bishop
(
Black
),
Bishop
,
'b'
);
verifyCreation
(
new
Rook
(
White
),
new
Rook
(
Black
),
Rook
,
'r'
);
verifyCreation
(
new
Knight
(
White
),
new
Knight
(
Black
),
Knight
,
'n'
);
verifyCreation
(
new
Queen
(
White
),
new
Queen
(
Black
),
Queen
,
'q'
);
}
private
void
verifyCreation
(
Piece
whitePiece
,
Piece
blackPiece
,
Piece
.
Type
type
,
char
representation
)
{
assertTrue
(
whitePiece
.
isWhite
());
assertEquals
(
type
,
whitePiece
.
getType
());
assertEquals
(
representation
,
whitePiece
.
getRepresentation
());
assertTrue
(
blackPiece
.
isBlack
());
assertEquals
(
type
,
blackPiece
.
getType
());
assertEquals
(
Character
.
toUpperCase
(
representation
),
blackPiece
.
getRepresentation
());
}
public
void
testStrength
()
{
Board
board
=
Board
.
createEmpty
();
board
.
placePiece
(
Piece
.
create
Pawn
(
Black
),
"a7"
);
board
.
placePiece
(
new
Pawn
(
Black
),
"a7"
);
assertEquals
(
1.0
,
board
.
getPieceAtPosition
(
"a7"
).
getStrength
());
board
.
placePiece
(
Piece
.
create
Pawn
(
Black
),
"a6"
);
board
.
placePiece
(
new
Pawn
(
Black
),
"a6"
);
assertEquals
(
0.5
,
board
.
getPieceAtPosition
(
"a6"
).
getStrength
());
}
...
...
app/src/test/java/com/example/paktalin/agilejava_exercises/PositionTest.java
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
com.example.paktalin.agilejava_exercises.pieces.Bishop
;
import
com.example.paktalin.agilejava_exercises.pieces.Pawn
;
import
com.example.paktalin.agilejava_exercises.pieces.Piece
;
import
junit.framework.TestCase
;
import
java.util.List
;
...
...
@@ -24,9 +28,9 @@ public class PositionTest extends TestCase {
}
public
void
testStrengthByPosition
()
{
verifyStrength
(
Piece
.
create
Bishop
(
Piece
.
Color
.
Black
),
"b5"
,
3.0
);
verifyStrength
(
Piece
.
create
Pawn
(
Piece
.
Color
.
White
),
"a2"
,
1.0
);
verifyStrength
(
Piece
.
create
Pawn
(
Piece
.
Color
.
White
),
"a3"
,
0.5
);
verifyStrength
(
new
Bishop
(
Piece
.
Color
.
Black
),
"b5"
,
3.0
);
verifyStrength
(
new
Pawn
(
Piece
.
Color
.
White
),
"a2"
,
1.0
);
verifyStrength
(
new
Pawn
(
Piece
.
Color
.
White
),
"a3"
,
0.5
);
assertEquals
(
0.5
,
board
.
getPieceAtPosition
(
"a2"
).
getStrength
());
}
...
...
@@ -35,33 +39,4 @@ public class PositionTest extends TestCase {
piece
=
board
.
getPieceAtPosition
(
position
);
assertEquals
(
strength
,
piece
.
getStrength
());
}
public
void
testSetPawnStrength
()
{
board
.
placePiece
(
Piece
.
createPawn
(
Piece
.
Color
.
Black
),
"a6"
);
board
.
placePiece
(
Piece
.
createKnight
(
Piece
.
Color
.
Black
),
"a5"
);
board
.
placePiece
(
Piece
.
createPawn
(
Piece
.
Color
.
Black
),
"a4"
);
Piece
currentPiece
=
board
.
getPieceAtPosition
(
"a4"
);
char
column
=
currentPiece
.
getPosition
().
getColumn
();
assertEquals
(
'a'
,
column
);
int
row
=
currentPiece
.
getPosition
().
getRow
();
assertEquals
(
3
,
row
);
List
<
Piece
>
pieces
=
board
.
getPieces
(
Piece
.
Color
.
Black
);
Piece
secondPiece
=
pieces
.
get
(
1
);
//assertTrue((secondPiece.getPosition().getColumn() == column) && (secondPiece.getPosition().getRow() != row));
for
(
Piece
piece
:
board
.
getPieces
(
Piece
.
Color
.
Black
))
{
Position
position
=
piece
.
getPosition
();
if
((
position
.
getColumn
()
==
column
)
&&
(
position
.
getRow
()
!=
row
))
{
piece
.
setStrength
(
0.5
);
currentPiece
.
setStrength
(
0.5
);
}
}
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/moves/KingMoveStrategyTest.java
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
.
moves
;
import
com.example.paktalin.agilejava_exercises.Piece
;
import
com.example.paktalin.agilejava_exercises.pieces.King
;
import
com.example.paktalin.agilejava_exercises.pieces.Piece
;
import
com.example.paktalin.agilejava_exercises.Position
;
/**
...
...
@@ -11,7 +12,7 @@ public class KingMoveStrategyTest extends MoveStrategyTest {
@Override
void
putPieceOnBoard
()
{
board
.
placePiece
(
Piece
.
create
King
(
Piece
.
Color
.
Black
),
currentPosition
);
board
.
placePiece
(
new
King
(
Piece
.
Color
.
Black
),
currentPosition
);
}
@Override
...
...
app/src/test/java/com/example/paktalin/agilejava_exercises/moves/MoveStrategyTest.java
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
.
moves
;
import
com.example.paktalin.agilejava_exercises.Board
;
import
com.example.paktalin.agilejava_exercises.Piece
;
import
com.example.paktalin.agilejava_exercises.
pieces.
Piece
;
import
com.example.paktalin.agilejava_exercises.Position
;
import
junit.framework.TestCase
;
...
...
app/src/test/java/com/example/paktalin/agilejava_exercises/moves/QueenMoveStrategyTest.java
View file @
75f9d515
package
com
.
example
.
paktalin
.
agilejava_exercises
.
moves
;
import
com.example.paktalin.agilejava_exercises.Piece
;
import
com.example.paktalin.agilejava_exercises.
pieces.
Piece
;
import
com.example.paktalin.agilejava_exercises.Position
;
import
com.example.paktalin.agilejava_exercises.pieces.Queen
;
/**
* Created by Paktalin on 31/05/2018.
...
...
@@ -11,7 +12,7 @@ public class QueenMoveStrategyTest extends MoveStrategyTest {
@Override
void
putPieceOnBoard
()
{
board
.
placePiece
(
Piece
.
create
Queen
(
Piece
.
Color
.
Black
),
currentPosition
);
board
.
placePiece
(
new
Queen
(
Piece
.
Color
.
Black
),
currentPosition
);
}
@Override
...
...
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