Commit 06a3cff9 authored by 9611046's avatar 9611046

King position corected

parent 05d5172b
...@@ -173,19 +173,21 @@ private Main() { ...@@ -173,19 +173,21 @@ private Main() {
} }
else if (i == 0 && j == 3) { else if (i == 0 && j == 3) {
//piece = bk; //piece = bk;
chessBoardSquares[i][j].setPiece(bk); chessBoardSquares[i][j].setPiece(bq);
} }
else if (i == 0 && j == 4) { else if (i == 0 && j == 4) {
//piece = bq; //piece = bq;
chessBoardSquares[i][j].setPiece(bq);
chessBoardSquares[i][j].setPiece(bk);
} }
else if (i == 7 && j == 3) { else if (i == 7 && j == 3) {
//piece = wk; //piece = wk;
chessBoardSquares[i][j].setPiece(wk); chessBoardSquares[i][j].setPiece(wq);
} }
else if (i == 7 && j == 4) { else if (i == 7 && j == 4) {
//piece = wq; //piece = wq;
chessBoardSquares[i][j].setPiece(wq);
chessBoardSquares[i][j].setPiece(wk);
} }
else if (i == 1) { else if (i == 1) {
// piece = bp[j]; // piece = bp[j];
......
...@@ -28,6 +28,12 @@ public class Square extends JButton { ...@@ -28,6 +28,12 @@ public class Square extends JButton {
this.y = y; this.y = y;
}
public Square(int x, int y,Piece piece){
this.x=x;
this.y = y;
this.piece=piece;
} }
/** /**
...@@ -37,9 +43,11 @@ public class Square extends JButton { ...@@ -37,9 +43,11 @@ public class Square extends JButton {
public void setPiece(Piece piece) { public void setPiece(Piece piece) {
this.piece = piece; this.piece = piece;
hasPiece = true; hasPiece = true;
ImageIcon img=new ImageIcon(this.getClass().getResource(piece.getImage())); // ImageIcon img=new ImageIcon(this.getClass().getResource(piece.getImage()));
image=new JLabel(img); // image=new JLabel(img);
this.add(image); // JLabel imgLabel = new JLabel(new ImageIcon(piece.getImage()));
this.add(new JLabel(new ImageIcon(piece.getImage())));
// this.add(image);
} }
/** /**
......
...@@ -23,7 +23,7 @@ public class Bishop extends Piece { ...@@ -23,7 +23,7 @@ public class Bishop extends Piece {
{ {
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
} }
else if(boardSquares[tempx][tempy].getPiece().getPieceColor()==this.getPieceColor()) else if(boardSquares[tempx][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else else
{ {
...@@ -38,7 +38,7 @@ public class Bishop extends Piece { ...@@ -38,7 +38,7 @@ public class Bishop extends Piece {
{ {
if(boardSquares[tempx][tempy].getPiece()==null) if(boardSquares[tempx][tempy].getPiece()==null)
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
else if(boardSquares[tempx][tempy].getPiece().getPieceColor()==this.getPieceColor()) else if(boardSquares[tempx][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else else
{ {
...@@ -53,7 +53,7 @@ public class Bishop extends Piece { ...@@ -53,7 +53,7 @@ public class Bishop extends Piece {
{ {
if(boardSquares[tempx][tempy].getPiece()==null) if(boardSquares[tempx][tempy].getPiece()==null)
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
else if(boardSquares[tempx][tempy].getPiece().getPieceColor()==this.getPieceColor()) else if(boardSquares[tempx][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else else
{ {
...@@ -68,7 +68,7 @@ public class Bishop extends Piece { ...@@ -68,7 +68,7 @@ public class Bishop extends Piece {
{ {
if(boardSquares[tempx][tempy].getPiece()==null) if(boardSquares[tempx][tempy].getPiece()==null)
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
else if(boardSquares[tempx][tempy].getPiece().getPieceColor()==this.getPieceColor()) else if(boardSquares[tempx][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else else
{ {
......
...@@ -16,7 +16,7 @@ public class Knight extends Piece { ...@@ -16,7 +16,7 @@ public class Knight extends Piece {
int posy[] = {y - 2, y + 2, y - 1, y + 1, y - 2, y + 2, y - 1, y + 1}; int posy[] = {y - 2, y + 2, y - 1, y + 1, y - 2, y + 2, y - 1, y + 1};
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
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 ((boardSquares[posx[i]][posy[i]].getPiece() == null || boardSquares[posx[i]][posy[i]].getPiece().getPieceColor() != 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;
......
...@@ -26,9 +26,9 @@ public class Pawn extends Piece { ...@@ -26,9 +26,9 @@ public class Pawn extends Piece {
possibleSquares.add(boardSquares[4][y]); possibleSquares.add(boardSquares[4][y]);
} }
} }
if ((y > 0) && (boardSquares[x - 1][y - 1].getPiece() != null) && (boardSquares[x - 1][y - 1].getPiece().getPieceColor() != this.getPieceColor())) if ((y > 0) && (boardSquares[x - 1][y - 1].getPiece() != null) && (!boardSquares[x - 1][y - 1].getPiece().getPieceColor().equals(this.getPieceColor())))
possibleSquares.add(boardSquares[x - 1][y - 1]); possibleSquares.add(boardSquares[x - 1][y - 1]);
if ((y < 7) && (boardSquares[x - 1][y + 1].getPiece() != null) && (boardSquares[x - 1][y + 1].getPiece().getPieceColor() != this.getPieceColor())) if ((y < 7) && (boardSquares[x - 1][y + 1].getPiece() != null) && (!boardSquares[x - 1][y + 1].getPiece().getPieceColor().equals(this.getPieceColor())))
possibleSquares.add(boardSquares[x - 1][y + 1]); possibleSquares.add(boardSquares[x - 1][y + 1]);
} }
else if(getPieceColor().equals("b")) { else if(getPieceColor().equals("b")) {
...@@ -41,9 +41,9 @@ public class Pawn extends Piece { ...@@ -41,9 +41,9 @@ public class Pawn extends Piece {
possibleSquares.add(boardSquares[3][y]); possibleSquares.add(boardSquares[3][y]);
} }
} }
if ((y > 0) && (boardSquares[x + 1][y - 1].getPiece() != null) && (boardSquares[x + 1][y - 1].getPiece().getPieceColor() != this.getPieceColor())) if ((y > 0) && (boardSquares[x + 1][y - 1].getPiece() != null) && (!boardSquares[x + 1][y - 1].getPiece().getPieceColor().equals(this.getPieceColor())))
possibleSquares.add(boardSquares[x + 1][y - 1]); possibleSquares.add(boardSquares[x + 1][y - 1]);
if ((y < 7) && (boardSquares[x + 1][y + 1].getPiece() != null) && (boardSquares[x + 1][y + 1].getPiece().getPieceColor() != this.getPieceColor())) if ((y < 7) && (boardSquares[x + 1][y + 1].getPiece() != null) && (!boardSquares[x + 1][y + 1].getPiece().getPieceColor().equals(this.getPieceColor())))
possibleSquares.add(boardSquares[x + 1][y + 1]); possibleSquares.add(boardSquares[x + 1][y + 1]);
} }
......
package pieces; package pieces;
import game.Square;
import java.util.ArrayList;
/** /**
* Piece is an abstract class which means that it cannot be instantiated * Piece is an abstract class which means that it cannot be instantiated
*Rook, Knight, Queen, King and Pawn are subclasses of Piece *Rook, Knight, Queen, King and Pawn are subclasses of Piece
...@@ -10,6 +14,13 @@ private String pieceColor; ...@@ -10,6 +14,13 @@ private String pieceColor;
private String imagePath; private String imagePath;
//possibleSquares is protected in Piece class therefore subclasses have access to it
//for all subclasses used one arrayList and before using it in each class content will be cleared
protected ArrayList<Square> possibleSquares = new ArrayList<>();
public void setImage(String image) { public void setImage(String image) {
imagePath = image; imagePath = image;
} }
......
...@@ -5,6 +5,7 @@ import game.Square; ...@@ -5,6 +5,7 @@ import game.Square;
import java.util.ArrayList; import java.util.ArrayList;
public class Queen extends Piece { public class Queen extends Piece {
public Queen(String ID,String imagePath,String color){ public Queen(String ID,String imagePath,String color){
setPieceId(ID); setPieceId(ID);
setImage( imagePath); setImage( imagePath);
...@@ -17,7 +18,7 @@ public class Queen extends Piece { ...@@ -17,7 +18,7 @@ public class Queen extends Piece {
while (tempx >= 0) { while (tempx >= 0) {
if (boardSquares[tempx][y].getPiece() == null) if (boardSquares[tempx][y].getPiece() == null)
possibleSquares.add(boardSquares[tempx][y]); possibleSquares.add(boardSquares[tempx][y]);
else if (boardSquares[tempx][y].getPiece().getPieceColor() == this.getPieceColor()) else if (boardSquares[tempx][y].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else { else {
possibleSquares.add(boardSquares[tempx][y]); possibleSquares.add(boardSquares[tempx][y]);
...@@ -30,7 +31,7 @@ public class Queen extends Piece { ...@@ -30,7 +31,7 @@ public class Queen extends Piece {
while (tempx < 8) { while (tempx < 8) {
if (boardSquares[tempx][y].getPiece() == null) if (boardSquares[tempx][y].getPiece() == null)
possibleSquares.add(boardSquares[tempx][y]); possibleSquares.add(boardSquares[tempx][y]);
else if (boardSquares[tempx][y].getPiece().getPieceColor() == this.getPieceColor()) else if (boardSquares[tempx][y].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else { else {
possibleSquares.add(boardSquares[tempx][y]); possibleSquares.add(boardSquares[tempx][y]);
...@@ -45,7 +46,7 @@ public class Queen extends Piece { ...@@ -45,7 +46,7 @@ public class Queen extends Piece {
while (tempy >= 0) { while (tempy >= 0) {
if (boardSquares[x][tempy].getPiece() == null) if (boardSquares[x][tempy].getPiece() == null)
possibleSquares.add(boardSquares[x][tempy]); possibleSquares.add(boardSquares[x][tempy]);
else if (boardSquares[x][tempy].getPiece().getPieceColor() == this.getPieceColor()) else if (boardSquares[x][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else { else {
possibleSquares.add(boardSquares[x][tempy]); possibleSquares.add(boardSquares[x][tempy]);
...@@ -57,7 +58,7 @@ public class Queen extends Piece { ...@@ -57,7 +58,7 @@ public class Queen extends Piece {
while (tempy < 8) { while (tempy < 8) {
if (boardSquares[x][tempy].getPiece() == null) if (boardSquares[x][tempy].getPiece() == null)
possibleSquares.add(boardSquares[x][tempy]); possibleSquares.add(boardSquares[x][tempy]);
else if (boardSquares[x][tempy].getPiece().getPieceColor() == this.getPieceColor()) else if (boardSquares[x][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else { else {
possibleSquares.add(boardSquares[x][tempy]); possibleSquares.add(boardSquares[x][tempy]);
...@@ -72,7 +73,7 @@ public class Queen extends Piece { ...@@ -72,7 +73,7 @@ public class Queen extends Piece {
while (tempx < 8 && tempy >= 0) { while (tempx < 8 && tempy >= 0) {
if (boardSquares[tempx][tempy].getPiece() == null) if (boardSquares[tempx][tempy].getPiece() == null)
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
else if (boardSquares[tempx][tempy].getPiece().getPieceColor() == this.getPieceColor()) else if (boardSquares[tempx][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else { else {
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
...@@ -83,10 +84,11 @@ public class Queen extends Piece { ...@@ -83,10 +84,11 @@ public class Queen extends Piece {
} }
tempx = x - 1; tempx = x - 1;
tempy = y + 1; tempy = y + 1;
while (tempx >= 0 && tempy < 8) { while (tempx >= 0 && tempy < 8) {
if (boardSquares[tempx][tempy].getPiece() == null) if (boardSquares[tempx][tempy].getPiece() == null)
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
else if (boardSquares[tempx][tempy].getPiece().getPieceColor() == this.getPieceColor()) else if (boardSquares[tempx][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else { else {
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
...@@ -100,7 +102,7 @@ public class Queen extends Piece { ...@@ -100,7 +102,7 @@ public class Queen extends Piece {
while (tempx >= 0 && tempy >= 0) { while (tempx >= 0 && tempy >= 0) {
if (boardSquares[tempx][tempy].getPiece() == null) if (boardSquares[tempx][tempy].getPiece() == null)
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
else if (boardSquares[tempx][tempy].getPiece().getPieceColor() == this.getPieceColor()) else if (boardSquares[tempx][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else { else {
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
...@@ -114,7 +116,7 @@ public class Queen extends Piece { ...@@ -114,7 +116,7 @@ public class Queen extends Piece {
while (tempx < 8 && tempy < 8) { while (tempx < 8 && tempy < 8) {
if (boardSquares[tempx][tempy].getPiece() == null) if (boardSquares[tempx][tempy].getPiece() == null)
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
else if (boardSquares[tempx][tempy].getPiece().getPieceColor() == this.getPieceColor()) else if (boardSquares[tempx][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else { else {
possibleSquares.add(boardSquares[tempx][tempy]); possibleSquares.add(boardSquares[tempx][tempy]);
......
...@@ -19,7 +19,7 @@ public class Rook extends Piece { ...@@ -19,7 +19,7 @@ public class Rook extends Piece {
{ {
if(boardSquares[tempx][y].getPiece()==null) if(boardSquares[tempx][y].getPiece()==null)
possibleSquares.add(boardSquares[tempx][y]); possibleSquares.add(boardSquares[tempx][y]);
else if(boardSquares[tempx][y].getPiece().getPieceColor()==this.getPieceColor()) else if(boardSquares[tempx][y].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else else
{ {
...@@ -33,7 +33,7 @@ public class Rook extends Piece { ...@@ -33,7 +33,7 @@ public class Rook extends Piece {
{ {
if(boardSquares[tempx][y].getPiece()==null) if(boardSquares[tempx][y].getPiece()==null)
possibleSquares.add(boardSquares[tempx][y]); possibleSquares.add(boardSquares[tempx][y]);
else if(boardSquares[tempx][y].getPiece().getPieceColor()==this.getPieceColor()) else if(boardSquares[tempx][y].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else else
{ {
...@@ -47,7 +47,7 @@ public class Rook extends Piece { ...@@ -47,7 +47,7 @@ public class Rook extends Piece {
{ {
if(boardSquares[x][tempy].getPiece()==null) if(boardSquares[x][tempy].getPiece()==null)
possibleSquares.add(boardSquares[x][tempy]); possibleSquares.add(boardSquares[x][tempy]);
else if(boardSquares[x][tempy].getPiece().getPieceColor()==this.getPieceColor()) else if(boardSquares[x][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else else
{ {
...@@ -61,7 +61,7 @@ public class Rook extends Piece { ...@@ -61,7 +61,7 @@ public class Rook extends Piece {
{ {
if(boardSquares[x][tempy].getPiece()==null) if(boardSquares[x][tempy].getPiece()==null)
possibleSquares.add(boardSquares[x][tempy]); possibleSquares.add(boardSquares[x][tempy]);
else if(boardSquares[x][tempy].getPiece().getPieceColor()==this.getPieceColor()) else if(boardSquares[x][tempy].getPiece().getPieceColor().equals(this.getPieceColor()))
break; break;
else else
{ {
......
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