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
c7036982
Commit
c7036982
authored
5 years ago
by
kimia
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
logic
parent
62c3ea26
master
No related merge requests found
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
175 additions
and
113 deletions
+175
-113
Bishop.java
src/Bishop.java
+36
-7
Cell.java
src/Cell.java
+2
-1
King.java
src/King.java
+2
-3
Knight.java
src/Knight.java
+4
-2
Main.java
src/Main.java
+11
-8
Pawn.java
src/Pawn.java
+42
-43
Piece.java
src/Piece.java
+29
-31
Queen.java
src/Queen.java
+14
-12
Rook.java
src/Rook.java
+35
-6
No files found.
src/Bishop.java
View file @
c7036982
public
class
Bishop
extends
Piece
{
public
class
Bishop
extends
Piece
{
public
Bishop
(
Cell
cell
,
Color
color
)
{
public
Bishop
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
super
(
cell
,
color
)
;
}
}
@Override
@Override
public
boolean
isValidMove
(
Cell
c
)
{
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][]
)
{
return
(
(
if
(
(
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())
==
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
()))
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())
==
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
()))
)
)
&&
c
.
isEmpty
()
)
{
&&
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
][
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
;
}
}
return
true
;
}
else
{
return
false
;
}
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/Cell.java
View file @
c7036982
public
class
Cell
{
public
class
Cell
{
private
int
row
,
col
;
private
int
row
,
col
;
private
boolean
empty
;
private
boolean
empty
;
...
@@ -36,4 +37,4 @@ public class Cell {
...
@@ -36,4 +37,4 @@ public class Cell {
enum
Color
{
enum
Color
{
BLACK
,
WHITE
BLACK
,
WHITE
}
;
}
This diff is collapsed.
Click to expand it.
src/King.java
View file @
c7036982
public
class
King
extends
Piece
{
public
class
King
extends
Piece
{
public
King
(
Cell
cell
,
Color
color
)
{
public
King
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
super
(
cell
,
color
)
;
}
}
@Override
@Override
public
boolean
isValidMove
(
Cell
c
)
{
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][]
)
{
return
(
(
return
(
(
(
this
.
getCell
().
getCol
()
==
c
.
getCol
()
&&
Math
.
abs
(
this
.
getCell
().
getRow
()
-
c
.
getRow
())
==
1
)
||
(
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
)
||
(
this
.
getCell
().
getRow
()
==
c
.
getRow
()
&&
Math
.
abs
(
this
.
getCell
().
getCol
()
-
c
.
getCol
())
==
1
)
||
...
...
This diff is collapsed.
Click to expand it.
src/Knight.java
View file @
c7036982
public
class
Knight
extends
Piece
{
public
class
Knight
extends
Piece
{
public
Knight
(
Cell
cell
,
Color
color
)
{
public
Knight
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
super
(
cell
,
color
)
;
}
}
@Override
@Override
public
boolean
isValidMove
(
Cell
c
)
{
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][]
)
{
return
(
(
return
(
(
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())
+
Math
.
abs
(
this
.
getCell
().
getRow
()
-
c
.
getRow
())
==
3
)
&&
(
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
)
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())
!=
0
&&
Math
.
abs
(
this
.
getCell
().
getRow
()
-
c
.
getRow
())
!=
0
)
...
@@ -14,3 +15,4 @@ public class Knight extends Piece {
...
@@ -14,3 +15,4 @@ public class Knight extends Piece {
)
;
)
;
}
}
}
}
This diff is collapsed.
Click to expand it.
src/Main.java
View file @
c7036982
import
java.util.Scanner
;
import
java.util.Scanner
;
public
class
Main
{
public
class
Main
{
private
static
char
boardChar
[][]
=
new
char
[
8
][
8
]
;
private
static
char
boardChar
[][]
=
new
char
[
8
][
8
]
;
private
static
void
printBoard
()
{
private
static
void
printBoard
()
{
...
@@ -80,7 +81,7 @@ public class Main{
...
@@ -80,7 +81,7 @@ public class Main{
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
{
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
{
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
for
(
int
j
=
0
;
j
<
8
;
++
j
)
{
for
(
int
j
=
0
;
j
<
8
;
++
j
)
{
if
(
p
.
isValidMove
(
board
[
i
][
j
]))
{
if
(
p
.
isValidMove
(
board
[
i
][
j
]
,
board
))
{
System
.
out
.
print
(
(
char
)((
int
)
'a'
+
i
)
)
;
System
.
out
.
print
(
(
char
)((
int
)
'a'
+
i
)
)
;
System
.
out
.
println
(
j
+
1
)
;
System
.
out
.
println
(
j
+
1
)
;
}
}
...
@@ -94,7 +95,7 @@ public class Main{
...
@@ -94,7 +95,7 @@ public class Main{
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
{
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
{
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
for
(
int
j
=
0
;
j
<
8
;
++
j
)
{
for
(
int
j
=
0
;
j
<
8
;
++
j
)
{
if
(
p
.
isValidMove
(
board
[
i
][
j
]))
{
if
(
p
.
isValidMove
(
board
[
i
][
j
]
,
board
))
{
System
.
out
.
print
(
(
char
)((
int
)
'a'
+
i
)
)
;
System
.
out
.
print
(
(
char
)((
int
)
'a'
+
i
)
)
;
System
.
out
.
println
(
j
+
1
)
;
System
.
out
.
println
(
j
+
1
)
;
}
}
...
@@ -108,12 +109,13 @@ public class Main{
...
@@ -108,12 +109,13 @@ public class Main{
for
(
Piece
p:
pw
)
{
for
(
Piece
p:
pw
)
{
if
(
p
.
getCell
().
getRow
()==
s
.
charAt
(
0
)-
'a'
&&
if
(
p
.
getCell
().
getRow
()==
s
.
charAt
(
0
)-
'a'
&&
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
{
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
{
if
(
p
.
isValidMove
(
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)))
{
if
(
p
.
isValidMove
(
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)
,
board
))
{
Cell
tmp
=
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)
;
Cell
tmp
=
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)
;
tmp
.
setEmpty
(
fals
e
)
;
board
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()].
setEmpty
(
tru
e
)
;
p
.
setCell
(
tmp
)
;
p
.
setCell
(
tmp
)
;
board
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()].
setEmpty
(
false
)
;
if
(
p
instanceof
Pawn
)
{
if
(
p
instanceof
Pawn
)
{
Pawn
myPawn
=
(
Pawn
)
p
;
Pawn
myPawn
=
(
Pawn
)
p
;
myPawn
.
setOnce
(
false
)
;
myPawn
.
setOnce
(
false
)
;
}
}
for
(
Piece
blackPiece:
pb
)
{
for
(
Piece
blackPiece:
pb
)
{
...
@@ -127,10 +129,11 @@ public class Main{
...
@@ -127,10 +129,11 @@ public class Main{
for
(
Piece
p:
pb
)
{
for
(
Piece
p:
pb
)
{
if
(
p
.
getCell
().
getRow
()==
s
.
charAt
(
0
)-
'a'
&&
if
(
p
.
getCell
().
getRow
()==
s
.
charAt
(
0
)-
'a'
&&
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
if
(
p
.
isValidMove
(
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)
)
)
{
if
(
p
.
isValidMove
(
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)
,
board
)
)
{
Cell
tmp
=
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)
;
Cell
tmp
=
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)
;
tmp
.
setEmpty
(
fals
e
)
;
board
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()].
setEmpty
(
tru
e
)
;
p
.
setCell
(
tmp
)
;
p
.
setCell
(
tmp
)
;
board
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()].
setEmpty
(
false
)
;
if
(
p
instanceof
Pawn
)
{
if
(
p
instanceof
Pawn
)
{
Pawn
myPawn
=
(
Pawn
)
p
;
Pawn
myPawn
=
(
Pawn
)
p
;
myPawn
.
setOnce
(
false
)
;
myPawn
.
setOnce
(
false
)
;
...
@@ -153,4 +156,4 @@ public class Main{
...
@@ -153,4 +156,4 @@ public class Main{
}
}
\ No newline at end of file
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/Pawn.java
View file @
c7036982
public
class
Pawn
extends
Piece
{
public
class
Pawn
extends
Piece
{
boolean
once
,
end
;
boolean
once
,
end
;
public
Pawn
(
Cell
cell
,
Color
color
)
{
public
Pawn
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
super
(
cell
,
color
)
;
this
.
once
=
true
;
this
.
once
=
true
;
this
.
end
=
false
;
this
.
end
=
false
;
}
}
public
void
setOnce
(
boolean
once
)
{
public
void
setOnce
(
boolean
once
)
{
this
.
once
=
once
;
this
.
once
=
once
;
}
}
public
void
setEnd
(
boolean
end
)
{
public
void
setEnd
(
boolean
end
)
{
this
.
end
=
end
;
this
.
end
=
end
;
}
}
@Override
@Override
public
boolean
isValidMove
(
Cell
c
)
{
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][]
)
{
if
(
super
.
getColor
()==
Color
.
BLACK
&&
super
.
getCell
().
getRow
()==
0
)
end
=
true
;
if
(
super
.
getColor
()==
Color
.
BLACK
&&
super
.
getCell
().
getRow
()==
0
)
end
=
true
;
if
(
super
.
getColor
()==
Color
.
WHITE
&&
super
.
getCell
().
getRow
()==
7
)
end
=
true
;
if
(
super
.
getColor
()==
Color
.
WHITE
&&
super
.
getCell
().
getRow
()==
7
)
end
=
true
;
...
@@ -54,5 +54,4 @@ public class Pawn extends Piece {
...
@@ -54,5 +54,4 @@ public class Pawn extends Piece {
}
}
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
src/Piece.java
View file @
c7036982
...
@@ -11,7 +11,7 @@ public abstract class Piece {
...
@@ -11,7 +11,7 @@ public abstract class Piece {
this
.
deleted
=
false
;
this
.
deleted
=
false
;
}
}
public
abstract
boolean
isValidMove
(
Cell
cell
)
;
public
abstract
boolean
isValidMove
(
Cell
cell
,
Cell
board
[][]
)
;
public
Cell
getCell
()
{
public
Cell
getCell
()
{
return
cell
;
return
cell
;
...
@@ -37,6 +37,4 @@ public abstract class Piece {
...
@@ -37,6 +37,4 @@ public abstract class Piece {
this
.
deleted
=
deleted
;
this
.
deleted
=
deleted
;
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/Queen.java
View file @
c7036982
public
class
Queen
extends
Piece
{
public
class
Queen
extends
Piece
{
public
Queen
(
Cell
cell
,
Color
color
)
{
public
Queen
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
super
(
cell
,
color
)
;
}
}
@Override
@Override
public
boolean
isValidMove
(
Cell
c
)
{
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][]
)
{
return
(
(
return
(
(
(
this
.
getCell
().
getCol
()
==
c
.
getCol
())
||
(
this
.
getCell
().
getRow
()
==
c
.
getRow
()
)
||
(
this
.
getCell
().
getCol
()
==
c
.
getCol
())
||
(
this
.
getCell
().
getRow
()
==
c
.
getRow
()
)
||
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())
==
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
()))
(
Math
.
abs
(
this
.
getCell
().
getCol
()
-
c
.
getCol
())
==
Math
.
abs
(
this
.
getCell
().
getRow
()
-
c
.
getRow
()))
)
)
&&
c
.
isEmpty
()
&&
c
.
isEmpty
()
)
;
);
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/Rook.java
View file @
c7036982
...
@@ -6,11 +6,40 @@ public class Rook extends Piece {
...
@@ -6,11 +6,40 @@ public class Rook extends Piece {
}
}
@Override
@Override
public
boolean
isValidMove
(
Cell
c
)
{
public
boolean
isValidMove
(
Cell
c
,
Cell
board
[][]
)
{
return
(
(
if
(
(
(
this
.
getCell
().
getCol
()
==
c
.
getCol
())
||
(
this
.
getCell
().
getRow
()
==
c
.
getRow
())
(
this
.
getCell
().
getCol
()
==
c
.
getCol
())
||
(
this
.
getCell
().
getRow
()
==
c
.
getRow
())
)
)
&&
c
.
isEmpty
()
&&
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
;
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
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