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
fc9af304
authored
May 31, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Queen move strategy added
parent
1f52be6d
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
97 additions
and
13 deletions
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/KingMoveStrategy.java
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/MoveStrategy.java
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/QueenMoveStrategy.java
app/src/test/java/com/example/paktalin/agilejava_exercises/AllTests.java
app/src/test/java/com/example/paktalin/agilejava_exercises/moves/QueenMoveStrategyTest.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
View file @
fc9af304
...
...
@@ -4,6 +4,7 @@ import android.support.annotation.NonNull;
import
com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy
;
import
com.example.paktalin.agilejava_exercises.moves.MoveStrategy
;
import
com.example.paktalin.agilejava_exercises.moves.QueenMoveStrategy
;
import
java.util.List
;
...
...
@@ -34,7 +35,7 @@ public class Piece implements Comparable<Piece> {
Knight
(
2.5
,
'n'
),
Rook
(
5
,
'r'
),
Bishop
(
3
,
'b'
),
Queen
(
9
,
'q'
),
Queen
(
9
,
'q'
,
new
QueenMoveStrategy
()
),
King
(
0
,
'k'
,
new
KingMoveStrategy
());
private
double
strength
;
...
...
@@ -159,7 +160,7 @@ public class Piece implements Comparable<Piece> {
static
Piece
createKnight
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Knight
);
}
static
Piece
createQueen
(
Color
color
)
{
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/moves/KingMoveStrategy.java
View file @
fc9af304
...
...
@@ -6,18 +6,19 @@ import com.example.paktalin.agilejava_exercises.Position;
* Created by Paktalin on 31/05/2018.
*/
public
class
KingMoveStrategy
implement
s
MoveStrategy
{
public
class
KingMoveStrategy
extend
s
MoveStrategy
{
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
int
fromRow
=
from
.
getRow
();
int
fromColumn
=
from
.
getIntColumn
();
super
.
isMovable
(
from
,
to
);
return
!(
distantRows
()
||
distantColumns
());
}
int
toRow
=
to
.
getRow
();
int
toColumn
=
to
.
getIntColumn
();
private
boolean
distantRows
()
{
return
Math
.
abs
(
toRow
-
fromRow
)
>
1
;
}
if
((
Math
.
abs
(
toRow
-
fromRow
)
>
1
)
||
(
Math
.
abs
(
toColumn
-
fromColumn
)
>
1
))
return
false
;
return
true
;
private
boolean
distantColumns
()
{
return
Math
.
abs
(
toColumn
-
fromColumn
)
>
1
;
}
}
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/MoveStrategy.java
View file @
fc9af304
...
...
@@ -6,8 +6,21 @@ import com.example.paktalin.agilejava_exercises.Position;
* Created by Paktalin on 31/05/2018.
*/
public
interface
MoveStrategy
{
public
abstract
class
MoveStrategy
{
boolean
isMovable
(
Position
from
,
Position
to
);
int
fromRow
,
fromColumn
;
int
toRow
,
toColumn
;
private
void
initialize
(
Position
from
,
Position
to
)
{
fromRow
=
from
.
getRow
();
fromColumn
=
from
.
getIntColumn
();
toRow
=
to
.
getRow
();
toColumn
=
to
.
getIntColumn
();
}
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
initialize
(
from
,
to
);
return
false
;
}
}
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/QueenMoveStrategy.java
0 → 100644
View file @
fc9af304
package
com
.
example
.
paktalin
.
agilejava_exercises
.
moves
;
import
com.example.paktalin.agilejava_exercises.Position
;
/**
* Created by Paktalin on 31/05/2018.
*/
public
class
QueenMoveStrategy
extends
MoveStrategy
{
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
super
.
isMovable
(
from
,
to
);
return
sameRow
()
||
sameColumn
()
||
diagonal
();
}
private
boolean
sameRow
()
{
return
fromRow
==
toRow
;
}
private
boolean
sameColumn
()
{
return
fromColumn
==
toColumn
;
}
private
boolean
diagonal
()
{
return
Math
.
abs
(
toRow
-
fromRow
)
==
Math
.
abs
(
toColumn
-
fromColumn
);
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/AllTests.java
View file @
fc9af304
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
com.example.paktalin.agilejava_exercises.moves.
KingMoveStrategyTest
;
import
com.example.paktalin.agilejava_exercises.moves.
*
;
import
junit.framework.TestSuite
;
...
...
@@ -18,6 +18,7 @@ public class AllTests extends TestSuite {
suite
.
addTestSuite
(
CharacterTest
.
class
);
suite
.
addTestSuite
(
PositionTest
.
class
);
suite
.
addTestSuite
(
KingMoveStrategyTest
.
class
);
suite
.
addTestSuite
(
QueenMoveStrategyTest
.
class
);
return
suite
;
}
...
...
app/src/test/java/com/example/paktalin/agilejava_exercises/moves/QueenMoveStrategyTest.java
0 → 100644
View file @
fc9af304
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.Position
;
import
junit.framework.TestCase
;
/**
* Created by Paktalin on 31/05/2018.
*/
public
class
QueenMoveStrategyTest
extends
TestCase
{
private
Piece
queen
;
private
Board
board
;
@Override
protected
void
setUp
()
throws
Exception
{
board
=
Board
.
createEmpty
();
board
.
placePiece
(
Piece
.
createQueen
(
Piece
.
Color
.
Black
),
"b5"
);
queen
=
board
.
getPieceAtPosition
(
"b5"
);
}
public
void
testMove
()
{
Position
currentPosition
=
queen
.
getPosition
();
Position
accessiblePosition
=
Position
.
create
(
"d3"
);
board
.
move
(
currentPosition
,
accessiblePosition
);
assertTrue
(
queen
.
isAtPosition
(
accessiblePosition
));
assertFalse
(
queen
.
isAtPosition
(
currentPosition
));
currentPosition
=
accessiblePosition
;
Position
notAccessiblePosition
=
Position
.
create
(
"f7"
);
board
.
move
(
currentPosition
,
notAccessiblePosition
);
assertTrue
(
queen
.
isAtPosition
(
currentPosition
));
assertFalse
(
queen
.
isAtPosition
(
notAccessiblePosition
));
}
}
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