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
4371d38f
Commit
4371d38f
authored
Apr 29, 2019
by
Omid Sayfun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Final Update
parent
396290a7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
172 additions
and
46 deletions
+172
-46
Bishop.java
lab/game/Bishop.java
+15
-14
Board.java
lab/game/Board.java
+114
-24
Pawn.java
lab/game/Pawn.java
+9
-0
Piece.java
lab/game/Piece.java
+4
-0
Queen.java
lab/game/Queen.java
+1
-1
Rook.java
lab/game/Rook.java
+2
-1
Observ.java
lab/observ/Observ.java
+27
-6
No files found.
lab/game/Bishop.java
View file @
4371d38f
package
lab
.
game
;
import
javax.xml.stream.events.StartDocument
;
import
java.util.*
;
public
class
Bishop
extends
Piece
{
...
...
@@ -22,20 +23,20 @@ public class Bishop 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
){
public
static
boolean
checkWay
(
Piece
p
,
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
if
(
p
.
x
-
x
==
0
){
xShift
=
(
x
-
this
.
x
)
/
Math
.
abs
(
x
-
this
.
x
)
;
return
false
;
}
if
(
this
.
y
-
y
!
=
0
){
if
(
p
.
y
-
y
=
=
0
){
yShift
=
(
y
-
this
.
y
)
/
Math
.
abs
(
y
-
this
.
y
)
;
return
false
;
}
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
)(
this
.
x
+
i
*
xShift
)
&&
y
!=
this
.
y
+
i
*
yShift
){
if
(
checkTaken
(
pieces
,
(
char
)(
this
.
x
+
i
*
xShift
),
this
.
y
+
i
*
yShift
)
){
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
;
}
...
...
@@ -44,12 +45,12 @@ public class Bishop extends Piece{
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
);
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
)(
p
.
x
+
i
*
xShift
)
&&
y
!=
p
.
y
+
i
*
yShift
){
if
(
checkTaken
(
pieces
,
(
char
)(
p
.
x
+
i
*
xShift
),
p
.
y
+
i
*
yShift
)
){
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
;
}
...
...
lab/game/Board.java
View file @
4371d38f
...
...
@@ -4,6 +4,8 @@ import java.util.*;
public
class
Board
{
private
ArrayList
<
Piece
>
whitePieces
;
private
ArrayList
<
Piece
>
blackPieces
;
public
int
whiteScore
=
0
;
public
int
blackScore
=
0
;
public
Board
(){
this
.
whitePieces
=
new
ArrayList
<
Piece
>();
...
...
@@ -139,34 +141,64 @@ public class Board{
System
.
out
.
println
(
"WN: White Knight\t|\tBN: Black Knight"
);
System
.
out
.
println
(
"WR: White Rook \t|\tBR: Black Rook"
);
System
.
out
.
println
(
"WP: White Pawn \t|\tBP: Black Pawn"
);
System
.
out
.
println
(
"Acceptable Command: '2D 4D'(Move From 2D to 4D)"
);
System
.
out
.
println
(
"Acceptable Command: 'D2 D4'(Move From 2D to 4D)"
);
System
.
out
.
println
(
"Press any key to start the game..."
);
}
public
void
printBoard
(){
for
(
int
j
=
9
;
j
>
0
;
j
--){
for
(
char
i
=
'A'
;
i
<
'J'
;
i
++){
if
(
j
==
9
){
if
(
i
-
'A'
>
0
){
public
void
printBoard
(
String
player
){
if
(
player
.
equals
(
"W"
)
){
System
.
out
.
print
((
char
)(
i
-
1
)
+
" "
);
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
{
System
.
out
.
print
(
" "
);
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
(
"- "
);
}
}
}
}
else
{
if
(
i
==
'A'
){
System
.
out
.
print
(
j
+
" "
);
}
else
{
// Main Clause
char
mapedI
=
(
char
)(
i
-
1
);
if
(
isTaken
(
j
,
mapedI
)
){
}
System
.
out
.
println
();
}
}
else
{
for
(
int
j
=
0
;
j
<
9
;
j
++){
for
(
char
i
=
'I'
;
i
>=
'A'
;
i
--){
if
(
j
==
0
){
if
(
i
==
'I'
){
System
.
out
.
print
(
takenBy
(
j
,
mapedI
).
getName
()
+
"
"
);
System
.
out
.
print
(
"
"
);
}
else
{
System
.
out
.
print
(
"- "
);
System
.
out
.
print
((
char
)(
i
)
+
" "
);
}
}
else
{
if
(
i
==
'I'
){
System
.
out
.
print
(
j
+
" "
);
}
else
{
// Main Clause
char
mapedI
=
(
char
)(
i
);
if
(
isTaken
(
j
,
mapedI
)
){
System
.
out
.
print
(
takenBy
(
j
,
mapedI
).
getName
()
+
" "
);
}
else
{
System
.
out
.
print
(
"- "
);
}
}
}
}
System
.
out
.
println
();
}
System
.
out
.
println
();
}
}
...
...
@@ -217,13 +249,18 @@ public class Board{
if
(
found
.
checkWay
(
all
,
toArray
[
1
],
toArray
[
0
]
-
'0'
)
){
if
(
attack
!=
null
){
if
(
(
found
instanceof
Pawn
)
&&
found
.
crossMove
(
toArray
[
1
],
toArray
[
0
]
-
'0'
)
&&
attack
==
null
){
return
false
;
}
else
{
if
(
attack
!=
null
){
enemy
.
remove
(
attack
);
enemy
.
remove
(
attack
);
}
found
.
setY
(
toArray
[
0
]
-
'0'
);
found
.
setX
(
toArray
[
1
]);
return
true
;
}
found
.
setY
(
toArray
[
0
]
-
'0'
);
found
.
setX
(
toArray
[
1
]);
return
true
;
}
else
{
return
false
;
}
...
...
@@ -247,7 +284,7 @@ public class Board{
}
}
public
boolean
kingCheck
(
ArrayList
<
Piece
>
all
,
ArrayList
<
Piece
>
base
,
ArrayList
<
Piece
>
oponent
){
public
boolean
kingCheck
(
ArrayList
<
Piece
>
all
,
ArrayList
<
Piece
>
base
,
ArrayList
<
Piece
>
enemy
){
// find king
int
kingY
=
0
;
char
kingX
=
'A'
;
...
...
@@ -258,15 +295,68 @@ public class Board{
kingX
=
p
.
x
;
}
}
for
(
Piece
p
:
oponent
){
for
(
Piece
p
:
enemy
){
if
(
p
.
canMove
(
kingX
,
kingY
)
){
// Check if move is valid
if
(
p
.
checkWay
(
all
,
kingX
,
kingY
)
){
System
.
out
.
println
(
p
.
getName
());
return
true
;
}
}
}
return
false
;
}
private
boolean
kingMate
(
ArrayList
<
Piece
>
all
,
ArrayList
<
Piece
>
base
,
ArrayList
<
Piece
>
enemy
){
int
kingY
=
0
;
char
kingX
=
'A'
;
for
(
Piece
p
:
base
){
if
(
p
instanceof
King
){
kingY
=
p
.
y
;
kingX
=
p
.
x
;
}
}
int
[]
X
=
{
1
,
1
,
1
,
0
,
-
1
,
-
1
,
-
1
,
0
};
int
[]
Y
=
{
1
,
0
,
-
1
,
-
1
,
-
1
,
0
,
1
,
1
};
for
(
int
i
=
0
;
i
<
8
;
i
++){
char
tempX
=
(
char
)(
kingX
+
X
[
i
]);
int
tempY
=
kingY
+
Y
[
i
];
boolean
flag
=
false
;
for
(
Piece
p
:
enemy
){
if
(
p
.
canMove
(
tempX
,
tempY
)
){
if
(
p
.
checkWay
(
all
,
tempX
,
tempY
)
){
flag
=
true
;
}
}
}
if
(
!
flag
){
return
false
;
}
}
return
true
;
}
public
String
checkMate
(){
ArrayList
<
Piece
>
all
=
new
ArrayList
<
Piece
>();
for
(
Piece
p
:
this
.
blackPieces
){
all
.
add
(
p
);
}
for
(
Piece
p
:
this
.
whitePieces
){
all
.
add
(
p
);
}
if
(
kingMate
(
all
,
this
.
whitePieces
,
this
.
blackPieces
)
){
return
"B"
;
}
else
if
(
kingMate
(
all
,
this
.
whitePieces
,
this
.
blackPieces
)
){
return
"W"
;
}
else
{
return
null
;
}
}
}
lab/game/Pawn.java
View file @
4371d38f
...
...
@@ -40,6 +40,15 @@ public class Pawn extends Piece{
return
false
;
}
@Override
public
boolean
crossMove
(
char
x
,
int
y
){
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
){
if
(
Math
.
abs
(
this
.
x
-
x
)
==
1
&&
Math
.
abs
(
this
.
y
-
y
)
==
1
){
...
...
lab/game/Piece.java
View file @
4371d38f
...
...
@@ -57,6 +57,10 @@ public abstract class Piece{
return
false
;
}
public
boolean
crossMove
(
char
x
,
int
y
){
return
false
;
}
abstract
boolean
canMove
(
char
x
,
int
y
);
abstract
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
);
...
...
lab/game/Queen.java
View file @
4371d38f
...
...
@@ -15,7 +15,7 @@ public class Queen extends Piece{
}
public
boolean
checkWay
(
ArrayList
<
Piece
>
pieces
,
char
x
,
int
y
){
if
(
Rook
.
checkWay
(
this
,
pieces
,
x
,
y
)
&&
Bishop
.
checkWay
(
this
,
pieces
,
x
,
y
)
){
if
(
Rook
.
checkWay
(
this
,
pieces
,
x
,
y
)
||
Bishop
.
checkWay
(
this
,
pieces
,
x
,
y
)
){
return
true
;
}
...
...
lab/game/Rook.java
View file @
4371d38f
...
...
@@ -56,13 +56,14 @@ public class Rook extends Piece{
yShift
=
(
y
-
p
.
y
)
/
Math
.
abs
(
y
-
p
.
y
);
}
int
i
=
1
;
while
(
x
!=
(
char
)(
p
.
x
+
i
*
xShift
)
&&
y
!=
p
.
y
+
i
*
yShift
){
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
++;
}
System
.
out
.
println
(
"Fuck1"
);
return
true
;
}
}
lab/observ/Observ.java
View file @
4371d38f
...
...
@@ -9,12 +9,16 @@ public class Observ{
Scanner
sc
=
new
Scanner
(
System
.
in
);
Board
mainBoard
=
new
Board
();
mainBoard
.
initPieces
();
// mainBoard.printHelp();
new
ProcessBuilder
(
"cmd"
,
"/c"
,
"cls"
).
inheritIO
().
start
().
waitFor
();
mainBoard
.
printHelp
();
sc
.
nextLine
();
String
player
=
"W"
;
for
(
int
i
=
0
;
i
<
500
;
i
++){
String
checkMate
=
null
;
boolean
play
=
true
;
while
(
play
){
while
(
true
){
new
ProcessBuilder
(
"cmd"
,
"/c"
,
"cls"
).
inheritIO
().
start
().
waitFor
();
mainBoard
.
printBoard
();
mainBoard
.
printBoard
(
player
);
if
(
player
.
equals
(
"W"
)
){
System
.
out
.
print
(
"White "
);
...
...
@@ -23,10 +27,18 @@ public class Observ{
}
System
.
out
.
print
(
"Player Move: "
);
String
input
=
sc
.
nextLine
();
// Check if input is valid
if
(
mainBoard
.
move
(
input
.
split
(
" "
)[
0
],
input
.
split
(
" "
)[
1
],
player
)
){
if
(
input
.
split
(
" "
).
length
==
2
){
break
;
checkMate
=
mainBoard
.
checkMate
();
if
(
checkMate
==
null
){
if
(
mainBoard
.
move
(
new
StringBuilder
(
input
.
split
(
" "
)[
0
]).
reverse
().
toString
(),
new
StringBuilder
(
input
.
split
(
" "
)[
1
]).
reverse
().
toString
(),
player
)
){
break
;
}
}
else
{
play
=
false
;
}
}
}
if
(
player
.
equals
(
"W"
)
){
// Change PLayer
...
...
@@ -36,6 +48,15 @@ public class Observ{
player
=
"W"
;
}
}
new
ProcessBuilder
(
"cmd"
,
"/c"
,
"cls"
).
inheritIO
().
start
().
waitFor
();
System
.
out
.
println
(
"\t The Game is over"
);
if
(
checkMate
.
equals
(
"W"
)
){
System
.
out
.
print
(
"White "
);
}
else
{
System
.
out
.
print
(
"Black "
);
}
System
.
out
.
println
(
"Player Won"
);
sc
.
close
();
}
}
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