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
Feb 26, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring in Position class
parent
90aeec8c
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
90 additions
and
44 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
;
import
com.paktalin.chess.coordinates.Column
;
import
com.paktalin.chess.coordinates.Row
;
/**
* Created by Paktalin on 31/05/2018.
*/
public
class
Position
{
private
int
row
;
private
char
column
;
private
Column
column
;
private
Row
row
;
private
Position
(
int
row
,
char
column
)
{
private
Position
(
Row
row
,
Column
column
)
{
this
.
row
=
row
;
this
.
column
=
column
;
}
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
)
{
return
new
Position
(
row
,
intToChar
(
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
);
return
new
Position
(
new
Row
(
row
),
new
Column
(
column
));
}
@Override
public
String
toString
()
{
return
column
+
""
+
(
row
+
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
;
return
column
.
getCharCode
()
+
""
+
(
row
.
getIntCode
()
+
1
);
}
public
int
getRow
()
{
return
row
;
return
row
.
getIntCode
()
;
}
public
char
getColumn
()
{
return
column
;
return
column
.
getCharCode
()
;
}
public
int
getIntColumn
()
{
return
c
harToInt
(
column
);
return
c
olumn
.
getIntCode
(
);
}
}
}
\ No newline at end of file
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
;
}
}
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
;
}
}
src/main/com/paktalin/chess/pieces/Piece.java
View file @
a5d90626
...
...
@@ -11,9 +11,8 @@ import java.util.List;
*/
public
abstract
class
Piece
implements
Comparable
<
Piece
>
{
Color
color
;
private
Position
position
;
Color
color
;
double
strength
;
MoveStrategy
moveStrategy
;
char
representation
;
...
...
@@ -33,7 +32,9 @@ public abstract class Piece implements Comparable<Piece> {
this
.
position
=
position
;
}
public
void
setStrength
(
List
<
Piece
>
pieces
)
{}
public
void
setStrength
(
List
<
Piece
>
pieces
)
{
}
void
setStrength
(
double
strength
){
this
.
strength
=
strength
;
...
...
src/test/com/paktalin/chess/BoardTest.java
View file @
a5d90626
...
...
@@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test;
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
;
/**
...
...
src/test/com/paktalin/chess/PositionTest.java
View file @
a5d90626
...
...
@@ -30,9 +30,9 @@ class PositionTest {
@Test
void
testStrengthByPosition
()
{
verifyStrength
(
new
Bishop
(
Piece
.
Color
.
Black
),
"b5"
,
3.0
);
verifyStrength
(
new
Pawn
(
Piece
.
Color
.
White
),
"a2"
,
1.0
);
verifyStrength
(
new
Pawn
(
Piece
.
Color
.
White
),
"a3"
,
0.5
);
verifyStrength
(
new
Bishop
(
Color
.
Black
),
"b5"
,
3.0
);
verifyStrength
(
new
Pawn
(
Color
.
White
),
"a2"
,
1.0
);
verifyStrength
(
new
Pawn
(
Color
.
White
),
"a3"
,
0.5
);
assertEquals
(
0.5
,
board
.
getPieceAtPosition
(
"a2"
).
getStrength
());
}
...
...
src/test/com/paktalin/chess/pieces/BishopTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
* Created by Paktalin on 01/06/2018.
*/
...
...
@@ -12,7 +14,7 @@ class BishopTest extends PieceTest {
}
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
Bishop
(
color
);
}
...
...
src/test/com/paktalin/chess/pieces/KingTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
* Created by Paktalin on 01/06/2018.
*/
...
...
@@ -12,7 +14,7 @@ class KingTest extends PieceTest {
}
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
King
(
color
);
}
...
...
src/test/com/paktalin/chess/pieces/KnightTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
* Created by Paktalin on 01/06/2018.
*/
...
...
@@ -12,7 +14,7 @@ class KnightTest extends PieceTest {
}
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
Knight
(
color
);
}
...
...
src/test/com/paktalin/chess/pieces/PawnTest.java
View file @
a5d90626
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
;
/**
...
...
@@ -15,7 +17,7 @@ class PawnTest extends PieceTest {
}
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
Pawn
(
color
);
}
...
...
src/test/com/paktalin/chess/pieces/PieceTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Board
;
import
com.paktalin.chess.Color
;
import
com.paktalin.chess.Position
;
import
org.junit.jupiter.api.BeforeEach
;
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
.
assertTrue
;
...
...
@@ -27,7 +28,7 @@ abstract class PieceTest {
}
abstract
void
setExpectedRepresentation
();
abstract
Piece
createPiece
(
Piece
.
Color
color
);
abstract
Piece
createPiece
(
Color
color
);
abstract
void
setExpectedStrength
();
...
...
src/test/com/paktalin/chess/pieces/QueenTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
* Created by Paktalin on 01/06/2018.
*/
...
...
@@ -12,7 +14,7 @@ class QueenTest extends PieceTest {
}
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
Queen
(
color
);
}
...
...
src/test/com/paktalin/chess/pieces/RookTest.java
View file @
a5d90626
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
* Created by Paktalin on 01/06/2018.
*/
...
...
@@ -12,7 +14,7 @@ class RookTest extends PieceTest {
}
@Override
Piece
createPiece
(
Piece
.
Color
color
)
{
Piece
createPiece
(
Color
color
)
{
return
new
Rook
(
color
);
}
...
...
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