Commit c7036982 authored by kimia's avatar kimia

logic

parent 62c3ea26
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) { public boolean isValidMove(Cell c, Cell board[][]) {
return ( ( 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() 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 ;
}
}
return true ;
}
else {
return false ;
}
} }
} }
\ No newline at end of file
public class Cell { public class Cell {
private int row, col ; private int row, col ;
private boolean empty ; private boolean empty ;
...@@ -36,4 +37,4 @@ public class Cell { ...@@ -36,4 +37,4 @@ public class Cell {
enum Color { enum Color {
BLACK, WHITE BLACK, WHITE
} ; }
public class King extends Piece { public class King extends Piece {
public King(Cell cell, Color color) { public King(Cell cell, Color color) {
super(cell, color) ; super(cell, color) ;
} }
@Override @Override
public boolean isValidMove(Cell c) { public boolean isValidMove(Cell c, Cell board[][]) {
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) ||
...@@ -14,4 +13,4 @@ public class King extends Piece { ...@@ -14,4 +13,4 @@ public class King extends Piece {
&& c.isEmpty() && c.isEmpty()
) ; ) ;
} }
} }
\ No newline at end of file
public class Knight extends Piece { public class Knight extends Piece {
public Knight(Cell cell, Color color) { public Knight(Cell cell, Color color) {
super(cell, color) ; super(cell, color) ;
} }
@Override @Override
public boolean isValidMove(Cell c) { public boolean isValidMove(Cell c, Cell board[][]) {
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)
...@@ -13,4 +14,5 @@ public class Knight extends Piece { ...@@ -13,4 +14,5 @@ public class Knight extends Piece {
&& c.isEmpty() && c.isEmpty()
) ; ) ;
} }
} }
\ No newline at end of file
import java.util.Scanner; import java.util.Scanner;
public class Main{ public class Main{
private static char boardChar[][] = new char [8][8] ; private static char boardChar[][] = new char [8][8] ;
private static void printBoard() { private static void printBoard() {
...@@ -80,7 +81,7 @@ public class Main{ ...@@ -80,7 +81,7 @@ public class Main{
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])) { if (p.isValidMove(board[i][j], board)) {
System.out.print( (char)((int)'a'+i) ) ; System.out.print( (char)((int)'a'+i) ) ;
System.out.println(j+1) ; System.out.println(j+1) ;
} }
...@@ -94,7 +95,7 @@ public class Main{ ...@@ -94,7 +95,7 @@ public class Main{
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])) { if (p.isValidMove(board[i][j], board)) {
System.out.print( (char)((int)'a'+i) ) ; System.out.print( (char)((int)'a'+i) ) ;
System.out.println(j+1) ; System.out.println(j+1) ;
} }
...@@ -108,12 +109,13 @@ public class Main{ ...@@ -108,12 +109,13 @@ 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(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'))) { 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') ; Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
tmp.setEmpty(false) ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
p.setCell(tmp) ; p.setCell(tmp) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
if (p instanceof Pawn) { if (p instanceof Pawn) {
Pawn myPawn = (Pawn) p; Pawn myPawn = (Pawn) p ;
myPawn.setOnce(false) ; myPawn.setOnce(false) ;
} }
for (Piece blackPiece:pb) { for (Piece blackPiece:pb) {
...@@ -127,10 +129,11 @@ public class Main{ ...@@ -127,10 +129,11 @@ 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(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'))) { 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') ; Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
tmp.setEmpty(false) ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
p.setCell(tmp) ; p.setCell(tmp) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
if (p instanceof Pawn) { if (p instanceof Pawn) {
Pawn myPawn = (Pawn) p; Pawn myPawn = (Pawn) p;
myPawn.setOnce(false) ; myPawn.setOnce(false) ;
...@@ -153,4 +156,4 @@ public class Main{ ...@@ -153,4 +156,4 @@ public class Main{
} }
\ No newline at end of file \ No newline at end of file
public class Pawn extends Piece { public class Pawn extends Piece{
boolean once, end ; 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) { public boolean isValidMove(Cell c, Cell board[][]) {
if (super.getColor()==Color.BLACK && super.getCell().getRow()==0) 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 (super.getColor()==Color.WHITE && super.getCell().getRow()==7) end = true ;
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 ; return true ;
else return false ; else return false ;
} }
else { else {
if (end) { if (end) {
if( if(
((this.getCell().getCol() == c.getCol()) && ((this.getCell().getCol() == c.getCol()) &&
(Math.abs(this.getCell().getRow()-c.getRow())==1)) || (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())==Math.abs(this.getCell().getRow()-c.getRow()) &&
this.getCell().getRow()-c.getRow()==1) this.getCell().getRow()-c.getRow()==1)
) return true ; ) return true ;
else return false ; else return false ;
} }
else { else {
if( if(
(this.getCell().getCol() == c.getCol() && (this.getCell().getCol() == c.getCol() &&
((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK) || ((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE)) ) (this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE)) )
|| ||
(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()) &&
((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK) || ((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE)) ) (this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE)) )
) return true ; ) return true ;
else return false ; else return false ;
} }
} }
} }
} }
public abstract class Piece { public abstract class Piece {
private Cell cell ; private Cell cell ;
private Color color ; private Color color ;
private boolean deleted ; private boolean deleted ;
public Piece (Cell cell, Color color) { public Piece (Cell cell, Color color) {
this.cell = cell ; this.cell = cell ;
this.cell.setEmpty(false) ; this.cell.setEmpty(false) ;
this.color = color ; this.color = color ;
this.deleted = false ; this.deleted = false ;
} }
public abstract boolean isValidMove (Cell cell) ;
public Cell getCell() {
return cell;
}
public Color getColor() { public abstract boolean isValidMove (Cell cell, Cell board[][]) ;
return color;
}
public boolean isDeleted() { public Cell getCell() {
return deleted; return cell;
} }
public void setCell(Cell cell) { public Color getColor() {
this.cell = cell; return color;
} }
public void setColor(Color color) { public boolean isDeleted() {
this.color = color; return deleted;
} }
public void setDeleted(boolean deleted) { public void setCell(Cell cell) {
this.deleted = deleted; this.cell = cell;
} }
public void setColor(Color color) {
this.color = color;
} }
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
}
\ No newline at end of file
public class Queen extends Piece { public class Queen extends Piece {
public Queen(Cell cell, Color color) {
super(cell, color) ;
}
@Override
public boolean isValidMove(Cell c) { public Queen(Cell cell, Color color) {
return ( ( super(cell, color);
(this.getCell().getCol() == c.getCol()) || (this.getCell().getRow() == c.getRow() ) || }
( Math.abs(this.getCell().getCol()-c.getCol()) == Math.abs(this.getCell().getRow()-c.getRow()))
) @Override
&& c.isEmpty() public boolean isValidMove(Cell c, Cell board[][]) {
) ; return ((
(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()
);
}
} }
}
\ No newline at end of file
...@@ -6,11 +6,40 @@ public class Rook extends Piece { ...@@ -6,11 +6,40 @@ public class Rook extends Piece {
} }
@Override @Override
public boolean isValidMove(Cell c) { public boolean isValidMove(Cell c, Cell board[][]) {
return ( ( 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() ) {
) ; 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()) 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()) 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 ;
} }
} }
\ 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