Commit 755a7ca0 authored by Omid Sayfun's avatar Omid Sayfun

Added Move Validaity

parent 41a183a5
public class Bishop extends Piece{
public Bishop(char x, int y, String name){
super(x, y, name);
public Bishop(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Math.abs(this.x - x) / Math.abs(this.y - y) == 1 ){
return true;
}
return false;
}
public static boolean canMove(Piece p, char x, int y){ // Ignore the presence of other pieces
if( Math.abs(p.x - x) / Math.abs(p.y - y) == 1 ){
return true;
}
return false;
}
}
......@@ -13,56 +13,56 @@ public class Board{
// White Players
// Pawns
for(char ch = 'A'; ch < 'I'; ch++){
Pawn newPawn = new Pawn(ch, 2, "WP");
Pawn newPawn = new Pawn(ch, 2, true, "WP");
this.whitePieces.add(newPawn);
}
// Rook
for(char ch = 'A'; ch < 'I'; ch += 7){
Rook newRook = new Rook(ch, 1, "WR");
Rook newRook = new Rook(ch, 1, true, "WR");
this.whitePieces.add(newRook);
}
// Knight
for(char ch = 'B'; ch < 'H'; ch += 5){
Knight newKnight = new Knight(ch, 1, "WN");
Knight newKnight = new Knight(ch, 1, true, "WN");
this.whitePieces.add(newKnight);
}
// Bishop
for(char ch = 'C'; ch < 'G'; ch += 3){
Bishop newBishop = new Bishop(ch, 1, "WB");
Bishop newBishop = new Bishop(ch, 1, true, "WB");
this.whitePieces.add(newBishop);
}
// Queen
Queen whiteQueen = new Queen('E', 1, "WQ");
Queen whiteQueen = new Queen('E', 1, true, "WQ");
this.whitePieces.add(whiteQueen);
// King
King whiteKing = new King('D', 1, "WK");
King whiteKing = new King('D', 1, true, "WK");
this.whitePieces.add(whiteKing);
// Black Players
// Pawns
for(char ch = 'A'; ch < 'I'; ch++){
Pawn newPawn = new Pawn(ch, 7, "BP");
Pawn newPawn = new Pawn(ch, 7, false, "BP");
this.blackPieces.add(newPawn);
}
// Rook
for(char ch = 'A'; ch < 'I'; ch += 7){
Rook newRook = new Rook(ch, 8, "BR");
Rook newRook = new Rook(ch, 8, false, "BR");
this.blackPieces.add(newRook);
}
// Knight
for(char ch = 'B'; ch < 'H'; ch += 5){
Knight newKnight = new Knight(ch, 8, "BN");
Knight newKnight = new Knight(ch, 8, false, "BN");
this.blackPieces.add(newKnight);
}
// Bishop
for(char ch = 'C'; ch < 'G'; ch += 3){
Bishop newBishop = new Bishop(ch, 8, "BB");
Bishop newBishop = new Bishop(ch, 8, false, "BB");
this.blackPieces.add(newBishop);
}
// Queen
Queen blackQueen = new Queen('E', 8, "BQ");
Queen blackQueen = new Queen('E', 8, false, "BQ");
this.blackPieces.add(blackQueen);
// King
King blackKing = new King('D', 8, "BK");
King blackKing = new King('D', 8, false, "BK");
this.blackPieces.add(blackKing);
}
......@@ -138,6 +138,7 @@ public class Board{
System.out.println("WN: White Knight\t|\tBN: Black Knight");
System.out.println("WR: White Rook \t|\tBR: Black Rook");
System.out.println("WP: White Pawn \t|\tBP: Black Pawn");
System.out.println("Acceptable Command: '2D 4D'(Move From 2D to 4D)");
}
public void printBoard(){
......@@ -193,11 +194,18 @@ public class Board{
if( found && !toBusy ){// Found and to is not busy
for(Piece p : selector){
if( p.getY() == fromArray[0] - '0' && p.getX() == fromArray[1] ){
p.setY(toArray[0] - '0');
p.setX(toArray[1]);
return true;
if( p.getY() == fromArray[0] - '0' && p.getX() == fromArray[1] ){ // Find Piece in select
// To Do: Check if pathway is busy
// To Do: Check if king is checked and have to move king
if( p.canMove(toArray[1], toArray[0] - '0') ){ // Check if move is valid
p.setY(toArray[0] - '0');
p.setX(toArray[1]);
return true;
}else{
return false;
}
}
}
return false;
......
public class King extends Piece{
public King(char x, int y, String name){
super(x, y, name);
public King(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Math.abs(this.x - x) < 2 && Math.abs(this.y - y) < 2 ){
return true;
}
return false;
}
}
public class Knight extends Piece{
public Knight(char x, int y, String name){
super(x, y, name);
public Knight(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){
int X[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int Y[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
for (int i = 0; i < 8; i++) {
if( y == this.y + Y[i] && x == (char)(this.x + X[i]) ){
return true;
}
}
return false;
}
}
import java.util.*;
import java.util.concurrent.*;
import java.io.IOException;
public class Main{
public static void main(String[] args){
public static void main(String[] args) throws IOException, InterruptedException{
Scanner sc = new Scanner(System.in);
Board mainBoard = new Board();
mainBoard.initPieces();
// mainBoard.printHelp();
mainBoard.move("8D", "3A", "B");
mainBoard.printBoard();
String player = "W";
for(int i = 0; i < 500; i++){
while(true){
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
mainBoard.printBoard();
if( player.equals("W") ){
System.out.print("White ");
}else{
System.out.print("Black ");
}
System.out.print("Player Move: ");
String input = sc.nextLine();
if( mainBoard.move(input.split(" ")[0], input.split(" ")[1], player) ){
break;
}
}
if( player.equals("W") ){// Change PLayer
player = "B";
}else{
player = "W";
}
}
sc.close();
}
}
public class Pawn extends Piece{
private boolean firstMove;
public Pawn(char x, int y, String name){
super(x, y, name);
public Pawn(char x, int y, boolean color, String name){
super(x, y, color, name);
this.firstMove = true;
}
......@@ -12,4 +12,26 @@ public class Pawn extends Piece{
public boolean getFirstMove(){
return this.firstMove;
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( this.x - x == 0 ){
if( Math.abs(this.y - y) == 1 || (this.firstMove && Math.abs(this.y - y) == 2) ){
if( (this.color && y - this.y > 0) || (!this.color && this.y - y > 0) ){ // Moving Backward
if( this.firstMove ){
this.firstMove = false;
}
return true;
}else{
return false;
}
}else{
return false;
}
}
return false;
}
}
public abstract class Piece{
private int y;
private char x;
protected int y;
protected char x;
protected boolean color;
private String name;
public Piece(char x, int y, String name){
public Piece(char x, int y, boolean color, String name){
setX(x);
setY(y);
setColor(color);
setName(name);
}
......@@ -21,6 +23,10 @@ public abstract class Piece{
this.name = name;
}
public void setColor(boolean color){
this.color = color;
}
public int getY(){
return this.y;
}
......@@ -32,4 +38,10 @@ public abstract class Piece{
public String getName(){
return this.name;
}
public boolean getColor(){
return this.color;
}
abstract boolean canMove(char x, int y);
}
public class Queen extends Piece{
public Queen(char x, int y, String name){
super(x, y, name);
public Queen(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Rook.canMove(this, x, y) || Bishop.canMove(this, x, y) ){
return true;
}
return false;
}
}
public class Rook extends Piece{
public Rook(char x, int y, String name){
super(x, y, name);
public Rook(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( this.x - x == 0 || this.y - y == 0 ){
return true;
}
return false;
}
public static boolean canMove(Piece p, char x, int y){ // Ignore the presence of other pieces
if( p.x - x == 0 || p.y - y == 0 ){
return true;
}
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