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