Commit 2a18e034 authored by Omid Sayfun's avatar Omid Sayfun

Graphic bugs fixed and Logic Improved

parent ff795df0
...@@ -24,6 +24,11 @@ class newJButton extends JButton{ ...@@ -24,6 +24,11 @@ class newJButton extends JButton{
private char X; private char X;
private int Y; private int Y;
public newJButton(){
this.piece = null;
this.setIcon(null);
}
/** /**
* Make new button * Make new button
* @param X The X-Axis * @param X The X-Axis
...@@ -79,11 +84,13 @@ class newJButton extends JButton{ ...@@ -79,11 +84,13 @@ class newJButton extends JButton{
*/ */
class Chess implements MouseListener{ class Chess implements MouseListener{
private Board board = null; private Board board = null;
private String color = "B"; private String color = "W";
private boolean isSelected = false; private boolean isSelected = false;
private newJButton inMove = null; private newJButton inMove = null;
private ArrayList<newJButton> btns = new ArrayList<newJButton>(); private ArrayList<newJButton> btns = new ArrayList<newJButton>();
private ArrayList<newJButton> whiteLost = new ArrayList<>();
private ArrayList<newJButton> blackLost = new ArrayList<>();
private JLabel caption;
/** /**
* Initial Run * Initial Run
*/ */
...@@ -182,29 +189,34 @@ class Chess implements MouseListener{ ...@@ -182,29 +189,34 @@ class Chess implements MouseListener{
JPanel topLeft = new JPanel(new GridLayout(2, 8)); JPanel topLeft = new JPanel(new GridLayout(2, 8));
for(char i = 0; i < 2; i++){ for(char i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){ for(int j = 0; j < 8; j++){
JButton btn = new JButton(); newJButton btn = new newJButton();
ImageIcon icon = new ImageIcon( ImageIcon icon = new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB)); new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB));
btn.setIcon(icon); btn.setIcon(icon);
btn.setBackground(Color.WHITE); btn.setFocusable(false);
btn.setBackground(new Color(0,255,0));
topLeft.add(btn); topLeft.add(btn);
whiteLost.add(btn);
} }
} }
// Middle Left // Middle Left
JPanel middleLeft = new JPanel(); 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); middleLeft.add(playing);
leftDivider.add(middleLeft); leftDivider.add(middleLeft);
// Bottom Left // Bottom Left
JPanel bottomLeft = new JPanel(new GridLayout(2, 8)); JPanel bottomLeft = new JPanel(new GridLayout(2, 8));
for(int i = 0; i < 2; i++){ for(int i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){ for(int j = 0; j < 8; j++){
JButton btn = new JButton(); newJButton btn = new newJButton();
ImageIcon icon = new ImageIcon( ImageIcon icon = new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB)); new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB));
btn.setIcon(icon); btn.setIcon(icon);
btn.setBackground(Color.BLACK); btn.setFocusable(false);
btn.setBackground(new Color(0,255,0));
bottomLeft.add(btn); bottomLeft.add(btn);
blackLost.add(btn);
} }
} }
// Add Panels // Add Panels
...@@ -251,8 +263,16 @@ class Chess implements MouseListener{ ...@@ -251,8 +263,16 @@ class Chess implements MouseListener{
}else{ }else{
if( this.board.canGo(this.inMove.getPiece(), source.getNewX(), source.getNewY(), this.color) ){ 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) ){ if( this.board.canAttack(this.inMove.getPiece(), source.getNewX(), source.getNewY(), this.color) ){
Piece temp = source.getPiece();
source.setPiece(this.inMove.getPiece()); source.setPiece(this.inMove.getPiece());
this.inMove.setPiece(null); this.inMove.setPiece(null);
if( this.color.equals("W") ){
killBlack(temp);
}else{
killWhite(temp);
}
}else{ }else{
Piece temp = this.inMove.getPiece(); Piece temp = this.inMove.getPiece();
this.inMove.setPiece(source.getPiece()); this.inMove.setPiece(source.getPiece());
...@@ -260,8 +280,11 @@ class Chess implements MouseListener{ ...@@ -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)); 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")){ if( this.color.equals("W")){
caption.setText("Black Player is playing");
this.color = "B"; this.color = "B";
}else{ }else{
caption.setText("White Player is playing");
this.color = "W"; this.color = "W";
} }
} }
...@@ -288,7 +311,71 @@ class Chess implements MouseListener{ ...@@ -288,7 +311,71 @@ class Chess implements MouseListener{
this.isSelected = false; this.isSelected = false;
this.inMove = null; 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 @Override
......
...@@ -338,6 +338,19 @@ public class Board{ ...@@ -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 * Check if playing player's king is checked
* @author Omiid * @author Omiid
...@@ -356,16 +369,10 @@ public class Board{ ...@@ -356,16 +369,10 @@ public class Board{
kingX = p.x; kingX = p.x;
} }
} }
for(Piece p : enemy){ if( virtualCheck(enemy, kingX, kingY) ){
if( p.canMove(kingX, kingY) ){ // Check if move is valid
if( p.checkWay(this.allPieces, kingX, kingY) ){
System.out.println(p.getName());
return true; return true;
} }
}
}
return false; return false;
} }
...@@ -472,6 +479,12 @@ public class Board{ ...@@ -472,6 +479,12 @@ public class Board{
if( found.checkWay(this.allPieces, x, y) ){ if( found.checkWay(this.allPieces, x, y) ){
if( (found instanceof Pawn) && found.crossMove(x, y) && attack == null ){ 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; return false;
}else{ }else{
return true; return true;
...@@ -516,9 +529,15 @@ public class Board{ ...@@ -516,9 +529,15 @@ public class Board{
return false; return false;
}else{ }else{
if( found.canMove(toArray[1], toArray[0] - '0') ){ // Check if move is valid 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 ){ 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; return false;
}else{ }else{
return true; return true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment