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
610acdb4
Commit
610acdb4
authored
May 12, 2019
by
hamed
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some bug fixed v1
parent
b2453bc2
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
213 additions
and
60 deletions
+213
-60
Board.java
src/Main/Board.java
+54
-21
Main.java
src/Main/Main.java
+68
-0
Bishop.java
src/Pieces/Bishop.java
+12
-5
King.java
src/Pieces/King.java
+12
-5
Knight.java
src/Pieces/Knight.java
+8
-1
Pawn.java
src/Pieces/Pawn.java
+24
-17
Piece.java
src/Pieces/Piece.java
+10
-1
Queen.java
src/Pieces/Queen.java
+12
-5
Rook.java
src/Pieces/Rook.java
+13
-5
No files found.
src/Main/Board.java
View file @
610acdb4
...
...
@@ -2,45 +2,78 @@ package Main;
import
Pieces.*
;
import
java.util.ArrayList
;
import
java.util.Queue
;
public
class
Board
{
private
Piece
board
[][]
=
new
Piece
[
8
][
8
];
public
Board
(){
board
[
0
][
0
]
=
new
Rook
(
0
,
0
,
false
,
this
);
board
[
0
][
7
]
=
new
Rook
(
0
,
7
,
false
,
this
);
board
[
0
][
1
]
=
new
Knight
(
0
,
1
,
false
,
this
);
board
[
0
][
6
]
=
new
Knight
(
0
,
6
,
false
,
this
);
board
[
0
][
2
]
=
new
Bishop
(
0
,
2
,
false
,
this
);
board
[
0
][
5
]
=
new
Bishop
(
0
,
5
,
false
,
this
);
board
[
0
][
3
]
=
new
King
(
0
,
3
,
false
,
this
);
board
[
0
][
4
]
=
new
Queen
(
0
,
4
,
false
,
this
);
board
[
7
][
0
]
=
new
Rook
(
7
,
0
,
false
,
this
);
board
[
1
][
0
]
=
new
Knight
(
1
,
0
,
false
,
this
);
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
);
board
[
4
][
0
]
=
new
Queen
(
4
,
0
,
false
,
this
);
for
(
int
i
=
0
;
i
<
8
;
i
++){
board
[
1
][
i
]
=
new
Pawn
(
1
,
i
,
false
,
this
);
board
[
6
][
i
]
=
new
Pawn
(
6
,
i
,
true
,
this
);
board
[
i
][
1
]
=
new
Pawn
(
i
,
1
,
false
,
this
);
board
[
i
][
6
]
=
new
Pawn
(
i
,
6
,
true
,
this
);
}
board
[
7
][
0
]
=
new
Rook
(
7
,
0
,
true
,
this
);
board
[
0
][
7
]
=
new
Rook
(
0
,
7
,
true
,
this
);
board
[
7
][
7
]
=
new
Rook
(
7
,
7
,
true
,
this
);
board
[
7
][
1
]
=
new
Knight
(
7
,
1
,
true
,
this
);
board
[
7
][
6
]
=
new
Knight
(
7
,
6
,
true
,
this
);
board
[
7
][
2
]
=
new
Bishop
(
7
,
2
,
true
,
this
);
board
[
7
][
5
]
=
new
Bishop
(
7
,
5
,
true
,
this
);
board
[
7
][
3
]
=
new
Queen
(
7
,
3
,
true
,
this
);
board
[
7
][
4
]
=
new
King
(
7
,
4
,
true
,
this
);
board
[
1
][
7
]
=
new
Knight
(
1
,
7
,
true
,
this
);
board
[
6
][
7
]
=
new
Knight
(
6
,
7
,
true
,
this
);
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
);
}
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);
// System.out.println(board[x][y]);
if
(
board
[
x
][
y
].
isValidMove
(
destX
,
destY
)){
// System.out.println("Reach");
board
[
x
][
y
].
setX
(
destX
);
board
[
x
][
y
].
setY
(
destY
);
board
[
destX
][
destY
]
=
board
[
x
][
y
]
;
board
[
destX
][
destY
]
=
getCopy
(
board
[
x
][
y
])
;
board
[
x
][
y
]
=
null
;
return
true
;
}
else
return
false
;
}
public
ArrayList
<
int
[]>
getMoves
(
int
x
,
int
y
){
if
(
board
[
x
][
y
]==
null
)
return
new
ArrayList
<>();
return
board
[
x
][
y
].
moves
();
}
public
Piece
getCopy
(
Piece
p
){
if
(
p
instanceof
Bishop
){
Bishop
ret
=
new
Bishop
(
p
.
getX
(),
p
.
getY
(),
p
.
isWhite
(),
p
.
getBoard
());
return
ret
;
}
else
if
(
p
instanceof
King
){
King
ret
=
new
King
(
p
.
getX
(),
p
.
getY
(),
p
.
isWhite
(),
p
.
getBoard
());
return
ret
;
}
else
if
(
p
instanceof
Knight
){
Knight
ret
=
new
Knight
(
p
.
getX
(),
p
.
getY
(),
p
.
isWhite
(),
p
.
getBoard
());
return
ret
;
}
else
if
(
p
instanceof
Pawn
){
Pawn
ret
=
new
Pawn
(
p
.
getX
(),
p
.
getY
(),
p
.
isWhite
(),
p
.
getBoard
());
return
ret
;
}
else
if
(
p
instanceof
Queen
){
Queen
ret
=
new
Queen
(
p
.
getX
(),
p
.
getY
(),
p
.
isWhite
(),
p
.
getBoard
());
return
ret
;
}
else
{
Rook
ret
=
new
Rook
(
p
.
getX
(),
p
.
getY
(),
p
.
isWhite
(),
p
.
getBoard
());
return
ret
;
}
}
public
Piece
[][]
getBoard
()
{
return
board
;
}
...
...
@@ -59,16 +92,16 @@ public class Board {
@Override
public
String
toString
()
{
String
str
=
""
;
for
(
int
i
=
0
;
i
<
8
;
i
++){
for
(
int
j
=
0
;
j
<
7
;
j
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
for
(
int
i
=
0
;
i
<
7
;
i
++){
if
(
board
[
i
][
j
]!=
null
)
{
str
=
str
+
board
[
i
][
j
]
+
" "
;
}
else
str
=
str
+
"0 "
;
}
if
(
board
[
i
][
7
]!=
null
)
{
str
=
str
+
board
[
i
][
7
]
+
"\n"
;
if
(
board
[
7
][
j
]!=
null
)
{
str
=
str
+
board
[
7
][
j
]
+
"\n"
;
}
else
str
=
str
+
"0\n"
;
...
...
src/Main/Main.java
View file @
610acdb4
package
Main
;
import
java.util.ArrayList
;
import
java.util.Scanner
;
public
class
Main
{
...
...
@@ -7,5 +8,72 @@ public class Main {
Scanner
in
=
new
Scanner
(
System
.
in
);
Board
b
=
new
Board
();
System
.
out
.
println
(
b
);
// if(b.getMove(1,1,1,3)){
// System.out.println("Okey");
// }
// System.out.println(b);
boolean
whichMove
=
true
;
while
(
true
){
if
(
whichMove
)
{
System
.
out
.
println
(
"White Turn.... insert a move in a line"
);
String
str
=
in
.
nextLine
();
String
splt
[]
=
str
.
split
(
" "
);
if
(
splt
.
length
==
1
)
{
int
dx
=
splt
[
0
].
charAt
(
0
)
-
'a'
;
int
dy
=
splt
[
0
].
charAt
(
1
)
-
'1'
;
ArrayList
<
int
[]>
moves
=
b
.
getMoves
(
dx
,
dy
);
printMoves
(
moves
);
}
else
if
(
splt
.
length
==
2
)
{
int
x
=
splt
[
0
].
charAt
(
0
)
-
'a'
;
int
y
=
splt
[
0
].
charAt
(
1
)
-
'1'
;
int
dx
=
splt
[
1
].
charAt
(
0
)
-
'a'
;
int
dy
=
splt
[
1
].
charAt
(
1
)
-
'1'
;
if
(
b
.
getColor
(
x
,
y
)!=
1
)
{
System
.
out
.
println
(
"Incorrect Move Color, Please insert Correct Movement"
);
continue
;
}
if
(
b
.
getMove
(
x
,
y
,
dx
,
dy
))
{
System
.
out
.
println
(
"Moved"
);
whichMove
=
false
;
}
else
{
System
.
out
.
println
(
"Incorrect Move, Please insert Correct Movement"
);
}
}
}
System
.
out
.
println
(
b
);
//--------------------------------------------------------------------------
if
(
whichMove
)
continue
;
System
.
out
.
println
(
"Black Turn.... insert a move in a line"
);
String
str
=
in
.
nextLine
();
String
splt
[]
=
str
.
split
(
" "
);
if
(
splt
.
length
==
1
){
int
dx
=
splt
[
0
].
charAt
(
0
)-
'a'
;
int
dy
=
splt
[
0
].
charAt
(
1
)-
'1'
;
ArrayList
<
int
[]>
moves
=
b
.
getMoves
(
dx
,
dy
);
printMoves
(
moves
);
}
else
if
(
splt
.
length
==
2
){
int
x
=
splt
[
0
].
charAt
(
0
)-
'a'
;
int
y
=
splt
[
0
].
charAt
(
1
)-
'1'
;
int
dx
=
splt
[
1
].
charAt
(
0
)-
'a'
;
int
dy
=
splt
[
1
].
charAt
(
1
)-
'1'
;
if
(
b
.
getColor
(
x
,
y
)!=
2
)
{
System
.
out
.
println
(
"Incorrect Move Color, Please insert Correct Movement"
);
continue
;
}
if
(
b
.
getMove
(
x
,
y
,
dx
,
dy
)){
System
.
out
.
println
(
"Moved"
);
whichMove
=
true
;
}
else
{
System
.
out
.
println
(
"Incorrect Move, Please insert Correct Movement"
);
}
}
System
.
out
.
println
(
b
);
}
}
public
static
void
printMoves
(
ArrayList
<
int
[]>
moves
){
for
(
int
m
[]
:
moves
){
System
.
out
.
print
((
char
)(
m
[
0
]+
'a'
)+
""
+(
m
[
1
]+
1
)+
"\t"
);
}
System
.
out
.
println
();
}
}
src/Pieces/Bishop.java
View file @
610acdb4
...
...
@@ -21,13 +21,20 @@ public class Bishop extends Piece {
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
if
(!
isCanMove
(
dx
,
dy
))
return
false
;
for
(
int
i
=
Math
.
min
(
dx
,
getX
());
i
<
Math
.
max
(
dx
,
getX
());
i
++){
for
(
int
j
=
Math
.
min
(
dy
,
getY
());
i
<
Math
.
max
(
dy
,
getY
());
j
++){
if
(
getBoard
().
getBoard
()[
i
][
j
]!=
null
)
return
false
;
// if(!isCanMove(dx, dy)) return false;
// for(int i = Math.min(dx,getX());i< Math.max(dx,getX());i++){
// for(int j =Math.min(dy,getY());i < Math.max(dy,getY());j++){
// if(getBoard().getBoard()[i][j]!=null) return false;
// }
// }
ArrayList
<
int
[]>
moves
=
moves
();
for
(
int
m
[]
:
moves
){
if
(
m
[
0
]==
dx
&&
m
[
1
]
==
dy
){
return
true
;
// break;
}
}
return
tru
e
;
return
fals
e
;
}
@Override
...
...
src/Pieces/King.java
View file @
610acdb4
...
...
@@ -24,13 +24,20 @@ public class King extends Piece {
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
if
(!
isCanMove
(
dx
,
dy
))
return
false
;
for
(
int
i
=
Math
.
min
(
dx
,
getX
());
i
<
Math
.
max
(
dx
,
getX
());
i
++){
for
(
int
j
=
Math
.
min
(
dy
,
getY
());
i
<
Math
.
max
(
dy
,
getY
());
j
++){
if
(
getBoard
().
getBoard
()[
i
][
j
]!=
null
)
return
false
;
// if(!isCanMove(dx, dy)) return false;
// for(int i = Math.min(dx,getX());i< Math.max(dx,getX());i++){
// for(int j =Math.min(dy,getY());i < Math.max(dy,getY());j++){
// if(getBoard().getBoard()[i][j]!=null) return false;
// }
// }
ArrayList
<
int
[]>
moves
=
moves
();
for
(
int
m
[]
:
moves
){
if
(
m
[
0
]==
dx
&&
m
[
1
]
==
dy
){
return
true
;
// break;
}
}
return
tru
e
;
return
fals
e
;
}
@Override
...
...
src/Pieces/Knight.java
View file @
610acdb4
...
...
@@ -23,7 +23,14 @@ public class Knight extends Piece {
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
return
isCanMove
(
dx
,
dy
);
ArrayList
<
int
[]>
moves
=
moves
();
for
(
int
m
[]
:
moves
){
if
(
m
[
0
]==
dx
&&
m
[
1
]
==
dy
){
return
true
;
// break;
}
}
return
false
;
}
@Override
...
...
src/Pieces/Pawn.java
View file @
610acdb4
...
...
@@ -19,7 +19,14 @@ public class Pawn extends Piece {
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
return
isCanMove
(
dx
,
dy
);
ArrayList
<
int
[]>
moves
=
moves
();
for
(
int
m
[]
:
moves
){
if
(
m
[
0
]==
dx
&&
m
[
1
]
==
dy
){
return
true
;
// break;
}
}
return
false
;
}
@Override
...
...
@@ -29,38 +36,38 @@ public class Pawn extends Piece {
int
y
=
getY
();
if
(
isWhite
())
{
if
(
x
==
0
)
if
(
y
==
0
)
return
moves
;
if
(
getBoard
().
isEmpty
(
x
-
1
,
y
))
if
(
getBoard
().
isEmpty
(
x
,
y
-
1
))
{
moves
.
add
(
new
int
[]{
x
-
1
,
y
});
if
(
x
==
6
)
moves
.
add
(
new
int
[]{
x
,
y
-
1
});
if
(
y
==
6
)
{
if
(
getBoard
().
isEmpty
(
4
,
y
))
moves
.
add
(
new
int
[]{
4
,
y
});
if
(
getBoard
().
isEmpty
(
x
,
4
))
moves
.
add
(
new
int
[]{
x
,
4
});
}
}
if
((
y
>
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
((
y
<
7
)&&((
getBoard
().
getColor
(
x
-
1
,
y
+
1
)==
2
&&
isWhite
()
||
getBoard
().
getColor
(
x
-
1
,
y
+
1
)==
1
&&
isBlack
())))
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
{
if
(
x
==
8
)
if
(
y
==
8
)
return
moves
;
if
(
getBoard
().
isEmpty
(
x
+
1
,
y
))
if
(
getBoard
().
isEmpty
(
x
,
y
+
1
))
{
moves
.
add
(
new
int
[]{
x
+
1
,
y
});
if
(
x
==
1
)
moves
.
add
(
new
int
[]{
x
,
y
+
1
});
if
(
y
==
1
)
{
if
(
getBoard
().
isEmpty
(
3
,
y
))
moves
.
add
(
new
int
[]{
3
,
y
});
if
(
getBoard
().
isEmpty
(
x
,
3
))
moves
.
add
(
new
int
[]{
x
,
3
});
}
}
if
((
y
>
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
((
y
<
7
)&&((
getBoard
().
getColor
(
x
+
1
,
y
+
1
)==
2
&&
isWhite
()
||
getBoard
().
getColor
(
x
+
1
,
y
+
1
)==
1
&&
isBlack
())))
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 @
610acdb4
...
...
@@ -4,7 +4,7 @@ import Main.Board;
import
java.util.ArrayList
;
public
abstract
class
Piece
{
public
abstract
class
Piece
implements
Cloneable
{
private
int
x
;
private
boolean
isWhite
;
private
int
y
;
...
...
@@ -48,4 +48,13 @@ public abstract class Piece {
return
false
;
return
true
;
}
public
Object
clone
(){
try
{
return
super
.
clone
();
}
catch
(
CloneNotSupportedException
e
)
{
System
.
err
.
println
(
"Clone Error"
);
e
.
printStackTrace
();
}
return
null
;
}
}
src/Pieces/Queen.java
View file @
610acdb4
...
...
@@ -26,13 +26,20 @@ public class Queen extends Piece {
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
if
(!
isCanMove
(
dx
,
dy
))
return
false
;
for
(
int
i
=
Math
.
min
(
dx
,
getX
());
i
<
Math
.
max
(
dx
,
getX
());
i
++){
for
(
int
j
=
Math
.
min
(
dy
,
getY
());
i
<
Math
.
max
(
dy
,
getY
());
j
++){
if
(
getBoard
().
getBoard
()[
i
][
j
]!=
null
)
return
false
;
// if(!isCanMove(dx, dy))return false;
// for(int i = Math.min(dx,getX());i< Math.max(dx,getX());i++){
// for(int j =Math.min(dy,getY());i < Math.max(dy,getY());j++){
// if(getBoard().getBoard()[i][j]!=null) return false;
// }
// }
ArrayList
<
int
[]>
moves
=
moves
();
for
(
int
m
[]
:
moves
){
if
(
m
[
0
]==
dx
&&
m
[
1
]
==
dy
){
return
true
;
// break;
}
}
return
tru
e
;
return
fals
e
;
}
@Override
...
...
src/Pieces/Rook.java
View file @
610acdb4
...
...
@@ -22,13 +22,21 @@ public class Rook extends Piece {
@Override
public
boolean
isValidMove
(
int
dx
,
int
dy
)
{
if
(!
isCanMove
(
dx
,
dy
))
return
false
;
for
(
int
i
=
Math
.
min
(
dx
,
getX
());
i
<
Math
.
max
(
dx
,
getX
());
i
++){
for
(
int
j
=
Math
.
min
(
dy
,
getY
());
i
<
Math
.
max
(
dy
,
getY
());
j
++){
if
(
getBoard
().
getBoard
()[
i
][
j
]!=
null
)
return
false
;
// if(!isCanMove(dx, dy))return false;
// for(int i = Math.min(dx,getX());i< Math.max(dx,getX());i++){
// for(int j =Math.min(dy,getY());i < Math.max(dy,getY());j++){
// if(getBoard().getBoard()[i][j]!=null) return false;
// }
// }
// return true;
ArrayList
<
int
[]>
moves
=
moves
();
for
(
int
m
[]
:
moves
){
if
(
m
[
0
]==
dx
&&
m
[
1
]
==
dy
){
return
true
;
// break;
}
}
return
tru
e
;
return
fals
e
;
}
@Override
...
...
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