Commit 00cffa12 authored by kimia's avatar kimia

pics

parent 6f8bb035
...@@ -24,28 +24,49 @@ public class Bishop extends Piece { ...@@ -24,28 +24,49 @@ public class Bishop extends Piece {
) && (c.isEmpty() || (!c.isEmpty() && sw==1)) ) { ) && (c.isEmpty() || (!c.isEmpty() && sw==1)) ) {
if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() < c.getRow()) { if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() < c.getRow()) {
for (int i=-1 ; i>-8 ; --i) { for (int i=-1 ; i>-8 ; --i) {
if (!board[this.getCell().getRow()-i][this.getCell().getCol()-i].isEmpty()) return false; 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()) break ; if (this.getCell().getRow()-i == c.getRow()) break ;
} }
} }
if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() > c.getRow()) { if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() > c.getRow()) {
for (int i=-1 ; i>-8 ; --i) { for (int i=-1 ; i>-8 ; --i) {
if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) { if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) {
//System.out.println("294 294") ; Cell newCell = new Cell(this.getCell().getRow()+i,this.getCell().getCol()-i);
return false ; 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()) break ; if (this.getCell().getRow()+i == c.getRow()) break ;
} }
} }
if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() < c.getRow()) { if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() < c.getRow()) {
for (int i=1 ; i<8 ; ++i) { for (int i=1 ; i<8 ; ++i) {
if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) return false; 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().getCol()-i == c.getCol()) break ; if (this.getCell().getCol()-i == c.getCol()) break ;
} }
} }
if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() > c.getRow()) { if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() > c.getRow()) {
for (int i=-1 ; i>-8 ; --i) { for (int i=-1 ; i>-8 ; --i) {
if (!board[this.getCell().getRow()+i][this.getCell().getCol()+i].isEmpty()) return false; 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().getCol()+i == c.getCol()) break ; if (this.getCell().getCol()+i == c.getCol()) break ;
} }
} }
...@@ -55,4 +76,18 @@ public class Bishop extends Piece { ...@@ -55,4 +76,18 @@ public class Bishop extends Piece {
return false ; return false ;
} }
} }
private PieceColor checkCellPieceColor(Cell c, Cell board[][], Piece pw[], Piece pb[]){
for (Piece p : pw){
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()){
return p.getPieceColor();
}
}
for (Piece p : pb){
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()){
return p.getPieceColor();
}
}
return null;
}
} }
...@@ -18,12 +18,12 @@ public class ChessBoardGUI extends JPanel { ...@@ -18,12 +18,12 @@ public class ChessBoardGUI extends JPanel {
private Cell dest; private Cell dest;
private boolean[][] moveBoard = null; private boolean[][] moveBoard = null;
public ArrayList<Piece> deletedList = new ArrayList<>(); public ArrayList<Piece> deletedList = new ArrayList<>();
private JFrame parentPanel;
public ChessBoardGUI(JPanel removedPiecePanel){ public ChessBoardGUI(JPanel removedPiecePanel,JFrame parentPanel){
super(); super();
this.removedPiecePanel=removedPiecePanel;
this.removedPiecePanel=removedPiecePanel; this.parentPanel = parentPanel;
parentPanel.setTitle("White Turn...");
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,600); setSize(800,600);
initlizeGame(); initlizeGame();
...@@ -47,7 +47,7 @@ public class ChessBoardGUI extends JPanel { ...@@ -47,7 +47,7 @@ public class ChessBoardGUI extends JPanel {
if(moveBoard[finalI][finalJ] && source!=null && dest!=null){ if(moveBoard[finalI][finalJ] && source!=null && dest!=null){
movePiece(source,dest); movePiece(source,dest);
// System.out.println("Moves"); // System.out.println("Moves");
buttonRepaint();
updateDeletedPanel(); updateDeletedPanel();
//---------------- //----------------
int condition = checkCondition(board,pw,pb); int condition = checkCondition(board,pw,pb);
...@@ -65,11 +65,16 @@ public class ChessBoardGUI extends JPanel { ...@@ -65,11 +65,16 @@ public class ChessBoardGUI extends JPanel {
if(moveBoard !=null){ if(moveBoard !=null){
updateGUIBoard(moveBoard); updateGUIBoard(moveBoard);
} }
if(wORb%2==0){
parentPanel.setTitle("White Turn...");
}else {
parentPanel.setTitle("Black Turn...");
}
} }
}); });
add(button[i][j]); add(button[i][j]);
if((i+j)%2==0) if((i+j)%2==0)
button[i][j].setBackground(Color.lightGray); button[i][j].setBackground(Color.CYAN);
else { else {
button[i][j].setBackground(Color.WHITE); button[i][j].setBackground(Color.WHITE);
} }
...@@ -81,7 +86,7 @@ public class ChessBoardGUI extends JPanel { ...@@ -81,7 +86,7 @@ public class ChessBoardGUI extends JPanel {
for (int i = 0 ; i < 8 ;i++){ for (int i = 0 ; i < 8 ;i++){
for (int j = 0 ; j < 4 ;j++) { for (int j = 0 ; j < 4 ;j++) {
removedPieces[i][j] = new JButton(); removedPieces[i][j] = new JButton();
removedPieces[i][j].setBackground(Color.pink); removedPieces[i][j].setBackground(Color.YELLOW);
// added.setMinimumSize(new Dimension(50,50)); // added.setMinimumSize(new Dimension(50,50));
removedPiecePanel.add( removedPieces[i][j]); removedPiecePanel.add( removedPieces[i][j]);
} }
...@@ -114,7 +119,7 @@ public class ChessBoardGUI extends JPanel { ...@@ -114,7 +119,7 @@ public class ChessBoardGUI extends JPanel {
button[i][j].setIcon(new ImageIcon()); button[i][j].setIcon(new ImageIcon());
} }
if((i+j)%2==0) if((i+j)%2==0)
button[i][j].setBackground(Color.lightGray); button[i][j].setBackground(Color.CYAN);
else { else {
button[i][j].setBackground(Color.WHITE); button[i][j].setBackground(Color.WHITE);
} }
...@@ -130,7 +135,7 @@ public class ChessBoardGUI extends JPanel { ...@@ -130,7 +135,7 @@ public class ChessBoardGUI extends JPanel {
// System.out.println("Reach Thereeee..."); // System.out.println("Reach Thereeee...");
} else { } else {
if((i+j)%2==0) if((i+j)%2==0)
button[i][j].setBackground(Color.lightGray); button[i][j].setBackground(Color.CYAN);
else { else {
button[i][j].setBackground(Color.WHITE); button[i][j].setBackground(Color.WHITE);
} }
......
...@@ -15,10 +15,10 @@ public class ChessGameGUI extends JFrame { ...@@ -15,10 +15,10 @@ public class ChessGameGUI extends JFrame {
setResizable(false); setResizable(false);
parent.setLayout(new BoxLayout(parent,BoxLayout.X_AXIS)); parent.setLayout(new BoxLayout(parent,BoxLayout.X_AXIS));
JPanel p1 = new JPanel(); JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(8,4)); p1.setLayout(new GridLayout(8,5));
p1.setSize(500,600); p1.setSize(500,600);
JPanel gameBoard = new ChessBoardGUI(p1); JPanel gameBoard = new ChessBoardGUI(p1,this);
gameBoard.setSize(800,600); gameBoard.setSize(800,600);
parent.add(p1); parent.add(p1);
parent.add(gameBoard); parent.add(gameBoard);
......
import java.util.Scanner; import java.util.Scanner;
public class Main{ public class Main{
/** /**
*prints board and control the moves of the pieces *prints board and control the moves of the pieces
*@author kimiadorani *@author kimiadorani
*@version 1.0 *@version 1.0
*@since 2019-5-7 *@since 2019-5-7
*/ */
private static char boardChar[][] = new char [8][8] ;
private static char boardChar[][] = new char [8][8] ;
private static void printBoard() { private static void printBoard() {
for (int i=1 ; i<9 ; ++i) { for (int i=1 ; i<9 ; ++i) {
...@@ -142,7 +141,7 @@ public class Main{ ...@@ -142,7 +141,7 @@ public class Main{
for (Piece p:pw) { for (Piece p:pw) {
if (p.getCell().getRow()==s.charAt(0)-'a' && if (p.getCell().getRow()==s.charAt(0)-'a' &&
p.getCell().getCol()+1==s.charAt(1)-'0') { p.getCell().getCol()+1==s.charAt(1)-'0') {
if (p.isValidMove(board[s.charAt(3)-'a'][s.charAt(4)-'1'], board, pw, pb)) { if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb)) {
wORb++ ; wORb++ ;
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ; Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
...@@ -176,7 +175,7 @@ public class Main{ ...@@ -176,7 +175,7 @@ public class Main{
for (Piece p:pb) { for (Piece p:pb) {
if (p.getCell().getRow()==s.charAt(0)-'a' && if (p.getCell().getRow()==s.charAt(0)-'a' &&
p.getCell().getCol()+1==s.charAt(1)-'0') p.getCell().getCol()+1==s.charAt(1)-'0')
if (p.isValidMove(board[s.charAt(3)-'a'][s.charAt(4)-'1'], board, pw, pb) ) { if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb) ) {
wORb++ ; wORb++ ;
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ; Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
...@@ -212,4 +211,5 @@ public class Main{ ...@@ -212,4 +211,5 @@ public class Main{
} }
} }
} }
\ No newline at end of file
public class Pawn extends Piece{ public class Pawn extends Piece{
private boolean once, end ; private boolean once, end ;
public Pawn(Cell cell, PieceColor color) { public Pawn(Cell cell, PieceColor pieceColor) {
super(cell, color) ; super(cell, pieceColor) ;
this.once = true ; this.once = true ;
this.end = false ; this.end = false ;
} }
public void setOnce(boolean once) { public void setOnce(boolean once) {
this.once = once; this.once = once;
} }
public void setEnd(boolean end) { public void setEnd(boolean end) {
this.end = end; this.end = end;
} }
@Override @Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) { public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ; if (super.isDeleted()) return false ;
if (super.getPieceColor()==PieceColor.BLACK && super.getCell().getRow()==0) end = true ; 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.WHITE && super.getCell().getRow()==7) end = true ;
int sw=0 ; int sw=0 ;
if (super.getPieceColor() == PieceColor.BLACK) { if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) { for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol() if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
&& c.getRow()-this.getCell().getRow()==-1 }
&& Math.abs(this.getCell().getCol()-c.getCol())==1) sw=1 ; }
} else {
} for (Piece p:pb) {
else { if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
for (Piece p:pb) { }
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 ;
}
}
if (once) { if (once) {
//once = false ; //once = false ;
//System.out.println("row: " + this.getCell().getRow()) ; //System.out.println("row: " + this.getCell().getRow()) ;
//System.out.println("col: " + this.getCell().getCol()) ; //System.out.println("col: " + this.getCell().getCol()) ;
if ((this.getCell().getCol() == c.getCol()) && if ((this.getCell().getCol() == c.getCol()) &&
(Math.abs(this.getCell().getRow()-c.getRow())==1 || Math.abs(this.getCell().getRow()-c.getRow())==2) (Math.abs(this.getCell().getRow()-c.getRow())==1 || Math.abs(this.getCell().getRow()-c.getRow())==2)
&& c.isEmpty()) && c.isEmpty()) {
return true ; if(getCell().getRow() < c.getRow() && getPieceColor()==PieceColor.WHITE) {
else return false ; return true;
} }else if(getCell().getRow() > c.getRow() && getPieceColor()==PieceColor.BLACK){
else { return true;
if (end) { }else {
if( return false;
((this.getCell().getCol() == c.getCol()) &&
(Math.abs(this.getCell().getRow()-c.getRow())==1)) ||
(Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) &&
Math.abs(this.getCell().getCol()-c.getCol())==1 && sw==1)
) return true ;
else return false ;
}
else {
if(
(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)) )
) return true ;
else return false ;
}
} }
}
else return false ;
}
else {
if (end) {
if(
((this.getCell().getCol() == c.getCol()) &&
(Math.abs(this.getCell().getRow()-c.getRow())==1)) ||
(Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) &&
Math.abs(this.getCell().getCol()-c.getCol())==1 && sw==1)
) return true ;
else return false ;
}
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)) )
||
(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)) )
) return true ;
else return false ;
}
} }
}
} }
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