Commit c7036982 authored by kimia's avatar kimia

logic

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