Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Jan Rossmeisl
Chess
Commits
ccb70b5e
Commit
ccb70b5e
authored
Nov 26, 2021
by
Jan Rossmeisl
Browse files
implemented castling, en passe missing
parent
89e9d0ab
Changes
10
Hide whitespace changes
Inline
Side-by-side
FieldCurrent.txt
View file @
ccb70b5e
Pawn,blackPawn1,1,0,black
Pawn,blackPawn2,1,1,black
Pawn,blackPawn3,1,2,black
Pawn,blackPawn4,
3
,3,black
Pawn,blackPawn5,
3,3
,black
Pawn,blackPawn4,
2
,3,black
Pawn,blackPawn5,
1,4
,black
Pawn,blackPawn6,1,5,black
Pawn,blackPawn7,1,6,black
Pawn,blackPawn8,1,7,black
Pawn,whitePawn1,6,0,white
Pawn,whitePawn2,6,1,white
Pawn,whitePawn3,6,2,white
Pawn,whitePawn4,
4
,3,white
Pawn,whitePawn5,
2,3
,white
Pawn,whitePawn4,
6
,3,white
Pawn,whitePawn5,
5,4
,white
Pawn,whitePawn6,6,5,white
Pawn,whitePawn7,6,6,white
Pawn,whitePawn8,6,7,white
Rook,blackRook1,0,
0
,black
Rook,blackRook1,0,
3
,black
Rook,blackRook2,0,7,black
Rook,whiteRook1,7,0,white
Rook,whiteRook2,7,7,white
Knight,blackKnight1,
0,1
,black
Knight,blackKnight1,
2,2
,black
Knight,blackKnight2,0,6,black
Knight,whiteKnight1,7,1,white
Knight,whiteKnight2,
6,3
,white
Bishop,blackBishop1,
0,2
,black
Knight,whiteKnight2,
5,5
,white
Bishop,blackBishop1,
3,5
,black
Bishop,blackBishop2,0,5,black
Bishop,whiteBishop1,7,2,white
Bishop,whiteBishop2,
7,5
,white
King,blackKing,0,
4
,black
Bishop,whiteBishop2,
4,2
,white
King,blackKing,0,
2
,black
King,whiteKing,7,4,white
Queen,blackQueen,
0
,3,black
Queen,blackQueen,
1
,3,black
Queen,whiteQueen,7,3,white
src/application/ActionHandler.java
View file @
ccb70b5e
...
...
@@ -16,7 +16,7 @@ public class ActionHandler {
this
.
coordY
=
y
;
if
(
fileHandler
.
loadMove
())
{
// click 2
if
(
e
.
getSource
()
instanceof
ChessPiece
)
{
// check if second field is a -chess piece- or an -empty field-
...
...
@@ -25,11 +25,38 @@ public class ActionHandler {
if
(
chessPiece
.
getColor
().
equals
(
fileHandler
.
getColorMove
()))
{
// check if they have the same color
// yes
fileHandler
.
clearFile
();
deselectPiece
(
fileHandler
.
getCoordX1Move
(),
fileHandler
.
getCoordY1Move
());
if
(
chessPiece
.
checkCastling
(
field
))
{
if
(
chessPiece
instanceof
King
||
chessPiece
instanceof
Rook
)
{
if
(!
chessPiece
.
hasAlreadyMoved
())
{
if
(
field
.
recs
[
x
][
y
]
instanceof
King
)
{
((
King
)
field
.
recs
[
x
][
y
]
).
setAlreadyMoved
(
true
);
}
else
if
(
field
.
recs
[
x
][
y
]
instanceof
Rook
)
{
((
Rook
)
field
.
recs
[
x
][
y
]
).
setAlreadyMoved
(
true
);
}
field
.
changePlayer
();
return
;
}
}
}
else
{
fileHandler
.
clearFile
();
deselectPiece
(
fileHandler
.
getCoordX1Move
(),
fileHandler
.
getCoordY1Move
());
}
// no
}
else
if
(
field
.
movePieceValidated
(
fileHandler
.
getCoordX1Move
(),
fileHandler
.
getCoordY1Move
(),
x
,
y
))
{
if
(
field
.
recs
[
x
][
y
]
instanceof
King
)
{
((
King
)
field
.
recs
[
x
][
y
]
).
setAlreadyMoved
(
true
);
}
else
if
(
field
.
recs
[
x
][
y
]
instanceof
Rook
)
{
((
Rook
)
field
.
recs
[
x
][
y
]
).
setAlreadyMoved
(
true
);
}
field
.
changePlayer
();
return
;
}
else
{
fileHandler
.
clearFile
();
...
...
@@ -38,6 +65,13 @@ public class ActionHandler {
// empty field
}
else
if
(
field
.
movePieceValidated
(
fileHandler
.
getCoordX1Move
(),
fileHandler
.
getCoordY1Move
(),
x
,
y
))
{
if
(
field
.
recs
[
x
][
y
]
instanceof
King
)
{
((
King
)
field
.
recs
[
x
][
y
]
).
setAlreadyMoved
(
true
);
}
else
if
(
field
.
recs
[
x
][
y
]
instanceof
Rook
)
{
((
Rook
)
field
.
recs
[
x
][
y
]
).
setAlreadyMoved
(
true
);
}
field
.
changePlayer
();
return
;
}
else
{
fileHandler
.
clearFile
();
...
...
@@ -49,8 +83,18 @@ public class ActionHandler {
// chess piece
ChessPiece
chessPiece
=
(
ChessPiece
)
e
.
getSource
();
selectPiece
(
e
);
fileHandler
.
saveMove
(
coordX
,
coordY
,
chessPiece
.
getColor
());
if
(
field
.
playerTurn
==
1
)
{
// check whose turn
if
(
chessPiece
.
getColor
().
equals
(
"white"
))
{
selectPiece
(
e
);
fileHandler
.
saveMove
(
coordX
,
coordY
,
chessPiece
.
getColor
());
}
else
return
;
}
else
if
(
field
.
playerTurn
==
2
)
{
if
(
chessPiece
.
getColor
().
equals
(
"black"
))
{
selectPiece
(
e
);
fileHandler
.
saveMove
(
coordX
,
coordY
,
chessPiece
.
getColor
());
}
else
return
;
}
}
...
...
src/application/Bishop.java
View file @
ccb70b5e
...
...
@@ -116,4 +116,16 @@ public class Bishop extends ChessPiece {
}
@Override
public
boolean
checkCastling
(
Field
field
)
{
// TODO Auto-generated method stub
return
false
;
}
@Override
public
boolean
hasAlreadyMoved
()
{
// TODO Auto-generated method stub
return
false
;
}
}
src/application/ChessPiece.java
View file @
ccb70b5e
...
...
@@ -63,5 +63,9 @@ public abstract class ChessPiece extends ButtonsField {
public
abstract
ImageIcon
getPieceIcon
();
public
abstract
boolean
checkMoveset
(
Field
field
,
int
x
,
int
y
);
public
abstract
boolean
checkCastling
(
Field
field
);
public
abstract
boolean
hasAlreadyMoved
();
}
src/application/Field.java
View file @
ccb70b5e
...
...
@@ -37,6 +37,8 @@ public class Field {
String
letters
[]
=
{
"A"
,
"B"
,
"C"
,
"D"
,
"E"
,
"F"
,
"G"
,
"H"
};
JPanel
fieldPanel
;
public
int
playerTurn
;
public
Field
(
JPanel
fieldPanel
)
{
...
...
@@ -49,6 +51,7 @@ public class Field {
fileHandler
.
load
();
FileHandler
fileHandlerMove
=
new
FileHandler
(
"Move.txt"
);
fileHandlerMove
.
clearFile
();
playerTurn
=
1
;
pawnsBlack
=
fileHandler
.
getBlackPawns
();
pawnsWhite
=
fileHandler
.
getWhitePawns
();
...
...
@@ -232,6 +235,18 @@ public class Field {
fileHandler
.
clearFile
();
}
public
void
changePlayer
()
{
if
(
playerTurn
==
1
)
{
playerTurn
=
2
;
}
else
if
(
playerTurn
==
2
)
{
playerTurn
=
1
;
}
}
public
void
drawField
()
{
fieldPanel
.
removeAll
();
setLetterLabels
();
...
...
src/application/King.java
View file @
ccb70b5e
...
...
@@ -6,11 +6,13 @@ public class King extends ChessPiece {
private
static
final
long
serialVersionUID
=
1L
;
ImageLoader
imageLoader
=
new
ImageLoader
();
private
boolean
alreadyMoved
;
public
King
(
String
name
,
int
x
,
int
y
,
String
color
)
{
super
(
name
,
x
,
y
,
color
);
alreadyMoved
=
false
;
setPieceIcon
();
}
...
...
@@ -60,4 +62,51 @@ public class King extends ChessPiece {
}
@Override
public
boolean
checkCastling
(
Field
field
)
{
if
(!
alreadyMoved
)
{
if
(
this
.
getColor
().
equals
(
"black"
))
{
// for black side
if
(
field
.
recs
[
0
][
0
]
instanceof
Rook
&&
((
ChessPiece
)
field
.
recs
[
0
][
0
]).
getColor
().
equals
(
"black"
)
&&
!((
ChessPiece
)
field
.
recs
[
0
][
0
]).
hasAlreadyMoved
())
{
// check positions
if
(
field
.
recs
[
0
][
1
]
instanceof
EmptyField
&&
field
.
recs
[
0
][
2
]
instanceof
EmptyField
&&
field
.
recs
[
0
][
3
]
instanceof
EmptyField
)
{
// check if fields between are free
field
.
movePiece
(
0
,
0
,
0
,
3
);
field
.
movePiece
(
0
,
4
,
0
,
2
);
return
true
;
}
}
}
else
if
(
this
.
getColor
().
equals
(
"white"
))
{
// for white side
if
(
field
.
recs
[
7
][
7
]
instanceof
Rook
&&
((
ChessPiece
)
field
.
recs
[
7
][
7
]).
getColor
().
equals
(
"white"
)
&&
!((
ChessPiece
)
field
.
recs
[
7
][
7
]).
hasAlreadyMoved
())
{
// check positions
if
(
field
.
recs
[
7
][
5
]
instanceof
EmptyField
&&
field
.
recs
[
7
][
6
]
instanceof
EmptyField
)
{
// check if fields between are free
field
.
movePiece
(
7
,
4
,
7
,
6
);
field
.
movePiece
(
7
,
7
,
7
,
5
);
return
true
;
}
}
}
}
return
false
;
}
@Override
public
boolean
hasAlreadyMoved
()
{
return
alreadyMoved
;
}
public
void
setAlreadyMoved
(
boolean
alreadyMoved
)
{
this
.
alreadyMoved
=
alreadyMoved
;
}
}
src/application/Knight.java
View file @
ccb70b5e
...
...
@@ -60,4 +60,16 @@ public class Knight extends ChessPiece {
}
@Override
public
boolean
checkCastling
(
Field
field
)
{
// TODO Auto-generated method stub
return
false
;
}
@Override
public
boolean
hasAlreadyMoved
()
{
// TODO Auto-generated method stub
return
false
;
}
}
src/application/Pawn.java
View file @
ccb70b5e
...
...
@@ -108,4 +108,16 @@ public class Pawn extends ChessPiece {
return
false
;
}
@Override
public
boolean
checkCastling
(
Field
field
)
{
// TODO Auto-generated method stub
return
false
;
}
@Override
public
boolean
hasAlreadyMoved
()
{
// TODO Auto-generated method stub
return
false
;
}
}
src/application/Queen.java
View file @
ccb70b5e
...
...
@@ -175,4 +175,16 @@ public class Queen extends ChessPiece{
}
@Override
public
boolean
checkCastling
(
Field
field
)
{
// TODO Auto-generated method stub
return
false
;
}
@Override
public
boolean
hasAlreadyMoved
()
{
// TODO Auto-generated method stub
return
false
;
}
}
src/application/Rook.java
View file @
ccb70b5e
...
...
@@ -6,11 +6,13 @@ public class Rook extends ChessPiece {
private
static
final
long
serialVersionUID
=
1L
;
ImageLoader
imageLoader
=
new
ImageLoader
();
private
boolean
alreadyMoved
;
public
Rook
(
String
name
,
int
x
,
int
y
,
String
color
)
{
super
(
name
,
x
,
y
,
color
);
alreadyMoved
=
false
;
setPieceIcon
();
}
...
...
@@ -44,8 +46,8 @@ public class Rook extends ChessPiece {
if
(
moveValueY
<
0
)
{
for
(
int
i
=
0
;
i
>
moveValueY
;
i
--)
{
if
(
field
.
recs
[
this
.
getCoordX
()][
this
.
getCoordY
()
+
i
]
instanceof
ChessPiece
&&
field
.
recs
[
this
.
getCoordX
()][
this
.
getCoordY
()
+
i
]
!=
this
)
{
if
(
field
.
recs
[
this
.
getCoordX
()][
this
.
getCoordY
()
+
i
]
instanceof
ChessPiece
&&
field
.
recs
[
this
.
getCoordX
()][
this
.
getCoordY
()
+
i
]
!=
this
)
{
return
false
;
}
}
...
...
@@ -53,8 +55,8 @@ public class Rook extends ChessPiece {
if
(
moveValueY
>
0
)
{
for
(
int
i
=
0
;
i
<
moveValueY
;
i
++)
{
if
(
field
.
recs
[
this
.
getCoordX
()][
this
.
getCoordY
()
-
i
]
instanceof
ChessPiece
&&
field
.
recs
[
this
.
getCoordX
()][
this
.
getCoordY
()
-
i
]
!=
this
)
{
if
(
field
.
recs
[
this
.
getCoordX
()][
this
.
getCoordY
()
-
i
]
instanceof
ChessPiece
&&
field
.
recs
[
this
.
getCoordX
()][
this
.
getCoordY
()
-
i
]
!=
this
)
{
return
false
;
}
}
...
...
@@ -64,8 +66,8 @@ public class Rook extends ChessPiece {
if
(
moveValueX
<
0
)
{
for
(
int
i
=
0
;
i
>
moveValueX
;
i
--)
{
if
(
field
.
recs
[
this
.
getCoordX
()
+
i
][
this
.
getCoordY
()]
instanceof
ChessPiece
&&
field
.
recs
[
this
.
getCoordX
()
+
i
][
this
.
getCoordY
()]
!=
this
)
{
if
(
field
.
recs
[
this
.
getCoordX
()
+
i
][
this
.
getCoordY
()]
instanceof
ChessPiece
&&
field
.
recs
[
this
.
getCoordX
()
+
i
][
this
.
getCoordY
()]
!=
this
)
{
return
false
;
}
}
...
...
@@ -73,8 +75,8 @@ public class Rook extends ChessPiece {
if
(
moveValueX
>
0
)
{
for
(
int
i
=
0
;
i
<
moveValueX
;
i
++)
{
if
(
field
.
recs
[
this
.
getCoordX
()
+
i
][
this
.
getCoordY
()]
instanceof
ChessPiece
&&
field
.
recs
[
this
.
getCoordX
()
+
i
][
this
.
getCoordY
()]
!=
this
)
{
if
(
field
.
recs
[
this
.
getCoordX
()
+
i
][
this
.
getCoordY
()]
instanceof
ChessPiece
&&
field
.
recs
[
this
.
getCoordX
()
+
i
][
this
.
getCoordY
()]
!=
this
)
{
return
false
;
}
}
...
...
@@ -84,7 +86,7 @@ public class Rook extends ChessPiece {
return
true
;
}
@Override
public
boolean
checkMoveset
(
Field
field
,
int
destX
,
int
destY
)
{
...
...
@@ -117,4 +119,50 @@ public class Rook extends ChessPiece {
}
@Override
public
boolean
checkCastling
(
Field
field
)
{
if
(!
alreadyMoved
)
{
if
(
this
.
getColor
().
equals
(
"black"
))
{
// for black side
if
(
field
.
recs
[
0
][
4
]
instanceof
King
&&
((
ChessPiece
)
field
.
recs
[
0
][
4
]).
getColor
().
equals
(
"black"
)
&&
!((
ChessPiece
)
field
.
recs
[
0
][
4
]).
hasAlreadyMoved
())
{
// check positions
if
(
field
.
recs
[
0
][
1
]
instanceof
EmptyField
&&
field
.
recs
[
0
][
2
]
instanceof
EmptyField
&&
field
.
recs
[
0
][
3
]
instanceof
EmptyField
)
{
// check if fields between are free
field
.
movePiece
(
0
,
0
,
0
,
3
);
field
.
movePiece
(
0
,
4
,
0
,
2
);
return
true
;
}
}
}
else
if
(
this
.
getColor
().
equals
(
"white"
))
{
// for white side
if
(
field
.
recs
[
7
][
4
]
instanceof
King
&&
((
ChessPiece
)
field
.
recs
[
7
][
4
]).
getColor
().
equals
(
"white"
)
&&
!((
ChessPiece
)
field
.
recs
[
7
][
4
]).
hasAlreadyMoved
())
{
// check positions
if
(
field
.
recs
[
7
][
5
]
instanceof
EmptyField
&&
field
.
recs
[
7
][
6
]
instanceof
EmptyField
)
{
// check if fields between are free
field
.
movePiece
(
7
,
4
,
7
,
6
);
field
.
movePiece
(
7
,
7
,
7
,
5
);
return
true
;
}
}
}
}
return
false
;
}
@Override
public
boolean
hasAlreadyMoved
()
{
return
alreadyMoved
;
}
public
void
setAlreadyMoved
(
boolean
alreadyMoved
)
{
this
.
alreadyMoved
=
alreadyMoved
;
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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