Commit 06a3cff9 authored by 9611046's avatar 9611046

King position corected

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