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
Expand all
Hide 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
This diff is collapsed.
Click to expand it.
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