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
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
41 additions
and
94 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;
...
@@ -6,19 +6,16 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018.
* Created by Paktalin on 31/05/2018.
*/
*/
public
class
KingMoveStrategy
extend
s
MoveStrategy
{
public
class
KingMoveStrategy
implement
s
MoveStrategy
{
@Override
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
initialize
(
from
,
to
);
boolean
nearbyRows
=
zeroOrOneDistance
(
from
.
getRow
(),
to
.
getRow
());
return
!(
distantRows
()
||
distantColumns
());
boolean
nearbyColumns
=
zeroOrOneDistance
(
from
.
getIntColumn
(),
to
.
getIntColumn
());
return
nearbyRows
&&
nearbyColumns
;
}
}
private
boolean
distantRows
()
{
private
boolean
zeroOrOneDistance
(
int
from
,
int
to
)
{
return
Math
.
abs
(
toRow
-
fromRow
)
>
1
;
return
Math
.
abs
(
to
-
from
)
<=
1
;
}
private
boolean
distantColumns
()
{
return
Math
.
abs
(
toColumn
-
fromColumn
)
>
1
;
}
}
}
}
src/main/com/paktalin/chess/moves/MoveStrategy.java
View file @
baf5b2cd
...
@@ -6,16 +6,6 @@ import com.paktalin.chess.Position;
...
@@ -6,16 +6,6 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018.
* Created by Paktalin on 31/05/2018.
*/
*/
public
abstract
class
MoveStrategy
{
public
interface
MoveStrategy
{
int
fromRow
,
fromColumn
;
boolean
isMovable
(
Position
from
,
Position
to
);
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
);
}
}
src/main/com/paktalin/chess/moves/QueenMoveStrategy.java
View file @
baf5b2cd
...
@@ -6,23 +6,21 @@ import com.paktalin.chess.Position;
...
@@ -6,23 +6,21 @@ import com.paktalin.chess.Position;
* Created by Paktalin on 31/05/2018.
* Created by Paktalin on 31/05/2018.
*/
*/
public
class
QueenMoveStrategy
extend
s
MoveStrategy
{
public
class
QueenMoveStrategy
implement
s
MoveStrategy
{
@Override
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
initialize
(
from
,
to
);
boolean
sameRow
=
sameCoordinate
(
from
.
getRow
(),
to
.
getRow
());
return
sameRow
()
||
sameColumn
()
||
diagonal
();
boolean
sameColumn
=
sameCoordinate
(
from
.
getIntColumn
(),
to
.
getIntColumn
());
boolean
diagonal
=
diagonal
(
from
,
to
);
return
sameRow
||
sameColumn
||
diagonal
;
}
}
private
boolean
same
Row
(
)
{
private
boolean
same
Coordinate
(
int
from
,
int
to
)
{
return
from
Row
==
toRow
;
return
from
==
to
;
}
}
private
boolean
sameColumn
()
{
private
boolean
diagonal
(
Position
from
,
Position
to
)
{
return
fromColumn
==
toColumn
;
return
Math
.
abs
(
to
.
getRow
()
-
from
.
getRow
())
==
Math
.
abs
(
to
.
getIntColumn
()
-
from
.
getIntColumn
());
}
private
boolean
diagonal
()
{
return
Math
.
abs
(
toRow
-
fromRow
)
==
Math
.
abs
(
toColumn
-
fromColumn
);
}
}
}
}
src/test/com/paktalin/chess/moves/KingMoveStrategyTest.java
View file @
baf5b2cd
package
com
.
paktalin
.
chess
.
moves
;
package
com
.
paktalin
.
chess
.
moves
;
import
com.paktalin.chess.Position
;
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.
* Created by Paktalin on 31/05/2018.
*/
*/
class
KingMoveStrategyTest
extends
MoveStrategyTest
{
class
KingMoveStrategyTest
{
@Override
MoveStrategy
moveStrategy
()
{
return
new
KingMoveStrategy
();
}
@Override
private
Position
initialPosition
=
Position
.
create
(
"b5"
);
Position
accessiblePosition
()
{
private
MoveStrategy
moveStrategy
=
new
KingMoveStrategy
();
return
Position
.
create
(
"a6"
);
}
@Override
@Test
Position
notAccessiblePosition
()
{
void
isMovable
()
{
return
Position
.
create
(
"d4"
);
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
;
package
com
.
paktalin
.
chess
.
moves
;
import
com.paktalin.chess.Position
;
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.
* Created by Paktalin on 31/05/2018.
*/
*/
class
QueenMoveStrategyTest
extends
MoveStrategyTest
{
class
QueenMoveStrategyTest
{
@Override
MoveStrategy
moveStrategy
()
{
return
new
QueenMoveStrategy
();
}
@Override
private
Position
initialPosition
=
Position
.
create
(
"b5"
);
Position
accessiblePosition
()
{
private
MoveStrategy
moveStrategy
=
new
QueenMoveStrategy
();
return
Position
.
create
(
"d3"
);
}
@Override
@Test
Position
notAccessiblePosition
()
{
void
isMovable
()
{
return
Position
.
create
(
"f7"
);
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