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
e73c7a32
authored
May 25, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lesson 5 exercise 6 done + refactoring
parent
1642774c
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
45 deletions
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
app/src/main/java/com/example/paktalin/agilejava_exercises/util/StringUtil.java
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
app/src/test/java/com/example/paktalin/agilejava_exercises/PieceTest.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
View file @
e73c7a32
...
...
@@ -10,7 +10,7 @@ class Board {
static
final
int
ROW_COUNT
=
8
;
static
final
int
COLUMN_COUNT
=
8
;
private
Piece
[][]
positionState
=
new
Piece
[
ROW_COUNT
][
COLUMN_COUNT
];
private
Piece
[][]
board
=
new
Piece
[
ROW_COUNT
][
COLUMN_COUNT
];
void
initialize
()
{
initializeKingRank
(
Piece
.
Color
.
Black
,
0
);
...
...
@@ -20,34 +20,38 @@ class Board {
}
private
void
initializeKingRank
(
Piece
.
Color
color
,
int
rank
)
{
setPosition
(
Piece
.
createRook
(
color
),
rank
,
'a'
);
setPosition
(
Piece
.
createKnight
(
color
),
rank
,
'b'
);
setPosition
(
Piece
.
createBishop
(
color
),
rank
,
'c'
);
setPosition
(
Piece
.
createQueen
(
color
),
rank
,
'd'
);
setPosition
(
Piece
.
createKing
(
color
),
rank
,
'e'
);
setPosition
(
Piece
.
createBishop
(
color
),
rank
,
'f'
);
setPosition
(
Piece
.
createKnight
(
color
),
rank
,
'g'
);
setPosition
(
Piece
.
createRook
(
color
),
rank
,
'h'
);
placePiece
(
Piece
.
createRook
(
color
),
rank
,
'a'
);
placePiece
(
Piece
.
createKnight
(
color
),
rank
,
'b'
);
placePiece
(
Piece
.
createBishop
(
color
),
rank
,
'c'
);
placePiece
(
Piece
.
createQueen
(
color
),
rank
,
'd'
);
placePiece
(
Piece
.
createKing
(
color
),
rank
,
'e'
);
placePiece
(
Piece
.
createBishop
(
color
),
rank
,
'f'
);
placePiece
(
Piece
.
createKnight
(
color
),
rank
,
'g'
);
placePiece
(
Piece
.
createRook
(
color
),
rank
,
'h'
);
}
private
void
initializePawnRank
(
Piece
.
Color
color
,
int
rank
)
{
for
(
int
i
=
0
;
i
<
COLUMN_COUNT
;
i
++)
setPosition
(
Piece
.
createPawn
(
color
),
rank
,
i
);
placePiece
(
Piece
.
createPawn
(
color
),
rank
,
i
);
}
private
void
setPosition
(
Piece
piece
,
int
rank
,
char
file
)
{
setPosition
(
piece
,
rank
,
fileToColumn
(
file
));
private
void
placePiece
(
Piece
piece
,
int
row
,
char
file
)
{
placePiece
(
piece
,
row
,
fileToColumn
(
file
));
}
private
void
setPosition
(
Piece
piece
,
int
rank
,
int
column
)
{
positionState
[
rank
][
column
]
=
piece
;
private
void
placePiece
(
Piece
piece
,
int
row
,
int
column
)
{
board
[
row
][
column
]
=
piece
;
}
void
placePiece
(
Piece
piece
,
String
position
)
{
placePiece
(
piece
,
retrieveRow
(
position
),
retrieveColumn
(
position
));
}
int
pieceCount
()
{
int
count
=
0
;
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
if
(
positionState
[
i
][
j
]
!=
null
)
if
(
board
[
i
][
j
]
!=
null
)
count
++;
return
count
;
...
...
@@ -57,8 +61,8 @@ class Board {
int
counter
=
0
;
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
if
(
positionState
[
i
][
j
]
!=
null
)
if
(
positionState
[
i
][
j
].
getColor
()
==
color
)
if
(
board
[
i
][
j
]
!=
null
)
if
(
board
[
i
][
j
].
getColor
()
==
color
)
counter
++;
}
return
counter
;
...
...
@@ -68,8 +72,8 @@ class Board {
int
count
=
0
;
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
{
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
if
(
positionState
[
i
][
j
]
!=
null
)
if
(
positionState
[
i
][
j
].
isType
(
type
)
&&
positionState
[
i
][
j
].
getColor
()
==
color
)
if
(
board
[
i
][
j
]
!=
null
)
if
(
board
[
i
][
j
].
isType
(
type
)
&&
board
[
i
][
j
].
getColor
()
==
color
)
count
++;
}
}
...
...
@@ -80,10 +84,10 @@ class Board {
StringBuilder
buffer
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
ROW_COUNT
;
i
++)
{
for
(
int
j
=
0
;
j
<
COLUMN_COUNT
;
j
++)
{
if
(
positionState
[
i
][
j
]
==
null
)
if
(
board
[
i
][
j
]
==
null
)
buffer
.
append
(
"."
);
else
buffer
.
append
(
positionState
[
i
][
j
].
getRepresentation
());
buffer
.
append
(
board
[
i
][
j
].
getRepresentation
());
}
buffer
.
append
(
NEW_LINE
);
}
...
...
@@ -95,12 +99,18 @@ class Board {
return
(
int
)
file
-
(
int
)
firstColumnLetter
;
}
Piece
getPieceAtLocation
(
String
loca
tion
)
{
int
row
=
Integer
.
parseInt
(
loca
tion
.
split
(
""
)[
1
])
-
1
;
int
column
=
fileToColumn
(
location
.
toCharArray
()[
0
]);
private
int
retrieveRow
(
String
posi
tion
)
{
return
Integer
.
parseInt
(
posi
tion
.
split
(
""
)[
1
])
-
1
;
}
return
positionState
[
row
][
column
];
private
int
retrieveColumn
(
String
position
)
{
return
fileToColumn
(
position
.
toCharArray
()[
0
]);
}
Piece
getPieceAtPosition
(
String
position
)
{
int
row
=
retrieveRow
(
position
);
int
column
=
retrieveColumn
(
position
);
return
board
[
row
][
column
];
}
}
\ No newline at end of file
app/src/main/java/com/example/paktalin/agilejava_exercises/util/StringUtil.java
View file @
e73c7a32
...
...
@@ -13,4 +13,6 @@ public class StringUtil {
public
static
String
appendNewLine
(
String
string
)
{
return
string
+
NEW_LINE
;
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
View file @
e73c7a32
...
...
@@ -4,6 +4,9 @@ import com.example.paktalin.agilejava_exercises.util.StringUtil;
import
junit.framework.TestCase
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Type
.*;
/**
* Created by Paktalin on 23/05/2018.
*/
...
...
@@ -28,31 +31,40 @@ public class BoardTest extends TestCase {
StringUtil
.
appendNewLine
(
"rnbqkbnr"
),
board
.
print
());
assertEquals
(
16
,
board
.
pieceCount
(
Piece
.
Color
.
Black
));
assertEquals
(
16
,
board
.
pieceCount
(
Piece
.
Color
.
White
));
assertEquals
(
16
,
board
.
pieceCount
(
Black
));
assertEquals
(
16
,
board
.
pieceCount
(
White
));
}
public
void
testCountPieces
()
{
board
.
initialize
();
assertEquals
(
8
,
board
.
pieceCount
(
P
iece
.
Type
.
Pawn
,
Piece
.
Color
.
White
));
assertEquals
(
1
,
board
.
pieceCount
(
Piece
.
Type
.
King
,
Piece
.
Color
.
Black
));
assertEquals
(
1
,
board
.
pieceCount
(
Piece
.
Type
.
Queen
,
Piece
.
Color
.
White
));
assertEquals
(
2
,
board
.
pieceCount
(
Piece
.
Type
.
Bishop
,
Piece
.
Color
.
Black
));
assertEquals
(
2
,
board
.
pieceCount
(
Piece
.
Type
.
Knight
,
Piece
.
Color
.
White
));
assertEquals
(
2
,
board
.
pieceCount
(
Piece
.
Type
.
Rook
,
Piece
.
Color
.
Black
));
assertEquals
(
8
,
board
.
pieceCount
(
P
awn
,
White
));
assertEquals
(
1
,
board
.
pieceCount
(
King
,
Black
));
assertEquals
(
1
,
board
.
pieceCount
(
Queen
,
White
));
assertEquals
(
2
,
board
.
pieceCount
(
Bishop
,
Black
));
assertEquals
(
2
,
board
.
pieceCount
(
Knight
,
White
));
assertEquals
(
2
,
board
.
pieceCount
(
Rook
,
Black
));
}
public
void
testPieceLocation
()
{
board
.
initialize
();
Piece
piece
=
board
.
getPieceAtLocation
(
"a8"
);
Piece
piece
=
board
.
getPieceAtPosition
(
"a8"
);
assertEquals
(
Rook
,
piece
.
getType
());
assertEquals
(
White
,
piece
.
getColor
());
assertEquals
(
Piece
.
Type
.
Rook
,
piece
.
getType
());
assertEquals
(
Piece
.
Color
.
White
,
piece
.
getColor
());
piece
=
board
.
getPieceAtPosition
(
"e1"
);
assertEquals
(
King
,
piece
.
getType
());
assertEquals
(
Black
,
piece
.
getColor
());
}
public
void
testPlacePieces
()
{
board
.
initialize
();
piece
=
board
.
getPieceAtLocation
(
"e1"
);
assertEquals
(
Piece
.
Type
.
King
,
piece
.
getType
());
assertEquals
(
Piece
.
Color
.
Black
,
piece
.
getColor
());
board
.
placePiece
(
Piece
.
createBishop
(
Black
),
"a8"
);
Piece
bishop
=
board
.
getPieceAtPosition
(
"a8"
);
assertEquals
(
Bishop
,
bishop
.
getType
());
assertEquals
(
Black
,
bishop
.
getColor
());
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/PieceTest.java
View file @
e73c7a32
...
...
@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava_exercises;
import
junit.framework.TestCase
;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Type
.*;
import
static
com
.
example
.
paktalin
.
agilejava_exercises
.
Piece
.
Color
.*;
/**
* Created by Paktalin on 23/05/2018.
*/
...
...
@@ -9,12 +12,12 @@ import junit.framework.TestCase;
public
class
PieceTest
extends
TestCase
{
public
void
testCreate
()
{
verifyCreation
(
Piece
.
createPawn
(
Piece
.
Color
.
White
),
Piece
.
createPawn
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Pawn
,
'p'
);
verifyCreation
(
Piece
.
createKing
(
Piece
.
Color
.
White
),
Piece
.
createKing
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
King
,
'k'
);
verifyCreation
(
Piece
.
createBishop
(
Piece
.
Color
.
White
),
Piece
.
createBishop
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Bishop
,
'b'
);
verifyCreation
(
Piece
.
createRook
(
Piece
.
Color
.
White
),
Piece
.
createRook
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Rook
,
'r'
);
verifyCreation
(
Piece
.
createKnight
(
Piece
.
Color
.
White
),
Piece
.
createKnight
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Knight
,
'n'
);
verifyCreation
(
Piece
.
createQueen
(
Piece
.
Color
.
White
),
Piece
.
createQueen
(
Piece
.
Color
.
Black
),
Piece
.
Type
.
Queen
,
'q'
);
verifyCreation
(
Piece
.
createPawn
(
White
),
Piece
.
createPawn
(
Black
),
Pawn
,
'p'
);
verifyCreation
(
Piece
.
createKing
(
White
),
Piece
.
createKing
(
Black
),
King
,
'k'
);
verifyCreation
(
Piece
.
createBishop
(
White
),
Piece
.
createBishop
(
Black
),
Bishop
,
'b'
);
verifyCreation
(
Piece
.
createRook
(
White
),
Piece
.
createRook
(
Black
),
Rook
,
'r'
);
verifyCreation
(
Piece
.
createKnight
(
White
),
Piece
.
createKnight
(
Black
),
Knight
,
'n'
);
verifyCreation
(
Piece
.
createQueen
(
White
),
Piece
.
createQueen
(
Black
),
Queen
,
'q'
);
}
private
void
verifyCreation
(
Piece
whitePiece
,
Piece
blackPiece
,
Piece
.
Type
type
,
char
representation
)
{
...
...
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