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
a5d90626
authored
6 years ago
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in Position class
parent
90aeec8c
master
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
89 additions
and
43 deletions
src/main/com/paktalin/chess/Position.java
src/main/com/paktalin/chess/coordinates/Column.java
src/main/com/paktalin/chess/coordinates/Row.java
src/main/com/paktalin/chess/pieces/Piece.java
src/test/com/paktalin/chess/BoardTest.java
src/test/com/paktalin/chess/PositionTest.java
src/test/com/paktalin/chess/pieces/BishopTest.java
src/test/com/paktalin/chess/pieces/KingTest.java
src/test/com/paktalin/chess/pieces/KnightTest.java
src/test/com/paktalin/chess/pieces/PawnTest.java
src/test/com/paktalin/chess/pieces/PieceTest.java
src/test/com/paktalin/chess/pieces/QueenTest.java
src/test/com/paktalin/chess/pieces/RookTest.java
src/main/com/paktalin/chess/Position.java
View file @
a5d90626
package
com
.
paktalin
.
chess
;
package
com
.
paktalin
.
chess
;
import
com.paktalin.chess.coordinates.Column
;
import
com.paktalin.chess.coordinates.Row
;
/**
/**
* Created by Paktalin on 31/05/2018.
* Created by Paktalin on 31/05/2018.
*/
*/
public
class
Position
{
public
class
Position
{
private
int
row
;
private
Column
column
;
private
char
column
;
private
Row
row
;
private
Position
(
int
row
,
char
column
)
{
private
Position
(
Row
row
,
Column
column
)
{
this
.
row
=
row
;
this
.
row
=
row
;
this
.
column
=
column
;
this
.
column
=
column
;
}
}
public
static
Position
create
(
String
positionString
)
{
public
static
Position
create
(
String
positionString
)
{
return
create
(
retrieveRow
(
positionString
),
retrieveColumn
(
positionString
));
char
column
=
positionString
.
charAt
(
0
);
int
row
=
Character
.
getNumericValue
(
positionString
.
charAt
(
1
))
-
1
;
return
new
Position
(
new
Row
(
row
),
new
Column
(
column
));
}
}
static
Position
create
(
int
row
,
int
column
)
{
static
Position
create
(
int
row
,
int
column
)
{
return
new
Position
(
row
,
intToChar
(
column
));
return
new
Position
(
new
Row
(
row
),
new
Column
(
column
));
}
private
static
int
retrieveRow
(
String
position
)
{
return
Integer
.
parseInt
(
position
.
split
(
""
)[
1
])
-
1
;
}
private
static
int
retrieveColumn
(
String
position
)
{
char
file
=
position
.
toCharArray
()[
0
];
return
charToInt
(
file
);
}
}
@Override
@Override
public
String
toString
()
{
public
String
toString
()
{
return
column
+
""
+
(
row
+
1
);
return
column
.
getCharCode
()
+
""
+
(
row
.
getIntCode
()
+
1
);
}
private
static
char
intToChar
(
int
i
)
{
return
(
char
)(
i
+
'a'
);
}
private
static
int
charToInt
(
char
c
)
{
char
firstColumnLetter
=
'a'
;
return
(
int
)
c
-
(
int
)
firstColumnLetter
;
}
}
public
int
getRow
()
{
public
int
getRow
()
{
return
row
;
return
row
.
getIntCode
()
;
}
}
public
char
getColumn
()
{
public
char
getColumn
()
{
return
column
;
return
column
.
getCharCode
()
;
}
}
public
int
getIntColumn
()
{
public
int
getIntColumn
()
{
return
c
harToInt
(
column
);
return
c
olumn
.
getIntCode
(
);
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/main/com/paktalin/chess/coordinates/Column.java
0 → 100644
View file @
a5d90626
package
com
.
paktalin
.
chess
.
coordinates
;
public
class
Column
extends
Row
{
private
char
charCode
;
public
Column
(
int
intCode
)
{
super
(
intCode
);
this
.
charCode
=
generateCharCodeFromInt
(
intCode
);
}
public
Column
(
char
charCode
)
{
super
();
this
.
charCode
=
charCode
;
this
.
intCode
=
generateIntCodeFromChar
(
charCode
);
}
private
int
generateIntCodeFromChar
(
char
c
)
{
char
firstColumnLetter
=
'a'
;
return
(
int
)
c
-
(
int
)
firstColumnLetter
;
}
private
char
generateCharCodeFromInt
(
int
i
)
{
return
(
char
)(
i
+
'a'
);
}
public
char
getCharCode
()
{
return
charCode
;
}
}
This diff is collapsed.
Click to expand it.
src/main/com/paktalin/chess/coordinates/Row.java
0 → 100644
View file @
a5d90626
package
com
.
paktalin
.
chess
.
coordinates
;
public
class
Row
{
int
intCode
;
Row
()
{}
public
Row
(
int
intCode
)
{
this
.
intCode
=
intCode
;
}
public
int
getIntCode
()
{
return
intCode
;
}
}
This diff is collapsed.
Click to expand it.
src/main/com/paktalin/chess/pieces/Piece.java
View file @
a5d90626
...
@@ -11,9 +11,8 @@ import java.util.List;
...
@@ -11,9 +11,8 @@ import java.util.List;
*/
*/
public
abstract
class
Piece
implements
Comparable
<
Piece
>
{
public
abstract
class
Piece
implements
Comparable
<
Piece
>
{
Color
color
;
private
Position
position
;
private
Position
position
;
Color
color
;
double
strength
;
double
strength
;
MoveStrategy
moveStrategy
;
MoveStrategy
moveStrategy
;
char
representation
;
char
representation
;
...
@@ -33,7 +32,9 @@ public abstract class Piece implements Comparable<Piece> {
...
@@ -33,7 +32,9 @@ public abstract class Piece implements Comparable<Piece> {
this
.
position
=
position
;
this
.
position
=
position
;
}
}
public
void
setStrength
(
List
<
Piece
>
pieces
)
{}
public
void
setStrength
(
List
<
Piece
>
pieces
)
{
}
void
setStrength
(
double
strength
){
void
setStrength
(
double
strength
){
this
.
strength
=
strength
;
this
.
strength
=
strength
;
...
...
This diff is collapsed.
Click to expand it.
src/test/com/paktalin/chess/BoardTest.java
View file @
a5d90626
...
@@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test;
...
@@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test;
import
java.util.List
;
import
java.util.List
;
import
static
com
.
paktalin
.
chess
.
pieces
.
Piece
.
Color
.*;
import
static
com
.
paktalin
.
chess
.
Color
.*;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
/**
/**
...
...
This diff is collapsed.
Click to expand it.
src/test/com/paktalin/chess/PositionTest.java
View file @
a5d90626
...
@@ -30,9 +30,9 @@ class PositionTest {
...
@@ -30,9 +30,9 @@ class PositionTest {
@Test
@Test
void
testStrengthByPosition
()
{
void
testStrengthByPosition
()
{
verifyStrength
(
new
Bishop
(
Piece
.
Color
.
Black
),
"b5"
,
3.0
);
verifyStrength
(
new
Bishop
(
Color
.
Black
),
"b5"
,
3.0
);
verifyStrength
(
new
Pawn
(
Piece
.
Color
.
White
),
"a2"
,
1.0
);
verifyStrength
(
new
Pawn
(
Color
.
White
),
"a2"
,
1.0
);
verifyStrength
(
new
Pawn
(
Piece
.
Color
.
White
),
"a3"
,
0.5
);
verifyStrength
(
new
Pawn
(
Color
.
White
),
"a3"
,
0.5
);
assertEquals
(
0.5
,
board
.
getPieceAtPosition
(
"a2"
).
getStrength
());
assertEquals
(
0.5
,
board
.
getPieceAtPosition
(
"a2"
).
getStrength
());
}
}
...
...
This diff is collapsed.
Click to expand it.
src/test/com/paktalin/chess/pieces/BishopTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
/**
* Created by Paktalin on 01/06/2018.
* Created by Paktalin on 01/06/2018.
*/
*/
...
@@ -12,7 +14,7 @@ class BishopTest extends PieceTest {
...
@@ -12,7 +14,7 @@ class BishopTest extends PieceTest {
}
}
@Override
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
Bishop
(
color
);
return
new
Bishop
(
color
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/test/com/paktalin/chess/pieces/KingTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
/**
* Created by Paktalin on 01/06/2018.
* Created by Paktalin on 01/06/2018.
*/
*/
...
@@ -12,7 +14,7 @@ class KingTest extends PieceTest {
...
@@ -12,7 +14,7 @@ class KingTest extends PieceTest {
}
}
@Override
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
King
(
color
);
return
new
King
(
color
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/test/com/paktalin/chess/pieces/KnightTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
/**
* Created by Paktalin on 01/06/2018.
* Created by Paktalin on 01/06/2018.
*/
*/
...
@@ -12,7 +14,7 @@ class KnightTest extends PieceTest {
...
@@ -12,7 +14,7 @@ class KnightTest extends PieceTest {
}
}
@Override
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
Knight
(
color
);
return
new
Knight
(
color
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/test/com/paktalin/chess/pieces/PawnTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
package
com
.
paktalin
.
chess
.
pieces
;
import
static
com
.
paktalin
.
chess
.
pieces
.
Piece
.
Color
.
Black
;
import
com.paktalin.chess.Color
;
import
static
com
.
paktalin
.
chess
.
Color
.
Black
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
/**
/**
...
@@ -15,7 +17,7 @@ class PawnTest extends PieceTest {
...
@@ -15,7 +17,7 @@ class PawnTest extends PieceTest {
}
}
@Override
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
Pawn
(
color
);
return
new
Pawn
(
color
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/test/com/paktalin/chess/pieces/PieceTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Board
;
import
com.paktalin.chess.Board
;
import
com.paktalin.chess.Color
;
import
com.paktalin.chess.Position
;
import
com.paktalin.chess.Position
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
static
com
.
paktalin
.
chess
.
pieces
.
Piece
.
Color
.*;
import
static
com
.
paktalin
.
chess
.
Color
.*;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
...
@@ -27,7 +28,7 @@ abstract class PieceTest {
...
@@ -27,7 +28,7 @@ abstract class PieceTest {
}
}
abstract
void
setExpectedRepresentation
();
abstract
void
setExpectedRepresentation
();
abstract
Piece
createPiece
(
Piece
.
Color
color
);
abstract
Piece
createPiece
(
Color
color
);
abstract
void
setExpectedStrength
();
abstract
void
setExpectedStrength
();
...
...
This diff is collapsed.
Click to expand it.
src/test/com/paktalin/chess/pieces/QueenTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
/**
* Created by Paktalin on 01/06/2018.
* Created by Paktalin on 01/06/2018.
*/
*/
...
@@ -12,7 +14,7 @@ class QueenTest extends PieceTest {
...
@@ -12,7 +14,7 @@ class QueenTest extends PieceTest {
}
}
@Override
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
Queen
(
color
);
return
new
Queen
(
color
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/test/com/paktalin/chess/pieces/RookTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
/**
* Created by Paktalin on 01/06/2018.
* Created by Paktalin on 01/06/2018.
*/
*/
...
@@ -12,7 +14,7 @@ class RookTest extends PieceTest {
...
@@ -12,7 +14,7 @@ class RookTest extends PieceTest {
}
}
@Override
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
Rook
(
color
);
return
new
Rook
(
color
);
}
}
...
...
This diff is collapsed.
Click to expand it.
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