Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
agile-java
/
Chess
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
baf5b2cd
authored
Feb 27, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in MoveStrategy and its tests
parent
ec7d9442
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
40 additions
and
93 deletions
src/main/com/paktalin/chess/moves/KingMoveStrategy.java
src/main/com/paktalin/chess/moves/MoveStrategy.java
src/main/com/paktalin/chess/moves/QueenMoveStrategy.java
src/test/com/paktalin/chess/moves/KingMoveStrategyTest.java
src/test/com/paktalin/chess/moves/MoveStrategyTest.java
src/test/com/paktalin/chess/moves/QueenMoveStrategyTest.java
src/main/com/paktalin/chess/moves/KingMoveStrategy.java
View file @
baf5b2cd
...
...
@@ -6,19 +6,16 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018.
*/
public
class
KingMoveStrategy
extend
s
MoveStrategy
{
public
class
KingMoveStrategy
implement
s
MoveStrategy
{
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
initialize
(
from
,
to
);
return
!(
distantRows
()
||
distantColumns
());
boolean
nearbyRows
=
zeroOrOneDistance
(
from
.
getRow
(),
to
.
getRow
());
boolean
nearbyColumns
=
zeroOrOneDistance
(
from
.
getIntColumn
(),
to
.
getIntColumn
());
return
nearbyRows
&&
nearbyColumns
;
}
private
boolean
distantRows
()
{
return
Math
.
abs
(
toRow
-
fromRow
)
>
1
;
}
private
boolean
distantColumns
()
{
return
Math
.
abs
(
toColumn
-
fromColumn
)
>
1
;
private
boolean
zeroOrOneDistance
(
int
from
,
int
to
)
{
return
Math
.
abs
(
to
-
from
)
<=
1
;
}
}
src/main/com/paktalin/chess/moves/MoveStrategy.java
View file @
baf5b2cd
...
...
@@ -6,16 +6,6 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018.
*/
public
abstract
class
MoveStrategy
{
int
fromRow
,
fromColumn
;
int
toRow
,
toColumn
;
void
initialize
(
Position
from
,
Position
to
)
{
fromRow
=
from
.
getRow
();
fromColumn
=
from
.
getIntColumn
();
toRow
=
to
.
getRow
();
toColumn
=
to
.
getIntColumn
();
}
public
abstract
boolean
isMovable
(
Position
from
,
Position
to
);
public
interface
MoveStrategy
{
boolean
isMovable
(
Position
from
,
Position
to
);
}
src/main/com/paktalin/chess/moves/QueenMoveStrategy.java
View file @
baf5b2cd
...
...
@@ -6,23 +6,21 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018.
*/
public
class
QueenMoveStrategy
extend
s
MoveStrategy
{
public
class
QueenMoveStrategy
implement
s
MoveStrategy
{
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
initialize
(
from
,
to
);
return
sameRow
()
||
sameColumn
()
||
diagonal
();
boolean
sameRow
=
sameCoordinate
(
from
.
getRow
(),
to
.
getRow
());
boolean
sameColumn
=
sameCoordinate
(
from
.
getIntColumn
(),
to
.
getIntColumn
());
boolean
diagonal
=
diagonal
(
from
,
to
);
return
sameRow
||
sameColumn
||
diagonal
;
}
private
boolean
same
Row
(
)
{
return
from
Row
==
toRow
;
private
boolean
same
Coordinate
(
int
from
,
int
to
)
{
return
from
==
to
;
}
private
boolean
sameColumn
()
{
return
fromColumn
==
toColumn
;
}
private
boolean
diagonal
()
{
return
Math
.
abs
(
toRow
-
fromRow
)
==
Math
.
abs
(
toColumn
-
fromColumn
);
private
boolean
diagonal
(
Position
from
,
Position
to
)
{
return
Math
.
abs
(
to
.
getRow
()
-
from
.
getRow
())
==
Math
.
abs
(
to
.
getIntColumn
()
-
from
.
getIntColumn
());
}
}
src/test/com/paktalin/chess/moves/KingMoveStrategyTest.java
View file @
baf5b2cd
package
com
.
paktalin
.
chess
.
moves
;
import
com.paktalin.chess.Position
;
import
org.junit.jupiter.api.Test
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertFalse
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
/**
* Created by Paktalin on 31/05/2018.
*/
class
KingMoveStrategyTest
extends
MoveStrategyTest
{
@Override
MoveStrategy
moveStrategy
()
{
return
new
KingMoveStrategy
();
}
class
KingMoveStrategyTest
{
@Override
Position
accessiblePosition
()
{
return
Position
.
create
(
"a6"
);
}
private
Position
initialPosition
=
Position
.
create
(
"b5"
);
private
MoveStrategy
moveStrategy
=
new
KingMoveStrategy
();
@Override
Position
notAccessiblePosition
()
{
return
Position
.
create
(
"d4"
);
@Test
void
isMovable
()
{
assertTrue
(
moveStrategy
.
isMovable
(
initialPosition
,
Position
.
create
(
"a6"
)));
assertFalse
(
moveStrategy
.
isMovable
(
initialPosition
,
Position
.
create
(
"d4"
)));
}
}
\ No newline at end of file
src/test/com/paktalin/chess/moves/MoveStrategyTest.java
deleted
100644 → 0
View file @
ec7d9442
package
com
.
paktalin
.
chess
.
moves
;
import
com.paktalin.chess.Position
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertFalse
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
/**
* Created by Paktalin on 31/05/2018.
*/
abstract
class
MoveStrategyTest
{
private
Position
accessiblePosition
,
notAccessiblePosition
,
initialPosition
;
private
MoveStrategy
moveStrategy
;
@BeforeEach
void
init
()
{
initialPosition
=
Position
.
create
(
"b5"
);
moveStrategy
=
moveStrategy
();
accessiblePosition
=
accessiblePosition
();
notAccessiblePosition
=
notAccessiblePosition
();
}
abstract
MoveStrategy
moveStrategy
();
abstract
Position
accessiblePosition
();
abstract
Position
notAccessiblePosition
();
@Test
void
isMovable
()
{
assertTrue
(
moveStrategy
.
isMovable
(
initialPosition
,
accessiblePosition
));
assertFalse
(
moveStrategy
.
isMovable
(
initialPosition
,
notAccessiblePosition
));
}
}
src/test/com/paktalin/chess/moves/QueenMoveStrategyTest.java
View file @
baf5b2cd
package
com
.
paktalin
.
chess
.
moves
;
import
com.paktalin.chess.Position
;
import
org.junit.jupiter.api.Test
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertFalse
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
/**
* Created by Paktalin on 31/05/2018.
*/
class
QueenMoveStrategyTest
extends
MoveStrategyTest
{
@Override
MoveStrategy
moveStrategy
()
{
return
new
QueenMoveStrategy
();
}
class
QueenMoveStrategyTest
{
@Override
Position
accessiblePosition
()
{
return
Position
.
create
(
"d3"
);
}
private
Position
initialPosition
=
Position
.
create
(
"b5"
);
private
MoveStrategy
moveStrategy
=
new
QueenMoveStrategy
();
@Override
Position
notAccessiblePosition
()
{
return
Position
.
create
(
"f7"
);
@Test
void
isMovable
()
{
assertTrue
(
moveStrategy
.
isMovable
(
initialPosition
,
Position
.
create
(
"d3"
)));
assertFalse
(
moveStrategy
.
isMovable
(
initialPosition
,
Position
.
create
(
"f7"
)));
}
}
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