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
125aa959
authored
Jun 01, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in Piece and Pawn
parent
63f13924
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
33 deletions
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Bishop.java
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Knight.java
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Pawn.java
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Piece.java
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Bishop.java
View file @
125aa959
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
import
com.example.paktalin.agilejava_exercises.Position
;
/**
/**
* Created by Paktalin on 01/06/2018.
* Created by Paktalin on 01/06/2018.
*/
*/
public
class
Bishop
extends
Piece
{
public
class
Bishop
extends
Piece
{
public
Bishop
(
Piece
.
Color
color
)
{
public
Bishop
(
Piece
.
Color
color
)
{
strength
=
3
;
strength
=
3
;
representation
=
'b'
;
representation
=
'b'
;
...
...
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Knight.java
View file @
125aa959
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
import
com.example.paktalin.agilejava_exercises.Position
;
/**
/**
* Created by Paktalin on 01/06/2018.
* Created by Paktalin on 01/06/2018.
*/
*/
...
...
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Pawn.java
View file @
125aa959
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
package
com
.
example
.
paktalin
.
agilejava_exercises
.
pieces
;
import
com.example.paktalin.agilejava_exercises.Position
;
import
java.util.List
;
import
java.util.List
;
/**
/**
...
@@ -27,8 +25,16 @@ public class Pawn extends Piece {
...
@@ -27,8 +25,16 @@ public class Pawn extends Piece {
}
}
private
static
boolean
pawnsOnSameColumn
(
Piece
currentPiece
,
Piece
piece
)
{
private
static
boolean
pawnsOnSameColumn
(
Piece
currentPiece
,
Piece
piece
)
{
return
piece
.
getPosition
().
getColumn
()
==
currentPiece
.
getPosition
().
getColumn
()
&&
return
sameColumn
(
currentPiece
,
piece
)
&&
piece
.
getPosition
().
getRow
()
!=
currentPiece
.
getPosition
().
getRow
()
&&
sameRow
(
currentPiece
,
piece
)
&&
piece
.
getClass
()
==
currentPiece
.
getClass
();
piece
.
getClass
()
==
Pawn
.
class
;
}
private
static
boolean
sameColumn
(
Piece
piece1
,
Piece
piece2
)
{
return
piece2
.
getPosition
().
getColumn
()
==
piece1
.
getPosition
().
getColumn
();
}
private
static
boolean
sameRow
(
Piece
piece1
,
Piece
piece2
)
{
return
piece2
.
getPosition
().
getRow
()
==
piece1
.
getPosition
().
getRow
();
}
}
}
}
app/src/main/java/com/example/paktalin/agilejava_exercises/pieces/Piece.java
View file @
125aa959
...
@@ -17,22 +17,25 @@ public abstract class Piece implements Comparable<Piece> {
...
@@ -17,22 +17,25 @@ public abstract class Piece implements Comparable<Piece> {
public
enum
Color
{
White
,
Black
}
public
enum
Color
{
White
,
Black
}
Color
color
;
Color
color
;
private
Position
position
;
double
strength
;
double
strength
;
private
Position
position
;
MoveStrategy
moveStrategy
;
MoveStrategy
moveStrategy
;
char
representation
;
char
representation
;
Piece
()
{}
Piece
()
{}
public
void
setPosition
(
Position
position
)
{
@Override
this
.
position
=
position
;
public
int
compareTo
(
@NonNull
Piece
that
)
{
if
(
this
.
getStrength
()
>
that
.
getStrength
())
return
-
1
;
if
(
this
.
getStrength
()
<
that
.
getStrength
())
return
1
;
return
0
;
}
}
public
char
getRepresentation
()
{
public
void
setPosition
(
Position
position
)
{
if
(
isBlack
())
this
.
position
=
position
;
return
Character
.
toUpperCase
(
representation
);
return
representation
;
}
}
public
void
setStrength
(
List
<
Piece
>
pieces
)
{}
public
void
setStrength
(
List
<
Piece
>
pieces
)
{}
...
@@ -41,39 +44,37 @@ public abstract class Piece implements Comparable<Piece> {
...
@@ -41,39 +44,37 @@ public abstract class Piece implements Comparable<Piece> {
this
.
strength
=
strength
;
this
.
strength
=
strength
;
}
}
@Override
public
char
getRepresentation
()
{
public
int
compareTo
(
@NonNull
Piece
that
)
{
if
(
isBlack
())
if
(
this
.
getStrength
()
>
that
.
getStrength
())
return
Character
.
toUpperCase
(
representation
);
return
-
1
;
return
representation
;
if
(
this
.
getStrength
()
<
that
.
getStrength
())
return
1
;
return
0
;
}
public
boolean
isMovable
(
Position
from
,
Position
to
){
return
moveStrategy
.
isMovable
(
from
,
to
);
}
}
public
double
getStrength
()
{
public
double
getStrength
()
{
return
this
.
strength
;
return
strength
;
}
}
public
Color
getColor
()
{
public
Color
getColor
()
{
return
color
;
return
color
;
}
}
public
Position
getPosition
()
{
public
Position
getPosition
()
{
return
position
;
return
position
;
}
}
boolean
isColor
(
Color
color
)
{
return
this
.
color
==
color
;
}
public
boolean
isWhite
()
{
public
boolean
isWhite
()
{
return
isColor
(
Color
.
White
)
;
return
this
.
color
==
Color
.
White
;
}
}
public
boolean
isBlack
()
{
public
boolean
isBlack
()
{
return
isColor
(
Color
.
Black
)
;
return
this
.
color
==
Color
.
Black
;
}
}
public
boolean
isAtPosition
(
Position
position
)
{
public
boolean
isAtPosition
(
Position
position
)
{
return
this
.
position
.
equals
(
position
);
return
this
.
position
.
equals
(
position
);
}
}
public
boolean
isMovable
(
Position
from
,
Position
to
){
return
moveStrategy
.
isMovable
(
from
,
to
);
}
}
}
\ No newline at end of file
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