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
62c3ea26
Commit
62c3ea26
authored
May 13, 2019
by
kimia
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
project done
parent
677730f1
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
415 additions
and
23 deletions
+415
-23
Bishop.java
src/Bishop.java
+14
-2
Cell.java
src/Cell.java
+37
-2
King.java
src/King.java
+15
-2
Knight.java
src/Knight.java
+16
-2
Main.java
src/Main.java
+155
-3
Pawn.java
src/Pawn.java
+57
-1
Piece.java
src/Piece.java
+41
-3
Player.java
src/Player.java
+50
-3
Queen.java
src/Queen.java
+16
-3
Rook.java
src/Rook.java
+14
-2
No files found.
src/Bishop.java
View file @
62c3ea26
package
PACKAGE_NAME
;
public
class
Bishop
{
public
class
Bishop
extends
Piece
{
public
Bishop
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
}
@Override
public
boolean
isValidMove
(
Cell
c
)
{
return
(
(
(
Math
.
abs
(
this
.
getCell
().
getCol
()-
c
.
getCol
())
==
Math
.
abs
(
this
.
getCell
().
getRow
()-
c
.
getRow
()))
)
&&
c
.
isEmpty
()
)
;
}
}
}
src/Cell.java
View file @
62c3ea26
package
PACKAGE_NAME
;
public
class
Cell
{
public
class
Cell
{
private
int
row
,
col
;
private
boolean
empty
;
public
Cell
(
int
row
,
int
col
)
{
this
.
row
=
row
;
this
.
col
=
col
;
this
.
empty
=
true
;
}
public
int
getRow
()
{
return
row
;
}
public
int
getCol
()
{
return
col
;
}
public
boolean
isEmpty
()
{
return
empty
;
}
public
void
setRow
(
int
row
)
{
this
.
row
=
row
;
}
public
void
setCol
(
int
col
)
{
this
.
col
=
col
;
}
public
void
setEmpty
(
boolean
empty
)
{
this
.
empty
=
empty
;
}
}
}
enum
Color
{
BLACK
,
WHITE
}
;
src/King.java
View file @
62c3ea26
p
ackage
PACKAGE_NAME
;
p
ublic
class
King
extends
Piece
{
public
class
King
{
public
King
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
}
@Override
public
boolean
isValidMove
(
Cell
c
)
{
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
()
)
;
}
}
}
src/Knight.java
View file @
62c3ea26
public
class
Horse
{
public
class
Knight
extends
Piece
{
}
public
Knight
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
}
@Override
public
boolean
isValidMove
(
Cell
c
)
{
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
()
)
;
}
}
\ No newline at end of file
src/Main.java
View file @
62c3ea26
package
PACKAGE_NAME
;
import
java.util.Scanner
;
public
class
Main
{
public
class
Main
{
}
private
static
char
boardChar
[][]
=
new
char
[
8
][
8
]
;
private
static
void
printBoard
()
{
for
(
int
i
=
1
;
i
<
9
;
++
i
)
{
System
.
out
.
print
(
" "
+
i
)
;
}
System
.
out
.
print
(
"\n "
)
;
for
(
int
i
=
0
;
i
<
8
;
++
i
)
System
.
out
.
print
(
"+---"
)
;
System
.
out
.
print
(
"+\n"
)
;
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
System
.
out
.
print
(
(
char
)(
i
+
'a'
)
)
;
for
(
int
j
=
0
;
j
<
8
;
++
j
)
{
System
.
out
.
print
(
"| "
+
boardChar
[
i
][
j
]
+
" "
)
;
}
System
.
out
.
print
(
"|\n "
)
;
for
(
int
i2
=
0
;
i2
<
8
;
++
i2
)
System
.
out
.
print
(
"+---"
)
;
System
.
out
.
print
(
"+\n"
)
;
}
}
private
static
void
updateBoard
(
Piece
pw
[],
Piece
pb
[])
{
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
for
(
int
j
=
0
;
j
<
8
;
++
j
)
{
boardChar
[
i
][
j
]
=
' '
;
}
}
for
(
Piece
p:
pw
)
{
if
(
p
.
isDeleted
())
continue
;
if
(
p
instanceof
Pawn
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'P'
;
if
(
p
instanceof
Bishop
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'B'
;
if
(
p
instanceof
Queen
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'Q'
;
if
(
p
instanceof
King
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'$'
;
if
(
p
instanceof
Rook
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'R'
;
if
(
p
instanceof
Knight
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'K'
;
}
for
(
Piece
p:
pb
)
{
if
(
p
.
isDeleted
())
continue
;
if
(
p
instanceof
Pawn
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'P'
;
if
(
p
instanceof
Bishop
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'B'
;
if
(
p
instanceof
Queen
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'Q'
;
if
(
p
instanceof
King
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'$'
;
if
(
p
instanceof
Rook
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'R'
;
if
(
p
instanceof
Knight
)
boardChar
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
'K'
;
}
}
public
static
void
main
(
String
[]
args
)
{
Scanner
in
=
new
Scanner
(
System
.
in
)
;
Player
white
=
new
Player
(
Color
.
WHITE
)
;
Player
black
=
new
Player
(
Color
.
BLACK
)
;
Piece
pw
[]
=
white
.
getPieces
()
;
Piece
pb
[]
=
black
.
getPieces
()
;
Cell
board
[][]
=
new
Cell
[
8
][
8
]
;
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
for
(
int
j
=
0
;
j
<
8
;
++
j
)
{
board
[
i
][
j
]
=
new
Cell
(
i
,
j
)
;
}
}
for
(
Piece
p:
pw
)
{
board
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
p
.
getCell
()
;
}
for
(
Piece
p:
pb
)
{
board
[
p
.
getCell
().
getRow
()][
p
.
getCell
().
getCol
()]
=
p
.
getCell
()
;
}
int
wORb
=
0
;
updateBoard
(
pw
,
pb
)
;
printBoard
()
;
while
(
true
)
{
wORb
++
;
String
s
=
in
.
nextLine
()
;
if
(
s
.
length
()==
2
)
{
for
(
Piece
p:
pw
)
{
if
(
p
.
getCell
().
getRow
()==
s
.
charAt
(
0
)-
'a'
&&
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
{
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
for
(
int
j
=
0
;
j
<
8
;
++
j
)
{
if
(
p
.
isValidMove
(
board
[
i
][
j
]))
{
System
.
out
.
print
(
(
char
)((
int
)
'a'
+
i
)
)
;
System
.
out
.
println
(
j
+
1
)
;
}
}
}
}
}
for
(
Piece
p:
pb
)
{
if
(
p
.
getCell
().
getRow
()==
s
.
charAt
(
0
)-
'a'
&&
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
{
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
for
(
int
j
=
0
;
j
<
8
;
++
j
)
{
if
(
p
.
isValidMove
(
board
[
i
][
j
]))
{
System
.
out
.
print
(
(
char
)((
int
)
'a'
+
i
)
)
;
System
.
out
.
println
(
j
+
1
)
;
}
}
}
}
}
}
else
if
(
s
.
length
()==
5
)
{
for
(
Piece
p:
pw
)
{
if
(
p
.
getCell
().
getRow
()==
s
.
charAt
(
0
)-
'a'
&&
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
{
if
(
p
.
isValidMove
(
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)))
{
Cell
tmp
=
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)
;
tmp
.
setEmpty
(
false
)
;
p
.
setCell
(
tmp
)
;
if
(
p
instanceof
Pawn
)
{
Pawn
myPawn
=
(
Pawn
)
p
;
myPawn
.
setOnce
(
false
)
;
}
for
(
Piece
blackPiece:
pb
)
{
if
(
blackPiece
.
getCell
().
getRow
()==
tmp
.
getRow
()
&&
blackPiece
.
getCell
().
getCol
()==
tmp
.
getCol
())
blackPiece
.
setDeleted
(
true
)
;
}
}
}
}
for
(
Piece
p:
pb
)
{
if
(
p
.
getCell
().
getRow
()==
s
.
charAt
(
0
)-
'a'
&&
p
.
getCell
().
getCol
()+
1
==
s
.
charAt
(
1
)-
'0'
)
if
(
p
.
isValidMove
(
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)))
{
Cell
tmp
=
new
Cell
(
s
.
charAt
(
3
)-
'a'
,
s
.
charAt
(
4
)-
'1'
)
;
tmp
.
setEmpty
(
false
)
;
p
.
setCell
(
tmp
)
;
if
(
p
instanceof
Pawn
)
{
Pawn
myPawn
=
(
Pawn
)
p
;
myPawn
.
setOnce
(
false
)
;
}
for
(
Piece
whitePiece:
pw
)
{
if
(
whitePiece
.
getCell
().
getRow
()==
tmp
.
getRow
()
&&
whitePiece
.
getCell
().
getCol
()==
tmp
.
getCol
())
whitePiece
.
setDeleted
(
true
)
;
}
}
}
updateBoard
(
pw
,
pb
)
;
printBoard
()
;
}
else
{
System
.
out
.
println
(
"Please enter a valid input!"
)
;
}
}
}
}
\ No newline at end of file
src/Pawn.java
View file @
62c3ea26
public
class
Soldier
{
public
class
Pawn
extends
Piece
{
boolean
once
,
end
;
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
setEnd
(
boolean
end
)
{
this
.
end
=
end
;
}
@Override
public
boolean
isValidMove
(
Cell
c
)
{
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
;
}
}
}
}
}
src/Piece.java
View file @
62c3ea26
package
PACKAGE_NAME
;
public
abstract
class
Piece
{
private
Cell
cell
;
private
Color
color
;
private
boolean
deleted
;
public
Piece
(
Cell
cell
,
Color
color
)
{
this
.
cell
=
cell
;
this
.
cell
.
setEmpty
(
false
)
;
this
.
color
=
color
;
this
.
deleted
=
false
;
}
public
abstract
boolean
isValidMove
(
Cell
cell
)
;
public
Cell
getCell
()
{
return
cell
;
}
public
Color
getColor
()
{
return
color
;
}
public
boolean
isDeleted
()
{
return
deleted
;
}
public
void
setCell
(
Cell
cell
)
{
this
.
cell
=
cell
;
}
public
void
setColor
(
Color
color
)
{
this
.
color
=
color
;
}
public
void
setDeleted
(
boolean
deleted
)
{
this
.
deleted
=
deleted
;
}
}
public
class
Piece
{
}
src/Player.java
View file @
62c3ea26
package
PACKAGE_NAME
;
public
class
Player
{
public
class
Player
{
}
private
Piece
[]
pieces
=
new
Piece
[
16
]
;
private
Color
color
;
public
Piece
[]
getPieces
()
{
return
pieces
;
}
public
Color
getColor
()
{
return
color
;
}
public
void
setPieces
(
Piece
[]
pieces
)
{
this
.
pieces
=
pieces
;
}
public
void
setColor
(
Color
color
)
{
this
.
color
=
color
;
}
public
Player
(
Color
color
)
{
this
.
color
=
color
;
if
(
color
==
Color
.
WHITE
)
{
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
pieces
[
i
]
=
new
Pawn
(
new
Cell
(
1
,
i
),
color
)
;
}
pieces
[
8
]
=
new
Bishop
(
new
Cell
(
0
,
2
),
color
)
;
pieces
[
9
]
=
new
Bishop
(
new
Cell
(
0
,
5
),
color
)
;
pieces
[
10
]
=
new
Knight
(
new
Cell
(
0
,
1
),
color
)
;
pieces
[
11
]
=
new
Knight
(
new
Cell
(
0
,
6
),
color
)
;
pieces
[
12
]
=
new
Rook
(
new
Cell
(
0
,
0
),
color
)
;
pieces
[
13
]
=
new
Rook
(
new
Cell
(
0
,
7
),
color
)
;
pieces
[
14
]
=
new
King
(
new
Cell
(
0
,
4
),
color
)
;
pieces
[
15
]
=
new
Queen
(
new
Cell
(
0
,
3
),
color
)
;
}
else
{
for
(
int
i
=
0
;
i
<
8
;
i
++){
pieces
[
i
]
=
new
Pawn
(
new
Cell
(
6
,
i
),
color
)
;
}
pieces
[
8
]
=
new
Bishop
(
new
Cell
(
7
,
2
),
color
)
;
pieces
[
9
]
=
new
Bishop
(
new
Cell
(
7
,
5
),
color
)
;
pieces
[
10
]
=
new
Knight
(
new
Cell
(
7
,
1
),
color
)
;
pieces
[
11
]
=
new
Knight
(
new
Cell
(
7
,
6
),
color
)
;
pieces
[
12
]
=
new
Rook
(
new
Cell
(
7
,
0
),
color
)
;
pieces
[
13
]
=
new
Rook
(
new
Cell
(
7
,
7
),
color
)
;
pieces
[
14
]
=
new
King
(
new
Cell
(
7
,
4
),
color
)
;
pieces
[
15
]
=
new
Queen
(
new
Cell
(
7
,
3
),
color
)
;
}
}
}
\ No newline at end of file
src/Queen.java
View file @
62c3ea26
package
PACKAGE_NAME
;
public
class
Queen
{
public
class
Queen
extends
Piece
{
}
public
Queen
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
}
@Override
public
boolean
isValidMove
(
Cell
c
)
{
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
()
)
;
}
}
\ No newline at end of file
src/Rook.java
View file @
62c3ea26
package
PACKAGE_NAME
;
public
class
Rook
{
public
class
Rook
extends
Piece
{
public
Rook
(
Cell
cell
,
Color
color
)
{
super
(
cell
,
color
)
;
}
@Override
public
boolean
isValidMove
(
Cell
c
)
{
return
(
(
(
this
.
getCell
().
getCol
()
==
c
.
getCol
())
||
(
this
.
getCell
().
getRow
()
==
c
.
getRow
())
)
&&
c
.
isEmpty
()
)
;
}
}
}
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