Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
Chess
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
9631408
Chess
Commits
9dfb84e8
Commit
9dfb84e8
authored
May 15, 2019
by
kimia
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
logic with checkMate
parent
c7036982
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
376 additions
and
194 deletions
+376
-194
Bishop.java
src/Bishop.java
+16
-3
Cell.java
src/Cell.java
+1
-2
King.java
src/King.java
+15
-4
Knight.java
src/Knight.java
+22
-2
Main.java
src/Main.java
+161
-122
Pawn.java
src/Pawn.java
+60
-47
Piece.java
src/Piece.java
+2
-2
Queen.java
src/Queen.java
+78
-10
Rook.java
src/Rook.java
+21
-2
No files found.
src/Bishop.java
View file @
9dfb84e8
public
class
Bishop
extends
Piece
{
public
Bishop
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
}
@Override
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][])
{
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][],
Piece
pw
[],
Piece
pb
[])
{
if
(
super
.
isDeleted
())
return
false
;
int
sw
=
0
;
if
(
super
.
getColor
()
==
Color
.
BLACK
)
{
for
(
Piece
p:
pw
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
else
{
for
(
Piece
p:
pb
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
if
(
(
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())
==
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
()))
)
&&
c
.
isEmpty
(
)
)
{
)
&&
(
c
.
isEmpty
()
||
(!
c
.
isEmpty
()
&&
sw
==
1
)
)
)
{
if
(
this
.
getCell
().
getCol
()
<
c
.
getCol
()
&&
this
.
getCell
().
getRow
()
<
c
.
getRow
())
{
for
(
int
i
=-
1
;
i
>-
8
;
--
i
)
{
if
(!
board
[
this
.
getCell
().
getRow
()-
i
][
this
.
getCell
().
getCol
()-
i
].
isEmpty
())
return
false
;
...
...
@@ -42,4 +55,4 @@ public class Bishop extends Piece {
return
false
;
}
}
}
\ No newline at end of file
}
src/Cell.java
View file @
9dfb84e8
public
class
Cell
{
class
Cell
{
private
int
row
,
col
;
private
boolean
empty
;
...
...
src/King.java
View file @
9dfb84e8
...
...
@@ -4,13 +4,24 @@ public class King extends Piece {
}
@Override
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][])
{
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][],
Piece
pw
[],
Piece
pb
[])
{
if
(
super
.
isDeleted
())
return
false
;
int
sw
=
0
;
if
(
super
.
getColor
()
==
Color
.
BLACK
)
{
for
(
Piece
p:
pw
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
else
{
for
(
Piece
p:
pb
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
return
(
(
(
this
.
getCell
().
getCol
()
==
c
.
getCol
()
&&
Math
.
abs
(
this
.
getCell
().
getRow
()
-
c
.
getRow
())
==
1
)
||
(
this
.
getCell
().
getRow
()
==
c
.
getRow
()
&&
Math
.
abs
(
this
.
getCell
().
getCol
()
-
c
.
getCol
())
==
1
)
||
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())==
1
&&
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
())==
1
)
)
&&
c
.
isEmpty
()
)
&&
(
c
.
isEmpty
()
||
(!
c
.
isEmpty
()
&&
sw
==
1
))
)
;
}
}
\ No newline at end of file
}
src/Knight.java
View file @
9dfb84e8
/**
* knight class show the knight's moves that is like L
*@author kimiadorani
*@version 1.0
*@since 2019-5-7
*/
public
class
Knight
extends
Piece
{
...
...
@@ -6,13 +13,26 @@ public class Knight extends Piece {
}
@Override
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][])
{
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][],
Piece
pw
[],
Piece
pb
[])
{
if
(
super
.
isDeleted
())
return
false
;
int
sw
=
0
;
if
(
super
.
getColor
()
==
Color
.
BLACK
)
{
for
(
Piece
p:
pw
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
else
{
for
(
Piece
p:
pb
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
return
(
(
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())
+
Math
.
abs
(
this
.
getCell
().
getRow
()
-
c
.
getRow
())
==
3
)
&&
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())
!=
0
&&
Math
.
abs
(
this
.
getCell
().
getRow
()
-
c
.
getRow
())
!=
0
)
)
&&
c
.
isEmpty
(
)
&&
(
c
.
isEmpty
()
||
(!
c
.
isEmpty
()
&&
sw
==
1
)
)
)
;
}
}
src/Main.java
View file @
9dfb84e8
This diff is collapsed.
Click to expand it.
src/Pawn.java
View file @
9dfb84e8
public
class
Pawn
extends
Piece
{
boolean
once
,
end
;
private
boolean
once
,
end
;
public
Pawn
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
this
.
once
=
true
;
this
.
end
=
false
;
public
Pawn
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
this
.
once
=
true
;
this
.
end
=
false
;
}
public
void
setOnce
(
boolean
once
)
{
this
.
once
=
once
;
public
void
setOnce
(
boolean
once
)
{
this
.
once
=
once
;
}
public
void
setEnd
(
boolean
end
)
{
this
.
end
=
end
;
public
void
setEnd
(
boolean
end
)
{
this
.
end
=
end
;
}
@Override
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][])
{
if
(
super
.
getColor
()==
Color
.
BLACK
&&
super
.
getCell
().
getRow
()==
0
)
end
=
true
;
if
(
super
.
getColor
()==
Color
.
WHITE
&&
super
.
getCell
().
getRow
()==
7
)
end
=
true
;
@Override
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][],
Piece
pw
[],
Piece
pb
[])
{
if
(
super
.
isDeleted
())
return
false
;
if
(
super
.
getColor
()==
Color
.
BLACK
&&
super
.
getCell
().
getRow
()==
0
)
end
=
true
;
if
(
super
.
getColor
()==
Color
.
WHITE
&&
super
.
getCell
().
getRow
()==
7
)
end
=
true
;
if
(
once
)
{
//once = false ;
//System.out.println("row: " + this.getCell().getRow()) ;
//System.out.println("col: " + this.getCell().getCol()) ;
if
((
this
.
getCell
().
getCol
()
==
c
.
getCol
())
&&
(
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
())==
1
||
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
())==
2
)
&&
c
.
isEmpty
())
return
true
;
else
return
false
;
}
else
{
if
(
end
)
{
if
(
((
this
.
getCell
().
getCol
()
==
c
.
getCol
())
&&
(
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
())==
1
))
||
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())==
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
())
&&
this
.
getCell
().
getRow
()-
c
.
getRow
()==
1
)
)
return
true
;
else
return
false
;
}
else
{
if
(
(
this
.
getCell
().
getCol
()
==
c
.
getCol
()
&&
((
this
.
getCell
().
getRow
()-
c
.
getRow
()==
1
&&
super
.
getColor
()==
Color
.
BLACK
)
||
(
this
.
getCell
().
getRow
()-
c
.
getRow
()==-
1
&&
super
.
getColor
()==
Color
.
WHITE
))
)
||
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())==
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
())
&&
((
this
.
getCell
().
getRow
()-
c
.
getRow
()==
1
&&
super
.
getColor
()==
Color
.
BLACK
)
||
(
this
.
getCell
().
getRow
()-
c
.
getRow
()==-
1
&&
super
.
getColor
()==
Color
.
WHITE
))
)
)
return
true
;
else
return
false
;
}
}
}
int
sw
=
0
;
if
(
super
.
getColor
()
==
Color
.
BLACK
)
{
for
(
Piece
p:
pw
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
else
{
for
(
Piece
p:
pb
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
if
(
once
)
{
//once = false ;
//System.out.println("row: " + this.getCell().getRow()) ;
//System.out.println("col: " + this.getCell().getCol()) ;
if
((
this
.
getCell
().
getCol
()
==
c
.
getCol
())
&&
(
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
())==
1
||
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
())==
2
)
&&
c
.
isEmpty
())
return
true
;
else
return
false
;
}
else
{
if
(
end
)
{
if
(
((
this
.
getCell
().
getCol
()
==
c
.
getCol
())
&&
(
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
())==
1
))
||
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())==
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
())
&&
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())==
1
&&
sw
==
1
)
)
return
true
;
else
return
false
;
}
else
{
if
(
(
this
.
getCell
().
getCol
()
==
c
.
getCol
()
&&
((
this
.
getCell
().
getRow
()-
c
.
getRow
()==
1
&&
super
.
getColor
()==
Color
.
BLACK
)
||
(
this
.
getCell
().
getRow
()-
c
.
getRow
()==-
1
&&
super
.
getColor
()==
Color
.
WHITE
))
)
||
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())==
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
())
&&
((
this
.
getCell
().
getRow
()-
c
.
getRow
()==
1
&&
super
.
getColor
()==
Color
.
BLACK
&&
sw
==
1
)
||
(
this
.
getCell
().
getRow
()-
c
.
getRow
()==-
1
&&
super
.
getColor
()==
Color
.
WHITE
&&
sw
==
1
))
)
)
return
true
;
else
return
false
;
}
}
}
}
src/Piece.java
View file @
9dfb84e8
...
...
@@ -11,7 +11,7 @@ public abstract class Piece {
this
.
deleted
=
false
;
}
public
abstract
boolean
isValidMove
(
Cell
cell
,
Cell
board
[]
[])
;
public
abstract
boolean
isValidMove
(
Cell
c
,
Cell
board
[][],
Piece
pw
[],
Piece
pb
[])
;
public
Cell
getCell
()
{
return
cell
;
...
...
@@ -37,4 +37,4 @@ public abstract class Piece {
this
.
deleted
=
deleted
;
}
}
\ No newline at end of file
}
src/Queen.java
View file @
9dfb84e8
...
...
@@ -2,18 +2,86 @@
public
class
Queen
extends
Piece
{
public
Queen
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
}
public
Queen
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
);
@Override
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][],
Piece
pw
[],
Piece
pb
[])
{
if
(
super
.
isDeleted
())
return
false
;
int
sw
=
0
;
if
(
super
.
getColor
()
==
Color
.
BLACK
)
{
for
(
Piece
p:
pw
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
else
{
for
(
Piece
p:
pb
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
if
(
(
(
this
.
getCell
().
getCol
()
==
c
.
getCol
())
||
(
this
.
getCell
().
getRow
()
==
c
.
getRow
()
)
||
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())
==
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
()))
)
&&
(
c
.
isEmpty
()
||
(!
c
.
isEmpty
()
&&
sw
==
1
))
)
{
if
(
this
.
getCell
().
getCol
()
<
c
.
getCol
()
&&
this
.
getCell
().
getRow
()
<
c
.
getRow
())
{
for
(
int
i
=-
1
;
i
>-
8
;
--
i
)
{
if
(!
board
[
this
.
getCell
().
getRow
()-
i
][
this
.
getCell
().
getCol
()-
i
].
isEmpty
())
return
false
;
if
(
this
.
getCell
().
getRow
()-
i
==
c
.
getRow
())
break
;
}
}
if
(
this
.
getCell
().
getCol
()
<
c
.
getCol
()
&&
this
.
getCell
().
getRow
()
>
c
.
getRow
())
{
for
(
int
i
=-
1
;
i
>-
8
;
--
i
)
{
if
(!
board
[
this
.
getCell
().
getRow
()+
i
][
this
.
getCell
().
getCol
()-
i
].
isEmpty
())
{
//System.out.println("294 294") ;
return
false
;
}
if
(
this
.
getCell
().
getRow
()+
i
==
c
.
getRow
())
break
;
}
}
if
(
this
.
getCell
().
getCol
()
>
c
.
getCol
()
&&
this
.
getCell
().
getRow
()
<
c
.
getRow
())
{
for
(
int
i
=
1
;
i
<
8
;
++
i
)
{
if
(!
board
[
this
.
getCell
().
getRow
()+
i
][
this
.
getCell
().
getCol
()-
i
].
isEmpty
())
return
false
;
if
(
this
.
getCell
().
getCol
()-
i
==
c
.
getCol
())
break
;
}
}
if
(
this
.
getCell
().
getCol
()
>
c
.
getCol
()
&&
this
.
getCell
().
getRow
()
>
c
.
getRow
())
{
for
(
int
i
=-
1
;
i
>-
8
;
--
i
)
{
if
(!
board
[
this
.
getCell
().
getRow
()+
i
][
this
.
getCell
().
getCol
()+
i
].
isEmpty
())
return
false
;
if
(
this
.
getCell
().
getCol
()+
i
==
c
.
getCol
())
break
;
}
}
@Override
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][])
{
return
((
(
this
.
getCell
().
getCol
()
==
c
.
getCol
())
||
(
this
.
getCell
().
getRow
()
==
c
.
getRow
())
||
(
Math
.
abs
(
this
.
getCell
().
getCol
()
-
c
.
getCol
())
==
Math
.
abs
(
this
.
getCell
().
getRow
()
-
c
.
getRow
()))
)
&&
c
.
isEmpty
()
);
if
(
this
.
getCell
().
getCol
()
==
c
.
getCol
()
&&
this
.
getCell
().
getRow
()
<
c
.
getRow
())
{
for
(
int
i
=
1
;
i
<
8
;
++
i
)
{
if
(!
board
[
this
.
getCell
().
getRow
()+
i
][
c
.
getCol
()].
isEmpty
())
return
false
;
if
(
this
.
getCell
().
getRow
()+
i
==
c
.
getRow
())
break
;
}
}
if
(
this
.
getCell
().
getCol
()
==
c
.
getCol
()
&&
this
.
getCell
().
getRow
()
>
c
.
getRow
())
{
for
(
int
i
=-
1
;
i
>-
8
;
--
i
)
{
if
(!
board
[
this
.
getCell
().
getRow
()+
i
][
c
.
getCol
()].
isEmpty
())
{
//System.out.println("294 294") ;
return
false
;
}
if
(
this
.
getCell
().
getRow
()+
i
==
c
.
getRow
())
break
;
}
}
if
(
this
.
getCell
().
getCol
()
<
c
.
getCol
()
&&
this
.
getCell
().
getRow
()
==
c
.
getRow
())
{
for
(
int
i
=
1
;
i
<
8
;
++
i
)
{
if
(!
board
[
c
.
getRow
()][
this
.
getCell
().
getCol
()+
i
].
isEmpty
())
return
false
;
if
(
this
.
getCell
().
getCol
()+
i
==
c
.
getCol
())
break
;
}
}
if
(
this
.
getCell
().
getCol
()
>
c
.
getCol
()
&&
this
.
getCell
().
getRow
()
==
c
.
getRow
())
{
for
(
int
i
=-
1
;
i
>-
8
;
--
i
)
{
if
(!
board
[
c
.
getRow
()][
this
.
getCell
().
getCol
()+
i
].
isEmpty
())
return
false
;
if
(
this
.
getCell
().
getCol
()+
i
==
c
.
getCol
())
break
;
}
}
return
true
;
}
else
return
false
;
}
}
src/Rook.java
View file @
9dfb84e8
/**
* shows the moves of the rook , it can move straight
* @author kimiadorani
* @version 1.0
*/
public
class
Rook
extends
Piece
{
public
Rook
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
}
@Override
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][])
{
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][],
Piece
pw
[],
Piece
pb
[])
{
if
(
super
.
isDeleted
())
return
false
;
int
sw
=
0
;
if
(
super
.
getColor
()
==
Color
.
BLACK
)
{
for
(
Piece
p:
pw
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
else
{
for
(
Piece
p:
pb
)
{
if
(!
p
.
isDeleted
()
&&
p
.
getCell
().
getRow
()==
c
.
getRow
()
&&
p
.
getCell
().
getCol
()==
c
.
getCol
())
sw
=
1
;
}
}
if
(
(
(
this
.
getCell
().
getCol
()
==
c
.
getCol
())
||
(
this
.
getCell
().
getRow
()
==
c
.
getRow
())
)
&&
c
.
isEmpty
(
)
)
&&
(
c
.
isEmpty
()
||
(!
c
.
isEmpty
()
&&
sw
==
1
)
)
)
{
if
(
this
.
getCell
().
getCol
()
==
c
.
getCol
()
&&
this
.
getCell
().
getRow
()
<
c
.
getRow
())
{
for
(
int
i
=
1
;
i
<
8
;
++
i
)
{
...
...
@@ -42,4 +60,5 @@ public class Rook extends Piece {
}
else
return
false
;
}
}
\ No newline at end of file
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