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
502537f3
authored
May 27, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Separated the classes Board and BoardLayout. Exercise 9 complete
parent
e27c92e5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
134 additions
and
108 deletions
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
app/src/main/java/com/example/paktalin/agilejava_exercises/BoardLayout.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
View file @
502537f3
...
@@ -3,17 +3,13 @@ package com.example.paktalin.agilejava_exercises;
...
@@ -3,17 +3,13 @@ package com.example.paktalin.agilejava_exercises;
import
java.util.List
;
import
java.util.List
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
util
.
StringUtil
.
NEW_LINE
;
/**
/**
* Created by Paktalin on 23/05/2018.
* Created by Paktalin on 23/05/2018.
*/
*/
class
Board
{
class
Board
{
private
static
final
int
ROW_COUNT
=
8
;
private
BoardLayout
layout
;
private
static
final
int
COLUMN_COUNT
=
8
;
private
Piece
[][]
pieces
=
new
Piece
[
ROW_COUNT
][
COLUMN_COUNT
];
private
Side
whiteSide
=
new
Side
();
private
Side
whiteSide
=
new
Side
();
private
Side
blackSide
=
new
Side
();
private
Side
blackSide
=
new
Side
();
...
@@ -21,123 +17,31 @@ class Board {
...
@@ -21,123 +17,31 @@ class Board {
private
Board
()
{}
private
Board
()
{}
static
Board
createEmpty
()
{
static
Board
createEmpty
()
{
return
new
Board
();
}
static
Board
createInitialized
()
{
Board
board
=
new
Board
();
Board
board
=
new
Board
();
board
.
initialize
(
);
board
.
layout
=
BoardLayout
.
create
(
board
);
return
board
;
return
board
;
}
}
private
void
initialize
()
{
static
Board
createInitialized
()
{
initializeKingRank
(
Black
,
0
);
Board
board
=
Board
.
createEmpty
();
initializePawnRank
(
Black
,
1
);
board
.
layout
.
initialize
();
initializePawnRank
(
White
,
6
);
return
board
;
initializeKingRank
(
White
,
7
);
}
private
void
initializeKingRank
(
Piece
.
Color
color
,
int
row
)
{
placePiece
(
Piece
.
createRook
(
color
),
row
,
'a'
);
placePiece
(
Piece
.
createKnight
(
color
),
row
,
'b'
);
placePiece
(
Piece
.
createBishop
(
color
),
row
,
'c'
);
placePiece
(
Piece
.
createQueen
(
color
),
row
,
'd'
);
placePiece
(
Piece
.
createKing
(
color
),
row
,
'e'
);
placePiece
(
Piece
.
createBishop
(
color
),
row
,
'f'
);
placePiece
(
Piece
.
createKnight
(
color
),
row
,
'g'
);
placePiece
(
Piece
.
createRook
(
color
),
row
,
'h'
);
}
private
void
initializePawnRank
(
Piece
.
Color
color
,
int
row
)
{
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
placePiece
(
Piece
.
createPawn
(
color
),
row
,
column
);
}
}
void
placePiece
(
Piece
piece
,
String
position
)
{
void
placePiece
(
Piece
piece
,
String
position
)
{
placePiece
(
piece
,
retrieveRow
(
position
),
retrieveColumn
(
position
)
);
layout
.
placePiece
(
piece
,
position
);
}
}
private
void
placePiece
(
Piece
piece
,
int
row
,
char
file
)
{
Piece
getPieceAtPosition
(
String
position
)
{
placePiece
(
piece
,
row
,
fileToColumn
(
file
)
);
return
layout
.
getPieceAtPosition
(
position
);
}
}
private
void
placePiece
(
Piece
piece
,
int
row
,
int
column
)
{
String
print
()
{
pieces
[
row
][
column
]
=
piece
;
return
layout
.
print
();
setPieceStrength
(
piece
,
column
);
addToCollection
(
piece
);
}
}
void
addToCollection
(
Piece
piece
)
{
void
addToCollection
(
Piece
piece
)
{
getSide
(
piece
.
getColor
()).
addPiece
(
piece
);
getSide
(
piece
.
getColor
()).
addPiece
(
piece
);
}
}
private
Side
getSide
(
Piece
.
Color
color
){
private
void
setPieceStrength
(
Piece
piece
,
int
column
)
{
if
(
piece
.
isType
(
Piece
.
Type
.
Pawn
))
updatePawnsStrength
(
piece
.
getColor
(),
column
);
else
piece
.
setStrength
();
}
private
void
updatePawnsStrength
(
Piece
.
Color
color
,
int
column
)
{
double
strength
;
if
(
pawnsPerColumn
(
color
,
column
)
>
1
)
strength
=
0.5
;
else
strength
=
1.0
;
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
{
Piece
currentPiece
=
getPieceAtPosition
(
row
,
column
);
if
(
currentPiece
!=
null
)
if
(
currentPiece
.
isType
(
Piece
.
Type
.
Pawn
)
&&
currentPiece
.
isColor
(
color
))
currentPiece
.
setStrength
(
strength
);
}
}
private
int
pawnsPerColumn
(
Piece
.
Color
color
,
int
column
)
{
int
count
=
0
;
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
{
Piece
currentPiece
=
getPieceAtPosition
(
row
,
column
);
if
(
currentPiece
!=
null
)
if
(
currentPiece
.
isType
(
Piece
.
Type
.
Pawn
)
&&
currentPiece
.
isColor
(
color
))
count
++;
}
return
count
;
}
private
int
retrieveRow
(
String
position
)
{
return
ROW_COUNT
-
Integer
.
parseInt
(
position
.
split
(
""
)[
1
]);
}
private
int
retrieveColumn
(
String
position
)
{
return
fileToColumn
(
position
.
toCharArray
()[
0
]);
}
String
print
()
{
StringBuilder
buffer
=
new
StringBuilder
();
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
{
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
{
if
(
pieces
[
row
][
column
]
==
null
)
buffer
.
append
(
"."
);
else
buffer
.
append
(
pieces
[
row
][
column
].
getRepresentation
());
}
buffer
.
append
(
" "
+
(
ROW_COUNT
-
row
)
+
NEW_LINE
);
}
buffer
.
append
(
"abcdefgh"
);
return
buffer
.
toString
();
}
private
int
fileToColumn
(
char
file
)
{
char
firstColumnLetter
=
'a'
;
return
(
int
)
file
-
(
int
)
firstColumnLetter
;
}
private
Piece
getPieceAtPosition
(
int
row
,
int
column
)
{
return
pieces
[
row
][
column
];
}
Piece
getPieceAtPosition
(
String
position
)
{
return
getPieceAtPosition
(
retrieveRow
(
position
),
retrieveColumn
(
position
));
}
Side
getSide
(
Piece
.
Color
color
){
if
(
color
==
White
)
if
(
color
==
White
)
return
whiteSide
;
return
whiteSide
;
return
blackSide
;
return
blackSide
;
...
...
app/src/main/java/com/example/paktalin/agilejava_exercises/BoardLayout.java
0 → 100644
View file @
502537f3
package
com
.
example
.
paktalin
.
agilejava_exercises
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.
Black
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.
White
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
util
.
StringUtil
.
NEW_LINE
;
/**
* Created by Paktalin on 27/05/2018.
*/
class
BoardLayout
{
private
static
final
int
ROW_COUNT
=
8
;
private
static
final
int
COLUMN_COUNT
=
8
;
private
Piece
[][]
pieces
=
new
Piece
[
ROW_COUNT
][
COLUMN_COUNT
];
private
Board
board
;
private
BoardLayout
()
{}
void
initialize
()
{
initializeKingRank
(
Black
,
0
);
initializePawnRank
(
Black
,
1
);
initializePawnRank
(
White
,
6
);
initializeKingRank
(
White
,
7
);
}
static
BoardLayout
create
(
Board
board
)
{
BoardLayout
layout
=
new
BoardLayout
();
layout
.
board
=
board
;
return
layout
;
}
private
void
initializeKingRank
(
Piece
.
Color
color
,
int
row
)
{
placePiece
(
Piece
.
createRook
(
color
),
row
,
0
);
placePiece
(
Piece
.
createKnight
(
color
),
row
,
1
);
placePiece
(
Piece
.
createBishop
(
color
),
row
,
2
);
placePiece
(
Piece
.
createQueen
(
color
),
row
,
3
);
placePiece
(
Piece
.
createKing
(
color
),
row
,
4
);
placePiece
(
Piece
.
createBishop
(
color
),
row
,
5
);
placePiece
(
Piece
.
createKnight
(
color
),
row
,
6
);
placePiece
(
Piece
.
createRook
(
color
),
row
,
7
);
}
private
void
initializePawnRank
(
Piece
.
Color
color
,
int
row
)
{
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
placePiece
(
Piece
.
createPawn
(
color
),
row
,
column
);
}
void
placePiece
(
Piece
piece
,
String
position
)
{
placePiece
(
piece
,
retrieveRow
(
position
),
retrieveColumn
(
position
));
}
private
void
placePiece
(
Piece
piece
,
int
row
,
int
column
)
{
pieces
[
row
][
column
]
=
piece
;
setPieceStrength
(
piece
,
column
);
board
.
addToCollection
(
piece
);
}
private
void
setPieceStrength
(
Piece
piece
,
int
column
)
{
if
(
piece
.
isType
(
Piece
.
Type
.
Pawn
))
updatePawnsStrength
(
piece
.
getColor
(),
column
);
else
piece
.
setStrength
();
}
private
void
updatePawnsStrength
(
Piece
.
Color
color
,
int
column
)
{
double
strength
;
if
(
pawnsPerColumn
(
color
,
column
)
>
1
)
strength
=
0.5
;
else
strength
=
1.0
;
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
{
Piece
currentPiece
=
getPieceAtPosition
(
row
,
column
);
if
(
currentPiece
!=
null
)
if
(
currentPiece
.
isType
(
Piece
.
Type
.
Pawn
)
&&
currentPiece
.
isColor
(
color
))
currentPiece
.
setStrength
(
strength
);
}
}
private
int
pawnsPerColumn
(
Piece
.
Color
color
,
int
column
)
{
int
count
=
0
;
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
{
Piece
currentPiece
=
getPieceAtPosition
(
row
,
column
);
if
(
currentPiece
!=
null
)
if
(
currentPiece
.
isType
(
Piece
.
Type
.
Pawn
)
&&
currentPiece
.
isColor
(
color
))
count
++;
}
return
count
;
}
private
Piece
getPieceAtPosition
(
int
row
,
int
column
)
{
return
pieces
[
row
][
column
];
}
Piece
getPieceAtPosition
(
String
position
)
{
return
getPieceAtPosition
(
retrieveRow
(
position
),
retrieveColumn
(
position
));
}
String
print
()
{
StringBuilder
buffer
=
new
StringBuilder
();
for
(
int
row
=
0
;
row
<
ROW_COUNT
;
row
++)
{
for
(
int
column
=
0
;
column
<
COLUMN_COUNT
;
column
++)
{
if
(
pieces
[
row
][
column
]
==
null
)
buffer
.
append
(
"."
);
else
buffer
.
append
(
pieces
[
row
][
column
].
getRepresentation
());
}
buffer
.
append
(
" "
+
(
ROW_COUNT
-
row
)
+
NEW_LINE
);
}
buffer
.
append
(
"abcdefgh"
);
return
buffer
.
toString
();
}
private
int
retrieveRow
(
String
position
)
{
return
ROW_COUNT
-
Integer
.
parseInt
(
position
.
split
(
""
)[
1
]);
}
private
int
retrieveColumn
(
String
position
)
{
char
file
=
position
.
toCharArray
()[
0
];
char
firstColumnLetter
=
'a'
;
return
(
int
)
file
-
(
int
)
firstColumnLetter
;
}
}
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