Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
ChessProject
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
9631413
ChessProject
Commits
66d91632
Commit
66d91632
authored
May 17, 2019
by
hamed
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix King Kish bug & some improvment of code...
parent
cb2c3846
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
167 additions
and
115 deletions
+167
-115
Board.java
src/Main/Board.java
+49
-2
Cell.java
src/Main/Cell.java
+0
-20
ChessFrame.java
src/Main/ChessFrame.java
+18
-0
Main.java
src/Main/Main.java
+5
-0
PiecesName.java
src/Main/PiecesName.java
+0
-10
Bishop.java
src/Pieces/Bishop.java
+6
-10
King.java
src/Pieces/King.java
+16
-26
Knight.java
src/Pieces/Knight.java
+5
-10
Pawn.java
src/Pieces/Pawn.java
+11
-11
Piece.java
src/Pieces/Piece.java
+47
-3
Queen.java
src/Pieces/Queen.java
+5
-12
Rook.java
src/Pieces/Rook.java
+5
-11
No files found.
src/Main/Board.java
View file @
66d91632
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package
Main
;
import
Pieces.*
;
...
...
@@ -6,6 +11,8 @@ import java.util.ArrayList;
import
java.util.Queue
;
public
class
Board
{
private
King
whiteKing
;
private
King
blackKing
;
private
ArrayList
<
Piece
>
whiteRemovedList
=
new
ArrayList
<>();
private
ArrayList
<
Piece
>
blackRemovedList
=
new
ArrayList
<>();
private
Piece
board
[][]
=
new
Piece
[
8
][
8
];
...
...
@@ -17,7 +24,8 @@ public class Board {
board
[
6
][
0
]
=
new
Knight
(
6
,
0
,
false
,
this
);
board
[
2
][
0
]
=
new
Bishop
(
2
,
0
,
false
,
this
);
board
[
5
][
0
]
=
new
Bishop
(
5
,
0
,
false
,
this
);
board
[
3
][
0
]
=
new
King
(
3
,
0
,
false
,
this
);
blackKing
=
new
King
(
3
,
0
,
false
,
this
);
board
[
3
][
0
]
=
blackKing
;
board
[
4
][
0
]
=
new
Queen
(
4
,
0
,
false
,
this
);
...
...
@@ -33,7 +41,8 @@ public class Board {
board
[
2
][
7
]
=
new
Bishop
(
2
,
7
,
true
,
this
);
board
[
5
][
7
]
=
new
Bishop
(
5
,
7
,
true
,
this
);
board
[
3
][
7
]
=
new
Queen
(
3
,
7
,
true
,
this
);
board
[
4
][
7
]
=
new
King
(
4
,
7
,
true
,
this
);
whiteKing
=
new
King
(
4
,
7
,
true
,
this
);
board
[
4
][
7
]
=
whiteKing
;
}
public
ArrayList
<
Piece
>
getBlackRemovedList
(){
...
...
@@ -42,6 +51,9 @@ public class Board {
public
ArrayList
<
Piece
>
getWhiteRemovedList
(){
return
whiteRemovedList
;
}
public
boolean
isWhiteMove
(){
return
whiteMove
;
}
public
boolean
getMove
(
int
x
,
int
y
,
int
destX
,
int
destY
){
if
(
board
[
x
][
y
]==
null
)
return
false
;
// System.out.println(x+","+y+"->"+destX+","+destY);
...
...
@@ -95,6 +107,41 @@ public class Board {
return
ret
;
}
}
public
boolean
KishCheck
(
boolean
isWhite
){
for
(
int
i
=
0
;
i
<
8
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
Piece
p
=
board
[
i
][
j
];
if
(
p
instanceof
King
&&
p
.
isWhite
())
whiteKing
=
(
King
)
p
;
else
if
(
p
instanceof
King
&&
p
.
isBlack
())
blackKing
=
(
King
)
p
;
}
}
if
(
isWhite
){
for
(
int
i
=
0
;
i
<
8
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
if
(
board
[
i
][
j
]
!=
null
&&
board
[
i
][
j
].
isBlack
()){
int
xx
=
whiteKing
.
getX
();
int
yy
=
whiteKing
.
getY
();
if
(
board
[
i
][
j
].
isValidMove
(
xx
,
yy
))
return
true
;
}
}
}
}
else
{
for
(
int
i
=
0
;
i
<
8
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
if
(
board
[
i
][
j
]
!=
null
&&
board
[
i
][
j
].
isWhite
()){
int
xx
=
blackKing
.
getX
();
int
yy
=
blackKing
.
getY
();
if
(
board
[
i
][
j
].
isValidMove
(
xx
,
yy
))
return
true
;
}
}
}
}
return
false
;
}
public
Piece
[][]
getBoard
()
{
return
board
;
}
...
...
src/Main/Cell.java
deleted
100644 → 0
View file @
cb2c3846
package
Main
;
public
class
Cell
implements
Comparable
<
Cell
>{
int
x
;
int
y
;
public
Cell
(
int
x
,
int
y
){
this
.
x
=
x
;
this
.
y
=
y
;
}
@Override
public
int
compareTo
(
Cell
o
)
{
if
(
o
.
x
==
x
){
return
o
.
y
-
y
;
}
else
{
return
o
.
x
-
x
;
}
}
}
src/Main/ChessFrame.java
View file @
66d91632
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package
Main
;
import
Pieces.*
;
...
...
@@ -141,6 +146,19 @@ public class ChessFrame extends JFrame implements MouseListener, MouseMotionList
}
}
}
//---------------
g
.
setFont
(
new
Font
(
"Arial"
,
Font
.
BOLD
,
18
));
if
(
b
.
KishCheck
(
true
)){
g
.
drawString
(
"Kish of White King ..."
,
WIDTH
+
WIDTH_2
/
3
,
yBias
+
HEIGHT
/
2
);
}
else
if
(
b
.
KishCheck
(
false
)){
g
.
drawString
(
"Kish of Black King ..."
,
WIDTH
+
WIDTH_2
/
3
,
yBias
+
HEIGHT
/
2
);
}
else
if
(
b
.
isWhiteMove
())
{
g
.
drawString
(
"White Turn..."
,
WIDTH
+
WIDTH_2
/
3
,
yBias
+
HEIGHT
/
2
);
}
else
if
(!
b
.
isWhiteMove
())
{
g
.
drawString
(
"Black Turn..."
,
WIDTH
+
WIDTH_2
/
3
,
yBias
+
HEIGHT
/
2
);
}
}
@Override
...
...
src/Main/Main.java
View file @
66d91632
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package
Main
;
import
java.util.ArrayList
;
...
...
src/Main/PiecesName.java
deleted
100644 → 0
View file @
cb2c3846
package
Main
;
enum
PiecesName
{
KING
,
KNIGHT
,
PAWN
,
BISHOP
,
QUEEN
,
ROOK
}
\ No newline at end of file
src/Pieces/Bishop.java
View file @
66d91632
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package
Pieces
;
import
Main.Board
;
...
...
@@ -9,16 +14,7 @@ public class Bishop extends Piece {
public
Bishop
(
int
x
,
int
y
,
boolean
isWhite
,
Board
board
)
{
super
(
x
,
y
,
isWhite
,
board
);
}
@Override
public
boolean
isCanMove
(
int
dx
,
int
dy
)
{
if
(
isOutside
(
dx
,
dy
))
return
false
;
int
deltaX
=
Math
.
abs
(
dx
-
getX
());
int
deltaY
=
Math
.
abs
(
dy
-
getY
());
if
(
deltaX
==
deltaY
)
return
true
;
return
false
;
}
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
// if(!isCanMove(dx, dy)) return false;
...
...
src/Pieces/King.java
View file @
66d91632
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package
Pieces
;
import
Main.Board
;
import
Main.Cell
;
import
java.util.ArrayList
;
import
java.util.HashSet
;
public
class
King
extends
Piece
{
...
...
@@ -13,17 +16,6 @@ public class King extends Piece {
super
(
x
,
y
,
isWhite
,
board
);
}
@Override
public
boolean
isCanMove
(
int
dx
,
int
dy
)
{
if
(
isOutside
(
dx
,
dy
))
return
false
;
int
deltaX
=
Math
.
abs
(
dx
-
getX
());
int
deltaY
=
Math
.
abs
(
dy
-
getY
());
if
(
deltaX
==
deltaY
&&
deltaX
==
1
)
return
true
;
if
(
deltaX
==
1
&&
deltaY
==
0
)
return
true
;
if
(
deltaX
==
0
&&
deltaY
==
1
)
return
true
;
return
false
;
}
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
// if(!isCanMove(dx, dy)) return false;
...
...
@@ -36,32 +28,30 @@ public class King extends Piece {
for
(
int
m
[]
:
moves
){
if
(
m
[
0
]==
dx
&&
m
[
1
]
==
dy
){
return
true
;
// break;
}
}
return
false
;
}
public
HashSet
<
Cell
>
getThreatedPlaces
(){
HashSet
<
Cell
>
retSet
=
new
HashSet
<>();
private
boolean
isAttacked
(
int
x
,
int
y
){
for
(
int
i
=
0
;
i
<
8
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
Piece
p
=
getBoard
().
getBoard
()[
i
][
j
];
if
(
p
!=
null
&&
!(
p
instanceof
King
)
&&((
p
.
isWhite
()&&
isBlack
())
||(
p
.
isBlack
()
&&
isWhite
()))
){
ArrayList
<
int
[]>
moves
=
p
.
moves
();
for
(
int
m
[]
:
moves
){
Cell
c
=
new
Cell
(
m
[
0
],
m
[
1
]);
retSet
.
add
(
c
)
;
if
(
p
!=
null
&&
!(
p
instanceof
King
)){
if
(
p
.
isWhite
()
&&
isBlack
()){
if
(
p
.
isValidMove
(
x
,
y
))
return
true
;
}
else
if
(
p
.
isBlack
()
&&
isWhite
()){
if
(
p
.
isValidMove
(
x
,
y
))
return
true
;
}
}
}
}
return
retSet
;
return
false
;
}
@Override
public
ArrayList
<
int
[]>
moves
()
{
ArrayList
<
int
[]>
move
=
new
ArrayList
<>();
HashSet
<
Cell
>
threats
=
getThreatedPlaces
();
int
x
=
getX
();
int
y
=
getY
();
int
posx
[]={
x
,
x
,
x
+
1
,
x
+
1
,
x
+
1
,
x
-
1
,
x
-
1
,
x
-
1
};
...
...
@@ -70,10 +60,10 @@ public class King extends Piece {
if
((
posx
[
i
]>=
0
&&
posx
[
i
]<
8
&&
posy
[
i
]>=
0
&&
posy
[
i
]<
8
)){
if
(
getBoard
().
isEmpty
(
posx
[
i
],
posy
[
i
])
||
(
getBoard
().
getColor
(
posx
[
i
],
posy
[
i
])==
2
&&
this
.
isWhite
())
||(
getBoard
().
getColor
(
posx
[
i
],
posy
[
i
])==
1
&&
isBlack
())
)
{
Cell
cmp
=
new
Cell
(
posx
[
i
],
posy
[
i
]);
if
(!
threats
.
contains
(
cmp
))
{
if
(!
isAttacked
(
posx
[
i
],
posy
[
i
])){
move
.
add
(
new
int
[]{
posx
[
i
],
posy
[
i
]});
}
}
}
}
...
...
src/Pieces/Knight.java
View file @
66d91632
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package
Pieces
;
import
Main.Board
;
...
...
@@ -11,16 +16,6 @@ public class Knight extends Piece {
super
(
x
,
y
,
isWhite
,
board
);
}
@Override
public
boolean
isCanMove
(
int
dx
,
int
dy
)
{
if
(
isOutside
(
dx
,
dy
))
return
false
;
if
(
dx
!=
getX
()
-
1
&&
dx
!=
getX
()
+
1
&&
dx
!=
getX
()
+
2
&&
dx
!=
getX
()
-
2
)
return
false
;
if
(
dy
!=
getY
()
-
2
&&
dy
!=
getY
()
+
2
&&
dy
!=
getY
()
-
1
&&
dy
!=
getY
()
+
1
)
return
false
;
return
true
;
}
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
ArrayList
<
int
[]>
moves
=
moves
();
...
...
src/Pieces/Pawn.java
View file @
66d91632
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package
Pieces
;
import
Main.Board
;
...
...
@@ -11,11 +16,6 @@ public class Pawn extends Piece {
super
(
x
,
y
,
isWhite
,
board
);
}
@Override
public
boolean
isCanMove
(
int
dx
,
int
dy
)
{
if
(
isOutside
(
dx
,
dy
))
return
false
;
return
false
;
}
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
...
...
@@ -47,10 +47,10 @@ public class Pawn extends Piece {
moves
.
add
(
new
int
[]{
x
,
4
});
}
}
if
((
x
>
0
)&&
((
getBoard
().
getColor
(
x
-
1
,
y
-
1
)==
2
&&
isWhite
()
||
getBoard
().
getColor
(
x
-
1
,
y
-
1
)==
1
&&
isBlack
())))
if
((
x
>
0
)&&
((
getBoard
().
getColor
(
x
-
1
,
y
-
1
)==
2
&&
isWhite
()
)
||
(
getBoard
().
getColor
(
x
-
1
,
y
-
1
)==
1
&&
isBlack
())))
moves
.
add
(
new
int
[]{
x
-
1
,
y
-
1
});
if
((
x
<
7
)&&((
getBoard
().
getColor
(
x
+
1
,
y
-
1
)==
2
&&
isWhite
()
||
getBoard
().
getColor
(
x
+
1
,
y
-
1
)==
1
&&
isBlack
())))
moves
.
add
(
new
int
[]{
x
-
1
,
y
+
1
});
if
((
x
<
7
)&&((
getBoard
().
getColor
(
x
+
1
,
y
-
1
)==
2
&&
isWhite
()
)
||
(
getBoard
().
getColor
(
x
+
1
,
y
-
1
)==
1
&&
isBlack
())))
moves
.
add
(
new
int
[]{
x
+
1
,
y
-
1
});
}
else
{
...
...
@@ -65,9 +65,9 @@ public class Pawn extends Piece {
moves
.
add
(
new
int
[]{
x
,
3
});
}
}
if
((
x
>
0
)&&
((
getBoard
().
getColor
(
x
-
1
,
y
+
1
)==
2
&&
isWhite
()
||
getBoard
().
getColor
(
x
-
1
,
y
+
1
)==
1
&&
isBlack
())))
moves
.
add
(
new
int
[]{
x
+
1
,
y
-
1
});
if
((
x
<
7
)&&((
getBoard
().
getColor
(
x
+
1
,
y
+
1
)==
2
&&
isWhite
()
||
getBoard
().
getColor
(
x
+
1
,
y
+
1
)==
1
&&
isBlack
())))
if
((
x
>
0
)&&
((
getBoard
().
getColor
(
x
-
1
,
y
+
1
)==
2
&&
isWhite
()
)
||(
getBoard
().
getColor
(
x
-
1
,
y
+
1
)==
1
&&
isBlack
())))
moves
.
add
(
new
int
[]{
x
-
1
,
y
+
1
});
if
((
x
<
7
)&&((
getBoard
().
getColor
(
x
+
1
,
y
+
1
)==
2
&&
isWhite
()
)
||(
getBoard
().
getColor
(
x
+
1
,
y
+
1
)==
1
&&
isBlack
())))
moves
.
add
(
new
int
[]{
x
+
1
,
y
+
1
});
}
return
moves
;
...
...
src/Pieces/Piece.java
View file @
66d91632
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package
Pieces
;
import
Main.Board
;
...
...
@@ -15,12 +20,24 @@ public abstract class Piece implements Cloneable {
this
.
isWhite
=
isWhite
;
this
.
board
=
board
;
}
/**
* This is the method for Color of Piece
* @return isWhite.
*/
public
boolean
isWhite
(){
return
isWhite
;
}
/**
* This is the method for Color of Piece
* @return isBlack.
*/
public
boolean
isBlack
(){
return
!
isWhite
;
}
/**
* This is the method for x-axis of Piece
* @return x-axis.
*/
public
int
getX
()
{
return
x
;
}
...
...
@@ -29,20 +46,48 @@ public abstract class Piece implements Cloneable {
return
board
;
}
/**
* This is the method for set x-axis of Piece
* @param x x-axis
* @return void.
*/
public
void
setX
(
int
x
)
{
this
.
x
=
x
;
}
/**
* This is the method for y-axis of Piece
* @return y-axis.
*/
public
int
getY
()
{
return
y
;
}
/**
* This is the method for set y-axis of Piece
* @param y y-axis
* @return void.
*/
public
void
setY
(
int
y
)
{
this
.
y
=
y
;
}
abstract
public
boolean
isCanMove
(
int
dx
,
int
dy
);
/**
* This is the method for validate move of piece
* @param dx destination x
* @param dy destination y
* @return is a Valid move.
*/
abstract
public
boolean
isValidMove
(
int
dx
,
int
dy
);
/**
* This is the method for list of valid move of piece
* @return list of valid moves of piece.
*/
abstract
public
ArrayList
<
int
[]>
moves
();
/**
* This is the method for check outside of piece
* @param dx destination x.
* @param dy destination y.
* @return is destination outside?.
*/
public
boolean
isOutside
(
int
dx
,
int
dy
){
if
(
dx
<
0
||
dx
>
8
||
dy
<
0
||
dy
>
8
)
return
false
;
...
...
@@ -57,5 +102,4 @@ public abstract class Piece implements Cloneable {
}
return
null
;
}
// public void dra
}
src/Pieces/Queen.java
View file @
66d91632
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package
Pieces
;
import
Main.Board
;
...
...
@@ -11,18 +16,6 @@ public class Queen extends Piece {
super
(
x
,
y
,
isWhite
,
board
);
}
@Override
public
boolean
isCanMove
(
int
dx
,
int
dy
)
{
if
(
isOutside
(
dx
,
dy
))
return
false
;
int
deltaX
=
Math
.
abs
(
dx
-
getX
());
int
deltaY
=
Math
.
abs
(
dy
-
getY
());
if
(
deltaX
==
deltaY
)
return
true
;
if
(
dx
==
getX
())
return
true
;
if
(
dy
==
getY
())
return
true
;
return
false
;
}
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
...
...
src/Pieces/Rook.java
View file @
66d91632
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package
Pieces
;
import
Main.Board
;
...
...
@@ -9,17 +14,6 @@ public class Rook extends Piece {
public
Rook
(
int
x
,
int
y
,
boolean
isWhite
,
Board
board
)
{
super
(
x
,
y
,
isWhite
,
board
);
}
@Override
public
boolean
isCanMove
(
int
dx
,
int
dy
)
{
if
(
isOutside
(
dx
,
dy
))
return
false
;
if
(
dx
==
getX
())
return
true
;
if
(
dy
==
getY
())
return
true
;
return
false
;
}
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
// if(!isCanMove(dx, dy))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