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
Hide 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{
for
(
Piece
p
:
this
.
blackPieces
){
all
.
add
(
p
);
}
boolean
found
=
false
;
Piece
found
=
null
;
boolean
toBusy
=
false
;
Piece
attack
=
null
;
if
(
color
.
equals
(
"W"
)
){
selector
=
this
.
whitePieces
;
enemy
=
this
.
blackPieces
;
found
=
isTakenByWhite
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
toBusy
=
isTakenByWhite
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
selector
=
this
.
whitePieces
;
enemy
=
this
.
blackPieces
;
found
=
takenByWhite
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
toBusy
=
isTakenByWhite
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
attack
=
takenByBlack
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
}
else
{
selector
=
this
.
blackPieces
;
enemy
=
this
.
whitePieces
;
found
=
isTakenByBlack
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
toBusy
=
isTakenByBlack
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
selector
=
this
.
blackPieces
;
enemy
=
this
.
whitePieces
;
found
=
takenByBlack
(
fromArray
[
0
]
-
'0'
,
fromArray
[
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
(
p
.
getY
()
==
fromArray
[
0
]
-
'0'
&&
p
.
getX
()
==
fromArray
[
1
]
){
// Find Piece in select
if
(
found
.
getY
()
==
fromArray
[
0
]
-
'0'
&&
found
.
getX
()
==
fromArray
[
1
]
){
// Find Piece in select
// To Do: Check if pathway is busy
// 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
(
kingCheck
(
all
,
selector
,
enemy
)
&&
!(
found
instanceof
King
)
){
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
p
.
setY
(
toArray
[
0
]
-
'0'
);
p
.
setX
(
toArray
[
1
]);
if
(
found
.
checkWay
(
all
,
toArray
[
1
],
toArray
[
0
]
-
'0'
)
){
if
(
attack
!=
null
){
enemy
.
remove
(
attack
);
}
found
.
setY
(
toArray
[
0
]
-
'0'
);
found
.
setX
(
toArray
[
1
]);
return
true
;
}
else
{
return
false
;
...
...
@@ -238,4 +245,27 @@ public class Board{
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,28 +33,38 @@ public class Pawn extends Piece{
}
else
{
return
false
;
}
}
else
if
(
Math
.
abs
(
this
.
x
-
x
)
==
1
&&
Math
.
abs
(
this
.
y
-
y
)
==
1
){
return
true
;
}
return
false
;
}
public
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
int
yShift
=
0
;
if
(
this
.
y
-
y
!=
0
){
if
(
Math
.
abs
(
this
.
x
-
x
)
==
1
&&
Math
.
abs
(
this
.
y
-
y
)
==
1
){
yShift
=
(
y
-
this
.
y
)
/
Math
.
abs
(
y
-
this
.
y
);
}
int
i
=
1
;
while
(
y
!=
this
.
y
+
i
*
yShift
){
return
true
;
}
else
if
(
Math
.
abs
(
this
.
y
-
y
)
==
1
){
return
true
;
}
else
{
int
yShift
=
0
;
if
(
this
.
y
-
y
!=
0
){
yShift
=
(
y
-
this
.
y
)
/
Math
.
abs
(
y
-
this
.
y
);
}
int
i
=
1
;
while
(
y
!=
this
.
y
+
i
*
yShift
){
if
(
checkTaken
(
pieces
,
this
.
x
,
this
.
y
+
i
*
yShift
)
){
return
false
;
}
i
++;
}
if
(
checkTaken
(
pieces
,
this
.
x
,
this
.
y
+
i
*
yShift
)
){
return
false
;
}
i
++;
}
if
(
checkTaken
(
pieces
,
this
.
x
,
this
.
y
+
i
*
yShift
)
){
return
false
;
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