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
90aeec8c
authored
Feb 26, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring + completed 7.3.a
parent
816ed672
Show whitespace changes
Inline
Side-by-side
Showing
23 changed files
with
114 additions
and
96 deletions
src/main/com/paktalin/chess/Board.java
src/main/com/paktalin/chess/BoardLayout.java
src/main/com/paktalin/chess/Color.java
src/main/com/paktalin/chess/Position.java
src/main/com/paktalin/chess/language/Continue.java
src/main/com/paktalin/chess/language/Factorial.java
src/main/com/paktalin/chess/moves/KingMoveStrategy.java
src/main/com/paktalin/chess/moves/MoveStrategy.java
src/main/com/paktalin/chess/moves/QueenMoveStrategy.java
src/main/com/paktalin/chess/pieces/Bishop.java
src/main/com/paktalin/chess/pieces/King.java
src/main/com/paktalin/chess/pieces/Knight.java
src/main/com/paktalin/chess/pieces/Pawn.java
src/main/com/paktalin/chess/pieces/Piece.java
src/main/com/paktalin/chess/pieces/Queen.java
src/main/com/paktalin/chess/pieces/Rook.java
src/test/com/paktalin/chess/CharacterTest.java → src/test/com/paktalin/chess/language/CharacterTest.java
src/test/com/paktalin/chess/language/ContinueTest.java
src/test/com/paktalin/chess/language/FactorialTest.java
src/test/com/paktalin/chess/moves/KingMoveStrategyTest.java
src/test/com/paktalin/chess/moves/MoveStrategyTest.java
src/test/com/paktalin/chess/moves/QueenMoveStrategyTest.java
src/test/com/paktalin/chess/pieces/PieceTest.java
src/main/com/paktalin/chess/Board.java
View file @
90aeec8c
...
...
@@ -4,8 +4,7 @@ import com.paktalin.chess.pieces.Piece;
import
java.util.List
;
import
static
com
.
paktalin
.
chess
.
pieces
.
Piece
.
Color
.
Black
;
import
static
com
.
paktalin
.
chess
.
pieces
.
Piece
.
Color
.
White
;
import
static
com
.
paktalin
.
chess
.
Color
.*;
/**
* Created by Paktalin on 23/05/2018.
...
...
@@ -65,7 +64,7 @@ public class Board {
getSide
(
piece
.
getColor
()).
addPiece
(
piece
);
}
private
Side
getSide
(
Piece
.
Color
color
){
private
Side
getSide
(
Color
color
){
if
(
color
==
White
)
return
whiteSide
;
return
blackSide
;
...
...
@@ -74,13 +73,13 @@ public class Board {
int
getPiecesCount
()
{
return
getPiecesCount
(
Black
)
+
getPiecesCount
(
White
);
}
double
getStrength
(
Piece
.
Color
color
)
{
double
getStrength
(
Color
color
)
{
return
getSide
(
color
).
getStrength
();
}
List
<
Piece
>
getPieces
(
Piece
.
Color
color
)
{
List
<
Piece
>
getPieces
(
Color
color
)
{
return
getSide
(
color
).
getPieces
();
}
int
getPiecesCount
(
Piece
.
Color
color
){
int
getPiecesCount
(
Color
color
){
return
getSide
(
color
).
getPiecesCount
();
}
}
\ No newline at end of file
src/main/com/paktalin/chess/BoardLayout.java
View file @
90aeec8c
...
...
@@ -3,7 +3,7 @@ package com.paktalin.chess;
import
com.paktalin.chess.pieces.Piece
;
import
com.paktalin.chess.pieces.*
;
import
static
com
.
paktalin
.
chess
.
pieces
.
Piece
.
Color
.*;
import
static
com
.
paktalin
.
chess
.
Color
.*;
import
static
com
.
paktalin
.
chess
.
util
.
StringUtil
.
NEW_LINE
;
...
...
@@ -33,7 +33,7 @@ class BoardLayout {
return
layout
;
}
private
void
initializeKingRank
(
Piece
.
Color
color
,
int
row
)
{
private
void
initializeKingRank
(
Color
color
,
int
row
)
{
placePiece
(
new
Rook
(
color
),
Position
.
create
(
row
,
0
));
placePiece
(
new
Knight
(
color
),
Position
.
create
(
row
,
1
));
placePiece
(
new
Bishop
(
color
),
Position
.
create
(
row
,
2
));
...
...
@@ -44,7 +44,7 @@ class BoardLayout {
placePiece
(
new
Rook
(
color
),
Position
.
create
(
row
,
7
));
}
private
void
initializePawnRank
(
Piece
.
Color
color
,
int
row
)
{
private
void
initializePawnRank
(
Color
color
,
int
row
)
{
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
placePiece
(
new
Pawn
(
color
),
Position
.
create
(
row
,
column
));
}
...
...
src/main/com/paktalin/chess/Color.java
0 → 100644
View file @
90aeec8c
package
com
.
paktalin
.
chess
;
public
enum
Color
{
White
,
Black
}
src/main/com/paktalin/chess/Position.java
View file @
90aeec8c
...
...
@@ -9,8 +9,6 @@ public class Position {
private
int
row
;
private
char
column
;
private
Position
()
{}
private
Position
(
int
row
,
char
column
)
{
this
.
row
=
row
;
this
.
column
=
column
;
...
...
src/main/com/paktalin/chess/language/Continue.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
language
;
import
java.util.Vector
;
class
Continue
{
private
final
static
char
WHITESPACE
=
' '
;
private
final
static
char
ASTERISK
=
'*'
;
static
String
generate
OneToNString
(
int
n
)
{
static
String
generate
StringOneToN
(
int
n
)
{
if
(
n
<
1
)
return
null
;
StringBuilder
result
=
new
StringBuilder
();
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
...
...
@@ -15,4 +17,17 @@ class Continue {
}
return
result
.
toString
();
}
static
Vector
<
String
>
generateVectorOneToN
(
int
n
)
{
if
(
n
<
1
)
return
null
;
Vector
<
String
>
resultVector
=
new
Vector
<>();
StringBuilder
currentBuilder
;
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
currentBuilder
=
new
StringBuilder
(
String
.
valueOf
(
i
));
if
(
i
%
5
==
0
)
currentBuilder
.
append
(
ASTERISK
);
resultVector
.
add
(
currentBuilder
.
toString
());
}
return
resultVector
;
}
}
src/main/com/paktalin/chess/language/Factorial.java
View file @
90aeec8c
...
...
@@ -2,7 +2,7 @@ package com.paktalin.chess.language;
class
Factorial
{
static
int
getW
hileFactorial
(
int
number
)
{
static
int
w
hileFactorial
(
int
number
)
{
int
factorial
;
if
(
number
==
0
||
number
==
1
)
factorial
=
1
;
...
...
@@ -17,7 +17,7 @@ class Factorial {
return
factorial
;
}
static
int
getD
oWhileFactorial
(
int
number
)
{
static
int
d
oWhileFactorial
(
int
number
)
{
int
factorial
;
if
(
number
==
0
||
number
==
1
)
factorial
=
1
;
...
...
@@ -32,7 +32,7 @@ class Factorial {
return
factorial
;
}
static
int
getF
orFactorial
(
int
number
)
{
static
int
f
orFactorial
(
int
number
)
{
int
factorial
;
if
(
number
==
0
||
number
==
1
)
factorial
=
1
;
...
...
@@ -45,7 +45,7 @@ class Factorial {
return
factorial
;
}
static
int
getB
reakFactorial
(
int
number
)
{
static
int
b
reakFactorial
(
int
number
)
{
int
factorial
;
if
(
number
==
0
||
number
==
1
)
factorial
=
1
;
...
...
src/main/com/paktalin/chess/moves/KingMoveStrategy.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
moves
;
import
com.paktalin.chess.Position
;
/**
...
...
@@ -11,7 +10,7 @@ public class KingMoveStrategy extends MoveStrategy{
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
super
.
isMovabl
e
(
from
,
to
);
initializ
e
(
from
,
to
);
return
!(
distantRows
()
||
distantColumns
());
}
...
...
src/main/com/paktalin/chess/moves/MoveStrategy.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
moves
;
import
com.paktalin.chess.Position
;
/**
...
...
@@ -8,20 +7,15 @@ import com.paktalin.chess.Position;
*/
public
abstract
class
MoveStrategy
{
int
fromRow
,
fromColumn
;
int
toRow
,
toColumn
;
private
void
initialize
(
Position
from
,
Position
to
)
{
void
initialize
(
Position
from
,
Position
to
)
{
fromRow
=
from
.
getRow
();
fromColumn
=
from
.
getIntColumn
();
toRow
=
to
.
getRow
();
toColumn
=
to
.
getIntColumn
();
}
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
initialize
(
from
,
to
);
return
false
;
}
public
abstract
boolean
isMovable
(
Position
from
,
Position
to
);
}
src/main/com/paktalin/chess/moves/QueenMoveStrategy.java
View file @
90aeec8c
...
...
@@ -10,7 +10,7 @@ public class QueenMoveStrategy extends MoveStrategy {
@Override
public
boolean
isMovable
(
Position
from
,
Position
to
)
{
super
.
isMovabl
e
(
from
,
to
);
initializ
e
(
from
,
to
);
return
sameRow
()
||
sameColumn
()
||
diagonal
();
}
...
...
src/main/com/paktalin/chess/pieces/Bishop.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
* Created by Paktalin on 01/06/2018.
*/
public
class
Bishop
extends
Piece
{
public
Bishop
(
Piece
.
Color
color
)
{
public
Bishop
(
Color
color
)
{
strength
=
3
;
representation
=
'b'
;
this
.
color
=
color
;
...
...
src/main/com/paktalin/chess/pieces/King.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
import
com.paktalin.chess.moves.KingMoveStrategy
;
/**
...
...
src/main/com/paktalin/chess/pieces/Knight.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
* Created by Paktalin on 01/06/2018.
*/
public
class
Knight
extends
Piece
{
public
Knight
(
Piece
.
Color
color
)
{
public
Knight
(
Color
color
)
{
strength
=
2.5
;
representation
=
'n'
;
this
.
color
=
color
;
...
...
src/main/com/paktalin/chess/pieces/Pawn.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
import
java.util.List
;
/**
...
...
@@ -8,7 +10,7 @@ import java.util.List;
public
class
Pawn
extends
Piece
{
public
Pawn
(
Piece
.
Color
color
)
{
public
Pawn
(
Color
color
)
{
strength
=
1
;
representation
=
'p'
;
this
.
color
=
color
;
...
...
src/main/com/paktalin/chess/pieces/Piece.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
import
com.paktalin.chess.Position
;
import
com.paktalin.chess.moves.MoveStrategy
;
...
...
@@ -10,8 +11,6 @@ import java.util.List;
*/
public
abstract
class
Piece
implements
Comparable
<
Piece
>
{
public
enum
Color
{
White
,
Black
}
Color
color
;
private
Position
position
;
...
...
src/main/com/paktalin/chess/pieces/Queen.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
import
com.paktalin.chess.moves.QueenMoveStrategy
;
/**
...
...
@@ -8,7 +9,7 @@ import com.paktalin.chess.moves.QueenMoveStrategy;
public
class
Queen
extends
Piece
{
public
Queen
(
Piece
.
Color
color
)
{
public
Queen
(
Color
color
)
{
strength
=
9
;
representation
=
'q'
;
moveStrategy
=
new
QueenMoveStrategy
();
...
...
src/main/com/paktalin/chess/pieces/Rook.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
pieces
;
import
com.paktalin.chess.Color
;
/**
* Created by Paktalin on 01/06/2018.
*/
public
class
Rook
extends
Piece
{
public
Rook
(
Piece
.
Color
color
)
{
public
Rook
(
Color
color
)
{
strength
=
5
;
representation
=
'r'
;
this
.
color
=
color
;
...
...
src/test/com/paktalin/chess/CharacterTest.java
→
src/test/com/paktalin/chess/
language/
CharacterTest.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
;
package
com
.
paktalin
.
chess
.
language
;
import
org.junit.jupiter.api.Test
;
...
...
src/test/com/paktalin/chess/language/ContinueTest.java
View file @
90aeec8c
...
...
@@ -2,17 +2,30 @@ package com.paktalin.chess.language;
import
org.junit.jupiter.api.Test
;
import
java.util.List
;
import
java.util.Vector
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertNull
;
class
ContinueTest
{
@Test
void
testContinue
(){
int
n
=
12
;
void
generateStringOneToN
()
{
int
n
=
0
;
assertNull
(
Continue
.
generateStringOneToN
(
n
));
n
=
12
;
String
oneToN
=
"1 2 3 4 5* 6 7 8 9 10* 11 12"
;
assertEquals
(
oneToN
,
Continue
.
generateOneToNString
(
n
));
assertEquals
(
oneToN
,
Continue
.
generateStringOneToN
(
n
));
}
@Test
void
generateVectorOneToN
()
{
int
n
=
0
;
assertNull
(
Continue
.
generateStringOneToN
(
n
));
n
=
0
;
assertNull
(
Continue
.
generateOneToNString
(
n
));
n
=
10
;
Vector
<
String
>
oneToNVector
=
new
Vector
<>(
List
.
of
(
"1"
,
"2"
,
"3"
,
"4"
,
"5*"
,
"6"
,
"7"
,
"8"
,
"9"
,
"10*"
));
assertEquals
(
oneToNVector
,
Continue
.
generateVectorOneToN
(
n
));
}
}
\ No newline at end of file
src/test/com/paktalin/chess/language/FactorialTest.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
language
;
import
com.paktalin.chess.language.Factorial
;
import
org.junit.jupiter.api.Test
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
...
...
@@ -8,42 +7,42 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class
FactorialTest
{
@Test
void
testWhile
()
{
int
factorial
=
Factorial
.
getW
hileFactorial
(
5
);
void
whileFactorial
()
{
int
factorial
=
Factorial
.
w
hileFactorial
(
5
);
assertEquals
(
getFactorial
(
5
),
factorial
);
factorial
=
Factorial
.
getW
hileFactorial
(
4
);
factorial
=
Factorial
.
w
hileFactorial
(
4
);
assertEquals
(
getFactorial
(
4
),
factorial
);
factorial
=
Factorial
.
getW
hileFactorial
(
3
);
factorial
=
Factorial
.
w
hileFactorial
(
3
);
assertEquals
(
getFactorial
(
3
),
factorial
);
}
@Test
void
testDoWhile
()
{
int
factorial
=
Factorial
.
getD
oWhileFactorial
(
5
);
void
doWhileFactorial
()
{
int
factorial
=
Factorial
.
d
oWhileFactorial
(
5
);
assertEquals
(
getFactorial
(
5
),
factorial
);
factorial
=
Factorial
.
getD
oWhileFactorial
(
4
);
factorial
=
Factorial
.
d
oWhileFactorial
(
4
);
assertEquals
(
getFactorial
(
4
),
factorial
);
factorial
=
Factorial
.
getD
oWhileFactorial
(
3
);
factorial
=
Factorial
.
d
oWhileFactorial
(
3
);
assertEquals
(
getFactorial
(
3
),
factorial
);
}
@Test
void
testFor
()
{
int
factorial
=
Factorial
.
getF
orFactorial
(
5
);
void
forFactorial
()
{
int
factorial
=
Factorial
.
f
orFactorial
(
5
);
assertEquals
(
getFactorial
(
5
),
factorial
);
factorial
=
Factorial
.
getF
orFactorial
(
4
);
factorial
=
Factorial
.
f
orFactorial
(
4
);
assertEquals
(
getFactorial
(
4
),
factorial
);
factorial
=
Factorial
.
getF
orFactorial
(
3
);
factorial
=
Factorial
.
f
orFactorial
(
3
);
assertEquals
(
getFactorial
(
3
),
factorial
);
}
@Test
void
testBreak
()
{
int
factorial
=
Factorial
.
getB
reakFactorial
(
5
);
void
breakFactorial
()
{
int
factorial
=
Factorial
.
b
reakFactorial
(
5
);
assertEquals
(
getFactorial
(
5
),
factorial
);
factorial
=
Factorial
.
getB
reakFactorial
(
4
);
factorial
=
Factorial
.
b
reakFactorial
(
4
);
assertEquals
(
getFactorial
(
4
),
factorial
);
factorial
=
Factorial
.
getB
reakFactorial
(
3
);
factorial
=
Factorial
.
b
reakFactorial
(
3
);
assertEquals
(
getFactorial
(
3
),
factorial
);
}
...
...
src/test/com/paktalin/chess/moves/KingMoveStrategyTest.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
moves
;
import
com.paktalin.chess.Position
;
import
com.paktalin.chess.pieces.King
;
import
com.paktalin.chess.pieces.Piece
;
/**
* Created by Paktalin on 31/05/2018.
...
...
@@ -11,17 +9,17 @@ import com.paktalin.chess.pieces.Piece;
class
KingMoveStrategyTest
extends
MoveStrategyTest
{
@Override
void
putPieceOnBoard
()
{
board
.
placePiece
(
new
King
(
Piece
.
Color
.
Black
),
currentPosition
);
MoveStrategy
moveStrategy
()
{
return
new
KingMoveStrategy
(
);
}
@Override
void
setA
ccessiblePosition
()
{
accessiblePosition
=
Position
.
create
(
"a6"
);
Position
a
ccessiblePosition
()
{
return
Position
.
create
(
"a6"
);
}
@Override
void
setN
otAccessiblePosition
()
{
notAccessiblePosition
=
Position
.
create
(
"d4"
);
Position
n
otAccessiblePosition
()
{
return
Position
.
create
(
"d4"
);
}
}
src/test/com/paktalin/chess/moves/MoveStrategyTest.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
moves
;
import
com.paktalin.chess.Board
;
import
com.paktalin.chess.Position
;
import
com.paktalin.chess.pieces.Piece
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
...
...
@@ -14,33 +12,24 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/
abstract
class
MoveStrategyTest
{
private
Piece
piece
;
Board
board
;
Position
accessiblePosition
,
notAccessiblePosition
;
Position
currentPosition
=
Position
.
create
(
"b5"
);
private
Position
accessiblePosition
,
notAccessiblePosition
,
initialPosition
;
private
MoveStrategy
moveStrategy
;
@BeforeEach
void
init
()
{
board
=
Board
.
createEmpty
();
putPieceOnBoard
();
piece
=
board
.
getPieceAtPosition
(
currentPosition
);
setAccessiblePosition
();
setNotAccessiblePosition
();
initialPosition
=
Position
.
create
(
"b5"
);
moveStrategy
=
moveStrategy
();
accessiblePosition
=
accessiblePosition
();
notAccessiblePosition
=
notAccessiblePosition
();
}
abstract
void
putPieceOnBoard
();
abstract
void
setA
ccessiblePosition
();
abstract
void
setN
otAccessiblePosition
();
abstract
MoveStrategy
moveStrategy
();
abstract
Position
a
ccessiblePosition
();
abstract
Position
n
otAccessiblePosition
();
@Test
void
testMove
()
{
board
.
move
(
piece
,
accessiblePosition
);
assertTrue
(
piece
.
isAtPosition
(
accessiblePosition
));
assertFalse
(
piece
.
isAtPosition
(
currentPosition
));
currentPosition
=
accessiblePosition
;
board
.
move
(
piece
,
notAccessiblePosition
);
assertTrue
(
piece
.
isAtPosition
(
currentPosition
));
assertFalse
(
piece
.
isAtPosition
(
notAccessiblePosition
));
void
isMovable
()
{
assertTrue
(
moveStrategy
.
isMovable
(
initialPosition
,
accessiblePosition
));
assertFalse
(
moveStrategy
.
isMovable
(
initialPosition
,
notAccessiblePosition
));
}
}
src/test/com/paktalin/chess/moves/QueenMoveStrategyTest.java
View file @
90aeec8c
package
com
.
paktalin
.
chess
.
moves
;
import
com.paktalin.chess.Position
;
import
com.paktalin.chess.pieces.Piece
;
import
com.paktalin.chess.pieces.Queen
;
/**
* Created by Paktalin on 31/05/2018.
...
...
@@ -11,17 +9,17 @@ import com.paktalin.chess.pieces.Queen;
class
QueenMoveStrategyTest
extends
MoveStrategyTest
{
@Override
void
putPieceOnBoard
()
{
board
.
placePiece
(
new
Queen
(
Piece
.
Color
.
Black
),
currentPosition
);
MoveStrategy
moveStrategy
()
{
return
new
QueenMoveStrategy
(
);
}
@Override
void
setA
ccessiblePosition
()
{
accessiblePosition
=
Position
.
create
(
"d3"
);
Position
a
ccessiblePosition
()
{
return
Position
.
create
(
"d3"
);
}
@Override
void
setN
otAccessiblePosition
()
{
notAccessiblePosition
=
Position
.
create
(
"f7"
);
Position
n
otAccessiblePosition
()
{
return
Position
.
create
(
"f7"
);
}
}
src/test/com/paktalin/chess/pieces/PieceTest.java
View file @
90aeec8c
...
...
@@ -30,6 +30,8 @@ abstract class PieceTest {
abstract
Piece
createPiece
(
Piece
.
Color
color
);
abstract
void
setExpectedStrength
();
@Test
void
testCreate
()
{
Piece
whitePiece
=
createPiece
(
White
);
...
...
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