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
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
78 additions
and
27 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,7 +33,9 @@ public class Board {
...
@@ -33,7 +33,9 @@ public class Board {
}
}
public
void
move
(
Position
from
,
Position
to
)
{
public
void
move
(
Position
from
,
Position
to
)
{
layout
.
move
(
from
,
to
);
Piece
piece
=
layout
.
getPieceAtPosition
(
from
);
if
(
piece
.
isMovable
(
from
,
to
))
layout
.
move
(
from
,
to
);
}
}
public
Piece
getPieceAtPosition
(
String
position
)
{
public
Piece
getPieceAtPosition
(
String
position
)
{
...
...
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
View file @
1f52be6d
...
@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava_exercises;
...
@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava_exercises;
import
android.support.annotation.NonNull
;
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
;
import
java.util.List
;
/**
/**
...
@@ -9,6 +12,7 @@ import java.util.List;
...
@@ -9,6 +12,7 @@ import java.util.List;
*/
*/
public
class
Piece
implements
Comparable
<
Piece
>
{
public
class
Piece
implements
Comparable
<
Piece
>
{
public
enum
Color
{
White
,
Black
}
public
enum
Color
{
White
,
Black
}
private
Type
type
;
private
Type
type
;
...
@@ -16,6 +20,7 @@ public class Piece implements Comparable<Piece> {
...
@@ -16,6 +20,7 @@ public class Piece implements Comparable<Piece> {
private
double
strength
;
private
double
strength
;
private
Position
position
;
private
Position
position
;
private
MoveStrategy
moveStrategy
;
private
Piece
()
{}
private
Piece
()
{}
...
@@ -25,10 +30,22 @@ public class Piece implements Comparable<Piece> {
...
@@ -25,10 +30,22 @@ public class Piece implements Comparable<Piece> {
}
}
public
enum
Type
{
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
double
strength
;
private
char
representation
;
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
)
{
Type
(
double
strength
,
char
representation
)
{
this
.
strength
=
strength
;
this
.
strength
=
strength
;
...
@@ -42,6 +59,10 @@ public class Piece implements Comparable<Piece> {
...
@@ -42,6 +59,10 @@ public class Piece implements Comparable<Piece> {
public
double
getStrength
()
{
public
double
getStrength
()
{
return
strength
;
return
strength
;
}
}
public
MoveStrategy
getMoveStrategy
()
{
return
moveStrategy
;
}
}
}
public
void
setPosition
(
Position
position
)
{
public
void
setPosition
(
Position
position
)
{
...
@@ -90,6 +111,10 @@ public class Piece implements Comparable<Piece> {
...
@@ -90,6 +111,10 @@ public class Piece implements Comparable<Piece> {
piece
.
getType
()
==
currentPiece
.
getType
();
piece
.
getType
()
==
currentPiece
.
getType
();
}
}
boolean
isMovable
(
Position
from
,
Position
to
)
{
return
this
.
getType
().
getMoveStrategy
().
isMovable
(
from
,
to
);
}
double
getStrength
()
{
double
getStrength
()
{
return
strength
;
return
strength
;
}
}
...
@@ -115,6 +140,9 @@ public class Piece implements Comparable<Piece> {
...
@@ -115,6 +140,9 @@ public class Piece implements Comparable<Piece> {
boolean
isType
(
Piece
.
Type
type
)
{
boolean
isType
(
Piece
.
Type
type
)
{
return
this
.
getType
()
==
type
;
return
this
.
getType
()
==
type
;
}
}
public
boolean
isAtPosition
(
Position
position
)
{
return
this
.
position
.
equals
(
position
);
}
static
Piece
createPawn
(
Color
color
)
{
static
Piece
createPawn
(
Color
color
)
{
return
new
Piece
(
color
,
Type
.
Pawn
);
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 {
...
@@ -47,7 +47,7 @@ public class Position {
return
(
int
)
c
-
(
int
)
firstColumnLetter
;
return
(
int
)
c
-
(
int
)
firstColumnLetter
;
}
}
int
getRow
()
{
public
int
getRow
()
{
return
row
;
return
row
;
}
}
...
@@ -55,7 +55,7 @@ public class Position {
...
@@ -55,7 +55,7 @@ public class Position {
return
column
;
return
column
;
}
}
int
getIntColumn
()
{
public
int
getIntColumn
()
{
return
charToInt
(
column
);
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
;
package
com
.
example
.
paktalin
.
agilejava_exercises
.
moves
;
import
com.example.paktalin.agilejava_exercises.Position
;
/**
/**
* Created by Paktalin on 31/05/2018.
* 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
;
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
;
import
junit.framework.TestSuite
;
...
@@ -17,7 +17,7 @@ public class AllTests extends TestSuite {
...
@@ -17,7 +17,7 @@ public class AllTests extends TestSuite {
suite
.
addTestSuite
(
BoardTest
.
class
);
suite
.
addTestSuite
(
BoardTest
.
class
);
suite
.
addTestSuite
(
CharacterTest
.
class
);
suite
.
addTestSuite
(
CharacterTest
.
class
);
suite
.
addTestSuite
(
PositionTest
.
class
);
suite
.
addTestSuite
(
PositionTest
.
class
);
suite
.
addTestSuite
(
KingMove
s
Test
.
class
);
suite
.
addTestSuite
(
KingMove
Strategy
Test
.
class
);
return
suite
;
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;
...
@@ -10,7 +10,7 @@ import junit.framework.TestCase;
* Created by Paktalin on 31/05/2018.
* 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
Piece
king
;
private
Board
board
;
private
Board
board
;
...
@@ -22,10 +22,20 @@ public class KingMovesTest extends TestCase {
...
@@ -22,10 +22,20 @@ public class KingMovesTest extends TestCase {
king
=
board
.
getPieceAtPosition
(
"b5"
);
king
=
board
.
getPieceAtPosition
(
"b5"
);
}
}
public
void
testCreate
()
{
public
void
testMove
()
{
assertEquals
(
"b5"
,
king
.
getPosition
().
toString
());
Position
currentPosition
=
king
.
getPosition
();
board
.
move
(
Position
.
create
(
"b5"
),
Position
.
create
(
"a6"
));
assertEquals
(
"b5"
,
currentPosition
.
toString
());
assertEquals
(
"a6"
,
king
.
getPosition
().
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