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
2a18e034
Commit
2a18e034
authored
May 18, 2019
by
Omid Sayfun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Graphic bugs fixed and Logic Improved
parent
ff795df0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
122 additions
and
16 deletions
+122
-16
Main.java
src/Main.java
+94
-7
Board.java
src/lab/game/Board.java
+28
-9
No files found.
src/Main.java
View file @
2a18e034
...
...
@@ -24,6 +24,11 @@ class newJButton extends JButton{
private
char
X
;
private
int
Y
;
public
newJButton
(){
this
.
piece
=
null
;
this
.
setIcon
(
null
);
}
/**
* Make new button
* @param X The X-Axis
...
...
@@ -79,11 +84,13 @@ class newJButton extends JButton{
*/
class
Chess
implements
MouseListener
{
private
Board
board
=
null
;
private
String
color
=
"
B
"
;
private
String
color
=
"
W
"
;
private
boolean
isSelected
=
false
;
private
newJButton
inMove
=
null
;
private
ArrayList
<
newJButton
>
btns
=
new
ArrayList
<
newJButton
>();
private
ArrayList
<
newJButton
>
whiteLost
=
new
ArrayList
<>();
private
ArrayList
<
newJButton
>
blackLost
=
new
ArrayList
<>();
private
JLabel
caption
;
/**
* Initial Run
*/
...
...
@@ -182,29 +189,34 @@ class Chess implements MouseListener{
JPanel
topLeft
=
new
JPanel
(
new
GridLayout
(
2
,
8
));
for
(
char
i
=
0
;
i
<
2
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
JButton
btn
=
new
JButton
();
newJButton
btn
=
new
new
JButton
();
ImageIcon
icon
=
new
ImageIcon
(
new
BufferedImage
(
40
,
40
,
BufferedImage
.
TYPE_INT_ARGB
));
btn
.
setIcon
(
icon
);
btn
.
setBackground
(
Color
.
WHITE
);
btn
.
setFocusable
(
false
);
btn
.
setBackground
(
new
Color
(
0
,
255
,
0
));
topLeft
.
add
(
btn
);
whiteLost
.
add
(
btn
);
}
}
// Middle Left
JPanel
middleLeft
=
new
JPanel
();
JLabel
playing
=
new
JLabel
(
"White Player is in control"
);
JLabel
playing
=
new
JLabel
(
"White Player is playing"
);
caption
=
playing
;
middleLeft
.
add
(
playing
);
leftDivider
.
add
(
middleLeft
);
// Bottom Left
JPanel
bottomLeft
=
new
JPanel
(
new
GridLayout
(
2
,
8
));
for
(
int
i
=
0
;
i
<
2
;
i
++){
for
(
int
j
=
0
;
j
<
8
;
j
++){
JButton
btn
=
new
JButton
();
newJButton
btn
=
new
new
JButton
();
ImageIcon
icon
=
new
ImageIcon
(
new
BufferedImage
(
40
,
40
,
BufferedImage
.
TYPE_INT_ARGB
));
btn
.
setIcon
(
icon
);
btn
.
setBackground
(
Color
.
BLACK
);
btn
.
setFocusable
(
false
);
btn
.
setBackground
(
new
Color
(
0
,
255
,
0
));
bottomLeft
.
add
(
btn
);
blackLost
.
add
(
btn
);
}
}
// Add Panels
...
...
@@ -251,8 +263,16 @@ class Chess implements MouseListener{
}
else
{
if
(
this
.
board
.
canGo
(
this
.
inMove
.
getPiece
(),
source
.
getNewX
(),
source
.
getNewY
(),
this
.
color
)
){
if
(
this
.
board
.
canAttack
(
this
.
inMove
.
getPiece
(),
source
.
getNewX
(),
source
.
getNewY
(),
this
.
color
)
){
Piece
temp
=
source
.
getPiece
();
source
.
setPiece
(
this
.
inMove
.
getPiece
());
this
.
inMove
.
setPiece
(
null
);
if
(
this
.
color
.
equals
(
"W"
)
){
killBlack
(
temp
);
}
else
{
killWhite
(
temp
);
}
}
else
{
Piece
temp
=
this
.
inMove
.
getPiece
();
this
.
inMove
.
setPiece
(
source
.
getPiece
());
...
...
@@ -260,8 +280,11 @@ class Chess implements MouseListener{
}
System
.
out
.
println
(
this
.
board
.
move
(
inMove
.
getNewY
()
+
Character
.
toString
(
inMove
.
getNewX
()),
source
.
getNewY
()
+
Character
.
toString
(
source
.
getNewX
()),
this
.
color
));
if
(
this
.
color
.
equals
(
"W"
)){
caption
.
setText
(
"Black Player is playing"
);
this
.
color
=
"B"
;
}
else
{
caption
.
setText
(
"White Player is playing"
);
this
.
color
=
"W"
;
}
}
...
...
@@ -288,7 +311,71 @@ class Chess implements MouseListener{
this
.
isSelected
=
false
;
this
.
inMove
=
null
;
}
for
(
newJButton
btn
:
this
.
whiteLost
)
{
if
(
btn
.
getPiece
()
!=
null
){
Image
img
=
new
BufferedImage
(
40
,
40
,
BufferedImage
.
TYPE_INT_ARGB
);
try
{
img
=
ImageIO
.
read
(
Chess
.
class
.
getResource
(
"resources/images/"
+
btn
.
getPiece
().
getName
().
toLowerCase
()
+
".png"
));
}
catch
(
IOException
e1
)
{
e1
.
printStackTrace
();
}
btn
.
setIcon
(
new
ImageIcon
(
img
));
}
else
{
btn
.
setIcon
(
new
ImageIcon
(
new
BufferedImage
(
40
,
40
,
BufferedImage
.
TYPE_INT_ARGB
)));
}
// 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));
// }
// this.isSelected = false;
// this.inMove = null;
}
for
(
newJButton
btn
:
this
.
blackLost
)
{
if
(
btn
.
getPiece
()
!=
null
){
Image
img
=
new
BufferedImage
(
40
,
40
,
BufferedImage
.
TYPE_INT_ARGB
);
try
{
img
=
ImageIO
.
read
(
Chess
.
class
.
getResource
(
"resources/images/"
+
btn
.
getPiece
().
getName
().
toLowerCase
()
+
".png"
));
}
catch
(
IOException
e1
)
{
e1
.
printStackTrace
();
}
btn
.
setIcon
(
new
ImageIcon
(
img
));
}
else
{
btn
.
setIcon
(
new
ImageIcon
(
new
BufferedImage
(
40
,
40
,
BufferedImage
.
TYPE_INT_ARGB
)));
}
// 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));
// }
// this.isSelected = false;
// this.inMove = null;
}
}
}
public
boolean
killWhite
(
Piece
p
){
for
(
newJButton
btn
:
this
.
whiteLost
){
if
(
btn
.
getPiece
()
==
null
){
btn
.
setPiece
(
p
);
return
true
;
}
}
return
false
;
}
public
boolean
killBlack
(
Piece
p
){
for
(
newJButton
btn
:
this
.
blackLost
){
if
(
btn
.
getPiece
()
==
null
){
btn
.
setPiece
(
p
);
return
true
;
}
}
return
false
;
}
@Override
...
...
src/lab/game/Board.java
View file @
2a18e034
...
...
@@ -338,6 +338,19 @@ public class Board{
}
}
public
boolean
virtualCheck
(
ArrayList
<
Piece
>
enemy
,
char
x
,
int
y
){
for
(
Piece
p
:
enemy
){
if
(
p
.
canMove
(
x
,
y
)
){
// Check if move is valid
if
(
p
.
checkWay
(
this
.
allPieces
,
x
,
y
)
){
return
true
;
}
}
}
return
false
;
}
/**
* Check if playing player's king is checked
* @author Omiid
...
...
@@ -356,15 +369,9 @@ public class Board{
kingX
=
p
.
x
;
}
}
for
(
Piece
p
:
enemy
){
if
(
p
.
canMove
(
kingX
,
kingY
)
){
// Check if move is valid
if
(
p
.
checkWay
(
this
.
allPieces
,
kingX
,
kingY
)
){
if
(
virtualCheck
(
enemy
,
kingX
,
kingY
)
){
System
.
out
.
println
(
p
.
getName
());
return
true
;
}
}
return
true
;
}
return
false
;
}
...
...
@@ -472,6 +479,12 @@ public class Board{
if
(
found
.
checkWay
(
this
.
allPieces
,
x
,
y
)
){
if
(
(
found
instanceof
Pawn
)
&&
found
.
crossMove
(
x
,
y
)
&&
attack
==
null
){
return
false
;
}
else
if
(
(
found
instanceof
Pawn
)
&&
attack
!=
null
&&
found
.
getX
()
-
attack
.
getX
()
==
0
){
return
false
;
}
else
if
(
(
found
instanceof
King
)
&&
virtualCheck
(
enemy
,
x
,
y
)){
return
false
;
}
else
{
return
true
;
...
...
@@ -516,9 +529,15 @@ public class Board{
return
false
;
}
else
{
if
(
found
.
canMove
(
toArray
[
1
],
toArray
[
0
]
-
'0'
)
){
// Check if move is valid
if
(
found
.
checkWay
(
this
.
allPieces
,
toArray
[
1
],
toArray
[
0
]
-
'0'
)){
if
(
found
.
checkWay
(
this
.
allPieces
,
toArray
[
1
],
toArray
[
0
]
-
'0'
)
){
if
(
(
found
instanceof
Pawn
)
&&
found
.
crossMove
(
toArray
[
1
],
toArray
[
0
]
-
'0'
)
&&
attack
==
null
){
return
false
;
}
else
if
(
(
found
instanceof
Pawn
)
&&
toArray
[
1
]
-
fromArray
[
1
]
==
0
&&
attack
!=
null
){
return
false
;
}
else
if
(
(
found
instanceof
King
)
&&
virtualCheck
(
enemy
,
toArray
[
1
],
toArray
[
0
]
-
'0'
)){
return
false
;
}
else
{
return
true
;
...
...
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