Commit 00cffa12 authored by kimia's avatar kimia

pics

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