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
9611046
chess
Commits
86b34bce
Commit
86b34bce
authored
May 25, 2019
by
9611046
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first moves and icon of turn completed
parent
db33b6b9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
185 additions
and
39 deletions
+185
-39
Main.java
src/game/Main.java
+126
-33
Square.java
src/game/Square.java
+26
-2
Bishop.java
src/pieces/Bishop.java
+2
-1
King.java
src/pieces/King.java
+6
-0
Knight.java
src/pieces/Knight.java
+3
-1
Pawn.java
src/pieces/Pawn.java
+10
-1
Piece.java
src/pieces/Piece.java
+8
-1
Queen.java
src/pieces/Queen.java
+2
-0
Rook.java
src/pieces/Rook.java
+2
-0
No files found.
src/game/Main.java
View file @
86b34bce
...
...
@@ -3,6 +3,7 @@ package game;
import
pieces.*
;
import
javax.swing.*
;
import
javax.swing.border.Border
;
import
java.awt.*
;
import
java.awt.event.MouseEvent
;
import
java.awt.event.MouseListener
;
...
...
@@ -23,9 +24,14 @@ public class Main extends JFrame implements MouseListener{
private
static
Main
Mainboard
;
// private JButton[][] chessBoardSquares ;
private
Square
[][]
chessBoardSquares
;
private
ArrayList
<
JButton
>
outWhitePieces
;
private
ArrayList
<
JButton
>
outBlackPieces
;
private
boolean
select
=
false
;
private
Square
lastSquare
;
private
Piece
lastPiece
;
private
ArrayList
<
Square
>
possibleMoves
;
private
String
whosTurn
=
"w"
;
private
boolean
isMoved
=
false
;
private
JFrame
myFrame
;
private
static
Rook
wr01
,
wr02
,
br01
,
br02
;
private
static
Knight
wk01
,
wk02
,
bk01
,
bk02
;
...
...
@@ -33,8 +39,9 @@ public class Main extends JFrame implements MouseListener{
private
static
Pawn
wp
[],
bp
[];
private
static
Queen
wq
,
bq
;
private
static
King
wk
,
bk
;
private
JButton
turn
;
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
// Mainboard.setVisible(true);
wr01
=
new
Rook
(
"WR01"
,
"src\\game\\White_Rook.png"
,
"w"
);
...
...
@@ -63,32 +70,49 @@ public static void main(String[] args) {
}
Mainboard
=
new
Main
();
}
private
void
changeTurn
(){
if
(
whosTurn
.
equals
(
"w"
))
whosTurn
=
"b"
;
else
whosTurn
=
"w"
;
}
private
Main
()
{
chessBoardSquares
=
new
Square
[
8
][
8
];
outBlackPieces
=
new
ArrayList
<>(
16
);
outWhitePieces
=
new
ArrayList
<>(
16
);
myFrame
=
new
JFrame
(
"Chess"
);
myFrame
.
setLayout
(
new
BorderLayout
(
100
,
100
));
JPanel
centerpanel
=
new
JPanel
();
JPanel
westPanel
=
new
JPanel
();
westPanel
.
setBackground
(
Color
.
BLACK
);
westPanel
.
setPreferredSize
(
new
Dimension
(
400
,
3
0
));
westPanel
.
setPreferredSize
(
new
Dimension
(
400
,
20
0
));
westPanel
.
setLayout
(
new
BorderLayout
(
3
,
1
));
JPanel
northOfWest
=
new
JPanel
();
northOfWest
.
setPreferredSize
(
new
Dimension
(
400
,
1
00
));
northOfWest
.
setPreferredSize
(
new
Dimension
(
400
,
3
00
));
northOfWest
.
setBackground
(
new
Color
(
51
,
0
,
0
));
Border
emptyBorder
=
BorderFactory
.
createEmptyBorder
();
JPanel
centerOfWest
=
new
JPanel
();
centerOfWest
.
setPreferredSize
(
new
Dimension
(
400
,
100
));
centerOfWest
.
setBackground
(
new
Color
(
153
,
102
,
0
));
turn
=
new
JButton
(
"Let's start! "
);
turn
.
setBorder
(
emptyBorder
);
turn
.
setBackground
(
new
Color
(
153
,
102
,
0
));
turn
.
setFont
(
new
Font
(
"Arial"
,
Font
.
PLAIN
,
40
));
turn
.
setForeground
(
Color
.
black
);
turn
.
setPreferredSize
(
new
Dimension
(
300
,
50
));
centerOfWest
.
add
(
turn
);
JPanel
southOfWest
=
new
JPanel
();
southOfWest
.
setPreferredSize
(
new
Dimension
(
400
,
1
00
));
southOfWest
.
setPreferredSize
(
new
Dimension
(
400
,
3
00
));
southOfWest
.
setBackground
(
new
Color
(
51
,
0
,
0
));
westPanel
.
add
(
northOfWest
,
BorderLayout
.
NORTH
);
westPanel
.
add
(
centerOfWest
,
BorderLayout
.
CENTER
);
westPanel
.
add
(
southOfWest
,
BorderLayout
.
SOUTH
);
...
...
@@ -118,6 +142,21 @@ private Main() {
}
}
}
for
(
int
i
=
0
;
i
<
16
;
i
++)
{
outBlackPieces
.
add
(
new
JButton
());
outWhitePieces
.
add
(
new
JButton
());
}
for
(
int
i
=
0
;
i
<
16
;
i
++){
//outWhitePieces.get(i)=new JButton();
outWhitePieces
.
get
(
i
).
setBackground
(
new
Color
(
51
,
0
,
0
));
// outWhitePieces.get(i).setBorder(emptyBorder);
northOfWest
.
add
(
outWhitePieces
.
get
(
i
));
// outBlackPieces[i]=new JButton();
outBlackPieces
.
get
(
i
).
setBackground
(
new
Color
(
51
,
0
,
0
));
// outBlackPieces[i].setBorder(emptyBorder);
southOfWest
.
add
(
outBlackPieces
.
get
(
i
));
}
// Piece piece;
...
...
@@ -232,6 +271,7 @@ private Main() {
//
// }
myFrame
.
setVisible
(
true
);
myFrame
.
setResizable
(
true
);
myFrame
.
setExtendedState
(
myFrame
.
getExtendedState
()
|
JFrame
.
MAXIMIZED_BOTH
);
...
...
@@ -241,8 +281,7 @@ private Main() {
if
(!
select
)
{
((
Square
)
(
e
.
getSource
())).
selectSquare
();
}
else
{
}
else
{
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
for
(
int
j
=
0
;
j
<
8
;
j
++)
{
chessBoardSquares
[
i
][
j
].
cancleSelection
();
...
...
@@ -250,42 +289,96 @@ private Main() {
}
((
Square
)
(
e
.
getSource
())).
selectSquare
();
}
select
=
true
;
// Square square1 = null;
// Piece piece1 = null;
if
(((
Square
)
(
e
.
getSource
())).
getPiece
()!=
null
)
{
lastSquare
=
((
Square
)
(
e
.
getSource
()));
lastPiece
=
((
Square
)
(
e
.
getSource
())).
getPiece
();
}
select
=
true
;
//selecting a square and showing it's possible squares
if
(((
Square
)
(
e
.
getSource
())).
getPiece
()
!=
null
&&
((
Square
)
(
e
.
getSource
())).
getPiece
().
getPieceColor
().
equals
(
whosTurn
)
)
{
if
(((
Square
)
(
e
.
getSource
())).
getPiece
()!=
null
){
lastSquare
=
((
Square
)
(
e
.
getSource
()));
lastPiece
=
((
Square
)
(
e
.
getSource
())).
getPiece
();
ArrayList
<
Square
>
possibleMoves
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
for
(
int
j
=
0
;
j
<
8
;
j
++)
{
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
for
(
int
j
=
0
;
j
<
8
;
j
++)
{
//
if
(
chessBoardSquares
[
i
][
j
].
isSelected
())
{
if
(
chessBoardSquares
[
i
][
j
].
isSelected
())
{
Piece
piece
=
chessBoardSquares
[
i
][
j
].
getPiece
();
possibleMoves
=
piece
.
move
(
chessBoardSquares
,
i
,
j
);
possibleMoves
=
piece
.
move
(
chessBoardSquares
,
i
,
j
);
}
}
}
for
(
Square
ps:
possibleMoves
)
{
for
(
Square
ps
:
possibleMoves
)
{
ps
.
makePossible
();
}
// System.out.println(piece1+"/"+square1);
if
(((
Square
)
(
e
.
getSource
())).
isPossibleSquare
()&&((
Square
)
(
e
.
getSource
())).
isSelected
()){
}
//moving a piece to a possible square
else
if
(((
Square
)
(
e
.
getSource
())).
getPiece
()==
null
)
{
if
(((
Square
)
(
e
.
getSource
())).
isPossibleSquare
()
&&
((
Square
)
(
e
.
getSource
())).
isSelected
())
{
// if (lastSquare != null)
((
Square
)
(
e
.
getSource
())).
setPiece
(
lastPiece
);
// if (lastSquare != null) {
lastSquare
.
removePiece
();
isMoved
=
true
;
// chessBoardSquares[1][j].setPiece(bp[j]);
for
(
int
i
=
0
;
i
<
8
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
if
(
chessBoardSquares
[
i
][
j
]==
lastSquare
){
chessBoardSquares
[
i
][
j
].
removePiece
();
}
}
}
}
}
else
if
(!((
Square
)
(
e
.
getSource
())).
getPiece
().
getPieceColor
().
equals
(
whosTurn
)){
if
(((
Square
)
(
e
.
getSource
())).
isPossibleSquare
()
&&
((
Square
)
(
e
.
getSource
())).
isSelected
()){
((
Square
)
(
e
.
getSource
())).
removePiece
();
for
(
int
i
=
0
;
i
<
8
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
if
(
chessBoardSquares
[
i
][
j
]==
(
Square
)
(
e
.
getSource
())){
// if(((Square) (e.getSource())).getPiece().getPieceColor().equals("w")){
// JButton button = new JButton();
// JLabel imageIcon = new JLabel(new ImageIcon(((Square) (e.getSource())).getPiece().getImage()));
// button.add(imageIcon);
//
// outWhitePieces.add(button);
// }
chessBoardSquares
[
i
][
j
].
removePiece
();
}
}
}
((
Square
)
(
e
.
getSource
())).
setPiece
(
lastPiece
);
lastSquare
.
removePiece
();
isMoved
=
true
;
for
(
int
i
=
0
;
i
<
8
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
if
(
chessBoardSquares
[
i
][
j
]==
lastSquare
){
chessBoardSquares
[
i
][
j
].
removePiece
();
}
}
}
}
}
if
(
isMoved
)
{
changeTurn
();
possibleMoves
.
clear
();
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
for
(
int
j
=
0
;
j
<
8
;
j
++)
{
chessBoardSquares
[
i
][
j
].
canclePossibility
();
}
}
isMoved
=
false
;
}
turn
.
setText
(
"It's your turn! "
);
turn
.
setForeground
(
Color
.
GRAY
);
if
(
whosTurn
.
equals
(
"w"
))
turn
.
setBackground
(
Color
.
white
);
else
turn
.
setBackground
(
Color
.
BLACK
);
}
...
...
src/game/Square.java
View file @
86b34bce
...
...
@@ -29,7 +29,16 @@ public class Square extends JButton {
}
public
Square
(
int
x
,
int
y
,
Piece
piece
){
public
JLabel
getImage
()
{
return
image
;
}
public
void
setImage
(
JLabel
image
)
{
this
.
image
=
image
;
}
public
Square
(
int
x
,
int
y
,
Piece
piece
){
this
.
x
=
x
;
this
.
y
=
y
;
this
.
piece
=
piece
;
...
...
@@ -46,7 +55,8 @@ public class Square extends JButton {
// ImageIcon img=new ImageIcon(this.getClass().getResource(piece.getImage()));
// image=new JLabel(img);
// JLabel imgLabel = new JLabel(new ImageIcon(piece.getImage()));
this
.
add
(
new
JLabel
(
new
ImageIcon
(
piece
.
getImage
())));
image
=
new
JLabel
(
new
ImageIcon
(
piece
.
getImage
()));
this
.
add
(
image
);
// this.add(image);
}
...
...
@@ -59,6 +69,10 @@ public class Square extends JButton {
this
.
isSelected
=
true
;
}
public
boolean
isSelected
(){
return
isSelected
;
}
/**
* deselects a square by deleting its red border line
*/
...
...
@@ -75,12 +89,20 @@ public class Square extends JButton {
this
.
possibleSquare
=
true
;
}
protected
void
canclePossibility
(){
this
.
possibleSquare
=
false
;
}
/**
* removes a piece of a square
*/
public
void
removePiece
(){
this
.
piece
=
null
;
this
.
remove
(
image
);
// this.remove(this.piece);
// this.remove(image);
// this.setImage(null);
// this.remove(new JLabel(new ImageIcon(null));
}
/**
...
...
@@ -91,6 +113,8 @@ public class Square extends JButton {
return
hasPiece
;
}
/**
* @return true if square is possible for moving a piece
*/
...
...
src/pieces/Bishop.java
View file @
86b34bce
...
...
@@ -10,9 +10,10 @@ public class Bishop extends Piece {
setImage
(
imagePath
);
setPieceColor
(
color
);
}
@Override
public
ArrayList
<
Square
>
move
(
Square
[][]
boardSquares
,
int
x
,
int
y
)
{
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
ArrayList
<
Square
>
possibleSquares
=
new
ArrayList
<>();
possibleSquares
.
clear
();
...
...
src/pieces/King.java
View file @
86b34bce
...
...
@@ -30,8 +30,11 @@ public class King extends Piece {
{
return
y
;
}
@Override
public
ArrayList
<
Square
>
move
(
Square
[][]
squares
,
int
x
,
int
y
){
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
ArrayList
<
Square
>
possibleSquares
=
new
ArrayList
<>();
possibleSquares
.
clear
();
int
posx
[]={
x
,
x
,
x
+
1
,
x
+
1
,
x
+
1
,
x
-
1
,
x
-
1
,
x
-
1
};
int
posy
[]={
y
-
1
,
y
+
1
,
y
-
1
,
y
,
y
+
1
,
y
-
1
,
y
,
y
+
1
};
...
...
@@ -215,5 +218,8 @@ public class King extends Piece {
}
public
boolean
isCheckMate
(
Square
[][]
squares
){
return
false
;
}
}
src/pieces/Knight.java
View file @
86b34bce
...
...
@@ -10,8 +10,10 @@ public class Knight extends Piece {
setImage
(
imagePath
);
setPieceColor
(
color
);
}
@Override
public
ArrayList
<
Square
>
move
(
Square
[][]
boardSquares
,
int
x
,
int
y
)
{
possibleSquares
.
clear
();
ArrayList
<
Square
>
possibleSquares
=
new
ArrayList
<>();
//possibleSquares.clear();
int
posx
[]
=
{
x
+
1
,
x
+
1
,
x
+
2
,
x
+
2
,
x
-
1
,
x
-
1
,
x
-
2
,
x
-
2
};
int
posy
[]
=
{
y
-
2
,
y
+
2
,
y
-
1
,
y
+
1
,
y
-
2
,
y
+
2
,
y
-
1
,
y
+
1
};
for
(
int
i
=
0
;
i
<
8
;
i
++)
...
...
src/pieces/Pawn.java
View file @
86b34bce
...
...
@@ -6,14 +6,18 @@ import java.util.ArrayList;
public
class
Pawn
extends
Piece
{
public
Pawn
(
String
ID
,
String
imagePath
,
String
color
){
setPieceId
(
ID
);
setImage
(
imagePath
);
setPieceColor
(
color
);
}
@Override
public
ArrayList
<
Square
>
move
(
Square
[][]
boardSquares
,
int
x
,
int
y
){
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
possibleSquares
.
clear
();
// possibleSquares.clear();
ArrayList
<
Square
>
possibleSquares
=
new
ArrayList
<>();
if
(
getPieceColor
().
equals
(
"w"
)){
if
(
x
==
0
){
...
...
@@ -50,4 +54,9 @@ public class Pawn extends Piece {
return
possibleSquares
;
}
// @Override
// public ArrayList<Square> move() {
// return null;
// }
}
src/pieces/Piece.java
View file @
86b34bce
...
...
@@ -17,7 +17,7 @@ private String imagePath;
//possibleSquares is protected in Piece class therefore subclasses have access to it
//for all subclasses used one arrayList and before using it in each class content will be cleared
pr
otected
ArrayList
<
Square
>
possibleSquares
=
new
ArrayList
<>();
pr
ivate
ArrayList
<
Square
>
possibleSquares
=
new
ArrayList
<>();
...
...
@@ -44,4 +44,11 @@ protected ArrayList<Square> possibleSquares = new ArrayList<>();
public
String
getImage
()
{
return
imagePath
;
}
public
abstract
ArrayList
<
Square
>
move
(
Square
[][]
boardSquares
,
int
x
,
int
y
);
// public abstract ArrayList<Square> move();
}
src/pieces/Queen.java
View file @
86b34bce
...
...
@@ -11,8 +11,10 @@ public class Queen extends Piece {
setImage
(
imagePath
);
setPieceColor
(
color
);
}
@Override
public
ArrayList
<
Square
>
move
(
Square
[][]
boardSquares
,
int
x
,
int
y
)
{
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
ArrayList
<
Square
>
possibleSquares
=
new
ArrayList
<>();
possibleSquares
.
clear
();
int
tempx
=
x
-
1
;
while
(
tempx
>=
0
)
{
...
...
src/pieces/Rook.java
View file @
86b34bce
...
...
@@ -10,8 +10,10 @@ public class Rook extends Piece {
setImage
(
imagePath
);
setPieceColor
(
color
);
}
@Override
public
ArrayList
<
Square
>
move
(
Square
[][]
boardSquares
,
int
x
,
int
y
){
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
ArrayList
<
Square
>
possibleSquares
=
new
ArrayList
<>();
possibleSquares
.
clear
();
int
tempx
=
x
-
1
;
...
...
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