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
37d719c1
Commit
37d719c1
authored
May 18, 2019
by
Omid Sayfun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Final Looks Checked
parent
2a18e034
Pipeline
#617
canceled with stages
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
592 additions
and
558 deletions
+592
-558
Main.java
src/Main.java
+37
-5
Board.java
src/lab/game/Board.java
+555
-553
No files found.
src/Main.java
View file @
37d719c1
import
javax.imageio.ImageIO
;
import
javax.swing.*
;
import
javax.swing.border.EmptyBorder
;
import
java.awt.*
;
import
java.awt.event.MouseEvent
;
import
java.awt.event.MouseListener
;
import
java.awt.event.WindowEvent
;
import
java.awt.image.BufferedImage
;
import
java.util.*
;
import
lab.game.*
;
...
...
@@ -13,6 +15,10 @@ public class Main{
Chess
newChess
=
new
Chess
();
newChess
.
run
();
}
public
static
void
rerun
(){
Chess
newChess
=
new
Chess
();
newChess
.
run
();
}
}
/**
...
...
@@ -87,6 +93,7 @@ class Chess implements MouseListener{
private
String
color
=
"W"
;
private
boolean
isSelected
=
false
;
private
newJButton
inMove
=
null
;
private
JFrame
frame
;
private
ArrayList
<
newJButton
>
btns
=
new
ArrayList
<
newJButton
>();
private
ArrayList
<
newJButton
>
whiteLost
=
new
ArrayList
<>();
private
ArrayList
<
newJButton
>
blackLost
=
new
ArrayList
<>();
...
...
@@ -178,7 +185,7 @@ class Chess implements MouseListener{
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
(
4
,
51
,
106
));
btn
.
setBackground
(
new
Color
(
4
6
,
83
,
106
));
}
boardPanel
.
add
(
btn
);
}
...
...
@@ -187,34 +194,40 @@ class Chess implements MouseListener{
JPanel
leftDivider
=
new
JPanel
(
new
GridLayout
(
3
,
1
));
// Top Left
JPanel
topLeft
=
new
JPanel
(
new
GridLayout
(
2
,
8
));
topLeft
.
setBorder
(
new
EmptyBorder
(
50
,
10
,
50
,
0
));
for
(
char
i
=
0
;
i
<
2
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
newJButton
btn
=
new
newJButton
();
ImageIcon
icon
=
new
ImageIcon
(
new
BufferedImage
(
40
,
40
,
BufferedImage
.
TYPE_INT_ARGB
));
btn
.
setIcon
(
icon
);
btn
.
setPreferredSize
(
new
Dimension
(
70
,
70
));
btn
.
setFocusable
(
false
);
btn
.
setBackground
(
new
Color
(
0
,
255
,
0
));
btn
.
setBackground
(
new
Color
(
46
,
83
,
106
));
topLeft
.
add
(
btn
);
whiteLost
.
add
(
btn
);
}
}
// Middle Left
JPanel
middleLeft
=
new
JPanel
();
middleLeft
.
setBorder
(
new
EmptyBorder
(
100
,
0
,
0
,
0
));
JLabel
playing
=
new
JLabel
(
"White Player is playing"
);
playing
.
setFont
(
new
Font
(
"Segoe UI"
,
Font
.
BOLD
,
32
));
caption
=
playing
;
middleLeft
.
add
(
playing
);
leftDivider
.
add
(
middleLeft
);
// Bottom Left
JPanel
bottomLeft
=
new
JPanel
(
new
GridLayout
(
2
,
8
));
bottomLeft
.
setBorder
(
new
EmptyBorder
(
50
,
10
,
50
,
0
));
for
(
int
i
=
0
;
i
<
2
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
newJButton
btn
=
new
newJButton
();
ImageIcon
icon
=
new
ImageIcon
(
new
BufferedImage
(
40
,
40
,
BufferedImage
.
TYPE_INT_ARGB
));
btn
.
setPreferredSize
(
new
Dimension
(
70
,
70
));
btn
.
setIcon
(
icon
);
btn
.
setFocusable
(
false
);
btn
.
setBackground
(
new
Color
(
0
,
255
,
0
));
btn
.
setBackground
(
new
Color
(
219
,
217
,
164
));
bottomLeft
.
add
(
btn
);
blackLost
.
add
(
btn
);
}
...
...
@@ -232,6 +245,8 @@ class Chess implements MouseListener{
frame
.
setDefaultCloseOperation
(
JFrame
.
DISPOSE_ON_CLOSE
);
frame
.
pack
();
frame
.
setVisible
(
true
);
this
.
frame
=
frame
;
}
/**
...
...
@@ -306,14 +321,14 @@ class Chess implements MouseListener{
if
(
(
btn
.
getNewY
()
%
2
==
1
&&
btn
.
getNewX
()
%
2
==
1
)
||
(
btn
.
getNewY
()
%
2
==
0
&&
btn
.
getNewX
()
%
2
==
0
)
){
btn
.
setBackground
(
new
Color
(
219
,
217
,
164
));
}
else
{
btn
.
setBackground
(
new
Color
(
4
,
51
,
106
));
btn
.
setBackground
(
new
Color
(
4
6
,
83
,
106
));
}
this
.
isSelected
=
false
;
this
.
inMove
=
null
;
}
for
(
newJButton
btn
:
this
.
whiteLost
)
{
if
(
btn
.
getPiece
()
!=
null
){
Image
img
=
new
BufferedImage
(
40
,
4
0
,
BufferedImage
.
TYPE_INT_ARGB
);
Image
img
=
new
BufferedImage
(
50
,
5
0
,
BufferedImage
.
TYPE_INT_ARGB
);
try
{
img
=
ImageIO
.
read
(
Chess
.
class
.
getResource
(
"resources/images/"
+
btn
.
getPiece
().
getName
().
toLowerCase
()
+
".png"
));
}
catch
(
IOException
e1
)
{
...
...
@@ -353,6 +368,23 @@ class Chess implements MouseListener{
// this.isSelected = false;
// this.inMove = null;
}
if
(
this
.
board
.
checkMate
()
!=
null
){
String
prompt
=
""
;
if
(
this
.
board
.
checkMate
().
equals
(
"W"
)
){
prompt
=
prompt
.
concat
(
"Black Player Won!"
);
}
else
{
prompt
=
prompt
.
concat
(
"White Player Won!"
);
}
prompt
=
prompt
.
concat
(
" \nDo you want to start a new game?"
);
int
result
=
JOptionPane
.
showConfirmDialog
(
this
.
frame
,
prompt
,
"Finished"
,
JOptionPane
.
YES_NO_OPTION
);
if
(
result
==
JOptionPane
.
YES_OPTION
){
Main
.
rerun
();
}
this
.
frame
.
dispatchEvent
(
new
WindowEvent
(
this
.
frame
,
WindowEvent
.
WINDOW_CLOSING
));
}
}
}
...
...
src/lab/game/Board.java
View file @
37d719c1
...
...
@@ -302,21 +302,25 @@ public class Board{
if
(
canGo
(
fromArray
,
toArray
,
color
)
){
ArrayList
<
Piece
>
enemy
=
null
;
ArrayList
<
Piece
>
selector
=
null
;
Piece
found
=
null
;
Piece
attack
=
null
;
if
(
color
.
equals
(
"W"
)
){
enemy
=
this
.
blackPieces
;
selector
=
this
.
whitePieces
;
found
=
takenByWhite
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
attack
=
takenByBlack
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
}
else
{
enemy
=
this
.
whitePieces
;
selector
=
this
.
blackPieces
;
found
=
takenByBlack
(
fromArray
[
0
]
-
'0'
,
fromArray
[
1
]);
attack
=
takenByWhite
(
toArray
[
0
]
-
'0'
,
toArray
[
1
]);
}
if
(
attack
!=
null
){
enemy
.
remove
(
attack
);
this
.
allPieces
.
remove
(
attack
);
}
if
(
found
instanceof
Pawn
&&
((
Pawn
)
found
).
getFirstMove
()
==
true
){
((
Pawn
)
found
).
setFirstMove
(
false
);
...
...
@@ -386,35 +390,35 @@ public class Board{
private
boolean
kingMate
(
ArrayList
<
Piece
>
base
,
ArrayList
<
Piece
>
enemy
){
int
kingY
=
0
;
char
kingX
=
'A'
;
King
king
=
null
;
String
color
=
null
;
for
(
Piece
p
:
base
){
if
(
p
instanceof
King
){
kingY
=
p
.
y
;
kingX
=
p
.
x
;
king
=
(
King
)
p
;
color
=
Character
.
toString
(
p
.
getName
().
charAt
(
0
));
}
}
int
[]
X
=
{
1
,
1
,
1
,
0
,
-
1
,
-
1
,
-
1
,
0
};
int
[]
Y
=
{
1
,
0
,
-
1
,
-
1
,
-
1
,
0
,
1
,
1
};
int
counter
=
0
;
for
(
int
i
=
0
;
i
<
8
;
i
++){
char
tempX
=
(
char
)(
kingX
+
X
[
i
]);
int
tempY
=
kingY
+
Y
[
i
];
boolean
flag
=
false
;
for
(
Piece
p
:
enemy
){
if
(
p
.
canMove
(
tempX
,
tempY
)
){
if
(
canGo
(
king
,
tempX
,
tempY
,
color
)
){
if
(
p
.
checkWay
(
this
.
allPieces
,
tempX
,
tempY
)
){
flag
=
true
;
counter
++;
}
}
}
if
(
!
flag
){
if
(
counter
==
0
&&
virtualCheck
(
enemy
,
kingX
,
kingY
)
){
return
true
;
}
else
{
return
false
;
}
}
return
true
;
}
/**
* Check which player is checkmated
...
...
@@ -422,14 +426,7 @@ public class Board{
* @return W for white, B for Black
*/
public
String
checkMate
(){
ArrayList
<
Piece
>
all
=
new
ArrayList
<
Piece
>();
for
(
Piece
p
:
this
.
blackPieces
){
all
.
add
(
p
);
}
for
(
Piece
p
:
this
.
whitePieces
){
all
.
add
(
p
);
}
if
(
kingMate
(
this
.
whitePieces
,
this
.
blackPieces
)
){
if
(
kingMate
(
this
.
blackPieces
,
this
.
whitePieces
)
){
return
"B"
;
}
else
if
(
kingMate
(
this
.
whitePieces
,
this
.
blackPieces
)
){
...
...
@@ -469,6 +466,8 @@ public class Board{
toBusy
=
isTakenByBlack
(
y
,
x
);
attack
=
takenByWhite
(
y
,
x
);
}
if
(
x
>=
'A'
&&
x
<=
'H'
&&
y
>=
1
&&
y
<=
8
){
if
(
found
.
getColor
()
==
booleanColor
&&
!
toBusy
){
// Found and to is not busy
if
(
kingCheck
(
selector
,
enemy
)
&&
!(
found
instanceof
King
)
){
...
...
@@ -499,6 +498,9 @@ public class Board{
}
else
{
return
false
;
}
}
else
{
return
false
;
}
}
public
boolean
canGo
(
char
[]
fromArray
,
char
[]
toArray
,
String
color
){
ArrayList
<
Piece
>
selector
=
null
;
...
...
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