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
1f52be6d
authored
May 31, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Exercise 6.6 done. Now you can move a king, but only to accessible position
parent
0269972e
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
77 additions
and
26 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/main/java/com/example/paktalin/agilejava_exercises/Position.java
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/KingMoveStrategy.java
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/KingMoves.java
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/Moves.java → app/src/main/java/com/example/paktalin/agilejava_exercises/moves/MoveStrategy.java
app/src/test/java/com/example/paktalin/agilejava_exercises/AllTests.java
app/src/test/java/com/example/paktalin/agilejava_exercises/moves/KingMovesTest.java → app/src/test/java/com/example/paktalin/agilejava_exercises/moves/KingMoveStrategyTest.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
View file @
1f52be6d
...
...
@@ -33,6 +33,8 @@ public class Board {
}
public
void
move
(
Position
from
,
Position
to
)
{
Piece
piece
=
layout
.
getPieceAtPosition
(
from
);
if
(
piece
.
isMovable
(
from
,
to
))
layout
.
move
(
from
,
to
);
}
...
...
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
View file @
1f52be6d
...
...
@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava_exercises;
import
android.support.annotation.NonNull
;
import
com.example.paktalin.agilejava_exercises.moves.KingMoveStrategy
;
import
com.example.paktalin.agilejava_exercises.moves.MoveStrategy
;
import
java.util.List
;
/**
...
...
@@ -9,6 +12,7 @@ import java.util.List;
*/
public
class
Piece
implements
Comparable
<
Piece
>
{
public
enum
Color
{
White
,
Black
}
private
Type
type
;
...
...
@@ -16,6 +20,7 @@ public class Piece implements Comparable<Piece> {
private
double
strength
;
private
Position
position
;
private
MoveStrategy
moveStrategy
;
private
Piece
()
{}
...
...
@@ -25,10 +30,22 @@ public class Piece implements Comparable<Piece> {
}
public
enum
Type
{
Pawn
(
1
,
'p'
),
Knight
(
2.5
,
'n'
),
Rook
(
5
,
'r'
),
Bishop
(
3
,
'b'
),
Queen
(
9
,
'q'
),
King
(
0
,
'k'
);
Pawn
(
1
,
'p'
),
Knight
(
2.5
,
'n'
),
Rook
(
5
,
'r'
),
Bishop
(
3
,
'b'
),
Queen
(
9
,
'q'
),
King
(
0
,
'k'
,
new
KingMoveStrategy
());
private
double
strength
;
private
char
representation
;
private
MoveStrategy
moveStrategy
;
Type
(
double
strength
,
char
representation
,
MoveStrategy
moveStrategy
)
{
this
.
strength
=
strength
;
this
.
representation
=
representation
;
this
.
moveStrategy
=
moveStrategy
;
}
Type
(
double
strength
,
char
representation
)
{
this
.
strength
=
strength
;
...
...
@@ -42,6 +59,10 @@ public class Piece implements Comparable<Piece> {
public
double
getStrength
()
{
return
strength
;
}
public
MoveStrategy
getMoveStrategy
()
{
return
moveStrategy
;
}
}
public
void
setPosition
(
Position
position
)
{
...
...
@@ -90,6 +111,10 @@ public class Piece implements Comparable<Piece> {
piece
.
getType
()
==
currentPiece
.
getType
();
}
boolean
isMovable
(
Position
from
,
Position
to
)
{
return
this
.
getType
().
getMoveStrategy
().
isMovable
(
from
,
to
);
}
double
getStrength
()
{
return
strength
;
}
...
...
@@ -115,6 +140,9 @@ public class Piece implements Comparable<Piece> {
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
);
...
...
app/src/main/java/com/example/paktalin/agilejava_exercises/Position.java
View file @
1f52be6d
...
...
@@ -47,7 +47,7 @@ public class Position {
return
(
int
)
c
-
(
int
)
firstColumnLetter
;
}
int
getRow
()
{
public
int
getRow
()
{
return
row
;
}
...
...
@@ -55,7 +55,7 @@ public class Position {
return
column
;
}
int
getIntColumn
()
{
public
int
getIntColumn
()
{
return
charToInt
(
column
);
}
}
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/KingMoveStrategy.java
0 → 100644
View file @
1f52be6d
package
com
.
example
.
paktalin
.
agilejava_exercises
.
moves
;
import
com.example.paktalin.agilejava_exercises.Position
;
/**
* Created by Paktalin on 31/05/2018.
*/
public
class
KingMoveStrategy
implements
MoveStrategy
{
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
int
fromRow
=
from
.
getRow
();
int
fromColumn
=
from
.
getIntColumn
();
int
toRow
=
to
.
getRow
();
int
toColumn
=
to
.
getIntColumn
();
if
((
Math
.
abs
(
toRow
-
fromRow
)
>
1
)
||
(
Math
.
abs
(
toColumn
-
fromColumn
)
>
1
))
return
false
;
return
true
;
}
}
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/KingMoves.java
deleted
100644 → 0
View file @
0269972e
package
com
.
example
.
paktalin
.
agilejava_exercises
.
moves
;
import
com.example.paktalin.agilejava_exercises.Piece
;
import
com.example.paktalin.agilejava_exercises.Position
;
import
java.util.Map
;
/**
* Created by Paktalin on 31/05/2018.
*/
public
class
KingMoves
{
}
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/Move
s
.java
→
app/src/main/java/com/example/paktalin/agilejava_exercises/moves/Move
Strategy
.java
View file @
1f52be6d
package
com
.
example
.
paktalin
.
agilejava_exercises
.
moves
;
import
com.example.paktalin.agilejava_exercises.Position
;
/**
* Created by Paktalin on 31/05/2018.
*/
public
abstract
class
Moves
{
public
interface
MoveStrategy
{
boolean
isMovable
(
Position
from
,
Position
to
);
}
app/src/test/java/com/example/paktalin/agilejava_exercises/AllTests.java
View file @
1f52be6d
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
com.example.paktalin.agilejava_exercises.moves.KingMove
s
Test
;
import
com.example.paktalin.agilejava_exercises.moves.KingMove
Strategy
Test
;
import
junit.framework.TestSuite
;
...
...
@@ -17,7 +17,7 @@ public class AllTests extends TestSuite {
suite
.
addTestSuite
(
BoardTest
.
class
);
suite
.
addTestSuite
(
CharacterTest
.
class
);
suite
.
addTestSuite
(
PositionTest
.
class
);
suite
.
addTestSuite
(
KingMove
s
Test
.
class
);
suite
.
addTestSuite
(
KingMove
Strategy
Test
.
class
);
return
suite
;
}
...
...
app/src/test/java/com/example/paktalin/agilejava_exercises/moves/KingMove
s
Test.java
→
app/src/test/java/com/example/paktalin/agilejava_exercises/moves/KingMove
Strategy
Test.java
View file @
1f52be6d
...
...
@@ -10,7 +10,7 @@ import junit.framework.TestCase;
* Created by Paktalin on 31/05/2018.
*/
public
class
KingMove
s
Test
extends
TestCase
{
public
class
KingMove
Strategy
Test
extends
TestCase
{
private
Piece
king
;
private
Board
board
;
...
...
@@ -22,10 +22,20 @@ public class KingMovesTest extends TestCase {
king
=
board
.
getPieceAtPosition
(
"b5"
);
}
public
void
testCreate
()
{
assertEquals
(
"b5"
,
king
.
getPosition
().
toString
());
board
.
move
(
Position
.
create
(
"b5"
),
Position
.
create
(
"a6"
));
assertEquals
(
"a6"
,
king
.
getPosition
().
toString
());
public
void
testMove
()
{
Position
currentPosition
=
king
.
getPosition
();
assertEquals
(
"b5"
,
currentPosition
.
toString
());
Position
accessiblePosition
=
Position
.
create
(
"a6"
);
board
.
move
(
currentPosition
,
accessiblePosition
);
assertTrue
(
king
.
isAtPosition
(
accessiblePosition
));
assertFalse
(
king
.
isAtPosition
(
currentPosition
));
currentPosition
=
accessiblePosition
;
Position
notAccessiblePosition
=
Position
.
create
(
"d4"
);
board
.
move
(
currentPosition
,
notAccessiblePosition
);
assertTrue
(
king
.
isAtPosition
(
currentPosition
));
assertFalse
(
king
.
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