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
a00c86ad
authored
May 24, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lesson 5 in progress
parent
445de99b
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
124 additions
and
71 deletions
app/proguard-rules.pro
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
app/src/test/java/com/example/paktalin/agilejava_exercises/PieceTest.java
app/proguard-rules.pro
View file @
a00c86ad
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
# http://developer.android.com/guide/developing/tools/proguard.html
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class
nam
e to the JavaScript interface
# and specify the fully qualified class
typ
e to the JavaScript interface
# class:
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
# public *;
...
@@ -17,5 +17,5 @@
...
@@ -17,5 +17,5 @@
#-keepattributes SourceFile,LineNumberTable
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# If you keep the line number information, uncomment this to
# hide the original source file
nam
e.
# hide the original source file
typ
e.
#-renamesourcefileattribute SourceFile
#-renamesourcefileattribute SourceFile
app/src/main/java/com/example/paktalin/agilejava_exercises/Board.java
View file @
a00c86ad
...
@@ -15,32 +15,32 @@ class Board {
...
@@ -15,32 +15,32 @@ class Board {
void
initialize
()
{
void
initialize
()
{
fillFirstRank
();
fillFirstRank
();
for
(
int
i
=
0
;
i
<
8
;
i
++)
for
(
int
i
=
0
;
i
<
8
;
i
++)
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Nam
e
.
Pawn
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Typ
e
.
Pawn
));
for
(
int
i
=
0
;
i
<
8
;
i
++)
for
(
int
i
=
0
;
i
<
8
;
i
++)
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Nam
e
.
Pawn
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Typ
e
.
Pawn
));
fillEightRank
();
fillEightRank
();
}
}
private
void
fillFirstRank
()
{
private
void
fillFirstRank
()
{
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Nam
e
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Typ
e
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Nam
e
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Typ
e
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Nam
e
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Typ
e
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Nam
e
.
Queen
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Typ
e
.
Queen
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Nam
e
.
King
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Typ
e
.
King
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Nam
e
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Typ
e
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Nam
e
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Typ
e
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Nam
e
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Typ
e
.
Rook
));
}
}
private
void
fillEightRank
()
{
private
void
fillEightRank
()
{
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Nam
e
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Typ
e
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Nam
e
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Typ
e
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Nam
e
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Typ
e
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Nam
e
.
Queen
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Typ
e
.
Queen
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Nam
e
.
King
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Typ
e
.
King
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Nam
e
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Typ
e
.
Bishop
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Nam
e
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Typ
e
.
Knight
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Nam
e
.
Rook
));
pieces
.
add
(
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Typ
e
.
Rook
));
}
}
int
pieceCount
()
{
int
pieceCount
()
{
...
@@ -58,7 +58,7 @@ class Board {
...
@@ -58,7 +58,7 @@ class Board {
String
printRank
(
int
from
)
{
String
printRank
(
int
from
)
{
StringBuilder
buffer
=
new
StringBuilder
();
StringBuilder
buffer
=
new
StringBuilder
();
for
(
int
i
=
from
;
i
<
from
+
8
;
i
++)
for
(
int
i
=
from
;
i
<
from
+
8
;
i
++)
buffer
.
append
(
pieces
.
get
(
i
).
print
());
buffer
.
append
(
pieces
.
get
(
i
).
getRepresentation
());
return
buffer
.
toString
();
return
buffer
.
toString
();
}
}
...
@@ -76,4 +76,12 @@ class Board {
...
@@ -76,4 +76,12 @@ class Board {
return
buffer
.
toString
();
return
buffer
.
toString
();
}
}
int
getCount
(
Piece
.
Type
type
,
Piece
.
Color
color
)
{
int
count
=
0
;
for
(
Piece
piece
:
pieces
)
if
(
piece
.
getType
()
==
type
&&
piece
.
getColor
()
==
color
)
count
++;
return
count
;
}
}
}
\ No newline at end of file
app/src/main/java/com/example/paktalin/agilejava_exercises/Piece.java
View file @
a00c86ad
...
@@ -5,53 +5,45 @@ package com.example.paktalin.agilejava_exercises;
...
@@ -5,53 +5,45 @@ package com.example.paktalin.agilejava_exercises;
*/
*/
public
class
Piece
{
public
class
Piece
{
enum
Nam
e
{
Pawn
,
Knight
,
Rook
,
Bishop
,
Queen
,
King
}
enum
Typ
e
{
Pawn
,
Knight
,
Rook
,
Bishop
,
Queen
,
King
}
enum
Color
{
White
,
Black
}
enum
Color
{
White
,
Black
}
private
Name
nam
e
;
private
Type
typ
e
;
private
Color
color
;
private
Color
color
;
private
Piece
()
{}
private
Piece
()
{}
Piece
(
final
Color
color
,
final
Name
nam
e
)
{
Piece
(
final
Color
color
,
final
Type
typ
e
)
{
this
.
color
=
color
;
this
.
color
=
color
;
this
.
name
=
nam
e
;
this
.
type
=
typ
e
;
}
}
public
Color
getColor
()
{
public
Color
getColor
()
{
return
color
;
return
color
;
}
}
public
Name
getName
()
{
public
Type
getType
()
{
return
name
;
return
type
;
}
}
String
print
()
{
char
getRepresentation
()
{
String
letter
=
""
;
char
representation
=
0
;
switch
(
name
)
{
if
(
type
==
Type
.
King
)
case
King:
representation
=
'k'
;
letter
=
"k"
;
if
(
type
==
Type
.
Queen
)
break
;
representation
=
'q'
;
case
Queen:
if
(
type
==
Type
.
Pawn
)
letter
=
"q"
;
representation
=
'p'
;
break
;
if
(
type
==
Type
.
Rook
)
case
Pawn:
representation
=
'r'
;
letter
=
"p"
;
if
(
type
==
Type
.
Bishop
)
break
;
representation
=
'b'
;
case
Rook:
if
(
type
==
Type
.
Knight
)
letter
=
"r"
;
representation
=
'n'
;
break
;
case
Bishop:
if
(
this
.
isBlack
())
letter
=
"b"
;
return
Character
.
toUpperCase
(
representation
);
break
;
return
representation
;
case
Knight:
letter
=
"n"
;
break
;
}
if
(
color
==
Color
.
Black
)
return
letter
.
toUpperCase
();
return
letter
;
}
}
boolean
isWhite
()
{
boolean
isWhite
()
{
...
@@ -61,4 +53,46 @@ public class Piece {
...
@@ -61,4 +53,46 @@ public class Piece {
boolean
isBlack
()
{
boolean
isBlack
()
{
return
this
.
color
==
Color
.
Black
;
return
this
.
color
==
Color
.
Black
;
}
}
static
Piece
createWhitePawn
()
{
return
new
Piece
(
Color
.
White
,
Type
.
Pawn
);
}
static
Piece
createBlackPawn
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
Pawn
);
}
static
Piece
createWhiteKing
()
{
return
new
Piece
(
Color
.
White
,
Type
.
King
);
}
static
Piece
createBlackKing
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
King
);
}
static
Piece
createWhiteBishop
()
{
return
new
Piece
(
Color
.
White
,
Type
.
Bishop
);
}
static
Piece
createBlackBishop
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
Bishop
);
}
static
Piece
createWhiteRook
()
{
return
new
Piece
(
Color
.
White
,
Type
.
Rook
);
}
static
Piece
createBlackRook
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
Rook
);
}
static
Piece
createWhiteKnight
()
{
return
new
Piece
(
Color
.
White
,
Type
.
Knight
);
}
static
Piece
createBlackKnight
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
Knight
);
}
static
Piece
createWhiteQueen
()
{
return
new
Piece
(
Color
.
White
,
Type
.
Queen
);
}
static
Piece
createBlackQueen
()
{
return
new
Piece
(
Color
.
Black
,
Type
.
Queen
);
}
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/BoardTest.java
View file @
a00c86ad
...
@@ -31,4 +31,15 @@ public class BoardTest extends TestCase {
...
@@ -31,4 +31,15 @@ public class BoardTest extends TestCase {
assertEquals
(
16
,
board
.
colorCount
(
Piece
.
Color
.
Black
));
assertEquals
(
16
,
board
.
colorCount
(
Piece
.
Color
.
Black
));
assertEquals
(
16
,
board
.
colorCount
(
Piece
.
Color
.
White
));
assertEquals
(
16
,
board
.
colorCount
(
Piece
.
Color
.
White
));
}
}
public
void
testCountPieces
()
{
board
.
initialize
();
assertEquals
(
8
,
board
.
getCount
(
Piece
.
Type
.
Pawn
,
Piece
.
Color
.
White
));
assertEquals
(
1
,
board
.
getCount
(
Piece
.
Type
.
King
,
Piece
.
Color
.
Black
));
assertEquals
(
1
,
board
.
getCount
(
Piece
.
Type
.
Queen
,
Piece
.
Color
.
White
));
assertEquals
(
2
,
board
.
getCount
(
Piece
.
Type
.
Bishop
,
Piece
.
Color
.
Black
));
assertEquals
(
2
,
board
.
getCount
(
Piece
.
Type
.
Knight
,
Piece
.
Color
.
White
));
assertEquals
(
2
,
board
.
getCount
(
Piece
.
Type
.
Rook
,
Piece
.
Color
.
Black
));
}
}
}
app/src/test/java/com/example/paktalin/agilejava_exercises/PieceTest.java
View file @
a00c86ad
...
@@ -8,23 +8,22 @@ import junit.framework.TestCase;
...
@@ -8,23 +8,22 @@ import junit.framework.TestCase;
public
class
PieceTest
extends
TestCase
{
public
class
PieceTest
extends
TestCase
{
private
Piece
blackPawn
,
whitePawn
;
public
void
testCreate
()
{
verifyCreation
(
Piece
.
createWhitePawn
(),
Piece
.
createBlackPawn
(),
Piece
.
Type
.
Pawn
,
'p'
);
public
void
setUp
()
{
verifyCreation
(
Piece
.
createWhiteKing
(),
Piece
.
createBlackKing
(),
Piece
.
Type
.
King
,
'k'
);
blackPawn
=
new
Piece
(
Piece
.
Color
.
Black
,
Piece
.
Name
.
Pawn
);
verifyCreation
(
Piece
.
createWhiteBishop
(),
Piece
.
createBlackBishop
(),
Piece
.
Type
.
Bishop
,
'b'
);
whitePawn
=
new
Piece
(
Piece
.
Color
.
White
,
Piece
.
Name
.
Pawn
);
verifyCreation
(
Piece
.
createWhiteRook
(),
Piece
.
createBlackRook
(),
Piece
.
Type
.
Rook
,
'r'
);
verifyCreation
(
Piece
.
createWhiteKnight
(),
Piece
.
createBlackKnight
(),
Piece
.
Type
.
Knight
,
'n'
);
verifyCreation
(
Piece
.
createWhiteQueen
(),
Piece
.
createBlackQueen
(),
Piece
.
Type
.
Queen
,
'q'
);
}
}
public
void
testPawnColor
()
{
private
void
verifyCreation
(
Piece
whitePiece
,
Piece
blackPiece
,
Piece
.
Type
type
,
char
representation
)
{
assertEquals
(
Piece
.
Color
.
White
,
whitePawn
.
getColor
());
assertTrue
(
whitePiece
.
isWhite
());
assertEquals
(
Piece
.
Color
.
Black
,
blackPawn
.
getColor
());
assertEquals
(
type
,
whitePiece
.
getType
());
assertEquals
(
representation
,
whitePiece
.
getRepresentation
());
assertTrue
(
whitePawn
.
isWhite
());
assertFalse
(
blackPawn
.
isWhite
());
}
public
void
testPawnRepresentation
()
{
assertTrue
(
blackPiece
.
isBlack
());
assertEquals
(
"P"
,
blackPawn
.
print
());
assertEquals
(
type
,
blackPiece
.
getType
());
assertEquals
(
"p"
,
whitePawn
.
print
());
assertEquals
(
Character
.
toUpperCase
(
representation
),
blackPiece
.
getRepresentation
());
}
}
}
}
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