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
6240c6aa
Commit
6240c6aa
authored
Apr 27, 2019
by
Omid Sayfun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
KingCheck Added
parent
b81456de
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
31 deletions
+71
-31
Board.java
Board.java
+49
-19
Pawn.java
Pawn.java
+22
-12
No files found.
Board.java
View file @
6240c6aa
...
@@ -187,34 +187,41 @@ public class Board{
...
@@ -187,34 +187,41 @@ public class Board{
for
(
Piece
p
:
this
.
blackPieces
){
for
(
Piece
p
:
this
.
blackPieces
){
all
.
add
(
p
);
all
.
add
(
p
);
}
}
boolean
found
=
false
;
Piece
found
=
null
;
boolean
toBusy
=
false
;
boolean
toBusy
=
false
;
Piece
attack
=
null
;
if
(
color
.
equals
(
"W"
)
){
if
(
color
.
equals
(
"W"
)
){
selector
=
this
.
whitePieces
;
selector
=
this
.
whitePieces
;
enemy
=
this
.
blackPieces
;
enemy
=
this
.
blackPieces
;
found
=
isT
akenByWhite
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
found
=
t
akenByWhite
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
toBusy
=
isTakenByWhite
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
toBusy
=
isTakenByWhite
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
attack
=
takenByBlack
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
}
else
{
}
else
{
selector
=
this
.
blackPieces
;
selector
=
this
.
blackPieces
;
enemy
=
this
.
whitePieces
;
enemy
=
this
.
whitePieces
;
found
=
isT
akenByBlack
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
found
=
t
akenByBlack
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
toBusy
=
isTakenByBlack
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
toBusy
=
isTakenByBlack
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
attack
=
takenByWhite
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
}
}
if
(
found
&&
!
toBusy
){
// Found and to is not busy
if
(
found
!=
null
&&
!
toBusy
){
// Found and to is not busy
for
(
Piece
p
:
selector
){
if
(
found
.
getY
()
==
fromArray
[
0
]
-
'0'
&&
found
.
getX
()
==
fromArray
[
1
]
){
// Find Piece in select
if
(
p
.
getY
()
==
fromArray
[
0
]
-
'0'
&&
p
.
getX
()
==
fromArray
[
1
]
){
// Find Piece in select
// To Do: Check if pathway is busy
if
(
kingCheck
(
all
,
selector
,
enemy
)
&&
!(
found
instanceof
King
)
){
// To Do: Check if king is checked and have to move king
// To Do: Attack
if
(
p
.
canMove
(
toArray
[
1
],
toArray
[
0
]
-
'0'
)
){
// Check if move is valid
if
(
p
.
checkWay
(
all
,
toArray
[
1
],
toArray
[
0
]
-
'0'
)
){
return
false
;
}
else
{
if
(
found
.
canMove
(
toArray
[
1
],
toArray
[
0
]
-
'0'
)
){
// Check if move is valid
if
(
found
.
checkWay
(
all
,
toArray
[
1
],
toArray
[
0
]
-
'0'
)
){
p
.
setY
(
toArray
[
0
]
-
'0'
);
if
(
attack
!=
null
){
p
.
setX
(
toArray
[
1
]);
enemy
.
remove
(
attack
);
}
found
.
setY
(
toArray
[
0
]
-
'0'
);
found
.
setX
(
toArray
[
1
]);
return
true
;
return
true
;
}
else
{
}
else
{
return
false
;
return
false
;
...
@@ -238,4 +245,27 @@ public class Board{
...
@@ -238,4 +245,27 @@ public class Board{
return
false
;
return
false
;
}
}
}
}
public
boolean
kingCheck
(
ArrayList
<
Piece
>
all
,
ArrayList
<
Piece
>
base
,
ArrayList
<
Piece
>
oponent
){
// find king
int
kingY
=
0
;
char
kingX
=
'A'
;
for
(
Piece
p
:
base
){
if
(
p
instanceof
King
){
kingY
=
p
.
y
;
kingX
=
p
.
x
;
}
}
for
(
Piece
p
:
oponent
){
if
(
p
.
canMove
(
kingX
,
kingY
)
){
// Check if move is valid
if
(
p
.
checkWay
(
all
,
kingX
,
kingY
)
){
return
true
;
}
}
}
return
false
;
}
}
}
Pawn.java
View file @
6240c6aa
...
@@ -33,11 +33,20 @@ public class Pawn extends Piece{
...
@@ -33,11 +33,20 @@ public class Pawn extends Piece{
}
else
{
}
else
{
return
false
;
return
false
;
}
}
}
else
if
(
Math
.
abs
(
this
.
x
-
x
)
==
1
&&
Math
.
abs
(
this
.
y
-
y
)
==
1
){
return
true
;
}
}
return
false
;
return
false
;
}
}
public
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
public
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
if
(
Math
.
abs
(
this
.
x
-
x
)
==
1
&&
Math
.
abs
(
this
.
y
-
y
)
==
1
){
return
true
;
}
else
if
(
Math
.
abs
(
this
.
y
-
y
)
==
1
){
return
true
;
}
else
{
int
yShift
=
0
;
int
yShift
=
0
;
if
(
this
.
y
-
y
!=
0
){
if
(
this
.
y
-
y
!=
0
){
...
@@ -57,4 +66,5 @@ public class Pawn extends Piece{
...
@@ -57,4 +66,5 @@ public class Pawn extends Piece{
}
}
return
true
;
return
true
;
}
}
}
}
}
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