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
511efd5f
Commit
511efd5f
authored
Jul 01, 2019
by
Arghavan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chess finished
parent
28c207d0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
248 deletions
+86
-248
ChessFrame.java
src/Main/ChessFrame.java
+0
-231
ChessGUI.java
src/Main/ChessGUI.java
+86
-17
No files found.
src/Main/ChessFrame.java
deleted
100644 → 0
View file @
28c207d0
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package
Main
;
import
Pieces.*
;
import
javax.imageio.ImageIO
;
import
javax.swing.*
;
import
java.awt.*
;
import
java.awt.event.MouseEvent
;
import
java.awt.event.MouseListener
;
import
java.awt.event.MouseMotionListener
;
import
java.awt.image.BufferedImage
;
import
java.io.File
;
public
class
ChessFrame
extends
JFrame
implements
MouseListener
,
MouseMotionListener
{
private
static
Board
b
=
new
Board
();
private
static
int
WIDTH
=
750
;
private
static
int
WIDTH_2
=
500
;
private
static
int
HEIGHT
=
600
;
private
static
int
yBias
=
50
;
private
int
startX
;
private
int
startY
;
private
int
endX
;
private
int
endY
;
public
static
void
main
(
String
args
[]){
new
ChessFrame
();
}
public
ChessFrame
(){
super
();
addMouseListener
(
this
);
setSize
(
WIDTH_2
+
WIDTH
+
12
,
yBias
+
HEIGHT
+
12
);
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
setVisible
(
true
);
}
private
Image
getPieceImage
(
Piece
p
){
BufferedImage
ret
=
null
;
try
{
if
(
p
.
isWhite
())
{
if
(
p
instanceof
Bishop
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_blt60.png"
));
}
else
if
(
p
instanceof
King
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_klt60.png"
));
}
else
if
(
p
instanceof
Knight
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_nlt60.png"
));
}
else
if
(
p
instanceof
Pawn
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_plt60.png"
));
}
else
if
(
p
instanceof
Queen
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_qlt60.png"
));
}
else
if
(
p
instanceof
Rook
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_rlt60.png"
));
}
}
else
{
if
(
p
instanceof
Bishop
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_bdt60.png"
));
}
else
if
(
p
instanceof
King
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_kdt60.png"
));
}
else
if
(
p
instanceof
Knight
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_ndt60.png"
));
}
else
if
(
p
instanceof
Pawn
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_pdt60.png"
));
}
else
if
(
p
instanceof
Queen
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_qdt60.png"
));
}
else
if
(
p
instanceof
Rook
)
{
ret
=
ImageIO
.
read
(
new
File
(
"./Chess/Chess_rdt60.png"
));
}
}
}
catch
(
Exception
e
){
}
return
ret
;
}
@Override
public
void
paint
(
Graphics
g
){
super
.
paint
(
g
);
g
.
setColor
(
Color
.
WHITE
);
g
.
fillRect
(
0
,
0
,
getWidth
(),
getHeight
());
g
.
setColor
(
Color
.
BLACK
);
int
dy
=
HEIGHT
/
8
;
int
dx
=
WIDTH
/
8
;
for
(
int
i
=
0
;
i
<=
8
;
i
++){
// Pain Chess Board
int
H
=
i
*
dy
;
int
W
=
i
*
dx
;
g
.
drawLine
(
0
,
yBias
+
H
,
WIDTH
,
yBias
+
H
);
g
.
drawLine
(
0
,
yBias
+
H
+
1
,
WIDTH
,
yBias
+
H
+
1
);
g
.
drawLine
(
0
,
yBias
+
H
+
2
,
WIDTH
,
yBias
+
H
+
2
);
//-----------------------------------------------
g
.
drawLine
(
W
,
yBias
,
W
,
getHeight
());
g
.
drawLine
(
W
+
1
,
yBias
,
W
+
1
,
getHeight
());
g
.
drawLine
(
W
+
2
,
yBias
,
W
+
2
,
getHeight
());
}
g
.
setColor
(
Color
.
GRAY
);
for
(
int
i
=
0
;
i
<
8
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
if
((
i
+
j
)%
2
==
0
){
g
.
fillRect
(
i
*
dx
,
yBias
+
j
*
dy
,
dx
,
dy
);
}
if
(!
b
.
isEmpty
(
i
,
j
)){
Image
image
=
getPieceImage
(
b
.
getBoard
()[
i
][
j
]);
// SwingUtilities.
// g.dra
g
.
drawImage
(
image
,
i
*
dx
,
yBias
+
j
*
dy
,
dx
,
dy
,
this
);
}
}
}
//--------------------------- Paint Extra Board...
g
.
setColor
(
Color
.
BLACK
);
dx
=
WIDTH_2
/
8
;
for
(
int
i
=
0
;
i
<=
8
;
i
++){
int
W
=
i
*
dx
+
WIDTH
;
g
.
drawLine
(
W
,
yBias
,
W
,
yBias
+
dy
*
2
);
g
.
drawLine
(
W
+
1
,
yBias
,
W
+
1
,
yBias
+
dy
*
2
);
g
.
drawLine
(
W
+
2
,
yBias
,
W
+
2
,
yBias
+
dy
*
2
);
//--------------------------------
g
.
drawLine
(
W
,
HEIGHT
+
yBias
-
2
*
dy
,
W
,
getHeight
());
g
.
drawLine
(
W
+
1
,
HEIGHT
+
yBias
-
2
*
dy
,
W
+
1
,
getHeight
());
g
.
drawLine
(
W
+
2
,
HEIGHT
+
yBias
-
2
*
dy
,
W
+
2
,
getHeight
());
}
for
(
int
i
=
0
;
i
<
3
;
i
++){
g
.
drawLine
(
WIDTH
,
i
*
dy
+
yBias
,
getWidth
(),
i
*
dy
+
yBias
);
g
.
drawLine
(
WIDTH
,
i
*
dy
+
yBias
+
1
,
getWidth
(),
i
*
dy
+
yBias
+
1
);
g
.
drawLine
(
WIDTH
,
i
*
dy
+
yBias
+
2
,
getWidth
(),
i
*
dy
+
yBias
+
2
);
g
.
drawLine
(
WIDTH
,
HEIGHT
-(
i
*
dy
)+
yBias
,
getWidth
(),
HEIGHT
-(
i
*
dy
)+
yBias
);
g
.
drawLine
(
WIDTH
,
HEIGHT
-(
i
*
dy
-
1
)+
yBias
,
getWidth
(),
HEIGHT
-(
i
*
dy
-
1
)+
yBias
);
g
.
drawLine
(
WIDTH
,
HEIGHT
-(
i
*
dy
-
2
)+
yBias
,
getWidth
()+
yBias
,
HEIGHT
-(
i
*
dy
-
2
)+
yBias
);
}
//----------------------
for
(
int
i
=
0
;
i
<
8
;
i
++){
for
(
int
j
=
0
;
j
<
2
;
j
++){
int
ind
=
i
+
j
*
8
;
if
(
ind
<
b
.
getBlackRemovedList
().
size
()){
Image
image
=
getPieceImage
(
b
.
getBlackRemovedList
().
get
(
ind
));
g
.
drawImage
(
image
,
WIDTH
+
i
*
dx
,
yBias
+
j
*
dy
,
dx
,
dy
,
this
);
}
//------------------------------------------
if
(
ind
<
b
.
getWhiteRemovedList
().
size
()){
Image
image
=
getPieceImage
(
b
.
getWhiteRemovedList
().
get
(
ind
));
g
.
drawImage
(
image
,
WIDTH
+
i
*
dx
,
HEIGHT
+
yBias
-(
j
+
1
)*
dy
,
dx
,
dy
,
this
);
}
}
}
//---------------
g
.
setFont
(
new
Font
(
"Arial"
,
Font
.
BOLD
,
18
));
if
(
b
.
KishCheck
(
true
)){
g
.
drawString
(
"Kish of White King ..."
,
WIDTH
+
WIDTH_2
/
3
,
yBias
+
HEIGHT
/
2
);
if
(
b
.
checkGameOver
(
true
)){
g
.
drawString
(
"Black Color is Win ..."
,
WIDTH
+
WIDTH_2
/
3
,
yBias
+
yBias
+
HEIGHT
/
2
);
}
}
else
if
(
b
.
KishCheck
(
false
)){
g
.
drawString
(
"Kish of Black King ..."
,
WIDTH
+
WIDTH_2
/
3
,
yBias
+
HEIGHT
/
2
);
if
(
b
.
checkGameOver
(
false
)){
g
.
drawString
(
"White Color is Win ..."
,
WIDTH
+
WIDTH_2
/
3
,
yBias
+
yBias
+
HEIGHT
/
2
);
}
}
else
if
(
b
.
isWhiteMove
())
{
g
.
drawString
(
"White Turn..."
,
WIDTH
+
WIDTH_2
/
3
,
yBias
+
HEIGHT
/
2
);
}
else
if
(!
b
.
isWhiteMove
())
{
g
.
drawString
(
"Black Turn..."
,
WIDTH
+
WIDTH_2
/
3
,
yBias
+
HEIGHT
/
2
);
}
}
@Override
public
void
mouseClicked
(
MouseEvent
e
)
{
// if(SwingUtilities.isLeftMouseButton(e)){
// int x = e.getX();
// int y = e.getY()-yBias;
// int dx = WIDTH/8;
// int dy = HEIGHT/8;
// System.out.println(x/dx+","+y/dy);
// }
}
@Override
public
void
mousePressed
(
MouseEvent
e
)
{
if
(
SwingUtilities
.
isLeftMouseButton
(
e
)){
int
x
=
e
.
getX
();
int
y
=
e
.
getY
()-
yBias
;
if
(
x
>
WIDTH
||
y
>
HEIGHT
)
return
;
int
dx
=
WIDTH
/
8
;
int
dy
=
HEIGHT
/
8
;
startX
=
x
/
dx
;
startY
=
y
/
dy
;
System
.
out
.
println
(
x
/
dx
+
","
+
y
/
dy
);
}
}
@Override
public
void
mouseReleased
(
MouseEvent
e
)
{
if
(
SwingUtilities
.
isLeftMouseButton
(
e
)){
int
x
=
e
.
getX
();
int
y
=
e
.
getY
()-
yBias
;
if
(
x
>
WIDTH
||
y
>
HEIGHT
)
return
;
int
dx
=
WIDTH
/
8
;
int
dy
=
HEIGHT
/
8
;
endX
=
x
/
dx
;
endY
=
y
/
dy
;
System
.
out
.
println
(
x
/
dx
+
","
+
y
/
dy
);
b
.
getMove
(
startX
,
startY
,
endX
,
endY
);
repaint
();
}
}
@Override
public
void
mouseEntered
(
MouseEvent
e
)
{
}
@Override
public
void
mouseExited
(
MouseEvent
e
)
{
}
@Override
public
void
mouseDragged
(
MouseEvent
e
)
{
}
@Override
public
void
mouseMoved
(
MouseEvent
e
)
{
}
}
src/Main/ChessGUI.java
View file @
511efd5f
...
...
@@ -8,10 +8,16 @@ import java.awt.*;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
java.awt.image.BufferedImage
;
import
java.io.File
;
import
java.io.*
;
import
java.net.ServerSocket
;
import
java.net.Socket
;
import
java.util.Scanner
;
public
class
ChessGUI
extends
JFrame
{
private
Board
b
=
new
Board
();
private
Scanner
reader
;
private
PrintWriter
writer
;
private
int
defaultPort
=
4444
;
private
JButton
buttonBoard
[][]
=
new
JButton
[
8
][
8
];
private
JButton
blackRemoveButton
[]
=
new
JButton
[
16
];
private
JButton
whiteRemoveButton
[]
=
new
JButton
[
16
];
...
...
@@ -22,11 +28,22 @@ public class ChessGUI extends JFrame {
private
int
endX
;
private
int
endY
;
private
boolean
turn
=
true
;
private
boolean
isServer
=
false
;
public
static
void
main
(
String
args
[]){
new
ChessGUI
();
}
public
ChessGUI
(){
super
();
String
adress
=
JOptionPane
.
showInputDialog
(
"Insert Server Adress: (Insert 0 for be a server)"
);
if
(
adress
.
equalsIgnoreCase
(
"0"
)){
inilizeServer
();
isServer
=
true
;
setTitle
(
"Server"
);
}
else
{
intilizeClient
(
adress
);
setTitle
(
"Client"
);
isServer
=
false
;
}
setSize
(
1200
,
600
);
this
.
setLayout
(
new
GridLayout
(
1
,
2
));
JPanel
chessBoard
=
new
JPanel
();
...
...
@@ -52,6 +69,50 @@ public class ChessGUI extends JFrame {
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
setVisible
(
true
);
}
private
void
inilizeServer
(){
try
{
ServerSocket
server
=
new
ServerSocket
(
defaultPort
);
Socket
s
=
server
.
accept
();
reader
=
new
Scanner
(
s
.
getInputStream
());
writer
=
new
PrintWriter
(
s
.
getOutputStream
());
initThread
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
void
initThread
(){
Thread
t
=
new
Thread
()
{
@Override
public
void
run
()
{
while
(
reader
.
hasNextInt
()){
int
sX
=
reader
.
nextInt
();
int
sY
=
reader
.
nextInt
();
int
eX
=
reader
.
nextInt
();
int
eY
=
reader
.
nextInt
();
move
(
sX
,
sY
,
eX
,
eY
);
}
}
};
t
.
start
();
}
private
void
updateOtherClient
(
int
startX
,
int
startY
,
int
endX
,
int
endY
){
writer
.
println
(
startX
);
writer
.
println
(
startY
);
writer
.
println
(
endX
);
writer
.
println
(
endY
);
writer
.
flush
();
}
private
void
intilizeClient
(
String
addr
){
try
{
Socket
s
=
new
Socket
(
addr
,
defaultPort
);
reader
=
new
Scanner
(
s
.
getInputStream
());
writer
=
new
PrintWriter
(
s
.
getOutputStream
());
initThread
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
void
updateRemovePanel
(){
int
i
=
0
;
for
(
Piece
p
:
b
.
getWhiteRemovedList
()){
...
...
@@ -94,29 +155,19 @@ public class ChessGUI extends JFrame {
button
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
if
(
isServer
&&
!
turn
)
return
;
if
(!
isServer
&&
turn
)
return
;
startX
=
endX
;
startY
=
endY
;
endX
=
fI
;
endY
=
fJ
;
// System.out.println(startX+","+startY);
if
(
validMoves
!=
null
&&
validMoves
[
endX
][
endY
]){
b
.
getMove
(
startX
,
startY
,
endX
,
endY
);
if
(
b
.
KishCheck
(
true
)){
notificationLabel
.
setText
(
"White Kish..."
);
}
else
if
(
b
.
KishCheck
(
false
)){
notificationLabel
.
setText
(
"Black Kish..."
);
}
else
if
(
turn
){
notificationLabel
.
setText
(
"White Turn..."
);
}
else
if
(!
turn
){
notificationLabel
.
setText
(
"Black Turn..."
);
}
turn
=
!
turn
;
move
(
startX
,
startY
,
endX
,
endY
);
updateOtherClient
(
startX
,
startY
,
endX
,
endY
);
updateRemovePanel
();
// System.out.println("Moves Approved");
}
validMoves
=
getValidMoves
(
b
.
getBoard
()[
endX
][
endY
]);
updateButtons
();
...
...
@@ -127,6 +178,24 @@ public class ChessGUI extends JFrame {
}
}
}
private
void
move
(
int
startX
,
int
startY
,
int
endX
,
int
endY
){
b
.
getMove
(
startX
,
startY
,
endX
,
endY
);
turn
=
!
turn
;
if
(
b
.
KishCheck
(
true
)){
notificationLabel
.
setText
(
"White Kish..."
);
}
else
if
(
b
.
KishCheck
(
false
)){
notificationLabel
.
setText
(
"Black Kish..."
);
}
else
if
(
turn
){
notificationLabel
.
setText
(
"White Turn..."
);
}
else
if
(!
turn
){
notificationLabel
.
setText
(
"Black Turn..."
);
}
updateRemovePanel
();
updateButtons
();
}
private
void
updateButtons
(){
for
(
int
j
=
0
;
j
<
8
;
j
++){
for
(
int
i
=
0
;
i
<
8
;
i
++){
...
...
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