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
b81456de
Commit
b81456de
authored
Apr 27, 2019
by
Omid Sayfun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PathWay Check Added
parent
755a7ca0
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
156 additions
and
3 deletions
+156
-3
Bishop.java
Bishop.java
+30
-0
Board.java
Board.java
+19
-3
King.java
King.java
+6
-0
Knight.java
Knight.java
+6
-0
Main.java
Main.java
+2
-0
Pawn.java
Pawn.java
+23
-0
Piece.java
Piece.java
+14
-0
Queen.java
Queen.java
+10
-0
Rook.java
Rook.java
+46
-0
No files found.
Bishop.java
View file @
b81456de
import
java.util.*
;
public
class
Bishop
extends
Piece
{
public
Bishop
(
char
x
,
int
y
,
boolean
color
,
String
name
){
super
(
x
,
y
,
color
,
name
);
...
...
@@ -18,4 +20,32 @@ public class Bishop extends Piece{
}
return
false
;
}
public
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
int
xShift
=
(
x
-
this
.
x
)
/
Math
.
abs
(
x
-
this
.
x
);;
int
yShift
=
(
y
-
this
.
y
)
/
Math
.
abs
(
y
-
this
.
y
);
int
i
=
1
;
while
(
x
!=
(
char
)(
this
.
x
+
i
*
xShift
)
&&
y
!=
this
.
y
+
i
*
yShift
){
if
(
checkTaken
(
pieces
,
(
char
)(
this
.
x
+
i
*
xShift
),
this
.
y
+
i
*
yShift
)
){
return
false
;
}
i
++;
}
return
true
;
}
public
static
boolean
checkWay
(
Piece
p
,
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
int
xShift
=
(
x
-
p
.
x
)
/
Math
.
abs
(
x
-
p
.
x
);;
int
yShift
=
(
y
-
p
.
y
)
/
Math
.
abs
(
y
-
p
.
y
);
int
i
=
1
;
while
(
x
!=
(
char
)(
p
.
x
+
i
*
xShift
)
&&
y
!=
p
.
y
+
i
*
yShift
){
if
(
checkTaken
(
pieces
,
(
char
)(
p
.
x
+
i
*
xShift
),
p
.
y
+
i
*
yShift
)
){
return
false
;
}
i
++;
}
return
true
;
}
}
Board.java
View file @
b81456de
...
...
@@ -179,15 +179,25 @@ public class Board{
if
(
toArray
[
0
]
>=
'1'
&&
toArray
[
0
]
<=
'8'
&&
toArray
[
1
]
>=
'A'
&&
toArray
[
1
]
<=
'H'
){
// Check to bound
ArrayList
<
Piece
>
selector
=
null
;
ArrayList
<
Piece
>
enemy
=
null
;
ArrayList
<
Piece
>
all
=
new
ArrayList
<
Piece
>();
for
(
Piece
p
:
this
.
whitePieces
){
all
.
add
(
p
);
}
for
(
Piece
p
:
this
.
blackPieces
){
all
.
add
(
p
);
}
boolean
found
=
false
;
boolean
toBusy
=
false
;
if
(
color
.
equals
(
"W"
)
){
selector
=
this
.
whitePieces
;
enemy
=
this
.
blackPieces
;
found
=
isTakenByWhite
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
toBusy
=
isTakenByWhite
(
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
]);
}
...
...
@@ -198,11 +208,17 @@ public class Board{
// 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
p
.
setY
(
toArray
[
0
]
-
'0'
);
p
.
setX
(
toArray
[
1
]);
return
true
;
if
(
p
.
checkWay
(
all
,
toArray
[
1
],
toArray
[
0
]
-
'0'
)
){
p
.
setY
(
toArray
[
0
]
-
'0'
);
p
.
setX
(
toArray
[
1
]);
return
true
;
}
else
{
return
false
;
}
}
else
{
return
false
;
}
...
...
King.java
View file @
b81456de
import
java.util.*
;
public
class
King
extends
Piece
{
public
King
(
char
x
,
int
y
,
boolean
color
,
String
name
){
super
(
x
,
y
,
color
,
name
);
...
...
@@ -10,4 +12,8 @@ public class King extends Piece{
}
return
false
;
}
public
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
return
true
;
}
}
Knight.java
View file @
b81456de
import
java.util.*
;
public
class
Knight
extends
Piece
{
public
Knight
(
char
x
,
int
y
,
boolean
color
,
String
name
){
super
(
x
,
y
,
color
,
name
);
...
...
@@ -14,4 +16,8 @@ public class Knight extends Piece{
}
return
false
;
}
public
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
return
true
;
}
}
Main.java
View file @
b81456de
...
...
@@ -21,7 +21,9 @@ public class Main{
}
System
.
out
.
print
(
"Player Move: "
);
String
input
=
sc
.
nextLine
();
// Check if input is valid
if
(
mainBoard
.
move
(
input
.
split
(
" "
)[
0
],
input
.
split
(
" "
)[
1
],
player
)
){
break
;
}
}
...
...
Pawn.java
View file @
b81456de
import
java.util.*
;
public
class
Pawn
extends
Piece
{
private
boolean
firstMove
;
public
Pawn
(
char
x
,
int
y
,
boolean
color
,
String
name
){
...
...
@@ -34,4 +36,25 @@ public class Pawn extends Piece{
}
return
false
;
}
public
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
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
;
}
return
true
;
}
}
Piece.java
View file @
b81456de
import
java.util.*
;
public
abstract
class
Piece
{
protected
int
y
;
protected
char
x
;
...
...
@@ -43,5 +45,17 @@ public abstract class Piece{
return
this
.
color
;
}
public
static
boolean
checkTaken
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
for
(
Piece
p
:
pieces
){
if
(
p
.
x
==
x
&&
p
.
y
==
y
){
return
true
;
}
}
return
false
;
}
abstract
boolean
canMove
(
char
x
,
int
y
);
abstract
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
);
}
Queen.java
View file @
b81456de
import
java.util.*
;
public
class
Queen
extends
Piece
{
public
Queen
(
char
x
,
int
y
,
boolean
color
,
String
name
){
super
(
x
,
y
,
color
,
name
);
...
...
@@ -10,4 +12,12 @@ public class Queen extends Piece{
}
return
false
;
}
public
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
if
(
Rook
.
checkWay
(
this
,
pieces
,
x
,
y
)
&&
Bishop
.
checkWay
(
this
,
pieces
,
x
,
y
)
){
return
true
;
}
return
false
;
}
}
Rook.java
View file @
b81456de
import
java.util.*
;
public
class
Rook
extends
Piece
{
public
Rook
(
char
x
,
int
y
,
boolean
color
,
String
name
){
super
(
x
,
y
,
color
,
name
);
...
...
@@ -18,4 +20,48 @@ public class Rook extends Piece{
}
return
false
;
}
public
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
int
xShift
=
0
;
int
yShift
=
0
;
if
(
this
.
x
-
x
!=
0
){
xShift
=
(
x
-
this
.
x
)
/
Math
.
abs
(
x
-
this
.
x
);
}
if
(
this
.
y
-
y
!=
0
){
yShift
=
(
y
-
this
.
y
)
/
Math
.
abs
(
y
-
this
.
y
);
}
int
i
=
1
;
while
(
x
!=
(
char
)(
this
.
x
+
i
*
xShift
)
||
y
!=
this
.
y
+
i
*
yShift
){
if
(
checkTaken
(
pieces
,
(
char
)(
this
.
x
+
i
*
xShift
),
this
.
y
+
i
*
yShift
)
){
return
false
;
}
i
++;
}
return
true
;
}
public
static
boolean
checkWay
(
Piece
p
,
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
int
xShift
=
0
;
int
yShift
=
0
;
if
(
p
.
x
-
x
!=
0
){
xShift
=
(
x
-
p
.
x
)
/
Math
.
abs
(
x
-
p
.
x
);
}
if
(
p
.
y
-
y
!=
0
){
yShift
=
(
y
-
p
.
y
)
/
Math
.
abs
(
y
-
p
.
y
);
}
int
i
=
1
;
while
(
x
!=
(
char
)(
p
.
x
+
i
*
xShift
)
&&
y
!=
p
.
y
+
i
*
yShift
){
if
(
checkTaken
(
pieces
,
(
char
)(
p
.
x
+
i
*
xShift
),
p
.
y
+
i
*
yShift
)
){
return
false
;
}
i
++;
}
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