Commit 9dfb84e8 authored by kimia's avatar kimia

logic with checkMate

parent c7036982
public class Bishop extends Piece { public class Bishop extends Piece {
public Bishop(Cell cell, Color color) { public Bishop(Cell cell, Color color) {
super(cell, color) ; super(cell, color) ;
} }
@Override @Override
public boolean isValidMove(Cell c, Cell board[][]) { public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getColor() == Color.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 ( ( if ( (
(Math.abs(this.getCell().getCol()-c.getCol()) == Math.abs(this.getCell().getRow()-c.getRow())) (Math.abs(this.getCell().getCol()-c.getCol()) == Math.abs(this.getCell().getRow()-c.getRow()))
) && c.isEmpty() ) { ) && (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()) return false;
...@@ -42,4 +55,4 @@ public class Bishop extends Piece { ...@@ -42,4 +55,4 @@ public class Bishop extends Piece {
return false ; return false ;
} }
} }
} }
\ No newline at end of file
class Cell {
public class Cell {
private int row, col ; private int row, col ;
private boolean empty ; private boolean empty ;
......
...@@ -4,13 +4,24 @@ public class King extends Piece { ...@@ -4,13 +4,24 @@ public class King extends Piece {
} }
@Override @Override
public boolean isValidMove(Cell c, Cell board[][]) { public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getColor() == Color.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 ;
}
}
return ( ( return ( (
(this.getCell().getCol() == c.getCol() && Math.abs(this.getCell().getRow() - c.getRow()) == 1) || (this.getCell().getCol() == c.getCol() && Math.abs(this.getCell().getRow() - c.getRow()) == 1) ||
(this.getCell().getRow() == c.getRow() && Math.abs(this.getCell().getCol() - c.getCol()) == 1) || (this.getCell().getRow() == c.getRow() && Math.abs(this.getCell().getCol() - c.getCol()) == 1) ||
(Math.abs(this.getCell().getCol()-c.getCol())==1 && Math.abs(this.getCell().getRow()-c.getRow())==1) (Math.abs(this.getCell().getCol()-c.getCol())==1 && Math.abs(this.getCell().getRow()-c.getRow())==1)
) ) && (c.isEmpty() || (!c.isEmpty() && sw==1))
&& c.isEmpty()
) ; ) ;
} }
} }
\ No newline at end of file
/**
* knight class show the knight's moves that is like L
*@author kimiadorani
*@version 1.0
*@since 2019-5-7
*/
public class Knight extends Piece { public class Knight extends Piece {
...@@ -6,13 +13,26 @@ public class Knight extends Piece { ...@@ -6,13 +13,26 @@ public class Knight extends Piece {
} }
@Override @Override
public boolean isValidMove(Cell c, Cell board[][]) { public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getColor() == Color.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 ;
}
}
return ( ( return ( (
(Math.abs(this.getCell().getCol()-c.getCol()) + Math.abs(this.getCell().getRow() - c.getRow()) == 3) && (Math.abs(this.getCell().getCol()-c.getCol()) + Math.abs(this.getCell().getRow() - c.getRow()) == 3) &&
(Math.abs(this.getCell().getCol()-c.getCol()) != 0 && Math.abs(this.getCell().getRow() - c.getRow()) != 0) (Math.abs(this.getCell().getCol()-c.getCol()) != 0 && Math.abs(this.getCell().getRow() - c.getRow()) != 0)
) )
&& c.isEmpty() && (c.isEmpty() || (!c.isEmpty() && sw==1))
) ; ) ;
} }
} }
import java.util.Scanner; import java.util.Scanner;
public class Main{ public class Main{
/**
*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() {
private static void printBoard() {
for (int i=1 ; i<9 ; ++i) { for (int i=1 ; i<9 ; ++i) {
System.out.print(" " + i) ; System.out.print(" " + i) ;
} }
System.out.print("\n ") ; System.out.print("\n ") ;
for (int i=0 ; i<8 ; ++i) for (int i=0 ; i<8 ; ++i)
System.out.print("+---") ; System.out.print("+---") ;
System.out.print("+\n") ; System.out.print("+\n") ;
for (int i=0 ; i<8 ; ++i) { for (int i=0 ; i<8 ; ++i) {
System.out.print( (char)(i+'a') ) ; System.out.print( (char)(i+'a') ) ;
for (int j=0 ; j<8 ; ++j) { for (int j=0 ; j<8 ; ++j) {
System.out.print("| " + boardChar[i][j] + " ") ; System.out.print("| " + boardChar[i][j] + " ") ;
} }
System.out.print("|\n ") ; System.out.print("|\n ") ;
for (int i2=0 ; i2<8 ; ++i2) for (int i2=0 ; i2<8 ; ++i2)
System.out.print("+---") ; System.out.print("+---") ;
System.out.print("+\n") ; System.out.print("+\n") ;
} }
} }
private static void updateBoard(Piece pw[], Piece pb[]) { private static void updateBoard(Piece pw[], Piece pb[]) {
for (int i=0 ; i<8 ; ++i) { for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) { for (int j=0 ; j<8 ; ++j) {
boardChar[i][j] = ' ' ; boardChar[i][j] = ' ' ;
} }
} }
for (Piece p:pw) { for (Piece p:pw) {
if (p.isDeleted()) continue ; if (p.isDeleted()) continue ;
if (p instanceof Pawn) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'P' ; if (p instanceof Pawn) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'P' ;
if (p instanceof Bishop) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'B' ; if (p instanceof Bishop) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'B' ;
if (p instanceof Queen) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'Q' ; if (p instanceof Queen) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'Q' ;
if (p instanceof King) boardChar[p.getCell().getRow()][p.getCell().getCol()] = '$' ; if (p instanceof King) boardChar[p.getCell().getRow()][p.getCell().getCol()] = '$' ;
if (p instanceof Rook) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'R' ; if (p instanceof Rook) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'R' ;
if (p instanceof Knight) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'K' ; if (p instanceof Knight) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'K' ;
}
for (Piece p:pb) {
if (p.isDeleted()) continue ;
if (p instanceof Pawn) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'P' ;
if (p instanceof Bishop) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'B' ;
if (p instanceof Queen) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'Q' ;
if (p instanceof King) boardChar[p.getCell().getRow()][p.getCell().getCol()] = '$' ;
if (p instanceof Rook) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'R' ;
if (p instanceof Knight) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'K' ;
}
}
private static void checkCondition(Cell board[][], Piece pw[], Piece pb[]) {
for (Piece p:pw) {
if (p instanceof King) {
int sw1=0 ;
for (Piece p1:pb) {
if (p1.isValidMove(p.getCell(), board, pw, pb)) {
sw1=1 ;
}
}
if(sw1==1) System.out.println("Check condition for White player!!") ;
}
} }
for (Piece p:pb) { for (Piece p:pb) {
if (p.isDeleted()) continue ; if (p instanceof King) {
if (p instanceof Pawn) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'P' ; int sw1=0 ;
if (p instanceof Bishop) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'B' ; for (Piece p1:pw) {
if (p instanceof Queen) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'Q' ; if (p1.isValidMove(p.getCell(), board, pw, pb)) {
if (p instanceof King) boardChar[p.getCell().getRow()][p.getCell().getCol()] = '$' ; sw1=1 ;
if (p instanceof Rook) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'R' ; }
if (p instanceof Knight) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'K' ; }
} if(sw1==1) System.out.println("Check condition for Balck player!!") ;
} }
}
}
public static void main(String[] args) { public static void main(String[] args) {
Scanner in = new Scanner(System.in) ; Scanner in = new Scanner(System.in) ;
Player white = new Player(Color.WHITE) ; Player white = new Player(Color.WHITE) ;
Player black = new Player(Color.BLACK) ; Player black = new Player(Color.BLACK) ;
...@@ -59,101 +88,111 @@ public class Main{ ...@@ -59,101 +88,111 @@ public class Main{
Cell board[][] = new Cell[8][8] ; Cell board[][] = new Cell[8][8] ;
for (int i=0 ; i<8 ; ++i) { for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) { for (int j=0 ; j<8 ; ++j) {
board[i][j] = new Cell (i,j) ; board[i][j] = new Cell (i,j) ;
} }
} }
for (Piece p:pw) { for (Piece p:pw) {
board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ; board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ;
} }
for (Piece p:pb) { for (Piece p:pb) {
board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ; board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ;
} }
int wORb = 0 ; int wORb = 0 ;
updateBoard(pw, pb) ; updateBoard(pw, pb) ;
printBoard() ; printBoard() ;
while (true) { while (true) {
wORb++ ; wORb++ ;
String s = in.nextLine() ; String s = in.nextLine() ;
if (s.length()==2) { if (s.length()==2) {
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') {
for (int i=0 ; i<8 ; ++i) { for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) { for (int j=0 ; j<8 ; ++j) {
if (p.isValidMove(board[i][j], board)) { if (p.isValidMove(board[i][j], board, pw, pb)) {
System.out.print( (char)((int)'a'+i) ) ; System.out.print( (char)((int)'a'+i) ) ;
System.out.println(j+1) ; System.out.println(j+1) ;
} }
} }
} }
}
}
for (Piece p:pb) {
if (p.getCell().getRow()==s.charAt(0)-'a' &&
p.getCell().getCol()+1==s.charAt(1)-'0') {
for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) {
if (p.isValidMove(board[i][j], board)) {
System.out.print( (char)((int)'a'+i) ) ;
System.out.println(j+1) ;
}
}
}
} }
} }
} for (Piece p:pb) {
else if (s.length()==5) { if (p.getCell().getRow()==s.charAt(0)-'a' &&
for (Piece p:pw) { p.getCell().getCol()+1==s.charAt(1)-'0') {
if (p.getCell().getRow()==s.charAt(0)-'a' && for (int i=0 ; i<8 ; ++i) {
p.getCell().getCol()+1==s.charAt(1)-'0') { for (int j=0 ; j<8 ; ++j) {
if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board)) { if (p.isValidMove(board[i][j], board, pw, pb)) {
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ; System.out.print( (char)((int)'a'+i) ) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ; System.out.println(j+1) ;
p.setCell(tmp) ; }
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ; }
if (p instanceof Pawn) { }
Pawn myPawn = (Pawn) p ;
myPawn.setOnce(false) ;
}
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==tmp.getRow()
&& blackPiece.getCell().getCol()==tmp.getCol())
blackPiece.setDeleted(true) ;
}
}
}
}
for (Piece p:pb) {
if (p.getCell().getRow()==s.charAt(0)-'a' &&
p.getCell().getCol()+1==s.charAt(1)-'0')
if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board) ) {
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
p.setCell(tmp) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
if (p instanceof Pawn) {
Pawn myPawn = (Pawn) p;
myPawn.setOnce(false) ;
}
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==tmp.getRow()
&& whitePiece.getCell().getCol()==tmp.getCol())
whitePiece.setDeleted(true) ;
}
}
}
updateBoard(pw, pb) ;
printBoard() ;
}
else {
System.out.println("Please enter a valid input!") ;
}
}
}
}
}
}
else if (s.length()==5) {
for (Piece p:pw) {
if (p.getCell().getRow()==s.charAt(0)-'a' &&
p.getCell().getCol()+1==s.charAt(1)-'0') {
if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb)) {
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
p.setCell(tmp) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
for (Piece p1:pb) {
if (p1.getCell().getRow()==tmp.getRow() && p1.getCell().getCol()==tmp.getCol()) {
p1.setDeleted(true) ;
}
}
if (p instanceof Pawn) {
Pawn myPawn = (Pawn) p ;
myPawn.setOnce(false) ;
}
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==tmp.getRow()
&& blackPiece.getCell().getCol()==tmp.getCol())
blackPiece.setDeleted(true) ;
}
}
}
}
for (Piece p:pb) {
if (p.getCell().getRow()==s.charAt(0)-'a' &&
p.getCell().getCol()+1==s.charAt(1)-'0')
if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb) ) {
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
p.setCell(tmp) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
for (Piece p1:pw) {
if (p1.getCell().getRow()==tmp.getRow() && p1.getCell().getCol()==tmp.getCol()) {
p1.setDeleted(true) ;
}
}
if (p instanceof Pawn) {
Pawn myPawn = (Pawn) p;
myPawn.setOnce(false) ;
}
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==tmp.getRow()
&& whitePiece.getCell().getCol()==tmp.getCol())
whitePiece.setDeleted(true) ;
}
}
}
checkCondition(board, pw, pb) ;
updateBoard(pw, pb) ;
printBoard() ;
}
else {
System.out.println("Please enter a valid input!") ;
}
}
}
}
}
\ No newline at end of file
public class Pawn extends Piece{ public class Pawn extends Piece{
boolean once, end ; private boolean once, end ;
public Pawn(Cell cell, Color color) { public Pawn(Cell cell, Color color) {
super(cell, color) ; super(cell, color) ;
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[][]) { public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.getColor()==Color.BLACK && super.getCell().getRow()==0) end = true ; if (super.isDeleted()) return false ;
if (super.getColor()==Color.WHITE && super.getCell().getRow()==7) end = true ; if (super.getColor()==Color.BLACK && super.getCell().getRow()==0) end = true ;
if (super.getColor()==Color.WHITE && super.getCell().getRow()==7) end = true ;
if (once) { int sw=0 ;
//once = false ; if (super.getColor() == Color.BLACK) {
//System.out.println("row: " + this.getCell().getRow()) ; for (Piece p:pw) {
//System.out.println("col: " + this.getCell().getCol()) ; if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
if ((this.getCell().getCol() == c.getCol()) && }
(Math.abs(this.getCell().getRow()-c.getRow())==1 || Math.abs(this.getCell().getRow()-c.getRow())==2) }
&& c.isEmpty()) else {
return true ; for (Piece p:pb) {
else return false ; if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
} }
else { }
if (end) {
if( if (once) {
((this.getCell().getCol() == c.getCol()) && //once = false ;
(Math.abs(this.getCell().getRow()-c.getRow())==1)) || //System.out.println("row: " + this.getCell().getRow()) ;
(Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) && //System.out.println("col: " + this.getCell().getCol()) ;
this.getCell().getRow()-c.getRow()==1) if ((this.getCell().getCol() == c.getCol()) &&
) return true ; (Math.abs(this.getCell().getRow()-c.getRow())==1 || Math.abs(this.getCell().getRow()-c.getRow())==2)
else return false ; && c.isEmpty())
} return true ;
else { else return false ;
if( }
(this.getCell().getCol() == c.getCol() && else {
((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK) || if (end) {
(this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE)) ) if(
|| ((this.getCell().getCol() == c.getCol()) &&
(Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) && (Math.abs(this.getCell().getRow()-c.getRow())==1)) ||
((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK) || (Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) &&
(this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE)) ) Math.abs(this.getCell().getCol()-c.getCol())==1 && sw==1)
) return true ; ) return true ;
else return false ; else return false ;
} }
} else {
} if(
(this.getCell().getCol() == c.getCol() &&
((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE)) )
||
(Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) &&
((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK && sw==1) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE && sw==1)) )
) return true ;
else return false ;
}
}
} }
}
...@@ -11,7 +11,7 @@ public abstract class Piece { ...@@ -11,7 +11,7 @@ public abstract class Piece {
this.deleted = false ; this.deleted = false ;
} }
public abstract boolean isValidMove (Cell cell, Cell board[][]) ; public abstract boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) ;
public Cell getCell() { public Cell getCell() {
return cell; return cell;
...@@ -37,4 +37,4 @@ public abstract class Piece { ...@@ -37,4 +37,4 @@ public abstract class Piece {
this.deleted = deleted; this.deleted = deleted;
} }
} }
\ No newline at end of file
...@@ -2,18 +2,86 @@ ...@@ -2,18 +2,86 @@
public class Queen extends Piece { public class Queen extends Piece {
public Queen(Cell cell, Color color) {
super(cell, color) ;
}
public Queen(Cell cell, Color color) { @Override
super(cell, color); public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getColor() == Color.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 ( (
(this.getCell().getCol() == c.getCol()) || (this.getCell().getRow() == c.getRow() ) ||
( Math.abs(this.getCell().getCol()-c.getCol()) == Math.abs(this.getCell().getRow()-c.getRow()))
) && (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 (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()) 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 (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 (this.getCell().getCol()+i == c.getCol()) break ;
}
}
@Override if (this.getCell().getCol() == c.getCol() && this.getCell().getRow() < c.getRow()) {
public boolean isValidMove(Cell c, Cell board[][]) { for (int i=1 ; i<8 ; ++i) {
return (( if (!board[this.getCell().getRow()+i][c.getCol()].isEmpty()) return false;
(this.getCell().getCol() == c.getCol()) || (this.getCell().getRow() == c.getRow()) || if (this.getCell().getRow()+i == c.getRow()) break ;
(Math.abs(this.getCell().getCol() - c.getCol()) == Math.abs(this.getCell().getRow() - c.getRow())) }
) }
&& c.isEmpty() 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()) break ;
}
}
if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() == c.getRow()) {
for (int i=1 ; i<8 ; ++i) {
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 (!board[c.getRow()][this.getCell().getCol()+i].isEmpty()) return false;
if (this.getCell().getCol()+i == c.getCol()) break ;
}
}
return true ;
} }
else return false ;
} }
}
/**
* shows the moves of the rook , it can move straight
* @author kimiadorani
* @version 1.0
*/
public class Rook extends Piece { public class Rook extends Piece {
public Rook(Cell cell, Color color) { public Rook(Cell cell, Color color) {
super(cell, color) ; super(cell, color) ;
} }
@Override @Override
public boolean isValidMove(Cell c, Cell board[][]) { public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getColor() == Color.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 ( ( if ( (
(this.getCell().getCol() == c.getCol()) || (this.getCell().getRow() == c.getRow()) (this.getCell().getCol() == c.getCol()) || (this.getCell().getRow() == c.getRow())
) && c.isEmpty() ) && (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) {
...@@ -42,4 +60,5 @@ public class Rook extends Piece { ...@@ -42,4 +60,5 @@ public class Rook extends Piece {
} }
else return false ; else return false ;
} }
} }
\ No newline at end of file
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