Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
chess
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
9611046
chess
Commits
37957f2d
Commit
37957f2d
authored
May 17, 2019
by
9611046
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
made isCheck() method in King.java
parent
6e2c73e4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
176 additions
and
3 deletions
+176
-3
King.java
src/pieces/King.java
+176
-3
No files found.
src/pieces/King.java
View file @
37957f2d
...
@@ -30,17 +30,190 @@ public class King extends Piece {
...
@@ -30,17 +30,190 @@ public class King extends Piece {
{
{
return
y
;
return
y
;
}
}
public
ArrayList
<
Square
>
move
(
Square
state
[][]
,
int
x
,
int
y
){
public
ArrayList
<
Square
>
move
(
Square
[][]
squares
,
int
x
,
int
y
){
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
possibleSquares
.
clear
();
possibleSquares
.
clear
();
int
posx
[]={
x
,
x
,
x
+
1
,
x
+
1
,
x
+
1
,
x
-
1
,
x
-
1
,
x
-
1
};
int
posx
[]={
x
,
x
,
x
+
1
,
x
+
1
,
x
+
1
,
x
-
1
,
x
-
1
,
x
-
1
};
int
posy
[]={
y
-
1
,
y
+
1
,
y
-
1
,
y
,
y
+
1
,
y
-
1
,
y
,
y
+
1
};
int
posy
[]={
y
-
1
,
y
+
1
,
y
-
1
,
y
,
y
+
1
,
y
-
1
,
y
,
y
+
1
};
for
(
int
i
=
0
;
i
<
8
;
i
++)
for
(
int
i
=
0
;
i
<
8
;
i
++)
if
((
posx
[
i
]>=
0
&&
posx
[
i
]<
8
&&
posy
[
i
]>=
0
&&
posy
[
i
]<
8
))
if
((
posx
[
i
]>=
0
&&
posx
[
i
]<
8
&&
posy
[
i
]>=
0
&&
posy
[
i
]<
8
))
if
((
s
tate
[
posx
[
i
]][
posy
[
i
]].
getPiece
()==
null
||
!
state
[
posx
[
i
]][
posy
[
i
]].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
())))
if
((
s
quares
[
posx
[
i
]][
posy
[
i
]].
getPiece
()==
null
||
!
squares
[
posx
[
i
]][
posy
[
i
]].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
())))
possibleSquares
.
add
(
s
tate
[
posx
[
i
]][
posy
[
i
]]);
possibleSquares
.
add
(
s
quares
[
posx
[
i
]][
posy
[
i
]]);
return
possibleSquares
;
return
possibleSquares
;
}
}
public
boolean
isChecked
(
Square
[][]
squares
){
//Checking for attack from left,right,up and down
for
(
int
i
=
x
+
1
;
i
<
8
;
i
++)
{
if
(
squares
[
i
][
y
].
getPiece
()==
null
)
continue
;
else
if
(
squares
[
i
][
y
].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
()))
break
;
else
{
if
((
squares
[
i
][
y
].
getPiece
()
instanceof
Rook
)
||
(
squares
[
i
][
y
].
getPiece
()
instanceof
Queen
))
return
true
;
else
break
;
}
}
for
(
int
i
=
x
-
1
;
i
>=
0
;
i
--)
{
if
(
squares
[
i
][
y
].
getPiece
()==
null
)
continue
;
else
if
(
squares
[
i
][
y
].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
()))
break
;
else
{
if
((
squares
[
i
][
y
].
getPiece
()
instanceof
Rook
)
||
(
squares
[
i
][
y
].
getPiece
()
instanceof
Queen
))
return
true
;
else
break
;
}
}
for
(
int
i
=
y
+
1
;
i
<
8
;
i
++)
{
if
(
squares
[
x
][
i
].
getPiece
()==
null
)
continue
;
else
if
(
squares
[
x
][
i
].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
()))
break
;
else
{
if
((
squares
[
x
][
i
].
getPiece
()
instanceof
Rook
)
||
(
squares
[
x
][
i
].
getPiece
()
instanceof
Queen
))
return
true
;
else
break
;
}
}
for
(
int
i
=
y
-
1
;
i
>=
0
;
i
--)
{
if
(
squares
[
x
][
i
].
getPiece
()==
null
)
continue
;
else
if
(
squares
[
x
][
i
].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
()))
break
;
else
{
if
((
squares
[
x
][
i
].
getPiece
()
instanceof
Rook
)
||
(
squares
[
x
][
i
].
getPiece
()
instanceof
Queen
))
return
true
;
else
break
;
}
}
//checking for attack from diagonal direction
int
tempx
=
x
+
1
,
tempy
=
y
-
1
;
while
(
tempx
<
8
&&
tempy
>=
0
)
{
if
(
squares
[
tempx
][
tempy
].
getPiece
()==
null
)
{
tempx
++;
tempy
--;
}
else
if
(
squares
[
tempx
][
tempy
].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
()))
break
;
else
{
if
(
squares
[
tempx
][
tempy
].
getPiece
()
instanceof
Bishop
||
squares
[
tempx
][
tempy
].
getPiece
()
instanceof
Queen
)
return
true
;
else
break
;
}
}
tempx
=
x
-
1
;
tempy
=
y
+
1
;
while
(
tempx
>=
0
&&
tempy
<
8
)
{
if
(
squares
[
tempx
][
tempy
].
getPiece
()==
null
)
{
tempx
--;
tempy
++;
}
else
if
(
squares
[
tempx
][
tempy
].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
()))
break
;
else
{
if
(
squares
[
tempx
][
tempy
].
getPiece
()
instanceof
Bishop
||
squares
[
tempx
][
tempy
].
getPiece
()
instanceof
Queen
)
return
true
;
else
break
;
}
}
tempx
=
x
-
1
;
tempy
=
y
-
1
;
while
(
tempx
>=
0
&&
tempy
>=
0
)
{
if
(
squares
[
tempx
][
tempy
].
getPiece
()==
null
)
{
tempx
--;
tempy
--;
}
else
if
(
squares
[
tempx
][
tempy
].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
()))
break
;
else
{
if
(
squares
[
tempx
][
tempy
].
getPiece
()
instanceof
Bishop
||
squares
[
tempx
][
tempy
].
getPiece
()
instanceof
Queen
)
return
true
;
else
break
;
}
}
tempx
=
x
+
1
;
tempy
=
y
+
1
;
while
(
tempx
<
8
&&
tempy
<
8
)
{
if
(
squares
[
tempx
][
tempy
].
getPiece
()==
null
)
{
tempx
++;
tempy
++;
}
else
if
(
squares
[
tempx
][
tempy
].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
()))
break
;
else
{
if
(
squares
[
tempx
][
tempy
].
getPiece
()
instanceof
Bishop
||
squares
[
tempx
][
tempy
].
getPiece
()
instanceof
Queen
)
return
true
;
else
break
;
}
}
//Checking for attack from the Knight of opposite color
int
posx
[]={
x
+
1
,
x
+
1
,
x
+
2
,
x
+
2
,
x
-
1
,
x
-
1
,
x
-
2
,
x
-
2
};
int
posy
[]={
y
-
2
,
y
+
2
,
y
-
1
,
y
+
1
,
y
-
2
,
y
+
2
,
y
-
1
,
y
+
1
};
for
(
int
i
=
0
;
i
<
8
;
i
++)
if
((
posx
[
i
]>=
0
&&
posx
[
i
]<
8
&&
posy
[
i
]>=
0
&&
posy
[
i
]<
8
))
if
(
squares
[
posx
[
i
]][
posy
[
i
]].
getPiece
()!=
null
&&
!
squares
[
posx
[
i
]][
posy
[
i
]].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
())
&&
(
squares
[
posx
[
i
]][
posy
[
i
]].
getPiece
()
instanceof
Knight
))
{
return
true
;
}
//Checking for attack from the Pawn of opposite color
int
pox
[]={
x
+
1
,
x
+
1
,
x
+
1
,
x
,
x
,
x
-
1
,
x
-
1
,
x
-
1
};
int
poy
[]={
y
-
1
,
y
+
1
,
y
,
y
+
1
,
y
-
1
,
y
+
1
,
y
-
1
,
y
};
{
for
(
int
i
=
0
;
i
<
8
;
i
++)
if
((
pox
[
i
]>=
0
&&
pox
[
i
]<
8
&&
poy
[
i
]>=
0
&&
poy
[
i
]<
8
))
if
(
squares
[
pox
[
i
]][
poy
[
i
]].
getPiece
()!=
null
&&
!
squares
[
pox
[
i
]][
poy
[
i
]].
getPiece
().
getPieceColor
().
equals
(
this
.
getPieceColor
())
&&
(
squares
[
pox
[
i
]][
poy
[
i
]].
getPiece
()
instanceof
King
))
{
return
true
;
}
}
if
(
getPieceColor
().
equals
(
"w"
))
{
if
(
x
>
0
&&
y
>
0
&&
squares
[
x
-
1
][
y
-
1
].
getPiece
()!=
null
&&
squares
[
x
-
1
][
y
-
1
].
getPiece
().
getPieceColor
().
equals
(
"b"
)
&&(
squares
[
x
-
1
][
y
-
1
].
getPiece
()
instanceof
Pawn
))
return
true
;
if
(
x
>
0
&&
y
<
7
&&
squares
[
x
-
1
][
y
+
1
].
getPiece
()!=
null
&&
squares
[
x
-
1
][
y
+
1
].
getPiece
().
getPieceColor
().
equals
(
"b"
)
&&(
squares
[
x
-
1
][
y
+
1
].
getPiece
()
instanceof
Pawn
))
return
true
;
}
else
{
if
(
x
<
7
&&
y
>
0
&&
squares
[
x
+
1
][
y
-
1
].
getPiece
()!=
null
&&
squares
[
x
+
1
][
y
-
1
].
getPiece
().
getPieceColor
().
equals
(
"w"
)
&&(
squares
[
x
+
1
][
y
-
1
].
getPiece
()
instanceof
Pawn
))
return
true
;
if
(
x
<
7
&&
y
<
7
&&
squares
[
x
+
1
][
y
+
1
].
getPiece
()!=
null
&&
squares
[
x
+
1
][
y
+
1
].
getPiece
().
getPieceColor
().
equals
(
"w"
)
&&(
squares
[
x
+
1
][
y
+
1
].
getPiece
()
instanceof
Pawn
))
return
true
;
}
return
false
;
}
}
}
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