Commit b81456de authored by Omid Sayfun's avatar Omid Sayfun

PathWay Check Added

parent 755a7ca0
import java.util.*;
public class Bishop extends Piece{
public Bishop(char x, int y, boolean color, String name){
super(x, y, color, name);
......@@ -18,4 +20,32 @@ public class Bishop extends Piece{
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = (x - this.x) / Math.abs(x - this.x);;
int yShift = (y - this.y) / Math.abs(y - this.y);
int i = 1;
while( x != (char)(this.x + i * xShift) && y != this.y + i * yShift ){
if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) ){
return false;
}
i++;
}
return true;
}
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
int xShift = (x - p.x) / Math.abs(x - p.x);;
int yShift = (y - p.y) / Math.abs(y - p.y);
int i = 1;
while( x != (char)(p.x + i * xShift) && y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){
return false;
}
i++;
}
return true;
}
}
......@@ -179,15 +179,25 @@ public class Board{
if( toArray[0] >= '1' && toArray[0] <= '8' && toArray[1] >= 'A' && toArray[1] <= 'H' ){// Check to bound
ArrayList<Piece> selector = null;
ArrayList<Piece> enemy = null;
ArrayList<Piece> all = new ArrayList<Piece>();
for(Piece p : this.whitePieces){
all.add(p);
}
for(Piece p : this.blackPieces){
all.add(p);
}
boolean found = false;
boolean toBusy = false;
if( color.equals("W") ){
selector = this.whitePieces;
enemy = this.blackPieces;
found = isTakenByWhite(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByWhite(toArray[0] - '0', toArray[1]);
}else{
selector = this.blackPieces;
enemy = this.whitePieces;
found = isTakenByBlack(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByBlack(toArray[0] - '0', toArray[1]);
}
......@@ -198,11 +208,17 @@ public class Board{
// To Do: Check if pathway is busy
// To Do: Check if king is checked and have to move king
// To Do: Attack
if( p.canMove(toArray[1], toArray[0] - '0') ){ // Check if move is valid
p.setY(toArray[0] - '0');
p.setX(toArray[1]);
return true;
if( p.checkWay(all, toArray[1], toArray[0] - '0') ){
p.setY(toArray[0] - '0');
p.setX(toArray[1]);
return true;
}else{
return false;
}
}else{
return false;
}
......
import java.util.*;
public class King extends Piece{
public King(char x, int y, boolean color, String name){
super(x, y, color, name);
......@@ -10,4 +12,8 @@ public class King extends Piece{
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
return true;
}
}
import java.util.*;
public class Knight extends Piece{
public Knight(char x, int y, boolean color, String name){
super(x, y, color, name);
......@@ -14,4 +16,8 @@ public class Knight extends Piece{
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
return true;
}
}
......@@ -21,7 +21,9 @@ public class Main{
}
System.out.print("Player Move: ");
String input = sc.nextLine();
// Check if input is valid
if( mainBoard.move(input.split(" ")[0], input.split(" ")[1], player) ){
break;
}
}
......
import java.util.*;
public class Pawn extends Piece{
private boolean firstMove;
public Pawn(char x, int y, boolean color, String name){
......@@ -34,4 +36,25 @@ public class Pawn extends Piece{
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int yShift = 0;
if( this.y - y != 0 ){
yShift = (y - this.y) / Math.abs(y - this.y);
}
int i = 1;
while( y != this.y + i * yShift ){
if( checkTaken(pieces, this.x, this.y + i * yShift) ){
return false;
}
i++;
}
if( checkTaken(pieces, this.x, this.y + i * yShift) ){
return false;
}
return true;
}
}
import java.util.*;
public abstract class Piece{
protected int y;
protected char x;
......@@ -43,5 +45,17 @@ public abstract class Piece{
return this.color;
}
public static boolean checkTaken(ArrayList<Piece> pieces, char x, int y){
for(Piece p : pieces){
if( p.x == x && p.y == y ){
return true;
}
}
return false;
}
abstract boolean canMove(char x, int y);
abstract boolean checkWay(ArrayList<Piece> pieces, char x, int y);
}
import java.util.*;
public class Queen extends Piece{
public Queen(char x, int y, boolean color, String name){
super(x, y, color, name);
......@@ -10,4 +12,12 @@ public class Queen extends Piece{
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Rook.checkWay(this, pieces, x, y) && Bishop.checkWay(this, pieces, x, y) ){
return true;
}
return false;
}
}
import java.util.*;
public class Rook extends Piece{
public Rook(char x, int y, boolean color, String name){
super(x, y, color, name);
......@@ -18,4 +20,48 @@ public class Rook extends Piece{
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = 0;
int yShift = 0;
if( this.x - x != 0 ){
xShift = (x - this.x) / Math.abs(x - this.x);
}
if( this.y - y != 0 ){
yShift = (y - this.y) / Math.abs(y - this.y);
}
int i = 1;
while( x != (char)(this.x + i * xShift) || y != this.y + i * yShift ){
if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) ){
return false;
}
i++;
}
return true;
}
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
int xShift = 0;
int yShift = 0;
if( p.x - x != 0 ){
xShift = (x - p.x) / Math.abs(x - p.x);
}
if( p.y - y != 0 ){
yShift = (y - p.y) / Math.abs(y - p.y);
}
int i = 1;
while( x != (char)(p.x + i * xShift) && y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){
return false;
}
i++;
}
return true;
}
}
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