Commit d81eeeb2 authored by 9611046's avatar 9611046

correct function that checks a movement dosn't put the king of same color in checkmate completed

parent 21009d47
......@@ -24,11 +24,13 @@ public class Main extends JFrame implements MouseListener {
private static Rook wr01, wr02, br01, br02;
private static Knight wk01, wk02, bk01, bk02;
private static Bishop wb01, wb02, bb01, bb02;
private static Pawn wp[], bp[];
private static Pawn[] wp;
private static Pawn[] bp;
private static Queen wq, bq;
private static King wk, bk;
// private JButton[][] chessBoardSquares ;
private Square[][] chessBoardSquares;
private ArrayList<JButton> outWhitePieces;
private int outWhitePiecesNum = 0;
private ArrayList<JButton> outBlackPieces;
......@@ -39,6 +41,7 @@ public class Main extends JFrame implements MouseListener {
private ArrayList<Square> possibleMoves;
private String whosTurn = "w";
private boolean isMoved = false;
private boolean inDanger = false;
private JFrame myFrame;
private JButton turn;
......@@ -121,11 +124,11 @@ public class Main extends JFrame implements MouseListener {
for (int i = 0; i < 16; i++) {
//outWhitePieces.get(i)=new JButton();
outWhitePieces.get(i).setBackground(new Color(51, 0, 0));
outWhitePieces.get(i).setBackground(new Color(88, 0, 0));
// outWhitePieces.get(i).setBorder(emptyBorder);
northOfWest.add(outWhitePieces.get(i));
// outBlackPieces[i]=new JButton();
outBlackPieces.get(i).setBackground(new Color(51, 0, 0));
outBlackPieces.get(i).setBackground(new Color(88, 0, 0));
// outBlackPieces[i].setBorder(emptyBorder);
southOfWest.add(outBlackPieces.get(i));
}
......@@ -217,8 +220,6 @@ public class Main extends JFrame implements MouseListener {
}
myFrame.setVisible(true);
myFrame.setResizable(true);
myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
......@@ -260,10 +261,18 @@ public class Main extends JFrame implements MouseListener {
}
private boolean isChecked(String color, Square[][] board) {
if (color.equals("w")) return wk.isChecked(board);
else if (color.equals("b")) return bk.isChecked(board);
return false;
}
@Override
public void mouseClicked(MouseEvent e) {
inDanger = false;
//Selecting a square
if (!select) {
((Square) (e.getSource())).selectSquare();
} else {
......@@ -276,7 +285,7 @@ public class Main extends JFrame implements MouseListener {
}
select = true;
//selecting a square and showing it's possible squares
// showing the possible squares of a select
if (((Square) (e.getSource())).getPiece() != null && ((Square) (e.getSource())).getPiece().getPieceColor().equals(whosTurn)) {
lastSquare = ((Square) (e.getSource()));
......@@ -292,35 +301,52 @@ public class Main extends JFrame implements MouseListener {
}
}
}
for (Square ps : possibleMoves) {
ps.makePossible();
}
}
//moving a piece to a possible square
//moving a piece to a possible empty square
else if (((Square) (e.getSource())).getPiece() == null) {
if (((Square) (e.getSource())).isPossibleSquare() && ((Square) (e.getSource())).isSelected()) {
if(lastPiece instanceof King){
((King)(lastPiece)).setx(((Square) (e.getSource())).getMyX());
((King)(lastPiece)).sety(((Square) (e.getSource())).getMyY());
if (lastPiece instanceof King) {
((King) (lastPiece)).setx(((Square) (e.getSource())).getMyX());
((King) (lastPiece)).sety(((Square) (e.getSource())).getMyY());
}
((Square) (e.getSource())).setPiece(lastPiece);
lastSquare.removePiece();
// lastSquare.removePiece();
isMoved = true;
chessBoardSquares[lastSquare.getMyX()][lastSquare.getMyY()].removePiece();
if (isChecked(whosTurn, chessBoardSquares)) {
isMoved = false;
inDanger = true;
((Square) (e.getSource())).removePiece();
chessBoardSquares[lastSquare.getMyX()][lastSquare.getMyY()].setPiece(lastPiece);
if (lastPiece instanceof King) {
((King) (lastPiece)).setx(lastSquare.getMyX());
((King) (lastPiece)).sety(lastSquare.getMyY());
}
}
}
}
//zadan mohreh ba rng mokhalef
else if (!((Square) (e.getSource())).getPiece().getPieceColor().equals(whosTurn)) {
if (((Square) (e.getSource())).isPossibleSquare() && ((Square) (e.getSource())).isSelected()) {
if (!(((Square) (e.getSource())).getPiece() instanceof King)) {
Piece piece = ((Square) (e.getSource())).getPiece();
JLabel imageIcon = new JLabel(new ImageIcon(piece.getImage()));
......@@ -340,14 +366,41 @@ public class Main extends JFrame implements MouseListener {
((King) lastPiece).sety(((Square) (e.getSource())).getMyY());
}
((Square) (e.getSource())).setPiece(lastPiece);
lastSquare.removePiece();
isMoved = true;
chessBoardSquares[lastSquare.getMyX()][lastSquare.getMyY()].removePiece();
if (isChecked(whosTurn, chessBoardSquares)) {
isMoved = false;
inDanger = true;
if (lastPiece instanceof King) {
((King) lastPiece).setx(lastSquare.getMyX());
((King) lastPiece).sety(lastSquare.getMyY());
}
((Square) (e.getSource())).removePiece();
((Square) (e.getSource())).setPiece(piece);
lastSquare.setPiece(lastPiece);
if (piece.getPieceColor().equals("w")) {
outWhitePiecesNum--;
outWhitePieces.get(outWhitePiecesNum).remove(imageIcon);
outWhitePieces.remove(piece);
} else if (piece.getPieceColor().equals("b")) {
outBlackPiecesNum--;
outBlackPieces.get(outBlackPiecesNum).remove(imageIcon);
outBlackPieces.remove(piece);
}
}
}
}
}
if (isMoved) {
changeTurn();
possibleMoves.clear();
......@@ -360,12 +413,27 @@ public class Main extends JFrame implements MouseListener {
}
if (wk.isChecked(chessBoardSquares)) {
turn.setBackground(Color.GREEN);
turn.setFont(new Font("Arial", Font.PLAIN, 30));
turn.setBackground(Color.red);
turn.setFont(new Font("Arial", Font.PLAIN, 25));
turn.setText("White King is checked!");
turn.setForeground(Color.BLACK);
} else if (bk.isChecked(chessBoardSquares)) {
turn.setBackground(Color.RED);
turn.setBackground(Color.red);
turn.setFont(new Font("Arial", Font.PLAIN, 25));
turn.setForeground(Color.BLACK);
turn.setText("Black King is checked!");
} else if (inDanger) {
if (whosTurn.equals("w")) {
turn.setFont(new Font("Arial", Font.PLAIN, 25));
turn.setText("White King is in danger!");
// inDanger = false;
} else if (whosTurn.equals("b")) {
turn.setFont(new Font("Arial", Font.PLAIN, 25));
turn.setBackground(Color.BLACK);
turn.setText("Black King is in danger!");
// inDanger = false;
}
} else {
turn.setText("It's your turn! ");
turn.setForeground(Color.GRAY);
......@@ -373,8 +441,7 @@ public class Main extends JFrame implements MouseListener {
else turn.setBackground(Color.BLACK);
}
System.out.println(wk.getx()+","+wk.gety());
System.out.println(bk.getx()+","+bk.gety());
}
......
......@@ -8,7 +8,7 @@ import java.awt.*;
/**
* A chess board has 64 squares on a square might exist a piece or not
*/
public class Square extends JButton {
public class Square extends JButton {
private int x;
private int y;
......@@ -106,6 +106,7 @@ public class Square extends JButton {
*/
public void removePiece(){
this.piece = null;
if(this.getImage()!=null)
this.remove(image);
// this.remove(this.piece);
// this.remove(image);
......@@ -137,4 +138,10 @@ public class Square extends JButton {
public Piece getPiece(){
return piece;
}
public void setMyPiece(Piece piece){
this.piece = piece;
}
}
......@@ -3,6 +3,7 @@ package pieces;
import game.Square;
import java.util.ArrayList;
import java.util.Iterator;
public class Bishop extends Piece {
public Bishop(String ID, String imagePath, String color) {
......@@ -14,7 +15,7 @@ public class Bishop extends Piece {
public ArrayList<Square> move(Square[][] boardSquares, int x, int y) {
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
ArrayList<Square> possibleSquares=new ArrayList<>();
possibleSquares.clear();
int tempx=x+1,tempy=y-1;
......@@ -79,6 +80,9 @@ public class Bishop extends Piece {
tempx++;
tempy++;
}
return possibleSquares;
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ package pieces;
import game.Square;
import java.util.ArrayList;
import java.util.Iterator;
public class King extends Piece {
private int x,y;
......@@ -14,6 +15,10 @@ public class King extends Piece {
setImage( imagePath);
setPieceColor(color);
}
public King() {
}
public void setx(int x)
{
this.x=x;
......@@ -42,6 +47,9 @@ public class King extends Piece {
if((posx[i]>=0&&posx[i]<8&&posy[i]>=0&&posy[i]<8))
if((squares[posx[i]][posy[i]].getPiece()==null|| !squares[posx[i]][posy[i]].getPiece().getPieceColor().equals(this.getPieceColor())))
possibleSquares.add(squares[posx[i]][posy[i]]);
return possibleSquares;
}
......
......@@ -3,6 +3,7 @@ package pieces;
import game.Square;
import java.util.ArrayList;
import java.util.Iterator;
public class Knight extends Piece {
public Knight(String ID, String imagePath, String color) {
......@@ -21,6 +22,9 @@ public class Knight extends Piece {
if ((boardSquares[posx[i]][posy[i]].getPiece() == null || !boardSquares[posx[i]][posy[i]].getPiece().getPieceColor().equals(this.getPieceColor()))) {
possibleSquares.add(boardSquares[posx[i]][posy[i]]);
}
return possibleSquares;
}
}
......@@ -3,6 +3,7 @@ package pieces;
import game.Square;
import java.util.ArrayList;
import java.util.Iterator;
public class Pawn extends Piece {
......@@ -14,7 +15,7 @@ public class Pawn extends Piece {
}
@Override
public ArrayList<Square> move(Square [][] boardSquares, int x, int y){
public ArrayList<Square> move(Square [][] boardSquares, int x, int y) {
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
// possibleSquares.clear();
ArrayList<Square> possibleSquares=new ArrayList<>();
......@@ -51,6 +52,7 @@ public class Pawn extends Piece {
possibleSquares.add(boardSquares[x + 1][y + 1]);
}
return possibleSquares;
}
......
......@@ -3,6 +3,7 @@ package pieces;
import game.Square;
import java.util.ArrayList;
import java.util.Iterator;
public class Queen extends Piece {
......@@ -127,6 +128,9 @@ public class Queen extends Piece {
tempx++;
tempy++;
}
return possibleSquares;
}
}
......@@ -3,6 +3,7 @@ package pieces;
import game.Square;
import java.util.ArrayList;
import java.util.Iterator;
public class Rook extends Piece {
public Rook(String ID,String imagePath,String color){
......@@ -14,7 +15,7 @@ public class Rook extends Piece {
public ArrayList<Square> move(Square [][] boardSquares, int x, int y){
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
ArrayList<Square> possibleSquares=new ArrayList<>();
possibleSquares.clear();
int tempx=x-1;
while(tempx>=0)
......@@ -72,6 +73,10 @@ public class Rook extends Piece {
}
tempy++;
}
return possibleSquares;
}
}
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