Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
Chess-AP
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
Omid Sayfun
Chess-AP
Commits
9e9b637a
Commit
9e9b637a
authored
May 21, 2019
by
Omid Sayfun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor Bugs fixed
parent
8b09ab57
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
189 additions
and
99 deletions
+189
-99
Main.java
src/Main.java
+189
-99
No files found.
src/Main.java
View file @
9e9b637a
...
...
@@ -43,18 +43,19 @@ class Splash{
Dimension
dim
=
Toolkit
.
getDefaultToolkit
().
getScreenSize
();
panel
.
add
(
label
);
frame
.
add
(
label
);
frame
.
setDefaultCloseOperation
(
JFrame
.
DISPOSE_ON_CLOSE
);
frame
.
pack
();
frame
.
setLocation
(
dim
.
width
/
2
-
frame
.
getSize
().
width
/
2
,
dim
.
height
/
2
-
frame
.
getSize
().
height
/
2
);
frame
.
setVisible
(
true
);
Socket
socket
=
server
.
accept
();
System
.
out
.
println
(
"Client Connected"
);
frame
.
dispose
();
frame
.
dispatchEvent
(
new
WindowEvent
(
frame
,
WindowEvent
.
WINDOW_CLOSING
));
server
.
close
();
Chess
newChess
=
new
Chess
(
"W"
,
socket
);
newChess
.
run
();
socket
.
close
();
}
else
{
// Client
String
s
=
(
String
)
JOptionPane
.
showInputDialog
(
null
,
...
...
@@ -70,7 +71,6 @@ class Splash{
Chess
newChess
=
new
Chess
(
"B"
,
socket
);
newChess
.
run
();
socket
.
close
();
}
}
}
...
...
@@ -82,11 +82,9 @@ class newJButton extends JButton{
private
Piece
piece
;
private
char
X
;
private
int
Y
;
private
Boolean
color
;
public
newJButton
(){
this
.
piece
=
null
;
this
.
color
=
null
;
this
.
setIcon
(
null
);
}
...
...
@@ -110,11 +108,10 @@ class newJButton extends JButton{
* @param Y The Y-Axis
* @author Omiid
*/
public
newJButton
(
Piece
p
,
char
X
,
int
Y
,
boolean
color
){
public
newJButton
(
Piece
p
,
char
X
,
int
Y
){
this
.
piece
=
p
;
this
.
X
=
X
;
this
.
Y
=
Y
;
this
.
color
=
color
;
Image
img
=
null
;
try
{
img
=
ImageIO
.
read
(
Chess
.
class
.
getResource
(
"resources/images/"
+
p
.
getName
().
toLowerCase
()
+
".png"
));
...
...
@@ -141,9 +138,6 @@ class newJButton extends JButton{
return
this
.
Y
;
}
public
boolean
getColor
(){
return
this
.
color
;
}
}
/**
...
...
@@ -187,86 +181,171 @@ class Chess implements MouseListener{
JPanel
boardPanel
=
new
JPanel
(
new
GridLayout
(
8
,
8
));
ArrayList
<
Piece
>
whitePieces
=
new
ArrayList
<
Piece
>();
ArrayList
<
Piece
>
blackPieces
=
new
ArrayList
<
Piece
>();
for
(
int
i
=
1
;
i
<
9
;
i
++){
for
(
char
j
=
'A'
;
j
<
'I'
;
j
++){
newJButton
btn
=
null
;
if
(
i
==
8
){
if
(
j
==
'A'
||
j
==
'H'
){
Rook
newRook
=
new
Rook
(
j
,
i
,
true
,
"WR"
);
whitePieces
.
add
(
newRook
);
btn
=
new
newJButton
(
newRook
,
j
,
i
,
true
);
}
else
if
(
j
==
'B'
||
j
==
'G'
){
Knight
newKnight
=
new
Knight
(
j
,
i
,
true
,
"WN"
);
whitePieces
.
add
(
newKnight
);
btn
=
new
newJButton
(
newKnight
,
j
,
i
,
true
);
}
else
if
(
j
==
'C'
||
j
==
'F'
){
Bishop
newBishop
=
new
Bishop
(
j
,
i
,
true
,
"WB"
);
whitePieces
.
add
(
newBishop
);
btn
=
new
newJButton
(
newBishop
,
j
,
i
,
true
);
}
else
if
(
j
==
'D'
){
Queen
whiteQueen
=
new
Queen
(
j
,
i
,
true
,
"WQ"
);
whitePieces
.
add
(
whiteQueen
);
btn
=
new
newJButton
(
whiteQueen
,
j
,
i
,
true
);
}
else
if
(
j
==
'E'
){
King
whiteKing
=
new
King
(
j
,
i
,
true
,
"WK"
);
whitePieces
.
add
(
whiteKing
);
btn
=
new
newJButton
(
whiteKing
,
j
,
i
,
true
);
if
(
this
.
me
){
for
(
int
i
=
1
;
i
<
9
;
i
++){
for
(
char
j
=
'A'
;
j
<
'I'
;
j
++){
newJButton
btn
=
null
;
if
(
i
==
8
){
if
(
j
==
'A'
||
j
==
'H'
){
Rook
newRook
=
new
Rook
(
j
,
i
,
true
,
"WR"
);
whitePieces
.
add
(
newRook
);
btn
=
new
newJButton
(
newRook
,
j
,
i
);
}
else
if
(
j
==
'B'
||
j
==
'G'
){
Knight
newKnight
=
new
Knight
(
j
,
i
,
true
,
"WN"
);
whitePieces
.
add
(
newKnight
);
btn
=
new
newJButton
(
newKnight
,
j
,
i
);
}
else
if
(
j
==
'C'
||
j
==
'F'
){
Bishop
newBishop
=
new
Bishop
(
j
,
i
,
true
,
"WB"
);
whitePieces
.
add
(
newBishop
);
btn
=
new
newJButton
(
newBishop
,
j
,
i
);
}
else
if
(
j
==
'D'
){
Queen
whiteQueen
=
new
Queen
(
j
,
i
,
true
,
"WQ"
);
whitePieces
.
add
(
whiteQueen
);
btn
=
new
newJButton
(
whiteQueen
,
j
,
i
);
}
else
if
(
j
==
'E'
){
King
whiteKing
=
new
King
(
j
,
i
,
true
,
"WK"
);
whitePieces
.
add
(
whiteKing
);
btn
=
new
newJButton
(
whiteKing
,
j
,
i
);
}
}
else
if
(
i
==
7
){
Pawn
newPawn
=
new
Pawn
(
j
,
i
,
true
,
"WP"
);
whitePieces
.
add
(
newPawn
);
btn
=
new
newJButton
(
newPawn
,
j
,
i
);
}
else
if
(
i
==
1
){
if
(
j
==
'A'
||
j
==
'H'
){
Rook
newRook
=
new
Rook
(
j
,
i
,
false
,
"BR"
);
blackPieces
.
add
(
newRook
);
btn
=
new
newJButton
(
newRook
,
j
,
i
);
}
else
if
(
j
==
'B'
||
j
==
'G'
){
Knight
newKnight
=
new
Knight
(
j
,
i
,
false
,
"BN"
);
blackPieces
.
add
(
newKnight
);
btn
=
new
newJButton
(
newKnight
,
j
,
i
);
}
else
if
(
j
==
'C'
||
j
==
'F'
){
Bishop
newBishop
=
new
Bishop
(
j
,
i
,
false
,
"BB"
);
blackPieces
.
add
(
newBishop
);
btn
=
new
newJButton
(
newBishop
,
j
,
i
);
}
else
if
(
j
==
'D'
){
Queen
blackQueen
=
new
Queen
(
j
,
i
,
false
,
"BQ"
);
blackPieces
.
add
(
blackQueen
);
btn
=
new
newJButton
(
blackQueen
,
j
,
i
);
}
else
if
(
j
==
'E'
){
King
blackKing
=
new
King
(
j
,
i
,
false
,
"BQ"
);
blackPieces
.
add
(
blackKing
);
btn
=
new
newJButton
(
blackKing
,
j
,
i
);
}
}
else
if
(
i
==
2
){
Pawn
newPawn
=
new
Pawn
(
j
,
i
,
false
,
"BP"
);
blackPieces
.
add
(
newPawn
);
btn
=
new
newJButton
(
newPawn
,
j
,
i
);
// btn = new newJButton(j, i);
}
else
{
btn
=
new
newJButton
(
j
,
i
);
}
}
else
if
(
i
==
7
){
Pawn
newPawn
=
new
Pawn
(
j
,
i
,
true
,
"WP"
);
whitePieces
.
add
(
newPawn
);
btn
=
new
newJButton
(
newPawn
,
j
,
i
,
true
);
}
else
if
(
i
==
1
){
if
(
j
==
'A'
||
j
==
'H'
){
Rook
newRook
=
new
Rook
(
j
,
i
,
false
,
"BR"
);
blackPieces
.
add
(
newRook
);
btn
=
new
newJButton
(
newRook
,
j
,
i
,
false
);
}
else
if
(
j
==
'B'
||
j
==
'G'
){
Knight
newKnight
=
new
Knight
(
j
,
i
,
false
,
"BN"
);
blackPieces
.
add
(
newKnight
);
btn
=
new
newJButton
(
newKnight
,
j
,
i
,
false
);
}
else
if
(
j
==
'C'
||
j
==
'F'
){
Bishop
newBishop
=
new
Bishop
(
j
,
i
,
false
,
"BB"
);
blackPieces
.
add
(
newBishop
);
btn
=
new
newJButton
(
newBishop
,
j
,
i
,
false
);
}
else
if
(
j
==
'D'
){
Queen
blackQueen
=
new
Queen
(
j
,
i
,
false
,
"BQ"
);
blackPieces
.
add
(
blackQueen
);
btn
=
new
newJButton
(
blackQueen
,
j
,
i
,
false
);
}
else
if
(
j
==
'E'
){
King
blackKing
=
new
King
(
j
,
i
,
false
,
"BQ"
);
blackPieces
.
add
(
blackKing
);
btn
=
new
newJButton
(
blackKing
,
j
,
i
,
false
);
this
.
board
=
new
Board
(
whitePieces
,
blackPieces
);
this
.
btns
.
add
(
btn
);
btn
.
addMouseListener
(
this
);
btn
.
setFocusable
(
false
);
if
(
(
i
%
2
==
1
&&
j
%
2
==
1
)
||
(
i
%
2
==
0
&&
j
%
2
==
0
)
){
btn
.
setBackground
(
new
Color
(
219
,
217
,
164
));
}
else
{
btn
.
setBackground
(
new
Color
(
46
,
83
,
106
));
}
}
else
if
(
i
==
2
){
Pawn
newPawn
=
new
Pawn
(
j
,
i
,
false
,
"BP"
);
blackPieces
.
add
(
newPawn
);
btn
=
new
newJButton
(
newPawn
,
j
,
i
,
false
);
// btn = new newJButton(j, i);
}
else
{
btn
=
new
newJButton
(
j
,
i
);
boardPanel
.
add
(
btn
);
}
this
.
board
=
new
Board
(
whitePieces
,
blackPieces
);
this
.
btns
.
add
(
btn
);
btn
.
addMouseListener
(
this
);
btn
.
setFocusable
(
false
);
if
(
(
i
%
2
==
1
&&
j
%
2
==
1
)
||
(
i
%
2
==
0
&&
j
%
2
==
0
)
){
btn
.
setBackground
(
new
Color
(
219
,
217
,
164
));
}
else
{
btn
.
setBackground
(
new
Color
(
46
,
83
,
106
));
}
}
else
{
for
(
int
i
=
8
;
i
>=
1
;
i
--){
for
(
char
j
=
'H'
;
j
>=
'A'
;
j
--){
newJButton
btn
=
null
;
if
(
i
==
8
){
if
(
j
==
'A'
||
j
==
'H'
){
Rook
newRook
=
new
Rook
(
j
,
i
,
true
,
"WR"
);
whitePieces
.
add
(
newRook
);
btn
=
new
newJButton
(
newRook
,
j
,
i
);
}
else
if
(
j
==
'B'
||
j
==
'G'
){
Knight
newKnight
=
new
Knight
(
j
,
i
,
true
,
"WN"
);
whitePieces
.
add
(
newKnight
);
btn
=
new
newJButton
(
newKnight
,
j
,
i
);
}
else
if
(
j
==
'C'
||
j
==
'F'
){
Bishop
newBishop
=
new
Bishop
(
j
,
i
,
true
,
"WB"
);
whitePieces
.
add
(
newBishop
);
btn
=
new
newJButton
(
newBishop
,
j
,
i
);
}
else
if
(
j
==
'D'
){
Queen
whiteQueen
=
new
Queen
(
j
,
i
,
true
,
"WQ"
);
whitePieces
.
add
(
whiteQueen
);
btn
=
new
newJButton
(
whiteQueen
,
j
,
i
);
}
else
if
(
j
==
'E'
){
King
whiteKing
=
new
King
(
j
,
i
,
true
,
"WK"
);
whitePieces
.
add
(
whiteKing
);
btn
=
new
newJButton
(
whiteKing
,
j
,
i
);
}
}
else
if
(
i
==
7
){
Pawn
newPawn
=
new
Pawn
(
j
,
i
,
true
,
"WP"
);
whitePieces
.
add
(
newPawn
);
btn
=
new
newJButton
(
newPawn
,
j
,
i
);
}
else
if
(
i
==
1
){
if
(
j
==
'A'
||
j
==
'H'
){
Rook
newRook
=
new
Rook
(
j
,
i
,
false
,
"BR"
);
blackPieces
.
add
(
newRook
);
btn
=
new
newJButton
(
newRook
,
j
,
i
);
}
else
if
(
j
==
'B'
||
j
==
'G'
){
Knight
newKnight
=
new
Knight
(
j
,
i
,
false
,
"BN"
);
blackPieces
.
add
(
newKnight
);
btn
=
new
newJButton
(
newKnight
,
j
,
i
);
}
else
if
(
j
==
'C'
||
j
==
'F'
){
Bishop
newBishop
=
new
Bishop
(
j
,
i
,
false
,
"BB"
);
blackPieces
.
add
(
newBishop
);
btn
=
new
newJButton
(
newBishop
,
j
,
i
);
}
else
if
(
j
==
'D'
){
Queen
blackQueen
=
new
Queen
(
j
,
i
,
false
,
"BQ"
);
blackPieces
.
add
(
blackQueen
);
btn
=
new
newJButton
(
blackQueen
,
j
,
i
);
}
else
if
(
j
==
'E'
){
King
blackKing
=
new
King
(
j
,
i
,
false
,
"BQ"
);
blackPieces
.
add
(
blackKing
);
btn
=
new
newJButton
(
blackKing
,
j
,
i
);
}
}
else
if
(
i
==
2
){
Pawn
newPawn
=
new
Pawn
(
j
,
i
,
false
,
"BP"
);
blackPieces
.
add
(
newPawn
);
btn
=
new
newJButton
(
newPawn
,
j
,
i
);
// btn = new newJButton(j, i);
}
else
{
btn
=
new
newJButton
(
j
,
i
);
}
this
.
board
=
new
Board
(
whitePieces
,
blackPieces
);
this
.
btns
.
add
(
btn
);
btn
.
addMouseListener
(
this
);
btn
.
setFocusable
(
false
);
if
(
(
i
%
2
==
1
&&
j
%
2
==
1
)
||
(
i
%
2
==
0
&&
j
%
2
==
0
)
){
btn
.
setBackground
(
new
Color
(
219
,
217
,
164
));
}
else
{
btn
.
setBackground
(
new
Color
(
46
,
83
,
106
));
}
boardPanel
.
add
(
btn
);
}
boardPanel
.
add
(
btn
);
}
}
// Left Divider Panel
JPanel
leftDivider
=
new
JPanel
(
new
GridLayout
(
3
,
1
));
// Top Left
...
...
@@ -326,12 +405,14 @@ class Chess implements MouseListener{
this
.
frame
=
frame
;
String
line
=
""
;
while
(
!
line
.
equals
(
"Over"
)
)
while
(
true
)
{
try
{
line
=
this
.
input
.
readUTF
();
if
(
line
.
equals
(
"Over"
)
){
break
;
}
System
.
out
.
println
(
line
);
String
[]
splited
=
line
.
split
(
" "
);
for
(
newJButton
btn
:
this
.
btns
){
...
...
@@ -390,13 +471,17 @@ class Chess implements MouseListener{
System
.
out
.
println
(
i
);
}
}
try
{
this
.
output
.
writeUTF
(
"Over"
);
}
catch
(
IOException
e1
)
{
e1
.
printStackTrace
();
if
(
this
.
output
!=
null
){
try
{
this
.
output
.
writeUTF
(
"Over"
);
}
catch
(
IOException
e1
)
{
e1
.
printStackTrace
();
}
}
System
.
out
.
println
(
"Closing connection"
);
this
.
socket
.
close
();
this
.
input
.
close
();
this
.
output
.
close
();
}
...
...
@@ -416,7 +501,7 @@ class Chess implements MouseListener{
}
else
if
(
!
this
.
me
&&
this
.
color
.
equals
(
"B"
)
){
flag
=
true
;
}
if
(
source
.
getColor
()
==
this
.
me
&&
flag
){
if
(
source
.
get
Piece
()
!=
null
&&
source
.
getPiece
().
get
Color
()
==
this
.
me
&&
flag
){
for
(
newJButton
btn
:
this
.
btns
){
if
(
source
!=
btn
&&
source
.
getPiece
()
!=
null
&&
this
.
board
.
canGo
(
source
.
getPiece
(),
btn
.
getNewX
(),
btn
.
getNewY
(),
this
.
color
)
){
...
...
@@ -471,9 +556,9 @@ class Chess implements MouseListener{
caption
.
setText
(
"White Player is playing"
);
this
.
color
=
"W"
;
}
this
.
isSelected
=
false
;
this
.
inMove
=
null
;
}
this
.
isSelected
=
false
;
this
.
inMove
=
null
;
repaint
();
try
{
checkMate
();
...
...
@@ -535,10 +620,13 @@ class Chess implements MouseListener{
public
void
checkMate
()
throws
IOException
{
if
(
this
.
board
.
checkMate
()
!=
null
){
try
{
this
.
output
.
writeUTF
(
"Over"
);
}
catch
(
IOException
e1
)
{
e1
.
printStackTrace
();
if
(
!
this
.
socket
.
isClosed
()
){
try
{
this
.
output
.
writeUTF
(
"Over"
);
}
catch
(
IOException
e1
)
{
e1
.
printStackTrace
();
}
}
String
prompt
=
""
;
if
(
this
.
board
.
checkMate
().
equals
(
"W"
)
){
...
...
@@ -548,12 +636,14 @@ class Chess implements MouseListener{
prompt
=
prompt
.
concat
(
"White Player Won!"
);
}
prompt
=
prompt
.
concat
(
" \nDo you want to start a new game?"
);
this
.
frame
.
dispatchEvent
(
new
WindowEvent
(
this
.
frame
,
WindowEvent
.
WINDOW_CLOSING
));
int
result
=
JOptionPane
.
showConfirmDialog
(
this
.
frame
,
prompt
,
"Finished"
,
JOptionPane
.
YES_NO_OPTION
);
if
(
result
==
JOptionPane
.
YES_OPTION
){
Main
.
rerun
();
}
else
{
System
.
exit
(
0
);
}
this
.
frame
.
dispatchEvent
(
new
WindowEvent
(
this
.
frame
,
WindowEvent
.
WINDOW_CLOSING
));
}
}
...
...
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