Commit f6bd2726 authored by kimia's avatar kimia

true moves

parent 00cffa12
......@@ -24,49 +24,29 @@ public class Bishop extends Piece {
) && (c.isEmpty() || (!c.isEmpty() && sw==1)) ) {
if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() < c.getRow()) {
for (int i=-1 ; i>-8 ; --i) {
if (!board[this.getCell().getRow()-i][this.getCell().getCol()-i].isEmpty()){
Cell newCell = new Cell(this.getCell().getRow()-i,this.getCell().getCol()-i);
if(checkCellPieceColor(newCell,board,pw,pb)!=super.getPieceColor() && newCell.getRow()==c.getRow() && newCell.getCol()==c.getCol()){
return true;
}
return false;
}
if (this.getCell().getRow()-i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()-i][this.getCell().getCol()-i].isEmpty()) return false;
if (this.getCell().getRow()-i == c.getRow()) break ;
}
}
if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() > c.getRow()) {
for (int i=-1 ; i>-8 ; --i) {
if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) {
Cell newCell = new Cell(this.getCell().getRow()+i,this.getCell().getCol()-i);
if(checkCellPieceColor(newCell,board,pw,pb)!=super.getPieceColor() && newCell.getRow()==c.getRow() && newCell.getCol()==c.getCol()){
return true;
}
return false;
}
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) return false ;
if (this.getCell().getRow()+i == c.getRow()) break ;
}
}
if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() < c.getRow()) {
for (int i=1 ; i<8 ; ++i) {
if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) {
Cell newCell = new Cell(this.getCell().getRow()+i,this.getCell().getCol()-i);
if(checkCellPieceColor(newCell,board,pw,pb)!=super.getPieceColor() && newCell.getRow()==c.getRow() && newCell.getCol()==c.getCol()){
return true;
}
return false;
}
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) return false;
if (this.getCell().getCol()-i == c.getCol()) break ;
}
}
if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() > c.getRow()) {
for (int i=-1 ; i>-8 ; --i) {
if (!board[this.getCell().getRow()+i][this.getCell().getCol()+i].isEmpty()) {
Cell newCell = new Cell(this.getCell().getRow()+i,this.getCell().getCol()+i);
if(checkCellPieceColor(newCell,board,pw,pb)!=super.getPieceColor() && newCell.getRow()==c.getRow() && newCell.getCol()==c.getCol()){
return true;
}
return false;
}
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()+i][this.getCell().getCol()+i].isEmpty()) return false;
if (this.getCell().getCol()+i == c.getCol()) break ;
}
}
......
......@@ -6,8 +6,11 @@ import java.util.ArrayList;
public class ChessBoardGUI extends JPanel {
public JButton[][] button = new JButton[8][8];
JPanel removedPiecePanel;
JButton removedPieces[][] = new JButton[8][4];
private JPanel whiteRemovedPiecePanel;
private JPanel blackRemovedPiecePanel;
private JPanel turnPanel;
private JButton removedPieces[][] = new JButton[8][4];
private JLabel turnLabel = new JLabel();
private int wORb = 0;
private Piece pw[];
private Piece pb[];
......@@ -19,9 +22,11 @@ public class ChessBoardGUI extends JPanel {
private boolean[][] moveBoard = null;
public ArrayList<Piece> deletedList = new ArrayList<>();
private JFrame parentPanel;
public ChessBoardGUI(JPanel removedPiecePanel,JFrame parentPanel){
public ChessBoardGUI(JPanel whiteRemovedPiecePanel,JPanel blackRemovedPiecePanel,JPanel turnPanel,JFrame parentPanel){
super();
this.removedPiecePanel=removedPiecePanel;
this.whiteRemovedPiecePanel= whiteRemovedPiecePanel;
this.blackRemovedPiecePanel = blackRemovedPiecePanel;
this.turnPanel = turnPanel;
this.parentPanel = parentPanel;
parentPanel.setTitle("White Turn...");
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
......@@ -53,10 +58,10 @@ public class ChessBoardGUI extends JPanel {
int condition = checkCondition(board,pw,pb);
if(condition==2){
JOptionPane.showMessageDialog(removedPiecePanel,"Condition White");
JOptionPane.showMessageDialog(turnPanel,"Condition White");
}
else if(condition==1){
JOptionPane.showMessageDialog(removedPiecePanel,"Condition Black");
JOptionPane.showMessageDialog(turnPanel,"Condition Black");
}
//----------------
}
......@@ -66,9 +71,9 @@ public class ChessBoardGUI extends JPanel {
updateGUIBoard(moveBoard);
}
if(wORb%2==0){
parentPanel.setTitle("White Turn...");
turnLabel.setText("White Turn...");
}else {
parentPanel.setTitle("Black Turn...");
turnLabel.setText("Black Turn...");
}
}
});
......@@ -84,13 +89,18 @@ public class ChessBoardGUI extends JPanel {
}
private void inilizeRemovedPiecePanel(){
for (int i = 0 ; i < 8 ;i++){
for (int j = 0 ; j < 4 ;j++) {
for (int j = 0 ; j < 2 ;j++) {
removedPieces[i][j] = new JButton();
removedPieces[i][j].setBackground(Color.YELLOW);
// added.setMinimumSize(new Dimension(50,50));
removedPiecePanel.add( removedPieces[i][j]);
whiteRemovedPiecePanel.add( removedPieces[i][j]);
removedPieces[i][3-j] = new JButton();
removedPieces[i][3-j].setBackground(Color.YELLOW);
blackRemovedPiecePanel.add( removedPieces[i][3-j]);
}
}
turnPanel.setLayout( new FlowLayout(FlowLayout.CENTER));
turnPanel.add(turnLabel);
}
private void updateDeletedPanel(){
int wCount = 0;
......@@ -166,11 +176,11 @@ public class ChessBoardGUI extends JPanel {
boolean[][] binaryBoard = new boolean[8][8];
if (wORb%2==0) {
for (Piece p : pw){
if(p.getCell().getRow() == source.getRow() && p.getCell().getCol()==source.getCol()){
if(!p.isDeleted() && p.getCell().getRow() == source.getRow() && p.getCell().getCol()==source.getCol()){
for (int i = 0 ; i < 8;i++){
for (int j = 0 ; j < 8 ;j++){
Cell dest = new Cell(i,j);
if(p.isValidMove(dest,board,pw,pb)){
// Cell dest = new Cell(i,j);
if(p.isValidMove(board[i][j],board,pw,pb)){
binaryBoard[i][j] = true;
}
}
......@@ -180,11 +190,10 @@ public class ChessBoardGUI extends JPanel {
}
}else {
for (Piece p : pb){
if(p.getCell().getRow() == source.getRow() && p.getCell().getCol()==source.getCol()){
if(!p.isDeleted() && p.getCell().getRow() == source.getRow() && p.getCell().getCol()==source.getCol()){
for (int i = 0 ; i < 8;i++){
for (int j = 0 ; j < 8 ;j++){
Cell dest = new Cell(i,j);
if(p.isValidMove(dest,board,pw,pb)){
if(p.isValidMove(board[i][j],board,pw,pb)){
binaryBoard[i][j] = true;
}
}
......@@ -199,18 +208,14 @@ public class ChessBoardGUI extends JPanel {
//--------------
if (wORb%2==0) {
for (Piece p : pw) {
if (p.getCell().getRow() == source.getRow() &&
if (!p.isDeleted() && p.getCell().getRow() == source.getRow() &&
p.getCell().getCol() == source.getCol()) {
if (p.isValidMove(dest, board, pw, pb)) {
if (p.isValidMove(board[dest.getRow()][dest.getCol()], board, pw, pb)) {
dest = board[dest.getRow()][dest.getCol()];
wORb++;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true);
p.setCell(dest);
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false);
for (Piece p1 : pb) {
if (p1.getCell().getRow() == dest.getRow() && p1.getCell().getCol() == dest.getCol()) {
p1.setDeleted(true);
}
}
if (p instanceof Pawn) {
Pawn myPawn = (Pawn) p;
myPawn.setOnce(false);
......@@ -228,18 +233,14 @@ public class ChessBoardGUI extends JPanel {
}
}else {
for (Piece p:pb) {
if (p.getCell().getRow()==source.getRow() &&
if (!p.isDeleted() && p.getCell().getRow()==source.getRow() &&
p.getCell().getCol() == source.getCol())
if (p.isValidMove(dest, board, pw, pb) ) {
if (p.isValidMove(board[dest.getRow()][dest.getCol()], board, pw, pb) ) {
dest = board[dest.getRow()][dest.getCol()];
wORb++ ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
p.setCell(dest) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
for (Piece p1:pw) {
if (p1.getCell().getRow()==dest.getRow() && p1.getCell().getCol()==dest.getCol()) {
p1.setDeleted(true) ;
}
}
if (p instanceof Pawn) {
Pawn myPawn = (Pawn) p;
myPawn.setOnce(false) ;
......
......@@ -14,13 +14,23 @@ public class ChessGameGUI extends JFrame {
parent.setSize(1300,610);
setResizable(false);
parent.setLayout(new BoxLayout(parent,BoxLayout.X_AXIS));
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(8,5));
p1.setSize(500,600);
//-----------------------------------------
JPanel whiteRemovePanel = new JPanel();
whiteRemovePanel.setLayout(new GridLayout(8,2));
whiteRemovePanel.setSize(200,600);
//-----------------------------------------
JPanel turnPanel = new JPanel();
//-----------------------------------------
JPanel blackRemovePanel = new JPanel();
blackRemovePanel.setLayout(new GridLayout(8,2));
blackRemovePanel.setSize(200,600);
//-----------------------------------------
JPanel gameBoard = new ChessBoardGUI(p1,this);
JPanel gameBoard = new ChessBoardGUI(whiteRemovePanel,blackRemovePanel,turnPanel,this);
gameBoard.setSize(800,600);
parent.add(p1);
parent.add(whiteRemovePanel);
parent.add(turnPanel);
parent.add(blackRemovePanel);
parent.add(gameBoard);
add(parent);
setVisible(true);
......
......@@ -18,18 +18,22 @@ public class Pawn extends Piece{
@Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
if (super.getPieceColor()== PieceColor.BLACK && super.getCell().getRow()==0) end = true ;
if (super.getPieceColor()== PieceColor.WHITE && super.getCell().getRow()==7) end = true ;
if (super.getPieceColor()==PieceColor.BLACK && super.getCell().getRow()==0) end = true ;
if (super.getPieceColor()==PieceColor.WHITE && super.getCell().getRow()==7) end = true ;
int sw=0 ;
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()
&& c.getRow()-this.getCell().getRow()==-1
&& Math.abs(this.getCell().getCol()-c.getCol())==1) sw=1 ;
}
}
else {
for (Piece p:pb) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()
&& c.getRow()-this.getCell().getRow()==1
&& Math.abs(this.getCell().getCol()-c.getCol())==1) sw=1 ;
}
}
......@@ -39,7 +43,7 @@ public class Pawn extends Piece{
//System.out.println("col: " + this.getCell().getCol()) ;
if ((this.getCell().getCol() == c.getCol()) &&
(Math.abs(this.getCell().getRow()-c.getRow())==1 || Math.abs(this.getCell().getRow()-c.getRow())==2)
&& c.isEmpty()) {
&& c.isEmpty()){
if(getCell().getRow() < c.getRow() && getPieceColor()==PieceColor.WHITE) {
return true;
}else if(getCell().getRow() > c.getRow() && getPieceColor()==PieceColor.BLACK){
......@@ -62,13 +66,13 @@ public class Pawn extends Piece{
}
else {
if(
(this.getCell().getCol() == c.getCol() &&
((this.getCell().getRow()-c.getRow()==1 && super.getPieceColor()== PieceColor.BLACK) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getPieceColor()== PieceColor.WHITE)) )
(this.getCell().getCol() == c.getCol() && c.isEmpty() &&
((this.getCell().getRow()-c.getRow()==1 && super.getPieceColor()==PieceColor.BLACK) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getPieceColor()==PieceColor.WHITE)) )
||
(Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) &&
((this.getCell().getRow()-c.getRow()==1 && super.getPieceColor()== PieceColor.BLACK && sw==1) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getPieceColor()== PieceColor.WHITE && sw==1)) )
((this.getCell().getRow()-c.getRow()==1 && super.getPieceColor()==PieceColor.BLACK && sw==1) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getPieceColor()==PieceColor.WHITE && sw==1)) )
) return true ;
else return false ;
}
......
......@@ -27,27 +27,28 @@ public class Queen extends Piece {
) {
if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() < c.getRow()) {
for (int i=-1 ; i>-8 ; --i) {
if (this.getCell().getRow()-i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()-i][this.getCell().getCol()-i].isEmpty()) return false;
if (this.getCell().getRow()-i == c.getRow()) break ;
}
}
if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() > c.getRow()) {
for (int i=-1 ; i>-8 ; --i) {
if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) {
//System.out.println("294 294") ;
return false ;
}
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) return false ;
if (this.getCell().getRow()+i == c.getRow()) break ;
}
}
if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() < c.getRow()) {
for (int i=1 ; i<8 ; ++i) {
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) return false;
if (this.getCell().getCol()-i == c.getCol()) break ;
}
}
if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() > c.getRow()) {
for (int i=-1 ; i>-8 ; --i) {
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()+i][this.getCell().getCol()+i].isEmpty()) return false;
if (this.getCell().getCol()+i == c.getCol()) break ;
}
......@@ -55,27 +56,28 @@ public class Queen extends Piece {
if (this.getCell().getCol() == c.getCol() && this.getCell().getRow() < c.getRow()) {
for (int i=1 ; i<8 ; ++i) {
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()+i][c.getCol()].isEmpty()) return false;
if (this.getCell().getRow()+i == c.getRow()) break ;
}
}
if (this.getCell().getCol() == c.getCol() && this.getCell().getRow() > c.getRow()) {
for (int i=-1 ; i>-8 ; --i) {
if (!board[this.getCell().getRow()+i][c.getCol()].isEmpty()) {
//System.out.println("294 294") ;
return false;
}
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()+i][c.getCol()].isEmpty()) return false ;
if (this.getCell().getRow()+i == c.getRow()) break ;
}
}
if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() == c.getRow()) {
for (int i=1 ; i<8 ; ++i) {
if (this.getCell().getCol()+i == c.getCol() && sw==1) break ;
if (!board[c.getRow()][this.getCell().getCol()+i].isEmpty()) return false;
if (this.getCell().getCol()+i == c.getCol()) break ;
}
}
if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() == c.getRow()) {
for (int i=-1 ; i>-8 ; --i) {
if (this.getCell().getCol()+i == c.getCol() && sw==1) break ;
if (!board[c.getRow()][this.getCell().getCol()+i].isEmpty()) return false;
if (this.getCell().getCol()+i == c.getCol()) break ;
}
......
......@@ -31,27 +31,28 @@ public class Rook extends Piece {
) {
if (this.getCell().getCol() == c.getCol() && this.getCell().getRow() < c.getRow()) {
for (int i=1 ; i<8 ; ++i) {
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()+i][c.getCol()].isEmpty()) return false;
if (this.getCell().getRow()+i == c.getRow()) break ;
}
}
if (this.getCell().getCol() == c.getCol() && this.getCell().getRow() > c.getRow()) {
for (int i=-1 ; i>-8 ; --i) {
if (!board[this.getCell().getRow()+i][c.getCol()].isEmpty()) {
//System.out.println("294 294") ;
return false;
}
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()+i][c.getCol()].isEmpty()) return false;
if (this.getCell().getRow()+i == c.getRow()) break ;
}
}
if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() == c.getRow()) {
for (int i=1 ; i<8 ; ++i) {
if (this.getCell().getCol()+i == c.getCol() && sw==1) break ;
if (!board[c.getRow()][this.getCell().getCol()+i].isEmpty()) return false;
if (this.getCell().getCol()+i == c.getCol()) break ;
}
}
if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() == c.getRow()) {
for (int i=-1 ; i>-8 ; --i) {
if (this.getCell().getCol()+i == c.getCol() && sw==1) break ;
if (!board[c.getRow()][this.getCell().getCol()+i].isEmpty()) return false;
if (this.getCell().getCol()+i == c.getCol()) break ;
}
......
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