Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
AP Lab 7
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Omid Sayfun
AP Lab 7
Commits
41a183a5
Commit
41a183a5
authored
Apr 23, 2019
by
Omid Sayfun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Print and Move Added
parent
39136163
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
289 additions
and
1 deletion
+289
-1
Bishop.java
Bishop.java
+5
-0
Board.java
Board.java
+206
-0
King.java
King.java
+5
-0
Knight.java
Knight.java
+5
-0
Main.java
Main.java
+8
-1
Pawn.java
Pawn.java
+15
-0
Piece.java
Piece.java
+35
-0
Queen.java
Queen.java
+5
-0
Rook.java
Rook.java
+5
-0
No files found.
Bishop.java
View file @
41a183a5
public
class
Bishop
extends
Piece
{
public
Bishop
(
char
x
,
int
y
,
String
name
){
super
(
x
,
y
,
name
);
}
}
Board.java
View file @
41a183a5
import
java.util.*
;
public
class
Board
{
private
ArrayList
<
Piece
>
whitePieces
;
private
ArrayList
<
Piece
>
blackPieces
;
public
Board
(){
this
.
whitePieces
=
new
ArrayList
<
Piece
>();
this
.
blackPieces
=
new
ArrayList
<
Piece
>();
}
public
void
initPieces
(){
// White Players
// Pawns
for
(
char
ch
=
'A'
;
ch
<
'I'
;
ch
++){
Pawn
newPawn
=
new
Pawn
(
ch
,
2
,
"WP"
);
this
.
whitePieces
.
add
(
newPawn
);
}
// Rook
for
(
char
ch
=
'A'
;
ch
<
'I'
;
ch
+=
7
){
Rook
newRook
=
new
Rook
(
ch
,
1
,
"WR"
);
this
.
whitePieces
.
add
(
newRook
);
}
// Knight
for
(
char
ch
=
'B'
;
ch
<
'H'
;
ch
+=
5
){
Knight
newKnight
=
new
Knight
(
ch
,
1
,
"WN"
);
this
.
whitePieces
.
add
(
newKnight
);
}
// Bishop
for
(
char
ch
=
'C'
;
ch
<
'G'
;
ch
+=
3
){
Bishop
newBishop
=
new
Bishop
(
ch
,
1
,
"WB"
);
this
.
whitePieces
.
add
(
newBishop
);
}
// Queen
Queen
whiteQueen
=
new
Queen
(
'E'
,
1
,
"WQ"
);
this
.
whitePieces
.
add
(
whiteQueen
);
// King
King
whiteKing
=
new
King
(
'D'
,
1
,
"WK"
);
this
.
whitePieces
.
add
(
whiteKing
);
// Black Players
// Pawns
for
(
char
ch
=
'A'
;
ch
<
'I'
;
ch
++){
Pawn
newPawn
=
new
Pawn
(
ch
,
7
,
"BP"
);
this
.
blackPieces
.
add
(
newPawn
);
}
// Rook
for
(
char
ch
=
'A'
;
ch
<
'I'
;
ch
+=
7
){
Rook
newRook
=
new
Rook
(
ch
,
8
,
"BR"
);
this
.
blackPieces
.
add
(
newRook
);
}
// Knight
for
(
char
ch
=
'B'
;
ch
<
'H'
;
ch
+=
5
){
Knight
newKnight
=
new
Knight
(
ch
,
8
,
"BN"
);
this
.
blackPieces
.
add
(
newKnight
);
}
// Bishop
for
(
char
ch
=
'C'
;
ch
<
'G'
;
ch
+=
3
){
Bishop
newBishop
=
new
Bishop
(
ch
,
8
,
"BB"
);
this
.
blackPieces
.
add
(
newBishop
);
}
// Queen
Queen
blackQueen
=
new
Queen
(
'E'
,
8
,
"BQ"
);
this
.
blackPieces
.
add
(
blackQueen
);
// King
King
blackKing
=
new
King
(
'D'
,
8
,
"BK"
);
this
.
blackPieces
.
add
(
blackKing
);
}
public
boolean
isTakenByWhite
(
int
y
,
char
x
){
boolean
flag
=
false
;
for
(
Piece
p
:
whitePieces
){
if
(
p
.
getY
()
==
y
&&
p
.
getX
()
==
x
){
flag
=
true
;
break
;
}
}
return
flag
;
}
public
boolean
isTakenByBlack
(
int
y
,
char
x
){
boolean
flag
=
false
;
for
(
Piece
p
:
blackPieces
){
if
(
p
.
getY
()
==
y
&&
p
.
getX
()
==
x
){
flag
=
true
;
break
;
}
}
return
flag
;
}
public
boolean
isTaken
(
int
y
,
char
x
){
boolean
flag
=
false
;
flag
=
isTakenByWhite
(
y
,
x
);
if
(
!
flag
)
flag
=
isTakenByBlack
(
y
,
x
);
return
flag
;
}
public
Piece
takenByWhite
(
int
y
,
char
x
){
Piece
newPiece
=
null
;
for
(
Piece
p
:
whitePieces
){
if
(
p
.
getY
()
==
y
&&
p
.
getX
()
==
x
){
newPiece
=
p
;
break
;
}
}
return
newPiece
;
}
public
Piece
takenByBlack
(
int
y
,
char
x
){
Piece
newPiece
=
null
;
for
(
Piece
p
:
blackPieces
){
if
(
p
.
getY
()
==
y
&&
p
.
getX
()
==
x
){
newPiece
=
p
;
break
;
}
}
return
newPiece
;
}
public
Piece
takenBy
(
int
y
,
char
x
){
Piece
newPiece
=
null
;
newPiece
=
takenByWhite
(
y
,
x
);
if
(
newPiece
==
null
)
newPiece
=
takenByBlack
(
y
,
x
);
return
newPiece
;
}
public
void
printHelp
(){
System
.
out
.
println
(
"***** Board Help *****"
);
System
.
out
.
println
(
"WK: White King \t|\tBK: Black King"
);
...
...
@@ -8,4 +139,79 @@ public class Board{
System
.
out
.
println
(
"WR: White Rook \t|\tBR: Black Rook"
);
System
.
out
.
println
(
"WP: White Pawn \t|\tBP: Black Pawn"
);
}
public
void
printBoard
(){
for
(
int
j
=
9
;
j
>
0
;
j
--){
for
(
char
i
=
'A'
;
i
<
'J'
;
i
++){
if
(
j
==
9
){
if
(
i
-
'A'
>
0
){
System
.
out
.
print
((
char
)(
i
-
1
)
+
" "
);
}
else
{
System
.
out
.
print
(
" "
);
}
}
else
{
if
(
i
==
'A'
){
System
.
out
.
print
(
j
+
" "
);
}
else
{
// Main Clause
char
mapedI
=
(
char
)(
i
-
1
);
if
(
isTaken
(
j
,
mapedI
)
){
System
.
out
.
print
(
takenBy
(
j
,
mapedI
).
getName
()
+
" "
);
}
else
{
System
.
out
.
print
(
"- "
);
}
}
}
}
System
.
out
.
println
();
}
}
public
boolean
move
(
String
from
,
String
to
,
String
color
){
// Name Format: 1D
char
[]
fromArray
=
from
.
toCharArray
();
char
[]
toArray
=
to
.
toCharArray
();
if
(
fromArray
.
length
==
2
&&
toArray
.
length
==
2
){
// Name is consisted of 2 chars
if
(
fromArray
[
0
]
>=
'1'
&&
fromArray
[
0
]
<=
'8'
&&
fromArray
[
1
]
>=
'A'
&&
fromArray
[
1
]
<=
'H'
){
// Check from bound
if
(
toArray
[
0
]
>=
'1'
&&
toArray
[
0
]
<=
'8'
&&
toArray
[
1
]
>=
'A'
&&
toArray
[
1
]
<=
'H'
){
// Check to bound
ArrayList
<
Piece
>
selector
=
null
;
boolean
found
=
false
;
boolean
toBusy
=
false
;
if
(
color
.
equals
(
"W"
)
){
selector
=
this
.
whitePieces
;
found
=
isTakenByWhite
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
toBusy
=
isTakenByWhite
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
}
else
{
selector
=
this
.
blackPieces
;
found
=
isTakenByBlack
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
toBusy
=
isTakenByBlack
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
}
if
(
found
&&
!
toBusy
){
// Found and to is not busy
for
(
Piece
p
:
selector
){
if
(
p
.
getY
()
==
fromArray
[
0
]
-
'0'
&&
p
.
getX
()
==
fromArray
[
1
]
){
p
.
setY
(
toArray
[
0
]
-
'0'
);
p
.
setX
(
toArray
[
1
]);
return
true
;
}
}
return
false
;
}
else
{
return
false
;
}
}
else
{
return
false
;
}
}
else
{
return
false
;
}
}
else
{
return
false
;
}
}
}
King.java
View file @
41a183a5
public
class
King
extends
Piece
{
public
King
(
char
x
,
int
y
,
String
name
){
super
(
x
,
y
,
name
);
}
}
Knight.java
View file @
41a183a5
public
class
Knight
extends
Piece
{
public
Knight
(
char
x
,
int
y
,
String
name
){
super
(
x
,
y
,
name
);
}
}
Main.java
View file @
41a183a5
import
java.util.*
;
public
class
Main
{
public
static
void
main
(
String
[]
args
){
Scanner
sc
=
new
Scanner
(
System
.
in
);
Board
mainBoard
=
new
Board
();
mainBoard
.
printHelp
();
mainBoard
.
initPieces
();
// mainBoard.printHelp();
mainBoard
.
move
(
"8D"
,
"3A"
,
"B"
);
mainBoard
.
printBoard
();
sc
.
close
();
}
}
Pawn.java
0 → 100644
View file @
41a183a5
public
class
Pawn
extends
Piece
{
private
boolean
firstMove
;
public
Pawn
(
char
x
,
int
y
,
String
name
){
super
(
x
,
y
,
name
);
this
.
firstMove
=
true
;
}
public
void
setFirstMove
(
boolean
flag
){
this
.
firstMove
=
flag
;
}
public
boolean
getFirstMove
(){
return
this
.
firstMove
;
}
}
Piece.java
View file @
41a183a5
public
abstract
class
Piece
{
private
int
y
;
private
char
x
;
private
String
name
;
public
Piece
(
char
x
,
int
y
,
String
name
){
setX
(
x
);
setY
(
y
);
setName
(
name
);
}
public
void
setY
(
int
y
){
this
.
y
=
y
;
}
public
void
setX
(
char
x
){
this
.
x
=
x
;
}
public
void
setName
(
String
name
){
this
.
name
=
name
;
}
public
int
getY
(){
return
this
.
y
;
}
public
char
getX
(){
return
this
.
x
;
}
public
String
getName
(){
return
this
.
name
;
}
}
Queen.java
View file @
41a183a5
public
class
Queen
extends
Piece
{
public
Queen
(
char
x
,
int
y
,
String
name
){
super
(
x
,
y
,
name
);
}
}
Rook.java
View file @
41a183a5
public
class
Rook
extends
Piece
{
public
Rook
(
char
x
,
int
y
,
String
name
){
super
(
x
,
y
,
name
);
}
}
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